-
Notifications
You must be signed in to change notification settings - Fork 0
/
LODGEN.py
250 lines (137 loc) · 8.04 KB
/
LODGEN.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
bl_info = {
"name" : "LOD GEN",
"author" : "Šimon Ustal",
"version" : (1, 0),
"blender" : (2, 91 ,1),
"location" : "View3d > Tool",
"warning" : "",
"wiki_url" : "",
"category" : "Optimize Mesh",
}
import bpy
import math
import os
import bpy.props
# Init LODGENPanel
class LODGENPanel(bpy.types.Panel):
bl_label = "LOD Gen"
bl_idname = "PT_TestPanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'LOD Gen'
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text = "Select object...", icon = 'OBJECT_DATAMODE')
row = layout.row()
row.prop(obj, "name")
# Init MyProperties
class MyProperties(bpy.types.PropertyGroup):
my_string : bpy.props.StringProperty(name= "Exported Mesh name")
my_int : bpy.props.IntProperty(name= "Number Of LODS", soft_min= 1, soft_max= 7)
int_of_LOD : bpy.props.IntProperty(name= "LOD Index", soft_min= 1, soft_max= 7)
my_filepath : bpy.props.StringProperty(name= "Filepath")
int_of_LOD_string : bpy.props.StringProperty(name= "LOD")
quality_ratio : bpy.props.FloatProperty(name= "Quality Ratio", default=1.0, min=0.1, max=1.0)
my_enum : bpy.props.EnumProperty(
name= "File Format",
description= "sample text",
items= [('fbx', ".fbx", ""),
('obj', ".obj", ""),
('dae', ".dae", ""),
('glb/gltf', ".glb/gltf", "")
]
)
# Init ObjectCountPanel
class ObjectCount(bpy.types.Panel):
bl_label = "Settings to Export"
bl_idname = "PT_TestPanel2"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'LOD Gen'
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.my_tool
row = layout.row()
layout.prop(mytool, "my_string")
row = layout.row()
layout.prop(mytool, "my_int")
row = layout.row()
layout.prop(mytool, 'quality_ratio', slider=True)
row = layout.row()
layout.prop(mytool, "my_enum")
row = layout.row()
layout.prop(mytool, "my_filepath")
row = layout.row()
row.operator("object.property_example")
# Export LODs
class OBJECT_OT_property_example(bpy.types.Operator):
bl_idname = "object.property_example"
bl_label = "Export LODS"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
layout = self.layout
so = bpy.context.active_object
mytool.int_of_LOD = 0
mytool.int_of_LOD_string = str(mytool.int_of_LOD)
# FBX
if mytool.my_enum == 'fbx':
bpy.ops.export_scene.fbx(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".fbx")
for i in range (0, mytool.my_int):
mytool.int_of_LOD += 1
mytool.int_of_LOD_string = str(mytool.int_of_LOD)
so.modifiers.new("My Modifier", 'DECIMATE').ratio = mytool.quality_ratio
bpy.ops.object.modifier_apply(modifier="My Modifier")
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
bpy.ops.export_scene.fbx(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".fbx")
return {'FINISHED'}
#DAE
if mytool.my_enum == 'dae':
bpy.ops.export_scene.fbx(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".dae")
for i in range (0, mytool.my_int):
mytool.int_of_LOD += 1
mytool.int_of_LOD_string = str(mytool.int_of_LOD)
so.modifiers.new("My Modifier", 'DECIMATE').ratio = mytool.quality_ratio
bpy.ops.object.modifier_apply(modifier="My Modifier")
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
bpy.ops.export_scene.fbx(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".dae")
return {'FINISHED'}
#OBJ
if mytool.my_enum == 'obj':
bpy.ops.export_scene.obj(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".obj", check_existing=True, axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl", use_selection=False, use_animation=False, use_mesh_modifiers=True, use_edges=True, use_smooth_groups=False, use_smooth_groups_bitflags=False, use_normals=True, use_uvs=True, use_materials=True, use_triangles=False, use_nurbs=False, use_vertex_groups=False, use_blen_objects=True, group_by_object=False, group_by_material=False, keep_vertex_order=False, global_scale=1, path_mode='AUTO')
for i in range (0, mytool.my_int):
mytool.int_of_LOD += 1
mytool.int_of_LOD_string = str(mytool.int_of_LOD)
so.modifiers.new("My Modifier", 'DECIMATE').ratio = mytool.quality_ratio
bpy.ops.object.modifier_apply(modifier="My Modifier")
bpy.ops.export_scene.obj(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".obj", check_existing=True, axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl", use_selection=False, use_animation=False, use_mesh_modifiers=True, use_edges=True, use_smooth_groups=False, use_smooth_groups_bitflags=False, use_normals=True, use_uvs=True, use_materials=True, use_triangles=False, use_nurbs=False, use_vertex_groups=False, use_blen_objects=True, group_by_object=False, group_by_material=False, keep_vertex_order=False, global_scale=1, path_mode='AUTO')
return {'FINISHED'}
#GLB
if mytool.my_enum == 'glb/gltf':
bpy.ops.export_scene.gltf(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".gltf")
for i in range (0, mytool.my_int):
mytool.int_of_LOD += 1
mytool.int_of_LOD_string = str(mytool.int_of_LOD)
so.modifiers.new("My Modifier", 'DECIMATE').ratio = mytool.quality_ratio
bpy.ops.object.modifier_apply(modifier="My Modifier")
bpy.ops.export_scene.gltf(filepath=mytool.my_filepath+"/"+mytool.my_string+"_LOD"+mytool.int_of_LOD_string+".gltf")
return {'FINISHED'}
# Init Classes
classes = [MyProperties, ObjectCount, LODGENPanel, OBJECT_OT_property_example]
# Un/Register Objects
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.my_tool = bpy.props.PointerProperty(type= MyProperties)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.my_tool
if __name__ == "__main__":
register()