-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunregis_addon_279.py
More file actions
executable file
·442 lines (400 loc) · 18.1 KB
/
Copy pathunregis_addon_279.py
File metadata and controls
executable file
·442 lines (400 loc) · 18.1 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation,
Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
veröffentlichten Version, weiter verteilen und/oder modifizieren.
Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
OHNE JEDE GEWÄHR,; sogar ohne die implizite
Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License für weitere Einzelheiten.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
'''
bl_info = {
"name": "unregis AddOn",
"author": "unregi Resident",
"description": "Tools for merging and simplifying multiple objects to fit OpenSim / SL",
"version": (0, 2),
"category": "Mesh",
"blender": (2, 79, 0),
}
import bpy
import bmesh
import math
import random
icons_dict = {
"main": {"icon": 'OUTLINER_DATA_CAMERA'},
"view": {"icon": 'PARTICLEMODE'},
"merge": {"icon": 'MOD_SOLIDIFY'},
"physics": {"icon": 'MESH_ICOSPHERE'},
"cleanup": {"icon": 'MOD_WAVE'},
}
class SLMergeMeshes(bpy.types.Operator):
"""Merge slecte objects"""
bl_idname = "unregi.mergemesh"
bl_label = "Merge selected objects"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
meshlist = [o for o in context.selected_objects if o.type == 'MESH']
if len(meshlist) <= 1:
self.report({'WARNING'}, "No multiple meshes to merge selected")
return {'CANCELLED'}
ctx = context.copy()
#rename UV maps
salt = str(random.randint(100, 999))
uvname = 'UVMap' + salt
for o in meshlist:
for uvmap in o.data.uv_layers :
uvmap.name = uvname
#join into active object
ctx['acive_object'] = meshlist[0]
ctx['selected_objects'] = meshlist
ctx['selected_editable_bases'] = [context.scene.object_bases[o.name] for o in meshlist]
bpy.ops.object.join(ctx)
context.scene.update()
self.report({'INFO'}, "%d meshes joined" % (len(meshlist),))
return {'FINISHED'}
class SLMergeMaterials(bpy.types.Operator):
"""Merge Materialslots with same texture"""
bl_idname = "unregi.mergematslot"
bl_label = "Merge Material slots that use same textures"
bl_options = {'REGISTER', 'UNDO'}
diffuse = bpy.props.BoolProperty(name="Unique Diffuse Colors", default=True)
def getDiffuseColorOfMaterialSlot(self, matslot):
color = matslot.material.diffuse_color
return (str(color.r) + str(color.g) + str(color.b))
def getTextureOfMaterialSlot(self, matslot):
for texslot in matslot.material.texture_slots:
if texslot is not None and texslot.texture.type == 'IMAGE':
if texslot.texture.image is not None:
return texslot.texture.image.name
def execute(self, context):
mobject = context.scene.objects.active
textures = []
slot = 0
before_nummat = len(mobject.material_slots)
nummat = len(mobject.material_slots)
print("Checking " + str(nummat) + " materials for possible merge")
while slot < nummat:
if mobject.material_slots[slot].material is None:
print("No material assigned in slot " + str(slot) + " removing it")
context.object.active_material_index = slot
bpy.ops.object.material_slot_remove()
nummat -= 1
continue
texture = self.getTextureOfMaterialSlot(mobject.material_slots[slot])
if self.diffuse:
#if we should care about the color too, just add it to the string
if texture is None:
texture = ""
texture = texture + self.getDiffuseColorOfMaterialSlot(mobject.material_slots[slot])
if texture not in textures:
textures.append(texture)
slot += 1
else:
context.object.active_material_index = slot
targetslot = textures.index(texture) + 1
if targetslot != slot:
print("Have to move slot " + str(slot) + " to " + str(targetslot))
for _ in range(slot - targetslot):
bpy.ops.object.material_slot_move(direction='UP')
print("Merge slot " + str(targetslot) + " with " + str(targetslot - 1))
bpy.ops.object.material_slot_remove()
nummat -= 1
new_nummat = len(mobject.material_slots)
self.report({'INFO'}, "%d materials merged, %d materials left" % (before_nummat - new_nummat, new_nummat))
return {'FINISHED'}
class SLDeleteUnusedMaterials(bpy.types.Operator):
"""Remove unused Materials"""
bl_idname = "unregi.remmat"
bl_label = "Remove Materials that are not used"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
num_deleted = 0
for material in bpy.data.materials:
if not material.users:
num_deleted += 1
bpy.data.materials.remove(material)
self.report({'INFO'}, "%d Materials removed" % (num_deleted))
return {'FINISHED'}
class SLDeleteUnusedMaterialslots(bpy.types.Operator):
"""Remove unused Materialslots"""
bl_idname = "unregi.remmatslot"
bl_label = "Remove Materialslots that are not used"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
num_deleted = 0
for obj in objects:
faces = obj.data.polygons
used_material_indices = [0 for n in range(len(faces))]
faces.foreach_get('material_index', used_material_indices)
used_material_indices = set(used_material_indices)
all_slots = set(n for n in range(len(obj.material_slots)))
unused_slot_indices = all_slots - used_material_indices
ctx = context.copy()
ctx['object'] = obj
unused_slot_indices = list(unused_slot_indices)
unused_slot_indices.sort(reverse=True)
for slot_index in unused_slot_indices:
obj.active_material_index = slot_index
bpy.ops.object.material_slot_remove(ctx)
num_deleted += 1
self.report({'INFO'}, "%d Materialslots removed" % (num_deleted))
return {'FINISHED'}
class SLMaterialsShadeless(bpy.types.Operator):
"""Materials Shadeless"""
bl_idname = "unregi.shadeless"
bl_label = "Make all Materials shadeless"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
for item in bpy.data.materials:
item.use_shadeless = True
return {'FINISHED'}
class SLCleanup(bpy.types.Operator):
"""CleanUp Objects"""
bl_idname = "unregi.cleanup"
bl_label = "Remove Doubles and dissolve degenerates in all selected objects"
bl_options = {'REGISTER', 'UNDO'}
distance = bpy.props.FloatProperty(name="Distance", default=0.01, min=0.001, max=1.0)
def execute(self, context):
objects = [o.data for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
num_before = sum(len(m.vertices) for m in objects)
distance = self.distance / 1000
bm = bmesh.new()
for o in objects:
bm.from_mesh(o)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=distance)
bmesh.ops.dissolve_degenerate(bm, edges=bm.edges, dist=distance)
bm.to_mesh(o)
o.update()
bm.clear()
bm.free()
num_deleted = num_before - sum(len(m.vertices) for m in objects)
self.report({'INFO'}, "%d from %d vertices removed" % (num_deleted, num_before))
return {'FINISHED'}
class SLConvexHull(bpy.types.Operator):
"""Convert objects to Convex Hulls"""
bl_idname = "unregi.convexhull"
bl_label = "Convert selected objects to Convex Hulls"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
for obj in objects:
context.scene.objects.active = obj
#make convex
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.convex_hull()
bpy.ops.object.mode_set(mode='OBJECT')
#delete materials
slot = 0
nummat = len(obj.material_slots)
while slot < nummat:
bpy.ops.object.material_slot_remove()
slot += 1
return {'FINISHED'}
class SLMakeTris(bpy.types.Operator):
"""Convert to Triangles"""
bl_idname = "unregi.maketris"
bl_label = "Convert selected objects to triangles"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
num_before = 0
num_after = 0
for obj in objects:
num_before += len(obj.data.polygons)
context.scene.objects.active = obj
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.quads_convert_to_tris()
bpy.ops.object.editmode_toggle()
num_after += len(obj.data.polygons)
self.report({'INFO'}, "Conversion from %d to %d faces" % (num_before, num_after))
return {'FINISHED'}
class SLMakeQuads(bpy.types.Operator):
"""Convert Triangles to Quads"""
bl_idname = "unregi.makequads"
bl_label = "Convert selected objects to quads"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
num_before = sum(len(m.data.polygons) for m in objects)
for obj in objects:
context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.tris_convert_to_quads()
bpy.ops.object.mode_set(mode='OBJECT')
num_after = sum(len(m.data.polygons) for m in objects)
self.report({'INFO'}, "Conversion from %d to %d faces" % (num_before, num_after))
return {'FINISHED'}
class SLDeleteLoose(bpy.types.Operator):
"""Delete Loose Vertices / Edges / Faces"""
bl_idname = "unregi.deleteloose"
bl_label = "Delete loose vertices, edges and faces"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
num_before = sum(len(m.data.vertices) for m in objects)
for obj in objects:
context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.delete_loose(use_faces=True)
bpy.ops.object.mode_set(mode='OBJECT')
num_deleted = num_before - sum(len(m.data.vertices) for m in objects)
self.report({'INFO'}, "%d from %d vertices removed" % (num_deleted, num_before))
return {'FINISHED'}
class SLPlanarDecimate(bpy.types.Operator):
"""Planar Decimate Objects"""
bl_idname = "unregi.planardec"
bl_label = "Planar Decimate over selected objects"
bl_options = {'REGISTER', 'UNDO'}
angle = bpy.props.IntProperty(name="Max Angle", default=5, min=0, max=25)
def cleanAllDecimateModifiers(self, obj):
for m in obj.modifiers:
if(m.type=="DECIMATE"):
obj.modifiers.remove(modifier=m)
def execute(self, context):
ctx = context.copy()
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
modifiers = []
modifierName='DecimateMod'
num_before = 0
num_after = 0
for o in objects:
num_before += len(o.data.vertices)
self.cleanAllDecimateModifiers(o)
ctx['acive_object'] = o
modifier = o.modifiers.new(modifierName,'DECIMATE')
modifier.decimate_type = 'DISSOLVE'
modifier.angle_limit = self.angle * 2 * math.pi / 360
print("Planar decimate of mesh " + o.name + " by " + str(modifier.angle_limit))
bpy.ops.object.modifier_apply(modifier=modifier.name)
num_after += len(o.data.vertices)
self.report({'INFO'}, "%d from %d vertices removed" % (num_before - num_after, num_before))
context.scene.update()
return {'FINISHED'}
class SLDecimate(bpy.types.Operator):
"""Decimate Objects"""
bl_idname = "unregi.decimate"
bl_label = "Decimate over selected objects"
bl_options = {'REGISTER', 'UNDO'}
ratio = bpy.props.FloatProperty(name="Ratio", default=0.2, min=0.0, max=1.0)
def cleanAllDecimateModifiers(self, obj):
for m in obj.modifiers:
if(m.type=="DECIMATE"):
obj.modifiers.remove(modifier=m)
def execute(self, context):
ctx = context.copy()
objects = [o for o in context.selected_objects if o.type == 'MESH']
if not objects:
self.report({'WARNING'}, "No meshes selected")
return {'CANCELLED'}
modifiers = []
modifierName='DecimateMod'
num_before = 0
num_after = 0
for o in objects:
num_before += len(o.data.vertices)
self.cleanAllDecimateModifiers(o)
ctx['acive_object'] = o
modifier = o.modifiers.new(modifierName,'DECIMATE')
modifier.decimate_type = 'COLLAPSE'
modifier.ratio = self.ratio
bpy.ops.object.modifier_apply(modifier=modifier.name)
num_after += len(o.data.vertices)
self.report({'INFO'}, "%d from %d vertices removed" % (num_before - num_after, num_before))
context.scene.update()
return {'FINISHED'}
class UnregisPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_label = 'unregis Tools'
bl_context = 'objectmode'
bl_category = 'Tools'
def draw(self, context):
layout = self.layout
layout.label(text='View', **icons_dict["view"])
layout.operator('unregi.shadeless', text='Make Materials shadeless')
num_mesh_selected = sum(1 for o in context.selected_objects if o.type == 'MESH')
if num_mesh_selected:
layout.operator('unregi.remmatslot', text='Remove unused Materialslots')
layout.label(text='Merge', **icons_dict["merge"])
if num_mesh_selected > 1:
layout.operator('unregi.mergemesh', text='Merge Objects')
layout.operator('unregi.mergematslot', text='Merge Same Materials')
layout.label(text='Physics Shape', **icons_dict["physics"])
layout.operator('unregi.convexhull', text='Create Convex Hulls')
layout.operator('unregi.decimate', text='Decimate')
layout.label(text='CleanUp', **icons_dict["cleanup"])
layout.operator('unregi.cleanup', text='Remove Doubles and Degenerates')
layout.operator('unregi.deleteloose', text='Delete Loose')
layout.operator('unregi.planardec', text='Planar Decimate')
layout.operator('unregi.maketris', text='Convert to Triangles')
layout.operator('unregi.makequads', text='Triangles to Quads')
else:
layout.operator('unregi.remmat', text='Remove unused Materials')
def register():
bpy.utils.register_class(SLMaterialsShadeless)
bpy.utils.register_class(SLMakeTris)
bpy.utils.register_class(SLMakeQuads)
bpy.utils.register_class(SLCleanup)
bpy.utils.register_class(SLPlanarDecimate)
bpy.utils.register_class(SLMergeMaterials)
bpy.utils.register_class(SLDeleteUnusedMaterials)
bpy.utils.register_class(SLDeleteUnusedMaterialslots)
bpy.utils.register_class(SLMergeMeshes)
bpy.utils.register_class(SLConvexHull)
bpy.utils.register_class(SLDecimate)
bpy.utils.register_class(SLDeleteLoose)
bpy.utils.register_class(UnregisPanel)
def unregister():
bpy.utils.unregister_class(SLMaterialsShadeless)
bpy.utils.unregister_class(SLMakeTris)
bpy.utils.unregister_class(SLMakeQuads)
bpy.utils.unregister_class(SLCleanup)
bpy.utils.unregister_class(SLPlanarDecimate)
bpy.utils.unregister_class(SLMergeMaterials)
bpy.utils.unregister_class(SLDeleteUnusedMaterials)
bpy.utils.unregister_class(SLDeleteUnusedMaterialslots)
bpy.utils.unregister_class(SLMergeMeshes)
bpy.utils.unregister_class(SLConvexHull)
bpy.utils.unregister_class(SLDecimate)
bpy.utils.unregister_class(SLDeleteLoose)
bpy.utils.unregister_class(UnregisPanel)
if __name__ == '__main__':
register()