forked from jayanam/jremesh-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjrt_remesh_op.py
216 lines (148 loc) · 6.49 KB
/
jrt_remesh_op.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
import bpy
from bpy.types import Operator
import os
import shutil
import tempfile
import subprocess
from . jrt_pref import get_preferences
from . utils.select_utils import *
class JRT_OT_Remesh(Operator):
bl_idname = "object.jrt_remesh_op"
bl_label = "Quad remesh"
bl_description = "Execute quad remesher"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return len(context.selected_objects) > 0
def is_blender_quadriflow(self, context):
return context.scene.remesher == "Blender Quadriflow"
def is_instant_meshes(self, context):
return context.scene.remesher == "Instant Meshes"
def get_app_path(self, context):
pref = get_preferences()
if self.is_instant_meshes(context):
return pref.im_filepath
return None
def get_app_name(self, context):
if self.is_instant_meshes(context):
return "Instant Meshes"
return None
def execute(self, context):
# Instant meshes remesher is used
self.report({'INFO'}, "JRemesh started")
scn = context.scene
mode = get_mode()
to_object()
self.try_triangulate(context)
active_obj_name = context.active_object.name
if self.is_instant_meshes(context):
try:
app_name = self.get_app_name(context)
app_path = self.get_app_path(context)
if not os.path.isfile(app_path):
raise IOError(f"Path to {app_name} is missing.")
tmp_dir = tempfile.gettempdir()
orig = os.path.join(tmp_dir, 'orig_object.obj')
output = os.path.join(tmp_dir, 'remeshed_object.obj')
# Export original object
bpy.ops.export_scene.obj(filepath=orig,
check_existing=False,
use_selection=True,
use_mesh_modifiers=True,
use_edges=True,
use_smooth_groups=False,
use_smooth_groups_bitflags=False,
use_normals=True,
use_uvs=True )
orig_object = bpy.data.objects[active_obj_name]
self.do_remesh(app_path, orig, output, context)
# Import remeshed object
bpy.ops.import_scene.obj(filepath=output,
use_split_objects=False,
use_smooth_groups=False,
use_image_search=False)
# Post import remeshed object
remeshed_object = bpy.context.selected_objects[0]
remeshed_object.name = active_obj_name + '_rm'
remeshed_object.data.materials.clear()
for mat in orig_object.data.materials:
remeshed_object.data.materials.append(mat)
for edge in remeshed_object.data.edges:
edge.use_edge_sharp = False
deselect_all()
select(remeshed_object)
bpy.ops.object.shade_flat()
bpy.ops.mesh.customdata_custom_splitnormals_clear()
orig_object.hide_set(True)
make_active(remeshed_object)
os.remove(output)
except IOError as ioerr:
self.report({'ERROR'}, "JRemesh: {0}".format(ioerr))
else:
self.report({'INFO'}, "JRemesh completed")
# Quadriflow remesher is used
elif self.is_blender_quadriflow(context):
try:
# Duplicate active mesh
bpy.ops.object.duplicate()
orig_object = bpy.data.objects[active_obj_name]
bpy.ops.object.quadriflow_remesh(
use_mesh_symmetry=scn.qf_use_mesh_sym,
use_preserve_sharp=scn.qf_preserve_sharp,
use_preserve_boundary=scn.qf_preserve_mesh_boundary,
preserve_paint_mask=scn.qf_preserve_paint_mask,
smooth_normals=scn.qf_smooth_normals,
target_faces=scn.qf_face_count
)
orig_object.hide_set(True)
new_name = active_obj_name + "_rm"
get_active().name = new_name
# Remove modifiers if triangulate was set
if scn.rm_triangulate:
get_active().modifiers.clear()
except RuntimeError as re:
self.report({'ERROR'}, "JRemesh: {0}".format(re))
else:
self.report({'INFO'}, "JRemesh completed")
self.try_make_manifold(scn)
to_mode(mode)
return {'FINISHED'}
def try_triangulate(self, context):
if context.scene.rm_triangulate:
for m in get_active().modifiers:
if m.type == "TRIANGULATE":
return
bpy.ops.object.modifier_add(type='TRIANGULATE')
def try_make_manifold(self, scn):
if scn.rm_fill_holes:
to_edit()
select_mesh()
bpy.ops.mesh.fill_holes()
deselect_mesh()
def do_remesh(self, app_path, orig, output, context):
cmd = self.build_im_command(context, app_path, orig, output)
subprocess.run(cmd)
# Quadriflow modifier can be called?
#
# def build_qf_command(self, context, app_path, orig, output):
# options= []
# if context.scene.qf_sharp:
# options.append('-sharp')
# options.extend(['-i', orig,
# '-o', output,
# '-f', str(context.scene.qf_face_count)])
# return [app_path] + options
def build_im_command(self, context, app_path, orig, output):
options = ['-c', str(context.scene.crease),
'-v', str(context.scene.vertex_count),
'-S', str(context.scene.smooth),
'-o', output]
if context.scene.deterministic:
options.append('-d')
if context.scene.dominant:
options.append('-D')
if context.scene.intrinsic:
options.append('-i')
if context.scene.boundaries:
options.append('-b')
return [app_path] + options + [orig]