-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasy_lod.py
418 lines (327 loc) · 13 KB
/
easy_lod.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import bpy
import math
import os
bl_info = {
"name": "EasyLOD",
"author": "Alon Rubin",
"description": "EasyLOD allows users to quickly create LOD models.",
"blender": (3, 00, 0),
"version": (1, 1),
"location": "View3D > Sidebar > Easy LOD",
"warning": "",
"category": "LOD",
"doc_url": "https://github.com/alonrubintec/EasyLOD"
}
ui_space = 0.35
version = 1.1
class PanelClass:
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Easy LOD"
class MainPanel(PanelClass, bpy.types.Panel):
bl_idname = "MainPanel"
bl_label = f"Easy LOD - v{version}"
def draw(self, context):
layout = self.layout
split = layout.split(factor=1.0)
col1 = split.column(align=True)
col1.alignment = 'LEFT'
col1.label(text=" by Alon Rubin")
class Settings_Panel(PanelClass, bpy.types.Panel):
bl_parent_id = "MainPanel"
bl_label = "Settings"
def draw(self, context):
layout = self.layout
split = layout.split(factor=ui_space)
col1 = split.column(align=True)
col1.alignment = 'RIGHT'
col2 = split.column(align=True)
col2.alignment = 'RIGHT'
col1.label(text="Amount")
col2.prop(context.scene, "num_modifiers", slider=True)
split2 = layout.split(factor=ui_space)
col3 = split2.column(align=True)
col3.alignment = 'RIGHT'
col4 = split2.column(align=True)
col4.alignment = 'RIGHT'
col3.label(text="Intensity")
col4.prop(context.scene, "intensity", slider=True)
split4 = layout.split(factor=ui_space)
col7 = split4.column(align=True)
col7.alignment = 'RIGHT'
col8 = split4.column(align=True)
col8.alignment = 'RIGHT'
col7.label(text="Ratio")
col8.prop(context.scene, "ratio_power", slider=True)
layout.separator()
split3 = layout.split(factor=ui_space)
col5 = split3.column(align=True)
col5.alignment = 'RIGHT'
col6 = split3.column()
col5.label(text="Symmetry")
col6.prop(context.scene, "symmetry")
class Start_Panel(PanelClass, bpy.types.Panel):
bl_parent_id = "MainPanel"
bl_label = "LOD group"
def draw(self, context):
layout = self.layout
split = layout.split(factor=ui_space)
col1 = split.column(align=True)
col1.alignment = 'RIGHT'
col2 = split.column(align=True)
col2.alignment = 'RIGHT'
col1.label(text="")
col2.operator("object.add_decimate_modifier")
split2 = layout.split(factor=ui_space)
col3 = split2.column(align=True)
col3.alignment = 'RIGHT'
col4 = split2.column(align=True)
col4.alignment = 'RIGHT'
col3.label(text="")
col4.operator("object.remove_modifiers")
split3 = layout.split(factor=ui_space)
col5 = split3.column(align=True)
col5.alignment = 'RIGHT'
col6 = split3.column(align=True)
col6.alignment = 'RIGHT'
col5.label(text=f"{context.scene.current_modifier}")
col6.prop(context.scene, "visibility", slider=True)
def update_visibility(self, context):
# Get the maximum number of modifiers
max_modifiers = 0
for obj in context.selected_objects:
if obj.type == 'MESH' and len(obj.modifiers) > max_modifiers:
max_modifiers = len(obj.modifiers)
# Normalize the ui slider to the number of modifiers
visibility_percent = context.scene.visibility / 100
modifier_index = round(visibility_percent * (max_modifiers - 1))
# Set the visibility of the modifiers
current_modifier = None
for obj in context.selected_objects:
if obj.type == 'MESH':
for i, mod in enumerate(obj.modifiers):
if i == modifier_index:
mod.show_viewport = True
current_modifier = mod.name
else:
mod.show_viewport = False
# Set the current_modifier property of the scene to the name of the current modifier
if current_modifier:
context.scene.current_modifier = current_modifier
else:
context.scene.current_modifier = ""
# Register the update function as a handler for the visibility property
bpy.types.Scene.visibility = bpy.props.IntProperty(
name="",
default=0,
min=0,
max=100,
description="Visibility of the active LOD",
update=update_visibility
)
class Export_Panel(PanelClass, bpy.types.Panel):
bl_parent_id = "MainPanel"
bl_label = "Export"
def draw(self, context):
layout = self.layout
layout.separator()
split = layout.split(factor=ui_space)
col1 = split.column(align=True)
col1.alignment = 'RIGHT'
col2 = split.column(align=True)
col2.alignment = 'RIGHT'
col1.label(text="Path")
col2.prop(context.scene, "export_fbx_path")
split2 = layout.split(factor=ui_space)
col3 = split2.column(align=True)
col3.alignment = 'RIGHT'
col4 = split2.column(align=True)
col4.alignment = 'RIGHT'
col3.label(text="Export")
col4.operator("object.export", text="Export FBX")
layout.separator()
class ExportLODs(bpy.types.Operator):
bl_idname = "object.export"
bl_label = "Copy Object with Decimate Modifiers"
def execute(self, context):
if context.scene.export_fbx_path == "":
ShowMessageBox("No path to export!")
return {'FINISHED'}
# Check if any object selected
selected_objects = bpy.context.selected_objects
if len(selected_objects) < 1:
ShowMessageBox("No object selected to export!")
return {'FINISHED'}
# Check if there are modifier
selected_objects = bpy.context.selected_objects
for obj in selected_objects:
decimate_modifiers_objects = [modifier for modifier in obj.modifiers if modifier.type == 'DECIMATE']
if not decimate_modifiers_objects:
ShowMessageBox("NO LOD: Add LOD group", "ERROR", "ERROR")
return {'FINISHED'}
# Create collections with new models
collection_list = []
for current_object in selected_objects:
selected_object = current_object
decimate_modifiers = [modifier for modifier in selected_object.modifiers if modifier.type == 'DECIMATE']
Export_collection = bpy.data.collections.new(name=selected_object.name)
collection_list.append(Export_collection)
# Create new models
for modifier in decimate_modifiers:
copied_object = selected_object.copy()
copied_object.data = selected_object.data.copy()
copied_object.modifiers.clear()
decimate_modifier = copied_object.modifiers.new(name=modifier.name, type='DECIMATE')
decimate_modifier.ratio = modifier.ratio
copied_object.name = f"{selected_object.name}_{modifier.name}"
Export_collection.objects.link(copied_object)
# Deselect the original object
selected_object.select_set(False)
context.scene.collection.children.link(Export_collection)
# Apply Modifiers
for object in Export_collection.objects:
object.select_set(True)
bpy.context.view_layer.objects.active = object
bpy.ops.object.modifier_apply(modifier=modifier.name)
# Deselect all objects in the scene
bpy.ops.object.select_all(action='DESELECT')
# Get and clean path
export_path = context.scene.export_fbx_path
base_path, extension = os.path.splitext(export_path)
if extension:
export_path = base_path
for export_collection in collection_list:
temp_path = export_path
temp_path += export_collection.name
temp_path += '.fbx'
# Select all objects in the collection
for obj in export_collection.objects:
obj.select_set(True)
# Export
bpy.ops.export_scene.fbx(filepath=temp_path, use_selection=True)
for obj in export_collection.objects:
export_collection.objects.unlink(obj)
bpy.data.objects.remove(obj)
# Delete the collection
for delete_collection in collection_list:
bpy.data.collections.remove(delete_collection)
return {'FINISHED'}
class AddModifier(bpy.types.Operator):
bl_idname = "object.add_decimate_modifier"
bl_label = "Add"
bl_description = "Add LODs to Selected Objects"
def execute(self, context):
selected_objects = context.selected_objects
if len(selected_objects) < 1:
ShowMessageBox("No object selected!")
return {'FINISHED'}
context.scene.visibility = 100
# Check if object has Decimate Modifier
for obj in selected_objects:
if obj.type == 'MESH':
has_decimate_modifier = False
for modifier in obj.modifiers:
if modifier.type == 'DECIMATE':
has_decimate_modifier = True
break
if has_decimate_modifier:
return {'FINISHED'}
num_modifiers = context.scene.num_modifiers
ratio_power = context.scene.ratio_power
intensity = context.scene.intensity
# Invert intensity
intensity = 100 - intensity
# Map intensity to logarithmic scale between 0.001 and 0.1
ratio_intensity = 5
intensity = math.exp((intensity - 1) * ratio_intensity / 99.0 - ratio_intensity) / 10.0 + 0.001
for obj in selected_objects:
if obj.type == 'MESH':
for i in range(num_modifiers):
mod = obj.modifiers.new(name=f"LOD{i}", type='DECIMATE')
mod.show_expanded = False
mod.show_viewport = False
# Use logarithmic scale to set modifier ratio
ratio = (num_modifiers - i - 1) / (num_modifiers - 1)
ratio = math.exp(ratio * ratio_power) / math.exp(ratio_power)
mod.ratio = intensity + (1 - intensity) * ratio
if i == num_modifiers - 1:
mod.show_expanded = True
mod.show_viewport = True
# Check the value of the symmetry property
scene = bpy.context.scene
if scene.symmetry:
mod.use_symmetry = True
context.scene.current_modifier = mod.name
return {'FINISHED'}
class RemoveModifier(bpy.types.Operator):
bl_idname = "object.remove_modifiers"
bl_label = "Remove"
bl_description = "Remove all decimate modifiers from selected objects"
def execute(self, context):
selected_objects = context.selected_objects
if len(selected_objects) < 1:
ShowMessageBox("No object selected!")
return {'FINISHED'}
context.scene.visibility = -1
context.scene.current_modifier = ""
for obj in selected_objects:
if obj.type == 'MESH':
for mod in obj.modifiers:
if mod.type == 'DECIMATE':
obj.modifiers.remove(mod)
return {'FINISHED'}
bpy.types.Scene.num_modifiers = bpy.props.IntProperty(
name="",
default=6,
min=2,
max=10,
description="Number of decimate modifiers to add"
)
bpy.types.Scene.ratio_power = bpy.props.FloatProperty(
name="",
default=3.95,
min=2,
max=6,
description="Ratio",
precision=1
)
def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
Classes = (MainPanel, Settings_Panel, Start_Panel, Export_Panel,
AddModifier, RemoveModifier, ExportLODs)
def register():
# Classes
for c in Classes:
bpy.utils.register_class(c)
# Property
bpy.types.Scene.current_modifier = bpy.props.StringProperty()
bpy.types.Scene.intensity = bpy.props.IntProperty(
name="",
default=50,
min=1,
max=100,
description="Intensity for the decimate modifier ratio"
)
bpy.types.Scene.symmetry = bpy.props.BoolProperty(
name="",
description="Toggle symmetry",
default=False
)
bpy.types.Scene.export_fbx_path = bpy.props.StringProperty(
name="",
subtype="FILE_PATH",
default=""
)
def unregister():
# Classes
for c in Classes:
bpy.utils.unregister_class(c)
# Property
del bpy.types.Scene.intensity
del bpy.types.Scene.symmetry
del bpy.types.Scene.current_modifier
del bpy.types.Scene.export_fbx_path
if __name__ == "__main__":
register()