-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmesh2blender.py
More file actions
136 lines (114 loc) · 4.88 KB
/
mesh2blender.py
File metadata and controls
136 lines (114 loc) · 4.88 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
"""Mesh2Blender - load regional mesh created by Iso2Mesh back to Blender
* Authors: (c) 2021-2022 Qianqian Fang <q.fang at neu.edu>
(c) 2021 Yuxuan Zhang <zhang.yuxuan1 at northeastern.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
import os
from .utils import *
from .dependencies import safe_import, require_dependency
# Safe imports
jd = safe_import("jdata")
g_volmeshtype = "1"
enum_volmeshtype = [
("1", "Rasterization Mesh", "Run MCX"),
("2", "Tetrahedral Mesh", "Run MMC"),
]
class mesh2sceneTmesh(bpy.types.Operator):
bl_label = "Load tetrahedral mesh and setup simulation"
bl_description = "Import tetrahedral mesh to Blender for MMC photon simulations. Please remember to set the optical properties to each region"
bl_idname = "blenderphotonics.tmeshtoscene"
def importmesh(self):
if not require_dependency("jdata", "mesh import operations"):
return
# folder path for importing .jmsh files
outputdir = GetBPWorkFolder()
# Load tetrahedral mesh, remove all objects and load region mesh
for obj in bpy.data.objects:
bpy.data.objects.remove(obj)
bpy.ops.outliner.orphans_purge(do_recursive=True)
regiondata = jd.load(os.path.join(outputdir, "regionTmesh.jmsh"))
LoadReginalMesh(regiondata, "region_")
for obj in bpy.data.objects:
obj["mua"] = 0.001
obj["mus"] = 0.1
obj["g"] = 0.0
obj["n"] = 1.37
## add light source
light_data = bpy.data.lights.new(name="Lightsource", type="SPOT")
light_object = bpy.data.objects.new(name="Lightsource", object_data=light_data)
bpy.context.collection.objects.link(light_object)
bpy.context.view_layer.objects.active = light_object
light_object.location = (0, 0, 5)
light_object.scale = (0.1, 0.1, 1)
dg = bpy.context.evaluated_depsgraph_get()
dg.update()
# add cfg option
obj = bpy.data.objects["Lightsource"]
obj["nphoton"] = 10000
obj["srctype"] = "pencil"
obj["srcparam1"] = [0.0, 0.0, 0.0, 0.0]
obj["srcparam2"] = [0.0, 0.0, 0.0, 0.0]
obj["unitinmm"] = 1
def execute(self, context):
print("begin to set up tetrahedral mesh and light source")
self.importmesh()
return {"FINISHED"}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class mesh2sceneVmesh(bpy.types.Operator):
bl_label = "Load voxel mesh and setup simulation"
bl_description = "Import voxel mesh to Blender for MCX photon simulations. Please remember to set the optical properties to each region"
bl_idname = "blenderphotonics.vmeshtoscene"
def importmesh(self):
if not require_dependency("jdata", "mesh import operations"):
return
# folder path for importing .jmsh files
outputdir = GetBPWorkFolder()
# Load voxel mesh, remove all objects and load region mesh
for obj in bpy.data.objects:
bpy.data.objects.remove(obj)
bpy.ops.outliner.orphans_purge(do_recursive=True)
regiondata = jd.load(os.path.join(outputdir, "regionVmesh.jmsh"))
LoadReginalMesh(regiondata, "region_")
for obj in bpy.data.objects:
obj["mua"] = 0.001
obj["mus"] = 0.1
obj["g"] = 0.0
obj["n"] = 1.37
## add light source
light_data = bpy.data.lights.new(name="Lightsource", type="SPOT")
light_object = bpy.data.objects.new(name="Lightsource", object_data=light_data)
bpy.context.collection.objects.link(light_object)
bpy.context.view_layer.objects.active = light_object
light_object.location = (0, 0, 5)
light_object.scale = (0.1, 0.1, 1)
dg = bpy.context.evaluated_depsgraph_get()
dg.update()
# add cfg option
obj = bpy.data.objects["Lightsource"]
obj["nphoton"] = 10000
obj["srctype"] = "pencil"
obj["srcparam1"] = [0.0, 0.0, 0.0, 0.0]
obj["srcparam2"] = [0.0, 0.0, 0.0, 0.0]
obj["unitinmm"] = 1
def execute(self, context):
print("begin to set up voxel mesh and light source")
self.importmesh()
return {"FINISHED"}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)