-
Notifications
You must be signed in to change notification settings - Fork 78
/
geometry.py
374 lines (305 loc) · 14.5 KB
/
geometry.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
import bmesh
import mathutils
import time
import math
from bpy.types import Object, Mesh
from . import utils
def get_vertex_colors(mesh: Mesh, obj:Object, numVertices):
if obj.data.color_attributes:
#fill vcolArray(vert_idx + rgb_offset) = color_xyz
vcolArray = bytearray([0] * numVertices * 3)
active_color = obj.data.color_attributes.active_color
color_attribute = mesh.attributes.get(active_color.name, None)
# Pre-calculate vertex base indices for faster access
vertex_indices = [i * 3 for i in range(numVertices)]
for vert, vertex_index in zip(mesh.vertices, vertex_indices):
color_data = color_attribute.data[vert.index]
color = color_data.color_srgb
vcolArray[vertex_index] = int(255 * color[0])
vcolArray[vertex_index +1] = int(255 * color[1])
vcolArray[vertex_index +2] = int(255 * color[2])
else:
print('No vertex colors found')
# Ensure vcolArray is correctly populated
assert len(vcolArray) == numVertices * 3, "GoB vcolArray length mismatch"
return vcolArray
def apply_transformation(me, is_import=True):
mat_transform = None
scale = 1.0
if utils.prefs().use_scale == 'BUNITS':
scale = 1 / bpy.context.scene.unit_settings.scale_length
if utils.prefs().use_scale == 'MANUAL':
scale = 1 / utils.prefs().manual_scale
if utils.prefs().use_scale == 'ZUNITS':
if bpy.context.active_object:
obj = bpy.context.active_object
i, max = utils.max_list_value(obj.dimensions)
scale = 1 / utils.prefs().zbrush_scale * max
if utils.prefs().debug_output:
print("unit scale 2: ", obj.dimensions, i, max, scale, obj.dimensions * scale)
#import
if utils.prefs().flip_up_axis: # fixes bad mesh orientation for some people
if utils.prefs().flip_forward_axis:
if is_import:
me.transform(mathutils.Matrix([
(1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * scale
)
else:
#export
mat_transform = mathutils.Matrix([
(1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * (1/scale)
else:
if is_import:
#import
me.transform(mathutils.Matrix([
(-1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * scale
)
else:
#export
mat_transform = mathutils.Matrix([
(-1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * (1/scale)
else:
if utils.prefs().flip_forward_axis:
if is_import:
#import
me.transform(mathutils.Matrix([
(-1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * scale
)
#me.flip_normals()
else:
#export
mat_transform = mathutils.Matrix([
(-1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * (1/scale)
else:
if is_import:
#import
me.transform(mathutils.Matrix([
(1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * scale
)
else:
#export
mat_transform = mathutils.Matrix([
(1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, -1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0)]) * (1/scale)
return me, mat_transform
def mesh_welder(obj, d = 0.0001):
" merges vertices that are closer than d to each other"
d = utils.prefs().export_merge_distance
bm = bmesh.new()
bm.from_mesh(obj.data)
bmesh.ops.remove_doubles(bm, verts=bm.verts[:], dist=d)
bm.to_mesh(obj.data)
bm.free()
def restore_selection(selected, active):
bpy.ops.object.select_all(action='DESELECT')
for ob in selected:
bpy.data.objects[ob.name].select_set(state=True)
bpy.context.view_layer.objects.active = active
def remove_internal_faces(obj:Object):
"remove internal non-manifold faces where all edges have more than 2 face users https://github.com/JoseConseco/GoB/issues/210"
if utils.prefs().export_remove_internal_faces:
#remember whats selected
selected = bpy.context.selected_objects
active = bpy.context.active_object
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(state=True)
bpy.context.view_layer.objects.active = obj
last_context = obj.mode
last_select_mode = bpy.ops.mesh.select_mode
if utils.prefs().debug_output:
print("last_context: ", last_context, last_select_mode)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(use_extend=True,
use_expand=False,
type='VERT',
action='ENABLE')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_interior_faces() #Select faces where all edges have more than 2 face users
bpy.ops.mesh.select_non_manifold(extend=True,
use_wire=True,
use_boundary=False,
use_multi_face=True,
use_non_contiguous=True, #Non Contiguous, Edges between faces pointing in alternate directions
use_verts=True)
bpy.ops.mesh.delete(type='FACE')
bpy.ops.object.mode_set(mode=last_context)
restore_selection(selected, active)
def apply_modifiers(obj:Object) -> Mesh:
if utils.prefs().performance_profiling:
print("\\_____")
start_time = utils.profiler(time.perf_counter(), f"Export Profiling: {obj.name}")
start_total_time = utils.profiler(time.perf_counter(), "")
depsgraph = bpy.context.evaluated_depsgraph_get()
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh depsgraph")
object_eval = obj.evaluated_get(depsgraph)
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh object_eval")
if utils.prefs().export_modifiers == 'APPLY_EXPORT':
mesh_tmp = bpy.data.meshes.new_from_object(object_eval)
obj.data = mesh_tmp
obj.modifiers.clear()
elif utils.prefs().export_modifiers == 'ONLY_EXPORT':
mesh_tmp = object_eval.to_mesh(preserve_all_data_layers=True, depsgraph=depsgraph)
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh to_mesh")
else:
mesh_tmp = obj.data
#DO the triangulation of Ngons only, but do not write it to original object.
bm = bmesh.new()
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh bmesh new")
bm.from_mesh(mesh_tmp)
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh bmesh")
#join traingles only that are result of ngon triangulation
facesTotTriangulate = [f for f in bm.faces if len(f.edges) > 4]
if facesTotTriangulate:
result = bmesh.ops.triangulate(bm, faces=facesTotTriangulate)
bmesh.ops.join_triangles(
bm, faces = result['faces'],
cmp_seam=False, cmp_sharp=False, cmp_uvs=False,
cmp_vcols=False,cmp_materials=False,
angle_face_threshold=(math.pi), angle_shape_threshold=(math.pi))
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh triangulate1")
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh triangulate2")
mesh_tmp = bpy.data.meshes.new(name=f'{obj.name}_goz') # mesh is deleted in main loop
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh export_mesh")
bm.to_mesh(mesh_tmp)
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh to_mesh")
bm.free()
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh bm free")
obj.to_mesh_clear()
if utils.prefs().performance_profiling:
start_time = utils.profiler(start_time, "Make Mesh to_mesh_clear")
if utils.prefs().performance_profiling:
utils.profiler(start_total_time, "Make Mesh return\n _____/")
return mesh_tmp
def process_linked_objects(obj):
""" TODO: when linked system is finalized it could be possible to provide
# a option to modify the linked object. for now a copy
# of the linked object is created to goz it """
if obj.library:
new_obj = obj.copy()
new_obj.data = obj.data.copy()
bpy.context.view_layer.active_layer_collection.collection.objects.link(new_obj)
new_obj.select_set(state=True)
obj.select_set(state=False)
bpy.context.view_layer.objects.active = new_obj
def clone_as_object(obj, link=True):
" create a new object from a exiting one"
depsgraph = bpy.context.evaluated_depsgraph_get()
obj_to_clone = obj.evaluated_get(depsgraph)
#mesh_clone = obj.to_mesh(preserve_all_data_layers=True, depsgraph=depsgraph)
mesh_clone = bpy.data.meshes.new_from_object(obj_to_clone)
mesh_clone.transform(obj.matrix_world)
obj_clone = bpy.data.objects.new((obj.name + '_' + obj.type), mesh_clone)
if link:
bpy.context.view_layer.active_layer_collection.collection.objects.link(obj_clone)
return obj_clone
def check_export_candidates(obj):
if obj.type in {'MESH'}:
if not utils.prefs().export_modifiers in {'IGNORE'}:
if obj.modifiers:
for modifier in obj.modifiers:
geometry_modifiers=['Skin', 'Screw']
if modifier.name in geometry_modifiers and modifier.show_viewport:
# a mesh can have 0 faces but a modifier which adds polygons which makes is a valid export object
#print("numfaces 0, skin modifier: ", modifier.name in ['Skin'] and modifier.show_viewport)
return modifier.name in geometry_modifiers and modifier.show_viewport
else:
# when the modifier is disabled
# - it can result in 0 faces which makes it a invalid export candidate
# - or in a mesh with more than 0 faces which makes it a valid export candidate
numFaces = len(obj.data.polygons)
#print("numfaces 1, no skin modifiers: ", numFaces)
else:
#when a object has no modifiers, enable export when it has polygons, else disable
numFaces = len(obj.data.polygons)
#print("numfaces 2 modifier export: ", numFaces)
else:
# if export modifers is Ignored check for polygons to identify export candidates
numFaces = len(obj.data.polygons)
#print("numfaces 3 no active modifiers: ", numFaces)
elif obj.type in {'SURFACE', 'FONT', 'META'}:
#allow export for non mesh type objects
return True
elif obj.type in {'CURVE'}:
# curves will only get faces when they have a bevel or a extrude
if bpy.data.curves[obj.data.name].bevel_depth or bpy.data.curves[obj.data.name].extrude:
#print(bpy.data.curves[obj.name].bevel_depth , bpy.data.curves[obj.name].extrude)
return True
else:
return False
else:
if utils.prefs().debug_output:
print("GoB: unsupported object type:", obj.type)
return False
return numFaces
def export_poll(cls, context):
# do not allow export if no objects are selected
if not context.selected_objects:
return False
# if one object is selected, check amount of faces. 0 faces will crash zbrush!
elif len(context.selected_objects) == 1:
if context.active_object:
obj = context.active_object
export = check_export_candidates(obj)
else:
for obj in context.selected_objects:
export = check_export_candidates(obj)
#check for faces in multiple objects, only if any face in object is found exporting should be allowed
else:
exportCandidates=[]
for obj in context.selected_objects:
candidate = check_export_candidates(obj)
exportCandidates.append(candidate)
#print("any export candidate: ", any(exportCandidates))
export = any(exportCandidates)
return export