-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathutilOps.py
206 lines (157 loc) · 7.39 KB
/
utilOps.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
import bpy
from . import helper
class BooleanMeshDeformOperator(bpy.types.Operator):
'''Binds a deforming mesh to the object'''
bl_idname = "boolean.mesh_deform"
bl_label = "Mesh deform"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.active_object is not None and len(bpy.context.selected_objects)==2
def execute(self, context):
activeObj = context.active_object
for SelectedObject in bpy.context.selected_objects :
if SelectedObject != activeObj :
md = activeObj.modifiers.new('mesh_deform', 'MESH_DEFORM')
md.object = SelectedObject
bpy.ops.object.meshdeform_bind(modifier="mesh_deform")
bpy.context.scene.objects.active = SelectedObject
SelectedObject.draw_type="WIRE"
bpy.ops.object.mode_set(mode='EDIT')
return {'FINISHED'}
class ModApplyOperator(bpy.types.Operator):
'''Applies all modifiers for all selected objects. Also works in sculpt or edit mode.'''
bl_idname = "boolean.mod_apply"
bl_label = "Apply Modifiers"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
activeObj = context.active_object
for SelectedObject in bpy.context.selected_objects :
bpy.context.scene.objects.active = SelectedObject
oldMode = SelectedObject.mode
bpy.ops.object.mode_set(mode='OBJECT')
for md in SelectedObject.modifiers :
# apply the modifier
try:
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=md.name)
except:
pass
bpy.ops.object.mode_set(mode=oldMode)
bpy.context.scene.objects.active = activeObj
return {'FINISHED'}
class RemeshOperator(bpy.types.Operator):
'''Remesh an object at the given octree depth'''
bl_idname = "sculpt.remesh"
bl_label = "Sculpt Remesh"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.active_object is not None
def draw(self, context):
if context.active_object.mode != 'SCULPT':
wm = context.window_manager
layout = self.layout
layout.prop(wm, "remeshDepthInt", text="Depth")
layout.prop(wm, "remeshSubdivisions", text="Subdivisions")
layout.prop(wm, "remeshPreserveShape", text="Preserve Shape")
def execute(self, context):
# add a smooth remesh modifier
ob = context.active_object
wm = context.window_manager
oldMode = ob.mode
dyntopoOn = False;
if context.active_object.mode == 'SCULPT':
if context.sculpt_object.use_dynamic_topology_sculpting:
dyntopoOn = True
bpy.ops.sculpt.dynamic_topology_toggle()
bpy.ops.object.mode_set(mode='OBJECT')
if wm.remeshPreserveShape:
obCopy = helper.objDuplicate(ob)
md = ob.modifiers.new('sculptremesh', 'REMESH')
md.mode = 'SMOOTH'
md.octree_depth = wm.remeshDepthInt
md.scale = .99
md.use_remove_disconnected = False
# apply the modifier
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="sculptremesh")
if wm.remeshSubdivisions > 0:
mdsub = ob.modifiers.new('RemeshSubSurf', 'SUBSURF')
mdsub.levels = wm.remeshSubdivisions
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="RemeshSubSurf")
if wm.remeshPreserveShape:
md2 = ob.modifiers.new('RemeshShrinkwrap', 'SHRINKWRAP')
md2.wrap_method = 'PROJECT'
md2.use_negative_direction = True
md2.use_positive_direction = True
md2.target = obCopy
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="RemeshShrinkwrap")
bpy.data.scenes[0].objects.unlink(obCopy)
bpy.data.objects.remove(obCopy)
bpy.ops.object.mode_set(mode=oldMode)
if dyntopoOn == True:
bpy.ops.sculpt.dynamic_topology_toggle()
ob.select = True
return {'FINISHED'}
class XMirrorOperator(bpy.types.Operator):
'''Applies an X-axis mirror modifier to the selected object. If more objects are selected, they will be mirrored around the active object.'''
bl_idname = "boolean.mod_xmirror"
bl_label = "X-Mirror"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
activeObj = context.active_object
if len(bpy.context.selected_objects)>1 :
for SelectedObject in bpy.context.selected_objects :
if SelectedObject != activeObj :
oldMode = SelectedObject.mode
bpy.context.scene.objects.active = SelectedObject
bpy.ops.object.mode_set(mode='OBJECT')
md = SelectedObject.modifiers.new('xmirror', 'MIRROR')
md.mirror_object = activeObj
bpy.ops.object.mode_set(mode=oldMode)
bpy.context.scene.objects.active = activeObj
#if there's only one object selected, apply straight fo the active obj.
if len(bpy.context.selected_objects)==1 :
oldMode = activeObj.mode
bpy.ops.object.mode_set(mode='OBJECT')
md = activeObj.modifiers.new('xmirror', 'MIRROR')
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=md.name)
bpy.ops.object.mode_set(mode=oldMode)
return {'FINISHED'}
class DoubleSidedOffOperator(bpy.types.Operator):
'''Turn off double sided for all objects'''
bl_idname = "boolean.double_sided_off"
bl_label = "Double Sided Off"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
for mesh in bpy.data.meshes:
mesh.show_double_sided = False
return {'FINISHED'}
class SymmetrizeBoolMesh(bpy.types.Operator):
"""Copies one side of the mesh to the other along the chosen axis"""
bl_idname = "boolean.grease_symm"
bl_label = "Bool Mesh Symm Function"
bl_options = {'REGISTER', 'UNDO'}
symm_int = bpy.props.FloatProperty(name="Threshold", min = 0.0001, max = 1, default = .001)
@classmethod
def poll(cls, context):
return context.active_object is not None and context.active_object.mode == 'OBJECT' and context.active_object.type == 'MESH' or context.active_object is not None and context.active_object.mode == 'VERTEX_PAINT'
def execute(self, context):
func = bpy.ops
wm = context.window_manager
mode_curr = context.active_object.mode
func.object.editmode_toggle()
func.mesh.select_all(action='SELECT')
func.mesh.symmetrize(direction = wm.bolsymm, threshold= self.symm_int)
func.mesh.remove_doubles()
func.object.editmode_toggle()
if mode_curr == 'VERTEX_PAINT':
func.object.mode_set(mode='VERTEX_PAINT')
return {'FINISHED'}