-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_lightmap.py
296 lines (226 loc) · 9.88 KB
/
easy_lightmap.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
import bpy
from bpy.props import StringProperty, BoolProperty, IntProperty
from bpy.app.handlers import persistent
bl_info = {
"name": "Easy Light Map",
"author": "Mehdi Seifi",
"version": (0, 2),
"blender": (2, 7),
"location": "Render Tab > Easy Light Map",
"description": "Bakes light map for selected object easily!",
"warning": "",
"wiki_url": "https://github.com/mese79/easy_lightmap",
"category": "Render"
}
class General(object):
is_baking_started = False
original_color = None
textures_use = []
def __init__(self):
pass
class EasyLightMapProperties(bpy.types.PropertyGroup):
bake_path = StringProperty(
name="Bake folder:", default="", subtype="DIR_PATH", description="Path for saving baked maps.")
image_w = IntProperty(name="Width", default=1024, min=1, description="Image width")
image_h = IntProperty(name="Height", default=1024, min=1, description="Image height")
bake_diffuse = BoolProperty(name="Bake diffuse color", default=False, description="Bake material diffuse color into map.")
bake_textures = BoolProperty(name="Bake textures", default=False, description="Bake material textures into map.")
class EasyLightMapPrepare(bpy.types.Operator):
bl_idname = "object.easy_light_map_prepare"
bl_label = "Only Prepare For Baking"
bl_description = "Create two uv layers if there is not any then add an empty texture slot for baking."
settings = None
selected_object = None
material = None
@classmethod
def poll(cls, context):
return True
def execute(self, context):
self.selected_object = context.active_object
if self.selected_object is None or self.selected_object.type != "MESH":
self.report({"WARNING"}, "No mesh object was selected.")
self.material = self.selected_object.active_material
self.settings = context.scene.easyLightMap
# Check/Add UVs.
check_uv_layers(self.selected_object)
# Add new texture slot and new image for baking.
name = "Baked_" + self.material.name
img_path = bpy.path.abspath(self.settings.bake_path)
if not img_path:
img_path = bpy.path.abspath("//")
img_path = os.path.join(img_path, name + ".png")
img = bpy.data.images.get(name)
if img is None:
img = bpy.data.images.new(name, width=self.settings.image_w, height=self.settings.image_h, alpha=True)
img.file_format = "PNG"
img.filepath = img_path
elif img.size != [self.settings.image_w, self.settings.image_h]:
img.scale(self.settings.image_w, self.settings.image_h)
baked_tex = bpy.data.textures.get(name)
if baked_tex is None:
baked_tex = bpy.data.textures.new(name, type="IMAGE")
baked_tex.image = img
baked_tex.use_alpha = True
baked_slot = self.material.texture_slots.get(name)
if baked_slot is None:
baked_slot = self.material.texture_slots.add()
baked_slot.use = False
baked_slot.texture = baked_tex
baked_slot.texture_coords = "UV"
baked_slot.uv_layer = self.selected_object.data.uv_textures.active.name
baked_slot.blend_type = "MULTIPLY"
# Apply image to active UV layer.
uv_layer = self.selected_object.data.uv_textures.active
for uv in uv_layer.data:
uv.image = baked_tex.image
# Done!
return {"FINISHED"}
class EasyLightMapBake(bpy.types.Operator):
bl_idname = "object.easy_light_map_bake"
bl_label = "Bake It!"
bl_description = "Bake light map into new texture and add it into object material."
settings = None
selected_object = None
material = None
#original_color = None
#textures_use = []
@classmethod
def poll(cls, context):
return True
# def invoke(self, context, event):
# return self.execute(context)
def execute(self, context):
self.selected_object = context.active_object
if self.selected_object is None or self.selected_object.type != "MESH":
self.report({"WARNING"}, "No mesh object was selected.")
self.settings = context.scene.easyLightMap
self.material = self.selected_object.active_material
# Check UV layers.
check_uv_layers(self.selected_object)
# Add a new texture slot and a new image for baking.
name = "Baked_" + self.material.name
img_path = bpy.path.abspath(self.settings.bake_path)
if not img_path:
img_path = bpy.path.abspath("//")
img_path = os.path.join(img_path, name + ".png")
img = bpy.data.images.get(name)
if img is None:
img = bpy.data.images.new(name, width=self.settings.image_w, height=self.settings.image_h, alpha=True)
img.file_format = "PNG"
img.filepath = img_path
elif img.size != [self.settings.image_w, self.settings.image_h]:
img.scale(self.settings.image_w, self.settings.image_h)
baked_tex = bpy.data.textures.get(name)
if baked_tex is None:
baked_tex = bpy.data.textures.new(name, type="IMAGE")
baked_tex.image = img
baked_tex.use_alpha = True
baked_slot = self.material.texture_slots.get(name)
if baked_slot is None:
baked_slot = self.material.texture_slots.add()
baked_slot.use = False
baked_slot.texture = baked_tex
baked_slot.texture_coords = "UV"
baked_slot.uv_layer = self.selected_object.data.uv_textures.active.name
baked_slot.blend_type = "MULTIPLY"
# Set UV layer image.
uv_layer = self.selected_object.data.uv_textures.active
for uv in uv_layer.data:
uv.image = baked_tex.image
if not self.settings.bake_diffuse:
# Save diffuse color
General.original_color = self.material.diffuse_color.copy()
# Change it to pure white.
self.material.diffuse_color = [1.0, 1.0, 1.0]
# Check which texture to use.
General.textures_use = self.get_used_textures()
# Bake it.
bpy.app.handlers.scene_update_post.append(scene_update)
bpy.ops.object.bake_image("INVOKE_DEFAULT")
General.is_baking_started = True
# Done!
return {"FINISHED"}
def get_used_textures(self):
""" UnCheck textures if bake_textures is false. """
result = []
for slot in self.material.texture_slots:
if slot is not None and slot.use:
result.append(slot.use)
if not self.settings.bake_textures:
slot.use = False
return result
class EasyLightMapPanel(bpy.types.Panel):
bl_idname = "RENDER_PT_easy_light_map"
bl_label = "Easy Light Map"
bl_region_type = "WINDOW"
bl_space_type = "PROPERTIES"
bl_context = "render"
@classmethod
def poll(cls, context):
# if context.scene.render.engine == "BLENDER_RENDER":
return True
def draw(self, context):
layout = self.layout
props = context.scene.easyLightMap
row = layout.row(True)
row.prop(props, "bake_path")
row = layout.row(True)
row.prop(props, "image_w")
row.prop(props, "image_h")
layout.separator()
row = layout.row(True)
row.prop(props, "bake_diffuse")
row = layout.row(True)
row.prop(props, "bake_textures")
layout.separator()
layout.operator(EasyLightMapPrepare.bl_idname, text="Only Prepare For Baking")
layout.operator(EasyLightMapBake.bl_idname, text="Bake it!")
def check_uv_layers(selected_object):
""" Object must have two uv sets. """
if len(selected_object.data.uv_textures) == 0:
add_uv_map("Diffuse", selected_object)
add_uv_map("LightMap", selected_object)
elif len(selected_object.data.uv_textures) == 1:
add_uv_map("LightMap", selected_object)
def add_uv_map(name, selected_object):
""" Add new UV Map to object and unwrap it. """
uv = selected_object.data.uv_textures.new(name)
uv.active = True
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.smart_project(island_margin=0.05)
# Have to pass object, because context.object is None in render panel.
# (in unwrap function is_editmode = (context.object.mode == 'EDIT') will need it.)
# bpy.ops.uv.lightmap_pack({
# "object": selected_object,
# "PREF_CONTEXT": "ALL_OBJECTS",
# "PREF_PACK_IN_ONE": True,
# "PREF_IMG_PX_SIZE": settings.image_w
# })
bpy.ops.object.mode_set(mode="OBJECT")
uv.active = True
return uv
@persistent
def scene_update(context):
# Just when baking started texture.is_updated is true, after that it'll becomes false.
if bpy.context.active_object:
material = bpy.context.active_object.active_material
name = "Baked_" + material.name
if General.is_baking_started and not bpy.data.textures[name].is_updated:
# Baking is finished, so revert back the material color and texture slots use check boxes.
if General.original_color is not None:
material.diffuse_color = General.original_color
for index in range(len(General.textures_use)):
if material.texture_slots[index] is not None:
material.texture_slots[index].use = General.textures_use.pop(0)
# Remove handler
bpy.app.handlers.scene_update_post.remove(scene_update)
def register():
bpy.utils.register_class(EasyLightMapProperties)
bpy.types.Scene.easyLightMap = bpy.props.PointerProperty(type=EasyLightMapProperties)
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()