forked from blender/blender-addons-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_easy_lattice.py
735 lines (443 loc) · 16.8 KB
/
mesh_easy_lattice.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": " Easy Lattice",
"author": "Kursad Karatas / Mechanical Mustache Labs",
"version": ( 1, 0, 1 ),
"blender": ( 2, 80,0 ),
"location": "View3D > EZ Lattice",
"description": "Create a lattice for shape editing",
"warning": "",
"doc_url": "",
"tracker_url": "",
"category": "Mesh"}
import bpy
import copy
import bmesh
from bpy.app.handlers import persistent
from bpy.props import (EnumProperty, FloatProperty, FloatVectorProperty,
IntProperty, StringProperty, BoolProperty)
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Matrix, Vector
import mathutils
import numpy as np
LAT_TYPES= ( ( 'KEY_LINEAR', 'KEY_LINEAR', '' ), ( 'KEY_CARDINAL', 'KEY_CARDINAL', '' ), ( 'KEY_BSPLINE', 'KEY_BSPLINE', '' ) )
OP_TYPES= ( ( 'NEW', 'NEW', '' ), ( 'APPLY', 'APPLY', '' ), ( 'CLEAR', 'CLEAR', '' ) )
def defineSceneProps():
bpy.types.Scene.ezlattice_object = StringProperty(name="Object2Operate",
description="Object to be operated on",
default="")
bpy.types.Scene.ezlattice_objects = StringProperty(name="Objects2Operate",
description="Objects to be operated on",
default="")
bpy.types.Scene.ezlattice_mode = StringProperty(name="CurrentMode",
default="")
bpy.types.Scene.ezlattice_lattice = StringProperty(name="LatticeObName",
default="")
bpy.types.Scene.ezlattice_flag = BoolProperty(name="LatticeFlag", default=False)
def defineObjectProps():
bpy.types.Object.ezlattice_flag = BoolProperty(name="LatticeFlag", default=False)
bpy.types.Object.ezlattice_controller = StringProperty(name="LatticeController", default="")
bpy.types.Object.ezlattice_modifier = StringProperty(name="latticeModifier", default="")
def objectMode():
if isEditMode():
bpy.ops.object.mode_set(mode="OBJECT")
else:
return True
return
def isEditMode():
"""Check to see we are in edit mode
"""
try:
if bpy.context.object.mode == "EDIT":
return True
else:
return False
except:
print("No active mesh object")
def isObjectMode():
if bpy.context.object.mode == "OBJECT":
return True
else:
return False
def setMode(mode=None):
if mode:
bpy.ops.object.mode_set(mode=mode)
def curMode():
return bpy.context.object.mode
def editMode():
if not isEditMode():
bpy.ops.object.mode_set(mode="EDIT")
else:
return True
return
def setSelectActiveObject(context, obj):
if context.mode == 'OBJECT':
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = obj
obj.select_set(True)
def getObject(name=None):
try:
ob=[o for o in bpy.data.objects if o.name == name][0]
return ob
except:
return None
def buildTrnScl_WorldMat( obj ):
# This function builds a real world matrix that encodes translation and scale and it leaves out the rotation matrix
# The rotation is applied at obejct level if there is any
loc,rot,scl=obj.matrix_world.decompose()
mat_trans = mathutils.Matrix.Translation( loc)
mat_scale = mathutils.Matrix.Scale( scl[0], 4, ( 1, 0, 0 ) )
mat_scale @= mathutils.Matrix.Scale( scl[1], 4, ( 0, 1, 0 ) )
mat_scale @= mathutils.Matrix.Scale( scl[2], 4, ( 0, 0, 1 ) )
mat_final = mat_trans @ mat_scale
return mat_final
def getSelectedVerts(context):
"""
https://devtalk.blender.org/t/foreach-get-for-selected-vertex-indices/7712/6
v_sel = np.empty(len(me.vertices), dtype=bool)
me.vertices.foreach_get('select', v_sel)
sel_idx, = np.where(v_sel)
unsel_idx, = np.where(np.invert(v_sel))
"""
obj = context.active_object
m=obj.matrix_world
count=len(obj.data.vertices)
shape = (count, 3)
if obj.type=='MESH':
me = obj.data
bm = bmesh.from_edit_mesh(me)
verts=[[email protected] for v in bm.verts if v.select]
return np.array(verts, dtype=np.float32)
def getSelectedVertsNumPy(context):
obj = context.active_object
if obj.type=='MESH':
obj.update_from_editmode()
#BMESH
me = obj.data
bm = bmesh.from_edit_mesh(me)
verts_selected=np.array([v.select for v in bm.verts])
count=len(obj.data.vertices)
shape = (count, 3)
co = np.empty(count*3, dtype=np.float32)
obj.data.vertices.foreach_get("co",co)
co.shape=shape
return co[verts_selected]
def findSelectedVertsBBoxNumPy(context):
verts=getSelectedVerts(context)
x_min = verts[:,0].min()
y_min = verts[:,1].min()
z_min = verts[:,2].min()
x_max = verts[:,0].max()
y_max = verts[:,1].max()
z_max = verts[:,2].max()
x_avg = verts[:,0].mean()
y_avg = verts[:,1].mean()
z_avg = verts[:,2].mean()
middle=Vector( ( (x_min+x_max)/2,
(y_min+y_max)/2,
(z_min+z_max)/2 )
)
bbox= [ np.array([x_max,y_max,z_max], dtype=np.float32),
np.array([x_min, y_min, z_min], dtype=np.float32),
np.array([x_avg, y_avg, z_avg], dtype=np.float32),
np.array(middle)
]
return bbox
def addSelected2VertGrp():
C=bpy.context
grp=C.active_object.vertex_groups.new(name=".templatticegrp")
bpy.ops.object.vertex_group_assign()
bpy.ops.object.vertex_group_set_active( group = grp.name )
def removetempVertGrp():
C=bpy.context
grp=[g for g in C.active_object.vertex_groups if ".templatticegrp" in g.name]
if grp:
for g in grp:
bpy.context.object.vertex_groups.active_index = g.index
bpy.ops.object.vertex_group_remove(all=False, all_unlocked=False)
def cleanupLatticeObjects(context):
cur_obj=context.active_object
try:
lats=[l for l in bpy.data.objects if ".latticetemp" in l.name]
if lats:
for l in lats:
setSelectActiveObject(context, l)
bpy.data.objects.remove(l)
bpy.ops.ed.undo_push()
setSelectActiveObject(context, cur_obj)
except:
print("no cleanup")
def cleanupLatticeModifier(context):
scn=context.scene
obj_operated_name=scn.ezlattice_object
obj_operated=getObject(obj_operated_name)
curmode=curMode()
temp_mod=None
obj=None
if obj_operated:
if context.active_object.type=='LATTICE':
setMode('OBJECT')
setSelectActiveObject(context, obj_operated )
temp_mod=[m for m in obj_operated.modifiers if ".LatticeModTemp" in m.name]
obj=obj_operated
else:
temp_mod=[m for m in context.object.modifiers if ".LatticeModTemp" in m.name]
obj=context.object
if temp_mod:
for m in temp_mod:
bpy.ops.object.modifier_remove(modifier=m.name)
setMode(curmode)
return True
return False
def cleanupApplyPre(context):
scn=context.scene
obj_operated_name=scn.ezlattice_object
obj_operated=getObject(obj_operated_name)
cur_mode=curMode()
if obj_operated:
if context.active_object.type=='LATTICE':
setMode('OBJECT')
setSelectActiveObject(context, obj_operated )
temp_mod=[m for m in obj_operated.modifiers if ".LatticeModTemp" in m.name]
lats=[l for l in bpy.data.objects if ".latticetemp" in l.name]
cur_obj=context.active_object
curmode=curMode()
if isEditMode():
objectMode()
if temp_mod:
for m in temp_mod:
if m.object:
bpy.ops.object.modifier_apply(modifier=m.name)
else:
bpy.ops.object.modifier_remove(modifier=m.name)
if lats:
for l in lats:
bpy.data.objects.remove(l)
bpy.ops.ed.undo_push()
setSelectActiveObject(context, cur_obj)
setMode(curmode)
bpy.ops.object.mode_set(mode=curmode)
def createLatticeObject(context, loc=Vector((0,0,0)), scale=Vector((1,1,1)),
name=".latticetemp", divisions=[], interp="KEY_BSPLINE"):
C=bpy.context
lat_name=name+"_"+C.object.name
lat = bpy.data.lattices.new( lat_name )
ob = bpy.data.objects.new( lat_name, lat )
ob.data.use_outside=True
ob.data.points_u=divisions[0]
ob.data.points_v=divisions[1]
ob.data.points_w=divisions[2]
ob.data.interpolation_type_u = interp
ob.data.interpolation_type_v = interp
ob.data.interpolation_type_w = interp
scene = context.scene
scene.collection.objects.link(ob)
ob.location=loc
ob.scale = scale
return ob
def applyLatticeModifier():
try:
temp_mod=[m for m in bpy.context.object.modifiers if ".LatticeModTemp" in m.name]
if temp_mod:
for m in temp_mod:
bpy.ops.object.modifier_apply(modifier=m.name)
except:
print("no modifiers")
def addLatticeModifier(context, lat,vrtgrp=""):
bpy.ops.object.modifier_add(type='LATTICE')
bpy.context.object.modifiers['Lattice'].name=".LatticeModTemp"
bpy.context.object.modifiers[".LatticeModTemp"].object=lat
bpy.context.object.modifiers[".LatticeModTemp"].vertex_group=vrtgrp
bpy.context.object.modifiers[".LatticeModTemp"].show_in_editmode = True
bpy.context.object.modifiers[".LatticeModTemp"].show_on_cage = True
def newLatticeOp(obj, context,self):
scn=context.scene
if scn.ezlattice_flag:
applyLatticeOp(obj, context)
scn.ezlattice_flag=False
scn.ezlattice_mode=""
return
cur_obj=obj
scn.ezlattice_object=cur_obj.name
scn.ezlattice_mode=curMode()
cleanupApplyPre(context)
removetempVertGrp()
addSelected2VertGrp()
bbox=findSelectedVertsBBoxNumPy(context)
scale=bbox[0]-bbox[1]
loc_crs=Vector((bbox[3][0],bbox[3][1],bbox[3][2]))
lat=createLatticeObject(context, loc=loc_crs, scale=scale,
divisions=[self.lat_u,self.lat_w,self.lat_m],
interp=self.lat_type )
scn.ezlattice_lattice=lat.name
setSelectActiveObject(context, cur_obj)
addLatticeModifier(context, lat=lat, vrtgrp=".templatticegrp")
objectMode()
setSelectActiveObject(context, lat)
editMode()
scn.ezlattice_flag=True
return
def resetHelperAttrbs(context):
scn=context.scene
scn.ezlattice_mode=""
scn.ezlattice_object=""
scn.ezlattice_objects=""
scn.ezlattice_lattice=""
def applyLatticeOp(obj, context):
scn=context.scene
if scn.ezlattice_mode=="EDIT":
obj_operated_name=scn.ezlattice_object
obj_operated=getObject(obj_operated_name)
cur_mode=curMode()
if obj_operated:
if context.active_object.type=='LATTICE':
setMode('OBJECT')
setSelectActiveObject(context, obj_operated )
temp_mod=[m for m in obj_operated.modifiers if ".LatticeModTemp" in m.name]
lats=[l for l in bpy.data.objects if ".latticetemp" in l.name]
cur_obj=context.active_object
curmode=curMode()
if isEditMode():
objectMode()
if temp_mod:
for m in temp_mod:
if m.object:
bpy.ops.object.modifier_apply(modifier=m.name)
else:
bpy.ops.object.modifier_remove(modifier=m.name)
if lats:
for l in lats:
bpy.data.objects.remove(l)
bpy.ops.ed.undo_push()
setSelectActiveObject(context, cur_obj)
setMode(curmode)
bpy.ops.object.mode_set(mode=curmode)
removetempVertGrp()
resetHelperAttrbs(context)
return
def clearLatticeOps(obj, context):
scn=context.scene
if scn.ezlattice_mode=="EDIT":
cleanupLatticeModifier(context)
cleanupLatticeObjects(context)
resetHelperAttrbs(context)
return
def objectCheck(context):
if not [bool(o) for o in context.selected_objects if o.type!="MESH"]:
return True
else:
return False
class OBJECT_MT_EZLatticeOperator(bpy.types.Menu):
bl_label = "Easy Lattice Menu"
bl_idname = "LAT_MT_ezlattice"
def draw(self, context):
scn=context.scene
layout = self.layout
layout.operator_context = 'INVOKE_REGION_WIN'
if scn.ezlattice_flag:
op="Apply"
layout.operator("object.ezlattice_new", text=op)
else:
op="New"
layout.operator("object.ezlattice_new", text=op)
class OBJECT_OT_EZLatticeCall(Operator):
"""UV Operator description"""
bl_idname = "object.ezlatticecall"
bl_label = "Easy Lattice"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return bool(context.active_object)
def execute(self, context):
return {'FINISHED'}
class OBJECT_OT_EZLatticeOperatorNew(Operator):
"""Tooltip"""
bl_idname = "object.ezlattice_new"
bl_label = "Easy Lattice Creator"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
operation: StringProperty(options={'HIDDEN'})
isnew: BoolProperty(default=False, options={'HIDDEN'})
isobjectmode: BoolProperty(default=False, options={'HIDDEN'})
op_type: EnumProperty( name = "Operation Type", items = OP_TYPES)
lat_u: IntProperty( name = "Lattice u", default = 3 )
lat_w: IntProperty( name = "Lattice w", default = 3 )
lat_m: IntProperty( name = "Lattice m", default = 3 )
lat_type: EnumProperty( name = "Lattice Type", items = LAT_TYPES)
@classmethod
def poll(cls, context):
return (context.mode == 'EDIT_MESH') or (context.mode == 'EDIT_LATTICE') or (context.object.type=='LATTICE')
def execute(self, context):
cur_obj=context.active_object
objs=context.selected_objects
scn=context.scene
if self.isnew and isEditMode():
self.isobjectmode=False
scn.ezlattice_objects=""
scn.ezlattice_mode="EDIT"
newLatticeOp(cur_obj,context,self)
self.isnew=False
else:
newLatticeOp(cur_obj,context,self)
return {'FINISHED'}
def invoke( self, context, event ):
wm = context.window_manager
cur_obj=context.active_object
objs=context.selected_objects
scn=context.scene
if isEditMode() and cur_obj.type=='MESH':
self.isnew=True
if not scn.ezlattice_flag:
return wm.invoke_props_dialog( self )
else:
newLatticeOp(cur_obj,context,self)
return {'FINISHED'}
def menu_draw(self, context):
self.layout.separator()
self.layout.operator_context = 'INVOKE_REGION_WIN'
self.layout.menu(OBJECT_MT_EZLatticeOperator.bl_idname)
def draw_item(self, context):
layout = self.layout
layout.menu(OBJECT_MT_EZLatticeOperator.bl_idname)
classes = (
OBJECT_OT_EZLatticeOperatorNew,
OBJECT_MT_EZLatticeOperator,
)
@persistent
def resetProps(dummy):
context=bpy.context
resetHelperAttrbs(context)
return
def register():
defineSceneProps()
bpy.app.handlers.load_post.append(resetProps)
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.VIEW3D_MT_edit_mesh_context_menu.prepend(menu_draw)
bpy.types.VIEW3D_MT_edit_lattice.prepend(menu_draw)
bpy.types.VIEW3D_MT_edit_lattice_context_menu.prepend(menu_draw)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.types.VIEW3D_MT_edit_mesh_context_menu.remove(menu_draw)
bpy.types.VIEW3D_MT_edit_lattice.remove(menu_draw)
bpy.types.VIEW3D_MT_edit_lattice_context_menu.remove(menu_draw)
if __name__ == "__main__":
register()