-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathobj2surf.py
More file actions
522 lines (443 loc) · 17.9 KB
/
obj2surf.py
File metadata and controls
522 lines (443 loc) · 17.9 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
"""Blender2Surf - extracting, converting and processing Blender object surfaces
* Authors: (c) 2021-2022 Qianqian Fang <q.fang at neu.edu>
* License: GNU General Public License V3 or later (GPLv3)
* Website: http://mcx.space/bp
To cite this work, please use the below information
@article{BlenderPhotonics2022,
author = {Yuxuan Zhang and Qianqian Fang},
title = {{BlenderPhotonics: an integrated open-source software environment for three-dimensional meshing and photon simulations in complex tissues}},
volume = {27},
journal = {Journal of Biomedical Optics},
number = {8},
publisher = {SPIE},
pages = {1 -- 23},
year = {2022},
doi = {10.1117/1.JBO.27.8.083014},
URL = {https://doi.org/10.1117/1.JBO.27.8.083014}
}
"""
import bpy
from bpy_extras.io_utils import ImportHelper
import os
from bpy.utils import register_class, unregister_class
from .utils import *
from .dependencies import safe_import, require_dependency, show_error_message
# Safe imports
np = safe_import("numpy")
jd = safe_import("jdata")
def surf2jmesh(filename):
"""
Python implementation of surf2jmesh.m
Load a triangular surface mesh from a file
Parameters:
-----------
filename : str
Path to the surface mesh file. Supports formats:
- OFF (.off)
- MEDIT (.medit)
- Tetgen (.ele)
- JMesh (.jmsh/.json)
- Binary JMesh (.bmsh)
- STL (.stl)
- SMF (.smf)
- GTS (.gts)
Returns:
--------
dict
Dictionary containing:
- MeshVertex3: Nx3 array of vertex coordinates
- MeshTri3(1): Mx3 array of triangular surface elements (indexed for consistency with regional meshes)
"""
import re
import os.path
# Convert filename to lowercase for pattern matching
filename_lower = filename.lower()
nodedata = {}
try:
if re.search(r"\.off$", filename_lower):
# OFF format
if not require_dependency("iso2mesh", "OFF file loading"):
return None
from iso2mesh import readoff
vertex, faces = readoff(filename)
nodedata["MeshVertex3"] = vertex
nodedata["MeshTri3(1)"] = faces
elif re.search(r"\.medit$", filename_lower):
# MEDIT format
if not require_dependency("iso2mesh", "MEDIT file loading"):
return None
from iso2mesh import readmedit, volface
vertex, elem = readmedit(filename)
nodedata["MeshVertex3"] = vertex
if elem.shape[1] >= 4:
# Extract surface faces from tetrahedra
nodedata["MeshTri3(1)"] = volface(elem[:, :4])
else:
nodedata["MeshTri3(1)"] = elem
elif re.search(r"\.ele$", filename_lower):
# Tetgen format
if not require_dependency("iso2mesh", "Tetgen file loading"):
return None
from iso2mesh import readtetgen, volface
pathstr, name = os.path.split(filename)
name_no_ext = os.path.splitext(name)[0]
vertex, elem = readtetgen(os.path.join(pathstr, name_no_ext))
nodedata["MeshVertex3"] = vertex
nodedata["MeshTri3(1)"] = volface(elem[:, :4])
elif re.search(r"\.(jmsh|json)$", filename_lower):
# JMesh JSON format
if not require_dependency("jdata", "JSON/JMesh file loading"):
return None
nodedata = jd.load(filename)
# Convert MeshTri3 to MeshTri3(1) if it exists
if "MeshTri3" in nodedata and "MeshTri3(1)" not in nodedata:
nodedata["MeshTri3(1)"] = nodedata.pop("MeshTri3")
elif re.search(r"\.bmsh$", filename_lower):
# Binary JMesh format
if not require_dependency("jdata", "binary JMesh file loading"):
return None
nodedata = jd.load(filename)
# Convert MeshTri3 to MeshTri3(1) if it exists
if "MeshTri3" in nodedata and "MeshTri3(1)" not in nodedata:
nodedata["MeshTri3(1)"] = nodedata.pop("MeshTri3")
elif re.search(r"\.stl$", filename_lower):
# STL format - try to use readoff as fallback
try:
if not require_dependency("iso2mesh", "STL file loading"):
return None
from iso2mesh import readoff
vertex, faces = readoff(filename)
nodedata["MeshVertex3"] = vertex
nodedata["MeshTri3(1)"] = faces
except:
show_error_message(
"STL format not fully supported. Please convert to OFF or JMesh format.",
"Format Error",
)
return None
elif re.search(r"\.smf$", filename_lower):
# SMF format - try to use readoff as fallback
try:
if not require_dependency("iso2mesh", "SMF file loading"):
return None
from iso2mesh import readoff
vertex, faces = readoff(filename)
nodedata["MeshVertex3"] = vertex
nodedata["MeshTri3(1)"] = faces
except:
show_error_message(
"SMF format not fully supported. Please convert to OFF or JMesh format.",
"Format Error",
)
return None
elif re.search(r"\.gts$", filename_lower):
# GTS format
if not require_dependency("iso2mesh", "GTS file loading"):
return None
from iso2mesh import readgts
vertex, faces = readgts(filename)
nodedata["MeshVertex3"] = vertex
nodedata["MeshTri3(1)"] = faces
else:
show_error_message(f"Unsupported file format: {filename}", "Format Error")
return None
# Ensure we have the required fields
if "MeshVertex3" not in nodedata or "MeshTri3(1)" not in nodedata:
show_error_message(
"Invalid mesh data: missing vertex or face information", "Data Error"
)
return None
# Convert to numpy arrays if they aren't already and numpy is available
if np and hasattr(np, "array"):
if not isinstance(nodedata["MeshVertex3"], np.ndarray):
nodedata["MeshVertex3"] = np.array(nodedata["MeshVertex3"])
if not isinstance(nodedata["MeshTri3(1)"], np.ndarray):
nodedata["MeshTri3(1)"] = np.array(nodedata["MeshTri3(1)"])
return nodedata
except Exception as e:
show_error_message(f"Error loading surface mesh: {str(e)}", "Load Error")
return None
g_action = "repair"
g_actionparam = 1.0
g_convtri = True
enum_action = [
(
"import",
"Import surface mesh from file",
"Import surface mesh from JMesh/STL/OFF/SMF/ASC/MEDIT/GTS to Blender",
),
(
"export",
"Export selected to JSON/JMesh",
"Export selected objects to JSON/JMesh exchange file",
),
(
"boolean-resolve",
"Boolean-resolve: Two meshes slice each other",
"Output both objects, with each surface intersected by the other",
),
(
"boolean-first",
"Boolean-first: 1st mesh sliced by the 2nd",
"Return the 1st object but sliced by the 2nd object",
),
(
"boolean-second",
"Boolean-second: 2nd mesh sliced by the 1st",
"Return the 2nd object but sliced by the 1st object",
),
(
"boolean-diff",
"Boolean-diff: 1st mesh subtract 2nd",
"Return the 1st object subtracted by the 2nd",
),
(
"boolean-and",
"Boolean-and: Space in both objects",
"Return the surface of the overlapping region",
),
(
"boolean-or",
"Boolean-or: Space for joint/union space",
"Return the outer surface of the merged object",
),
(
"boolean-decouple",
"Boolean-decouple: Decouple two shell meshes",
"Insert a small gap between two touching objects",
),
("simplify", "Surface simplification", "Simplifing a surface by decimating edges"),
("remesh", "Remesh surface", "Remesh surface and remove badly shaped triangles"),
("smooth", "Smooth selected objects", "Smooth selected mesh object"),
(
"reorient",
"Reorient all triangles",
"Reorient all triangles in counter-clockwise direction",
),
(
"repair",
"Fix self-intersection and holes",
"Fix self-intersection and fill holes of a closed object",
),
]
class object2surf(bpy.types.Operator):
bl_label = "Process selected object surfaces"
bl_description = "Create surface meshes from selected objects (smoothing, refine, Boolean, repairing, simplification, ...)"
bl_idname = "blenderphotonics.blender2surf"
# creat a interface to set uesrs' model parameter.
bl_options = {"REGISTER", "UNDO"}
action: bpy.props.EnumProperty(
default=g_action, name="Operation", items=enum_action
)
actionparam: bpy.props.FloatProperty(
default=g_actionparam, name="Operation parameter"
)
convtri: bpy.props.BoolProperty(
default=g_convtri, name="Convert to triangular mesh first"
)
@classmethod
def description(cls, context, properties):
hints = {}
for item in enum_action:
hints[item[0]] = item[2]
return hints[properties.action]
def func(self):
if not require_dependency("jdata", "surface mesh operations"):
return
if not require_dependency("numpy", "numerical operations"):
return
outputdir = GetBPWorkFolder()
if not os.path.isdir(outputdir):
os.makedirs(outputdir)
if os.path.exists(os.path.join(outputdir, "surfacemesh.jmsh")):
os.remove(os.path.join(outputdir, "surfacemesh.jmsh"))
if len(bpy.context.selected_objects) < 1:
ShowMessageBox(
"Must select at least one object (for Boolean operations, select two)",
"BlenderPhotonics",
)
return
# remove camera and light objects
for ob in bpy.context.selected_objects:
print(ob.type)
if (
ob.type == "CAMERA"
or ob.type == "LIGHT"
or ob.type == "EMPTY"
or ob.type == "LAMP"
or ob.type == "SPEAKER"
):
ob.select_set(False)
bpy.ops.object.convert(target="MESH")
bpy.ops.object.mode_set(mode="EDIT")
if self.convtri:
bpy.ops.mesh.quads_convert_to_tris(
quad_method="BEAUTY", ngon_method="BEAUTY"
)
if len(bpy.context.selected_objects) < 1:
ShowMessageBox("No mesh-like object was selected, skip", "BlenderPhotonics")
return
bpy.ops.object.mode_set(mode="OBJECT")
surfdata = {
"_DataInfo_": {
"JMeshVersion": "0.5",
"Comment": "Object surface mesh created by BlenderPhotonics (http:\/\/mcx.space\/BlenderPhotonics)",
}
}
surfdata["MeshGroup"] = []
for ob in bpy.context.selected_objects:
objsurf = GetNodeFacefromObject(ob, self.convtri)
surfdata["MeshGroup"].append(objsurf)
surfdata["param"] = {"action": self.action, "level": self.actionparam}
jd.save(surfdata, os.path.join(outputdir, "blendersurf.jmsh"))
# at this point, objects are converted to mesh if possible
if self.action == "export":
bpy.ops.object2surf.invoke_export("INVOKE_DEFAULT")
return
try:
# Use Python-based implementation instead of backend selection
# TODO: Implement Python-based obj2surf functionality
show_error_message(
"obj2surf functionality is being updated to work with Python directly. Please check back later.",
"Python Implementation",
)
return
except ImportError as e:
show_error_message(
f"Python implementation failed: {str(e)}.", "Implementation Error"
)
return
# TODO: Replace with Python implementation
# oc.addpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),'script'))
# oc.feval('blender2surf',os.path.join(outputdir,'blendersurf.jmsh'), nargout=0)
# import volum mesh to blender(just for user to check the result)
if len(bpy.context.selected_objects) >= 1:
bpy.ops.object.delete()
surfdata = jd.load(os.path.join(outputdir, "surfacemesh.jmsh"))
idx = 1
if len(surfdata["MeshGroup"]) > 0:
ob = surfdata["MeshGroup"]
objname = "surf_" + str(idx)
if "MeshVertex3" in ob:
if ("_DataInfo_" in ob) and ("BlenderObjectName" in ob["_DataInfo_"]):
objname = ob["_DataInfo_"]["BlenderObjectName"]
AddMeshFromNodeFace(
ob["MeshVertex3"], (np.array(ob["MeshTri3"]) - 1).tolist(), objname
)
bpy.context.view_layer.objects.active = bpy.data.objects[objname]
else:
for ob in surfdata["MeshGroup"]:
objname = "surf_" + str(idx)
if ("_DataInfo_" in ob) and (
"BlenderObjectName" in ob["_DataInfo_"]
):
objname = ob["_DataInfo_"]["BlenderObjectName"]
AddMeshFromNodeFace(
ob["MeshVertex3"],
(np.array(ob["MeshTri3"]) - 1).tolist(),
objname,
)
bpy.context.view_layer.objects.active = bpy.data.objects[objname]
idx += 1
bpy.context.space_data.shading.type = "WIREFRAME"
ShowMessageBox(
"Mesh generation is complete. The combined surface mesh is imported for inspection.",
"BlenderPhotonics",
)
def execute(self, context):
print("begin to process object surface mesh")
self.func()
return {"FINISHED"}
def invoke(self, context, event):
if not self.action == "import":
return context.window_manager.invoke_props_dialog(self)
else:
return bpy.ops.object2surf.invoke_import("INVOKE_DEFAULT")
#
# Dialog to set meshing properties
#
class setmeshingprop(bpy.types.Panel):
bl_label = "Object surface mesh"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
global g_action, g_actionparam, g_convtri
self.layout.operator("object.dialog_operator")
# This operator will open Blender's file chooser when invoked
# and store the selected filepath in self.filepath and print it
# to the console using window_manager.fileselect_add()
class OBJECT2SURF_OT_invoke_export(bpy.types.Operator):
bl_idname = "object2surf.invoke_export"
bl_label = "Export to JMesh"
bl_description = "Export mesh in the JSON/JMesh format"
filepath: bpy.props.StringProperty(default="", subtype="DIR_PATH")
def execute(self, context):
print(self.filepath)
if not (self.filepath == ""):
if os.name == "nt":
os.popen(
"copy '"
+ os.path.join(GetBPWorkFolder(), "blendersurf.jmsh")
+ "' '"
+ self.filepath
+ "'"
)
else:
os.popen(
"cp '"
+ os.path.join(GetBPWorkFolder(), "blendersurf.jmsh")
+ "' '"
+ self.filepath
+ "'"
)
return {"FINISHED"}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {"RUNNING_MODAL"}
register_class(OBJECT2SURF_OT_invoke_export)
# This operator will open Blender's file chooser when invoked
# and store the selected filepath in self.filepath and print it
# to the console using window_manager.fileselect_add()
class OBJECT2SURF_OT_invoke_import(bpy.types.Operator, ImportHelper):
bl_idname = "object2surf.invoke_import"
bl_label = "Import Mesh"
bl_description = (
"Import triangular surfaces in .json,.jmsh,.bmsh,.off,.medit,.stl,.smf,.gts"
)
filename_ext = "*.json;*.jmsh;*.bmsh;*.off;*.medit;*.stl;*.smf;*.gts"
filepath: bpy.props.StringProperty(default="", subtype="DIR_PATH")
filter_glob: bpy.props.StringProperty(
default="*.json;*.jmsh;*.bmsh;*.off;*.medit;*.stl;*.smf;*.gts",
options={"HIDDEN"},
description="Reading triangular surface mesh from *.json;*.jmsh;*.bmsh;*.off;*.medit;*.stl;*.smf;*.gts",
maxlen=2048, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
if not require_dependency("jdata", "JSON/JMesh import"):
return {"CANCELLED"}
if not require_dependency("numpy", "numerical operations"):
return {"CANCELLED"}
if not require_dependency("iso2mesh", "mesh file import"):
return {"CANCELLED"}
try:
surfdata = surf2jmesh(self.filepath)
if surfdata is None:
show_error_message(
f"Failed to load surface mesh from {self.filepath}", "Import Error"
)
return {"CANCELLED"}
AddMeshFromNodeFace(
surfdata["MeshVertex3"],
(np.array(surfdata["MeshTri3(1)"]) - 1).tolist(),
"importedsurf",
)
return {"FINISHED"}
except Exception as e:
show_error_message(
f"Error importing surface mesh: {str(e)}", "Import Error"
)
return {"CANCELLED"}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {"RUNNING_MODAL"}
register_class(OBJECT2SURF_OT_invoke_import)