-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_mesh.py
192 lines (160 loc) · 5.37 KB
/
cleanup_mesh.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
import bpy
from bpy.types import Operator, Object
from bpy.props import BoolProperty
from math import pi
from mathutils import Vector
import bmesh
from typing import List
def cleanup_mesh(context
,obj: Object
,*
,remove_doubles = False
,quadrangulate = False
,weight_normals = True
,seams_from_islands = True
,clear_unused_UVs = True
,rename_single_UV = True
):
if len(obj.data.vertices) == 0:
return
# Mode management
org_active = context.object
org_mode = 'OBJECT'
org_selected = context.selected_objects[:]
if org_active:
org_mode = org_active.mode
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
# Setting auto-smooth to 180 is necessary so that splitnormals_clear() doesn't mark sharp edges
obj.data.use_auto_smooth = True
obj.data.auto_smooth_angle = pi
bpy.ops.mesh.customdata_custom_splitnormals_clear()
if remove_doubles:
bpy.ops.mesh.remove_doubles(threshold=0.00001)
bpy.ops.mesh.mark_sharp(clear=True)
if quadrangulate:
bpy.ops.mesh.tris_convert_to_quads(uvs=True, materials=True)
bpy.ops.object.mode_set(mode='OBJECT')
context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
### Removing useless UVMaps
mesh = obj.data
if clear_unused_UVs:
bm = bmesh.from_edit_mesh(mesh)
for uv_idx in reversed(range(0, len(mesh.uv_layers))): # For each UV layer
delet_this = True
mesh.uv_layers.active_index = uv_idx
bm.faces.ensure_lookup_table()
for face in bm.faces:
for loop in face.loops: # No idea what "loops" are.
loop_on_active = loop[bm.loops.layers.uv.active]
if loop_on_active.uv != Vector((0.0, 1.0)): # If the X or Y of the the loop's UVs first vert is NOT 0
delet_this = False
break
if not delet_this:
break
if delet_this:
obj.data.uv_layers.remove(obj.data.uv_layers[uv_idx])
bmesh.update_edit_mesh(mesh, loop_triangles=True)
# Renaming single UV maps
if len(mesh.uv_layers) == 1 and rename_single_UV:
mesh.uv_layers[0].name = 'UVMap'
# Seams from islands
if seams_from_islands and len(mesh.uv_layers) > 0:
context.scene.tool_settings.use_uv_select_sync = True
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.seams_from_islands(mark_seams=True, mark_sharp=False)
# Mark Sharp
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(type='EDGE')
bpy.ops.mesh.edges_select_sharp(sharpness=(pi/2)-0.01)
bpy.ops.mesh.mark_sharp()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
# Weight normals only works with remove doubles, otherwise throws ZeroDivisionError.
# It also has to come AFTER Mark Sharp for correct results.
if weight_normals and remove_doubles:
m = obj.modifiers.new(name='WN', type='WEIGHTED_NORMAL')
m.keep_sharp = True
bpy.ops.object.modifier_apply(modifier='WN')
# Mode management
for o in org_selected:
o.select_set(True)
if org_active:
context.view_layer.objects.active = org_active
bpy.ops.object.mode_set(mode=org_mode)
class OBJECT_OT_clean_up_game_mesh(Operator):
"""Clean up meshes imported from games"""
bl_idname = "object.mesh_cleanup"
bl_label = "Clean Up Mesh"
bl_options = {'REGISTER', 'UNDO'}
remove_doubles: BoolProperty(
name="Remove Doubles",
description="Enable remove doubles",
default=False
)
quadrangulate: BoolProperty(
name="Tris to Quads",
description="Enable Tris to Quads (UV Seams enabledd)",
default=False
)
weight_normals: BoolProperty(
name="Weight Normals",
description="Enable weighted normals",
default=False
)
seams_from_islands: BoolProperty(
name="Seams from Islands",
description="Create UV seams based on UV islands",
default=False
)
clear_unused_UVs: BoolProperty(
name="Delete Unused UV Maps",
description="If all UV verts' X coordinate is 0, the UV map will be deleted.",
default=True
)
rename_single_UV: BoolProperty(
name="Rename Singular UV Maps",
description="If an object is only left with one UV map, rename it to the default name, 'UVMap'.",
default=True
)
def execute(self, context):
for o in context.selected_objects:
cleanup_mesh(context, o,
remove_doubles = self.remove_doubles,
quadrangulate = self.quadrangulate,
weight_normals = self.weight_normals,
seams_from_islands = self.seams_from_islands,
clear_unused_UVs = self.clear_unused_UVs,
rename_single_UV = self.rename_single_UV)
return {'FINISHED'}
def delete_mesh_with_bad_materials(context, obj: Object, bad_mats: List[str]):
# Object should be selected and active.
# Find indicies of bad materials.
bad_mat_idxs = []
for i, ms in enumerate(obj.material_slots):
if ms.material and ms.material.name in bad_mats:
bad_mat_idxs.append(i)
if not bad_mat_idxs:
return
# Delete the geometry assigned to the bad materials.
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
for i in bad_mat_idxs:
obj.active_material_index = i
bpy.ops.object.material_slot_select()
bpy.ops.mesh.delete(type='VERT')
bpy.ops.object.mode_set(mode='OBJECT')
# Remove the material slot.
for i in reversed(bad_mat_idxs):
obj.active_material_index = i
bpy.ops.object.material_slot_remove()
registry = [
OBJECT_OT_clean_up_game_mesh
]