forked from absolute-quantum/cats-blender-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimation.py
356 lines (277 loc) · 12.2 KB
/
decimation.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
# MIT License
# Copyright (c) 2018 Hotox
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Code author: Hotox
# Repo: https://github.com/michaeldegroot/cats-blender-plugin
# Edits by:
import bpy
import tools.common
import tools.armature_bones as Bones
ignore_shapes = []
ignore_meshes = []
class ScanButton(bpy.types.Operator):
bl_idname = 'auto.scan'
bl_label = 'Scan for decimation models'
bl_description = 'Separates the mesh.'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
if context.scene.add_shape_key == "":
return False
return True
def execute(self, context):
shape = context.scene.add_shape_key
shapes = tools.common.get_shapekeys_decimation_list(self, context)
count = len(shapes)
if count > 1 and shapes.index(shape) == count - 1:
context.scene.add_shape_key = shapes[count - 2]
ignore_shapes.append(shape)
return {'FINISHED'}
class AddShapeButton(bpy.types.Operator):
bl_idname = 'add.shape'
bl_label = 'Add'
bl_description = 'Adds the selected shape key to the whitelist.\n' \
'This means that every mesh containing that shape key will be not decimated.'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
if context.scene.add_shape_key == "":
return False
return True
def execute(self, context):
shape = context.scene.add_shape_key
shapes = tools.common.get_shapekeys_decimation_list(self, context)
count = len(shapes)
if count > 1 and shapes.index(shape) == count - 1:
context.scene.add_shape_key = shapes[count - 2]
ignore_shapes.append(shape)
return {'FINISHED'}
class AddMeshButton(bpy.types.Operator):
bl_idname = 'add.mesh'
bl_label = 'Add'
bl_description = 'Adds the selected mesh to the whitelist.\n' \
'This means that this mesh will be not decimated.'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
if context.scene.add_mesh == "":
return False
return True
def execute(self, context):
ignore_meshes.append(context.scene.add_mesh)
return {'FINISHED'}
class RemoveShapeButton(bpy.types.Operator):
bl_idname = 'remove.shape'
bl_label = ''
bl_description = 'Removes the selected shape key from the whitelist.\n' \
'This means that this shape key is no longer decimation safe!'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
shape_name = bpy.props.StringProperty()
def execute(self, context):
ignore_shapes.remove(self.shape_name)
return {'FINISHED'}
class RemoveMeshButton(bpy.types.Operator):
bl_idname = 'remove.mesh'
bl_label = ''
bl_description = 'Removes the selected mesh from the whitelist.\n' \
'This means that this mesh will be decimated.'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
mesh_name = bpy.props.StringProperty()
def execute(self, context):
ignore_meshes.remove(self.mesh_name)
return {'FINISHED'}
class AutoDecimateButton(bpy.types.Operator):
bl_idname = 'auto.decimate'
bl_label = 'Quick Decimation'
bl_description = 'This will automatically decimate your model while preserving the shape keys.\n' \
'You should manually remove unimportant meshes first.'
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
obj = context.active_object
if obj and obj.type == 'MESH':
return True
i = 0
for ob in bpy.data.objects:
if ob.type == 'MESH':
if ob.parent is not None and ob.parent.type == 'ARMATURE':
i += 1
return i == 1
def execute(self, context):
obj = context.active_object
if not obj or (obj and obj.type != 'MESH'):
tools.common.unselect_all()
meshes = tools.common.get_meshes_objects()
if len(meshes) == 0:
return {'FINISHED'}
obj = meshes[0]
if context.scene.decimation_mode != 'CUSTOM':
tools.common.separate_by_materials(context, obj)
self.decimate(context)
mesh = tools.common.join_meshes(context)
if mesh is not None:
tools.common.repair_viseme_order(mesh.name)
return {'FINISHED'}
def decimate(self, context):
print('START DECIMATION')
tools.common.set_default_stage()
custom_decimation = context.scene.decimation_mode == 'CUSTOM'
full_decimation = context.scene.decimation_mode == 'FULL'
half_decimation = context.scene.decimation_mode == 'HALF'
safe_decimation = context.scene.decimation_mode == 'SAFE'
decimate_fingers = context.scene.decimate_fingers
meshes = []
current_tris_count = 0
tris_count = 0
meshes_obj = tools.common.get_meshes_objects()
for mesh in meshes_obj:
current_tris_count += len(mesh.data.polygons)
if decimate_fingers:
for mesh in meshes_obj:
if len(mesh.vertex_groups) > 0:
tools.common.select(mesh)
tools.common.switch('EDIT')
bpy.ops.mesh.select_mode(type='VERT')
for finger in Bones.bone_finger_list:
print(finger)
vgs = [mesh.vertex_groups.get(finger + 'L'), mesh.vertex_groups.get(finger + 'R')]
for vg in vgs:
if vg:
bpy.ops.object.vertex_group_set_active(group=vg.name)
bpy.ops.object.vertex_group_select()
try:
bpy.ops.mesh.separate(type='SELECTED')
except RuntimeError:
pass
bpy.ops.object.mode_set(mode='OBJECT')
tools.common.unselect_all()
for mesh in meshes_obj:
tools.common.select(mesh)
tris = len(mesh.data.polygons)
if custom_decimation and mesh.name in ignore_meshes:
tools.common.unselect_all()
continue
if mesh.data.shape_keys is not None:
if full_decimation:
print('FULL')
bpy.ops.object.shape_key_remove(all=True)
meshes.append((mesh, tris))
tris_count += tris
elif custom_decimation:
found = False
for shape in ignore_shapes:
if shape in mesh.data.shape_keys.key_blocks:
found = True
break
if found:
print('IGNORED')
tools.common.unselect_all()
continue
print('CUSTOM')
bpy.ops.object.shape_key_remove(all=True)
meshes.append((mesh, tris))
tris_count += tris
elif half_decimation and len(mesh.data.shape_keys.key_blocks) < 4:
print('HALF')
bpy.ops.object.shape_key_remove(all=True)
meshes.append((mesh, tris))
tris_count += tris
elif len(mesh.data.shape_keys.key_blocks) == 1:
print('SAVE')
bpy.ops.object.shape_key_remove(all=True)
meshes.append((mesh, tris))
tris_count += tris
else:
meshes.append((mesh, tris))
tris_count += tris
tools.common.unselect_all()
print(current_tris_count)
print(tris_count)
if (current_tris_count - tris_count) > context.scene.max_tris:
message = 'This model can not be decimated to the given tris count with the specified settings.\n'
if safe_decimation:
message += 'Try to use Custom, Half or Full decimation.'
elif half_decimation:
message += 'Try to use Custom or Full decimation.'
elif custom_decimation:
message += 'Select fewer shape keys and/or meshes or use Full decimation.'
if decimate_fingers:
message = message[:-1] + " or disable 'Save Fingers'."
self.report({'ERROR'}, message)
return
try:
decimation = (context.scene.max_tris - current_tris_count + tris_count) / tris_count
except ZeroDivisionError:
decimation = 1
if decimation >= 1:
self.report({'ERROR'}, 'The model already has less tris than given. Nothing had to be decimated.')
return
elif decimation <= 0:
self.report({'ERROR'}, 'The model could not be decimated to the given tris count. It got decimated as much as possible within the limits.')
meshes.sort(key=lambda x: x[1])
for mesh in reversed(meshes):
mesh_obj = mesh[0]
tris = mesh[1]
tools.common.select(mesh_obj)
print(mesh_obj.name)
# Calculate new decimation ratio
try:
decimation = (context.scene.max_tris - current_tris_count + tris_count) / tris_count
except ZeroDivisionError:
decimation = 1
print(decimation)
# Apply decimation mod
mod = mesh_obj.modifiers.new("Decimate", 'DECIMATE')
mod.ratio = decimation
mod.use_collapse_triangulate = True
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
tris_after = len(mesh_obj.data.polygons)
print(tris)
print(tris_after)
current_tris_count = current_tris_count - tris + tris_after
tris_count = tris_count - tris
tools.common.unselect_all()
# # Check if decimated correctly
# if decimation < 0:
# print('')
# print('RECHECK!')
#
# current_tris_count = 0
# tris_count = 0
#
# for mesh in tools.common.get_meshes_objects():
# tools.common.select(mesh)
# tris = len(bpy.context.active_object.data.polygons)
# tris_count += tris
# print(tris_count)
#
# for mesh in reversed(meshes):
# mesh_obj = mesh[0]
# tools.common.select(mesh_obj)
#
# # Calculate new decimation ratio
# decimation = (context.scene.max_tris - tris_count) / tris_count
# print(decimation)
#
# # Apply decimation mod
# mod = mesh_obj.modifiers.new("Decimate", 'DECIMATE')
# mod.ratio = decimation
# mod.use_collapse_triangulate = True
# bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)
#
# tools.common.unselect_all()
# break