-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
331 lines (256 loc) · 11.6 KB
/
__init__.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
bl_info = {
"name": "Crush Plane",
"author": "Seth Trowbidge",
"version": (1, 0, 0),
"blender": (2, 7, 8),
"location": "Edit Mesh > Tools > Crush Plane",
"description": ("Work with planes in edit mesh mode"),
"warning": "", # used for warning icon and text in addons panel
"category": "Mesh"}
import bpy
import bmesh
import mathutils
from mathutils import Vector
from mathutils import Matrix
class CrushPlane:
ProjectVector = Vector((0, 0, -1))
CustomVector = Vector((0, 0, -1))
DrawPlaneName = "CrushPlane Plane"
DrawPlane = False
def SetupPlane():
CrushPlane.DrawPlane = bpy.data.objects.new(CrushPlane.DrawPlaneName, None )
CrushPlane.DrawPlane.empty_draw_type = 'IMAGE'
CrushPlane.DrawPlane.empty_image_offset = [-0.5, -0.5]
CrushPlane.DrawPlane.empty_draw_size = 3
def ShowPlane():
bpy.context.scene.objects.link(CrushPlane.DrawPlane)
def HidePlane():
bpy.context.scene.objects.unlink(CrushPlane.DrawPlane)
def GetNormal():
members = CrushPlane.DrawPlane.matrix_world.col
normal = Vector((members[2][0], members[2][1], members[2][2]))
normal.normalize()
return normal
def GetPosition():
members = CrushPlane.DrawPlane.matrix_world.col
return Vector((members[3][0], members[3][1], members[3][2]))
def SetPlane(inObject):
three = []
mesh = bmesh.from_edit_mesh(inObject.data)
for vert in mesh.verts:
if vert.select:
three.append(inObject.matrix_world * vert.co)
if(len(three) == 3):
three[1] = three[1] - three[0]
three[2] = three[2] - three[0]
CrushPlane.PlaneVector = three[1].cross(three[2])
CrushPlane.PlaneVector.normalize()
CrushPlane.DrawPlane.matrix_world = Matrix.Translation(three[0])
vZ = CrushPlane.PlaneVector
vY = three[1]
vY.normalize()
vX = three[1].cross(CrushPlane.PlaneVector)
CrushPlane.DrawPlane.matrix_world.col[2][0] = vZ.x
CrushPlane.DrawPlane.matrix_world.col[2][1] = vZ.y
CrushPlane.DrawPlane.matrix_world.col[2][2] = vZ.z
CrushPlane.DrawPlane.matrix_world.col[1][0] = vY.x
CrushPlane.DrawPlane.matrix_world.col[1][1] = vY.y
CrushPlane.DrawPlane.matrix_world.col[1][2] = vY.z
CrushPlane.DrawPlane.matrix_world.col[0][0] = -vX.x
CrushPlane.DrawPlane.matrix_world.col[0][1] = -vX.y
CrushPlane.DrawPlane.matrix_world.col[0][2] = -vX.z
return
print("Not Enough Verticies selcted for plane")
def SetProjection(inObject):
two = []
mesh = bmesh.from_edit_mesh(inObject.data)
for vert in mesh.verts:
if vert.select:
two.append(inObject.matrix_world * vert.co)
if(len(two) == 2):
CrushPlane.CustomVector = two[1] - two[0]
return
print("Not Enough Verticies selcted for projection")
def SetPlaneOffset(inObject):
mesh = bmesh.from_edit_mesh(inObject.data)
for vert in mesh.verts:
if vert.select:
one = inObject.matrix_world * vert.co
CrushPlane.DrawPlane.matrix_world.col[3][0] = one.x
CrushPlane.DrawPlane.matrix_world.col[3][1] = one.y
CrushPlane.DrawPlane.matrix_world.col[3][2] = one.z
CrushPlane.PlaneOffset = one.dot(CrushPlane.PlaneVector)
return
#####
def CrushVerticies(inObject):
inverse = inObject.matrix_world.inverted()
mesh = bmesh.from_edit_mesh(inObject.data)
for vert in mesh.verts:
if vert.select:
normal = CrushPlane.GetNormal()
offset = CrushPlane.GetPosition().dot(normal)
worldSpace = inObject.matrix_world * vert.co
vOther = worldSpace + CrushPlane.ProjectVector
dVertex = worldSpace.dot(normal) - offset
dOther = vOther.dot(normal) - offset
dSum = dVertex - dOther
dPercent = dVertex/dSum
worldSpace.x = worldSpace.x + dPercent*(vOther.x - worldSpace.x)
worldSpace.y = worldSpace.y + dPercent*(vOther.y - worldSpace.y)
worldSpace.z = worldSpace.z + dPercent*(vOther.z - worldSpace.z)
vert.co = inverse * worldSpace
bmesh.update_edit_mesh(inObject.data)
###########################
class CrushPlaneSetPlane(bpy.types.Operator):
"""Infer the crush plane from selected geometry"""
bl_idname = "mesh.crush_plane_set_plane"
bl_label = "Crush Plane: Set Plane"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.SetPlane(context.active_object)
return {'FINISHED'}
class CrushPlaneSetPlaneOffset(bpy.types.Operator):
"""Move the crush plane to the selected vertex"""
bl_idname = "mesh.crush_plane_set_plane_offset"
bl_label = "Crush Plane: Set Plane Offset"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.SetPlaneOffset(context.active_object)
return {'FINISHED'}
class CrushPlaneSetProjection(bpy.types.Operator):
"""Infer the crush plane projection from selected geometry"""
bl_idname = "mesh.crush_plane_set_projection"
bl_label = "Crush Plane: Set Projection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.SetProjection(context.active_object)
return {'FINISHED'}
class CrushPlaneCrushCustom(bpy.types.Operator):
"""Crush verticies onto a plane along a direction"""
bl_idname = "mesh.crush_plane_crush_custom"
bl_label = "Crush Plane: Crush"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.ProjectVector = CrushPlane.CustomVector.copy();
CrushPlane.CrushVerticies(context.active_object)
return {'FINISHED'}
class CrushPlaneCrushZ(bpy.types.Operator):
"""Crush along local Z"""
bl_idname = "mesh.crush_plane_crush_z"
bl_label = "Crush Plane: Crush Z"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.ProjectVector = Vector((0, 0, 1))
CrushPlane.CrushVerticies(context.active_object)
return {'FINISHED'}
class CrushPlaneCrushY(bpy.types.Operator):
"""Crush along local Y"""
bl_idname = "mesh.crush_plane_crush_y"
bl_label = "Crush Plane: Crush Y"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.ProjectVector = Vector((0, 1, 0))
CrushPlane.CrushVerticies(context.active_object)
return {'FINISHED'}
class CrushPlaneCrushX(bpy.types.Operator):
"""Crush along local X"""
bl_idname = "mesh.crush_plane_crush_x"
bl_label = "Crush Plane: Crush X"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.ProjectVector = Vector((1, 0, 0))
CrushPlane.CrushVerticies(context.active_object)
return {'FINISHED'}
class CrushPlaneBisect(bpy.types.Operator):
"""Bisect with crush plane"""
bl_idname = "mesh.crush_plane_bisect"
bl_label = "Crush Plane: Bisect"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
bpy.ops.mesh.bisect(plane_co=CrushPlane.GetPosition(), plane_no=CrushPlane.GetNormal())
return {'FINISHED'}
class CrushPlaneSetupPlane(bpy.types.Operator):
"""Generate cut plane object"""
bl_idname = "mesh.crush_plane_setup_plane"
bl_label = "Crush Plane: Setup Plane"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.SetupPlane()
return {'FINISHED'}
class CrushPlaneShowPlane(bpy.types.Operator):
"""Add cut plane to scene"""
bl_idname = "mesh.crush_plane_show_plane"
bl_label = "Crush Plane: Show Plane"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.ShowPlane()
return {'FINISHED'}
class CrushPlaneHidePlane(bpy.types.Operator):
"""Remove cut plane from scene"""
bl_idname = "mesh.crush_plane_hide_plane"
bl_label = "Crush Plane: Hide Plane"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CrushPlane.HidePlane()
return {'FINISHED'}
########
class CrushPlaneUI(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_label = "Crush Plane"
bl_idname = "VIEW3D_PT_crush_plane"
bl_context = "mesh_edit"
bl_category = "Tools"
def draw(self, context):
layout = self.layout
obj = context.object
if(CrushPlane.DrawPlaneName not in bpy.data.objects):
col = layout.column(align=True)
col.operator("mesh.crush_plane_setup_plane", text="Setup")
else:
col = layout.column(align=True)
if(CrushPlane.DrawPlaneName not in bpy.context.scene.objects):
col.operator("mesh.crush_plane_show_plane", text="Show Plane")
else:
col.operator("mesh.crush_plane_hide_plane", text="Hide Plane")
col = layout.column(align=True)
row = col.row(align=True)
row.operator("mesh.crush_plane_set_plane", text="Get Plane")
row.operator("mesh.crush_plane_set_plane_offset", text="Move Plane")
row.operator("mesh.crush_plane_set_projection", text="Get Custom Projection")
col = layout.column(align=True)
row = col.row(align=True)
row.operator("mesh.crush_plane_crush_x", text="X")
row.operator("mesh.crush_plane_crush_y", text="Y")
row.operator("mesh.crush_plane_crush_z", text="Z")
row.operator("mesh.crush_plane_crush_custom", text="Custom")
col = layout.column(align=True)
col.operator("mesh.crush_plane_bisect", text="Bisect")
def register():
bpy.utils.register_class(CrushPlaneSetupPlane)
bpy.utils.register_class(CrushPlaneShowPlane)
bpy.utils.register_class(CrushPlaneHidePlane)
bpy.utils.register_class(CrushPlaneSetPlane)
bpy.utils.register_class(CrushPlaneSetPlaneOffset)
bpy.utils.register_class(CrushPlaneSetProjection)
bpy.utils.register_class(CrushPlaneCrushCustom)
bpy.utils.register_class(CrushPlaneCrushX)
bpy.utils.register_class(CrushPlaneCrushY)
bpy.utils.register_class(CrushPlaneCrushZ)
bpy.utils.register_class(CrushPlaneBisect)
bpy.utils.register_class(CrushPlaneUI)
def unregister():
bpy.utils.unregister_class(CrushPlaneSetupPlane)
bpy.utils.unregister_class(CrushPlaneShowPlane)
bpy.utils.unregister_class(CrushPlaneHidePlane)
bpy.utils.unregister_class(CrushPlaneSetPlane)
bpy.utils.unregister_class(CrushPlaneSetPlaneOffset)
bpy.utils.unregister_class(CrushPlaneSetProjection)
bpy.utils.unregister_class(CrushPlaneCrushCustom)
bpy.utils.unregister_class(CrushPlaneCrushX)
bpy.utils.unregister_class(CrushPlaneCrushY)
bpy.utils.unregister_class(CrushPlaneCrushZ)
bpy.utils.unregister_class(CrushPlaneBisect)
bpy.utils.unregister_class(CrushPlaneUI)
if __name__ == "__main__":
register()