-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop.py
150 lines (116 loc) · 4.82 KB
/
op.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# import os
import bpy
from .bake import setup_bake_image, setup_bake_nodes, setup_bake_uv, cleanup_bake_nodes
from .material import setup_bake_material
import logging
_l = logging.getLogger(__name__)
class QuickBake_OT_bake(bpy.types.Operator):
'''Do the bake.'''
bl_idname = 'render.quickbake_bake'
bl_label = 'Bake'
bl_options = {'REGISTER', 'UNDO'}
# material:
@classmethod
def poll(cls, context):
obj: bpy.types.Object = context.active_object # type: ignore
return (obj is not None and obj.type == 'MESH')
def create_material(self,
obj,
name,
uv_name,
diffuse=None,
roughness=None,
normal=None):
_l.info('Creating bake material %s for object %s', name, obj.name)
mat = bpy.data.materials.get(name)
if mat is not None:
_l.debug('Material already exists, skipping')
self.report({'INFO'}, 'Material already exists, skipping')
return mat
mat = setup_bake_material(
obj, name, uv_name, diffuse, roughness, normal)
return mat
def execute(self, context):
obj = context.active_object
if obj is None:
self.report({'ERROR'}, 'No active object')
return {'CANCELLED'}
if obj.type != 'MESH':
self.report({'ERROR'}, 'Active object must be a mesh')
return {'CANCELLED'}
props = context.scene.QuickBakeToolPropertyGroup
bake_nodes = setup_bake_nodes(obj)
bake_uv = setup_bake_uv(obj, props.bake_uv)
passes = []
if props.diffuse_enabled:
passes.append('DIFFUSE')
if props.normal_enabled:
passes.append('NORMAL')
if props.roughness_enabled:
passes.append('ROUGHNESS')
if props.ao_enabled:
passes.append('AO')
if props.shadow_enabled:
passes.append('SHADOW')
if props.position_enabled:
passes.append('POSITION')
if props.uv_enabled:
passes.append('UV')
if props.emit_enabled:
passes.append('EMIT')
if props.environment_enabled:
passes.append('ENVIRONMENT')
if props.glossy_enabled:
passes.append('GLOSSY')
if props.transmission_enabled:
passes.append('TRANSMISSION')
_l.debug('Enabled bake passes: %s', repr(passes))
img_cache = {}
for pass_type in passes:
_l.info('Baking pass %s', pass_type)
img = setup_bake_image(obj,
bake_nodes,
props.bake_name,
props.bake_size,
pass_type.lower(),
props.reuse_tex,
pass_type == 'NORMAL')
img_cache[pass_type] = img
self.report({'INFO'}, 'Baking pass %s' % pass_type)
_l.debug('Making object %s active', obj.name)
bpy.context.view_layer.objects.active = obj
save_mode = 'INTERNAL'
filepath = ''
# if props.save_img:
# _l.debug('Saving image externally')
# img_base_path = bpy.path.abspath(props.image_path)
# save_mode = 'EXTERNAL'
# filepath = os.path.join(img_base_path, img.name + '.png')
# _l.debug('Filepath %s', filepath)
self.report({'INFO'}, 'Save mode %s' % save_mode)
bpy.ops.object.bake(type=pass_type,
pass_filter={'COLOR'},
uv_layer='bake_uv',
use_clear=True,
# save_mode='INTERNAL',
# save_mode=save_mode,
# filepath=filepath,
)
# if props.save_img:
# _l.debug('Saving image externally %s', img.name)
# # filepath = os.path.join(props.image_path, img.name + '.png')
# img.filepath = filepath
# img.file_format = 'PNG'
# # img.save_render(filepath=filepath)
# img.save()
self.report({'INFO'}, 'Baking complete')
if props.clean_up and not props.create_mat:
cleanup_bake_nodes(obj)
if props.create_mat:
self.create_material(obj,
props.mat_name,
props.bake_uv,
img_cache.get('DIFFUSE'),
img_cache.get('ROUGHNESS'),
img_cache.get('NORMAL'))
return {'FINISHED'}