-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathop_LatticeCreate.py
616 lines (495 loc) · 23.4 KB
/
op_LatticeCreate.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
#Version 0.1.6
import bpy
from mathutils import Vector, Matrix, Quaternion, Euler
from . import util
# Recursivly transverse layer_collection for a particular name
def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found
class Op_LatticeCreateOperator(bpy.types.Operator):
bl_idname = "object.op_lattice_create"
bl_label = "Simple Lattice Create"
bl_description = "Creates and binds a lattice object to the current selection."
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_options = {"REGISTER", "UNDO"}
init = False
# presets = (('2', '2x2x2', ''),
# ('3', '3x3x3', ''),
# ('4', '4x4x4', ''))
# preset: bpy.props.EnumProperty(name="Presets", items=presets, default='2')
orientation_types = (('GLOBAL', 'Global', ''),
('LOCAL', 'Local', ''),
('CURSOR', 'Cursor', ''),
('NORMAL', 'Normal', ''))
# position_types = (('on_top','On Top',''),
# ('bottom','Bottom',''))
interpolation_types = (('KEY_LINEAR', 'Linear', ''),
('KEY_CARDINAL', 'Cardinal', ''),
('KEY_CATMULL_ROM', 'Catmull-Rom', ''),
('KEY_BSPLINE', 'BSpline', ''))
orientation: bpy.props.EnumProperty(
name="Orientation",
items=orientation_types,
default='NORMAL'
)
ignore_mods: bpy.props.BoolProperty (
name="Ignore Modifiers",
default = False,
description="Ignore Modifiers to calculate BBOX for lattice"
)
# modifier_position: bpy.props.EnumProperty (
# name="Modifier",
# items=position_types,
# default="bottom",
# description="Move modifier on top or bottom in the stack"
# )
resolution_u: bpy.props.IntProperty(
name="u",
default=2,
min=2
)
resolution_v: bpy.props.IntProperty(
name="v",
default=2,
min=2
)
resolution_w: bpy.props.IntProperty(
name="w",
default=2,
min=2
)
interpolation: bpy.props.EnumProperty(
name="Interpolation",
items=interpolation_types,
default='KEY_LINEAR'
)
scale: bpy.props.FloatProperty(
name="Scale",
default=1.0,
min=0.001,
soft_min=0.1,
soft_max=2.0
)
tweak_angles: bpy.props.BoolProperty(
name="Tweak Angles",
description="Tweak created lattice angles",
default=False,
options={'SKIP_SAVE'}
)
rot_x: bpy.props.FloatProperty(
name="X",
subtype='ANGLE',
default=0.0,
step=100.0,
precision=4,
options={'SKIP_SAVE'}
)
rot_y: bpy.props.FloatProperty(
name="Y",
subtype='ANGLE',
default=0.0,
step=100.0,
precision=4,
options={'SKIP_SAVE'}
)
rot_z: bpy.props.FloatProperty(
name="Z",
subtype='ANGLE',
default=0.0,
step=100.0,
precision=4,
options={'SKIP_SAVE'}
)
def draw(self, context):
layout = self.layout
layout.use_property_split = True
col = layout.column()
sub = col.row()
sub.prop(self, "orientation")
# col.separator()
# sub = col.row()
# sub.prop(self, "modifier_position", expand=True)
col.separator()
sub = col.row()
sub.prop(self, "ignore_mods")
col.separator()
sub = col.row()
sub2 = sub.column(align=True)
sub2.prop(self, "resolution_u", text="Resolution U")
sub2.prop(self, "resolution_v", text="V")
sub2.prop(self, "resolution_w", text="W")
col.separator()
sub = col.row()
sub.prop(self, "interpolation")
col.separator()
sub = col.row()
sub.prop(self, "scale")
col.separator()
sub = col.row()
sub.prop(self, "tweak_angles")
sub = col.row()
if self.tweak_angles:
sub2 = sub.box().column(align=True)
#sub2.enabled = False
sub2.prop(self, "rot_x", text="Rotation X")
sub2.prop(self, "rot_y")
sub2.prop(self, "rot_z")
@classmethod
def poll(self, context):
if (context.active_object is not None and
context.active_object.type in util.allowed_object_types and
context.active_object.mode == 'EDIT'):
return True
has_selection = len(context.selected_objects) != 0
if has_selection:
for obj in context.selected_objects:
if obj.type in util.allowed_object_types:
return True
return False
def execute(self, context):
# if add lattice in Edit mode,
# preventing undo objects data if settings in "Adjust last action" panel was changed
self.for_edit_mode(context)
# Defaults from Addon preferences
if not Op_LatticeCreateOperator.init:
prefs = bpy.context.preferences.addons[__package__].preferences
self.orientation = prefs.default_orientation
# self.modifier_position = prefs.default_position
self.ignore_mods = prefs.default_ignore_mods
self.resolution_u = prefs.default_resolution_u
self.resolution_v = prefs.default_resolution_v
self.resolution_w = prefs.default_resolution_w
self.interpolation = prefs.default_interpolation
self.scale = prefs.default_scale
Op_LatticeCreateOperator.init = True
objects = []
all_objecst_are_meshes = True
for obj in context.selected_objects:
if obj.type in util.allowed_object_types:
objects.append(obj)
if all_objecst_are_meshes and obj.type != 'MESH':
all_objecst_are_meshes = False
# for the shitty case when the active object is in edit mode,
# but is not selected..
if len(objects) == 0:
objects.append(context.active_object)
self.vertex_mode = all_objecst_are_meshes and objects[0].mode == 'EDIT'
# or objects[0].mode == 'EDIT_GPENCIL'
if len(objects) > 0:
#self.mapping = None
self.group_mapping = None
self.vert_mapping = None
#self.object_names = list(map(lambda x: x.name, objects))
self.cleanup(objects)
#---------
# modifiers_not_visible = []
# for obj in objects:
# for modifier in obj.modifiers:
# if modifier.show_viewport == False:
# modifiers_not_visible.append(modifier)
# print("Modifiers hidden = ", modifier.name)
# if self.ignore_mods == True:
# modifier.show_viewport = False
# if context.mode == 'OBJECT':
# context.view_layer.update()
#---------
if self.vertex_mode:
self.coords, self.vert_mapping = self.get_coords_from_verts(objects)
if len(self.coords) == 0:
bpy.ops.object.mode_set(mode='EDIT')
self.report({'INFO'}, 'Need to be at least 1 vertex selected')
return {'CANCELLED'}
self.group_mapping = self.set_vertex_group(objects, self.vert_mapping)
else:
self.coords = self.get_coords_from_objects(objects)
lattice = self.createLattice(context)
#self.lattice = lattice
#self.lattice_name = lattice.name
self.matrix = context.active_object.matrix_world.copy()
self.update_lattice_from_bbox(context, lattice, self.coords, self.matrix)
self.add_ffd_modifier(objects, lattice, self.group_mapping)
self.set_selection(context, lattice, objects)
context.view_layer.update()
#---------
# for obj in objects:
# for modifier in obj.modifiers:
# modifier.show_viewport = True
# for modifier in modifiers_not_visible:
# modifier.show_viewport = False
#---------
return {'FINISHED'}
else:
return {'CANCELLED'}
def set_selection(self, context, lattice, other):
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
lattice.select_set(True)
context.view_layer.objects.active = lattice
bpy.ops.object.editmode_toggle()
def get_coords_from_verts(self, objects):
worldspace_verts = []
vert_mapping = {}
for obj in objects:
vert_indices = []
if obj.type == "MESH":
bpy.ops.object.editmode_toggle()
vertices = obj.data.vertices
for vert in vertices:
if vert.select == True:
index = vert.index
vert_indices.append(index)
worldspace_verts.append(obj.matrix_world @ vert.co)
#==================================
'''
https://blender.stackexchange.com/questions/155844/how-can-i-calculate-global-coordinates-for-gpencilstrokepoint-in-blender-2-8-and
NOT IMPLEMENTED YET IN BLENDER API
https://blender.stackexchange.com/questions/282126/create-vertex-group-for-selected-points-in-grease-pencil-object
'''
if obj.type == "GPENCIL":
vertices = []
for layer in obj.data.layers:
for frame in layer.frames:
for stroke in frame.strokes:
for point in stroke.points:
vertices.append(point)
for i, vert in enumerate(vertices):
if vert.select == True:
vert_indices.append(i)
#==================================
vert_mapping[obj.name] = vert_indices
return worldspace_verts, vert_mapping
def get_coords_from_objects(self, objects):
# exeption if create lattice for GPENCIL in edit mode
if objects[0].mode == 'EDIT_GPENCIL':
self.report({'WARNING'}, 'Lattice for GPENCIL points not supported yet in Blender API. Switching to object mode.')
bpy.ops.object.mode_set(mode='OBJECT')
bbox_world_coords = []
for obj in objects:
if obj.type == 'MESH' and self.ignore_mods == True:
vertices = obj.data.vertices
for vert in vertices:
bbox_world_coords.append(obj.matrix_world @ vert.co)
else:
coords = obj.bound_box[:]
coords = [(obj.matrix_world @ Vector(p[:])).to_tuple() for p in coords]
bbox_world_coords.extend(coords)
return bbox_world_coords
def update_lattice_from_bbox(self, context, lattice, bbox_world_coords, matrix_world):
if self.orientation == 'GLOBAL':
rotation = Matrix.Identity(4)
# bbox = util.bounds(bbox_world_coords)
bpy.context.scene.transform_orientation_slots[0].type = 'GLOBAL'
elif self.orientation == 'NORMAL':
try:
orig_transform = bpy.context.scene.transform_orientation_slots[0].type
bpy.context.scene.transform_orientation_slots[0].type = 'SimpleLattice_Orientation'
co = bpy.context.scene.transform_orientation_slots[0].custom_orientation
# bpy.ops.transform.delete_orientation()
bpy.data.scenes[0].transform_orientation_slots[0].type = orig_transform
rotation = co.matrix.to_quaternion().to_matrix().to_4x4()
except:
rotation = matrix_world.to_quaternion().to_matrix().to_4x4()
# bbox = util.bounds(bbox_world_coords, rotation.inverted())
bpy.context.scene.transform_orientation_slots[0].type = 'LOCAL'
elif self.orientation == 'LOCAL':
rotation = matrix_world.to_quaternion().to_matrix().to_4x4()
# bbox = util.bounds(bbox_world_coords, rotation.inverted())
bpy.context.scene.transform_orientation_slots[0].type = 'LOCAL'
elif self.orientation == 'CURSOR':
mode = context.scene.cursor.rotation_mode
if mode == "QUATERNION":
rotation = context.scene.cursor.rotation_quaternion.to_matrix().to_4x4()
elif mode == "AXIS_ANGLE":
axis_angle = context.scene.cursor.rotation_axis_angle
rotation = Quaternion(axis_angle[1:], axis_angle[0])
rotation = rotation.to_matrix().to_4x4()
else:
rotation = context.scene.cursor.rotation_euler.to_matrix().to_4x4()
# bbox = util.bounds(bbox_world_coords, rotation.inverted())
bpy.context.scene.transform_orientation_slots[0].type = 'CURSOR'
# new rot angles
if not self.tweak_angles:
rot_x = 0
rot_y = 0
rot_z = 0
if self.tweak_angles:
rot_x = self.rot_x
rot_y = self.rot_y
rot_z = self.rot_z
rot_new = Euler((rot_x, rot_y, rot_z)).to_matrix()
rotation = rotation @ rot_new.to_4x4()
bbox = util.bounds(bbox_world_coords, rotation.inverted())
# removing custom orientation
try:
orig_transform = bpy.context.scene.transform_orientation_slots[0].type
bpy.context.scene.transform_orientation_slots[0].type = 'SimpleLattice_Orientation'
bpy.ops.transform.delete_orientation()
bpy.data.scenes[0].transform_orientation_slots[0].type = orig_transform
except: pass
# calc lattice center
bound_min = Vector((bbox.x.min, bbox.y.min, bbox.z.min))
bound_max = Vector((bbox.x.max, bbox.y.max, bbox.z.max))
offset = (bound_min + bound_max) * 0.5
# finally gather position/rotation/scaling for the lattice
location = rotation @ offset
scale = Vector((abs(bound_max.x - bound_min.x),
abs(bound_max.y - bound_min.y),
abs(bound_max.z - bound_min.z)))
self.updateLattice(lattice, location, rotation, scale)
def createLattice(self, context):
object_active = bpy.context.view_layer.objects.active
lattice_data = bpy.data.lattices.new(object_active.name + '_SimpleLattice')
lattice_obj = bpy.data.objects.new(object_active.name + '_SimpleLattice', lattice_data)
# create Lattice in the collection with the active selected object
obj = bpy.context.object
ucol = obj.users_collection
try:
for i in ucol:
layer_collection = bpy.context.view_layer.layer_collection
layerColl = recurLayerCollection(layer_collection, i.name)
bpy.data.collections[layerColl.name].objects.link(lattice_obj)
except:
context.scene.collection.objects.link(lattice_obj)
return lattice_obj
def updateLattice(self, lattice, location, rotation, scale):
lattice.data.points_u = self.resolution_u
lattice.data.points_v = self.resolution_v
lattice.data.points_w = self.resolution_w
lattice.data.interpolation_type_u = self.interpolation
lattice.data.interpolation_type_v = self.interpolation
lattice.data.interpolation_type_w = self.interpolation
lattice.location = location
lattice.rotation_euler = rotation.to_euler()
#fix for flat selection or surface
if scale.x == 0:
scale.x=scale.x+0.0001
if scale.y == 0:
scale.y=scale.y+0.0001
if scale.z == 0:
scale.z=scale.z+0.0001
#=================================
lattice.scale = Vector((scale.x * self.scale,
scale.y * self.scale,
scale.z * self.scale))
def add_ffd_modifier(self, objects, lattice, group_mapping):
for obj in objects:
if obj.type == "MESH" or obj.type == "CURVE" or obj.type == "SURFACE" or obj.type == "FONT":
ffd = obj.modifiers.new("SimpleLattice", "LATTICE")
# good to see modified vertices if add more than one Lattice to the mesh/es
obj.modifiers[ffd.name].show_in_editmode = True
obj.modifiers[ffd.name].show_on_cage = True
if obj.type == "GPENCIL":
ffd = obj.grease_pencil_modifiers.new("SimpleLattice", "GP_LATTICE")
obj.grease_pencil_modifiers[ffd.name].show_in_editmode = True
# Move Lattice modifier to the top of modifiers stack (if needed)
# https://blender.stackexchange.com/questions/223134/adding-a-modifier-to-the-top-of-the-stack-of-multiple-objects-without-overwritin
# if self.modifier_position == 'on_top':
# obj.select_set(True)
# bpy.context.view_layer.objects.active = obj
# bpy.ops.object.modifier_move_to_index(modifier=ffd.name, index=0)
# if self.modifier_position == 'bottom':
# pass
ffd.object = lattice
if group_mapping != None:
vertex_group_name = group_mapping[obj.name]
ffd.name = vertex_group_name
ffd.vertex_group = vertex_group_name
obj.update_tag()
def cleanup(self, objects):
for obj in objects:
used_vertex_groups = set()
obsolete_modifiers = []
if obj.type == "MESH":
for modifier in obj.modifiers:
if modifier.type == 'LATTICE' and "SimpleLattice" in modifier.name:
if modifier.vertex_group != "":
used_vertex_groups.add(modifier.vertex_group)
elif (modifier.object == None or (modifier.vertex_group == "" and modifier.vertex_group not in obj.vertex_groups)):
#a,b,c = modifier.object == None, modifier.vertex_group == "", modifier.vertex_group not in obj.vertex_groups
#print(f"obj:{a} - vertexgrp empty: {b} - not in groups: {c}" )
obsolete_modifiers.append(modifier)
if obj.type == "GPENCIL":
for modifier in obj.grease_pencil_modifiers:
if modifier.type == 'GP_LATTICE' and "SimpleLattice" in modifier.name:
if modifier.vertex_group != "":
used_vertex_groups.add(modifier.vertex_group)
elif (modifier.object == None or (modifier.vertex_group == "" and modifier.vertex_group not in obj.vertex_groups)):
#a,b,c = modifier.object == None, modifier.vertex_group == "", modifier.vertex_group not in obj.vertex_groups
#print(f"obj:{a} - vertexgrp empty: {b} - not in groups: {c}" )
obsolete_modifiers.append(modifier)
obsolete_groups = []
for grp in obj.vertex_groups:
if "SimpleLattice" in grp.name:
if grp.name not in used_vertex_groups:
obsolete_groups.append(grp)
for group in obsolete_groups:
print(f"removed vertex_group: {group.name}")
obj.vertex_groups.remove(group)
for modifier in obsolete_modifiers:
if obj.type == "MESH":
print(f"removed modifier: {modifier.name}")
obj.modifiers.remove(modifier)
if obj.type == "GPENCIL":
print(f"removed grease pencil modifier: {modifier.name}")
obj.grease_pencil_modifiers.remove(modifier)
def set_vertex_group(self, objects, vert_mapping):
group_mapping = {}
for obj in objects:
mode = obj.mode
if obj.mode == 'EDIT':
bpy.ops.object.editmode_toggle()
#group_index = 0
#for grp in obj.vertex_groups:
#if "SimpleLattice." in grp.name:
#index = int(grp.name.split(".")[-1])
#group_index = max(group_index, index)
#group = obj.vertex_groups.new(name=f"SimpleLattice.{group_index}")
if obj.type == 'MESH':
#print("creating group for MESH")
group = obj.vertex_groups.new(name=f"SimpleLattice")
group.add(vert_mapping[obj.name], 1.0, "REPLACE")
group_mapping[obj.name] = group.name
if obj.type == 'GPENCIL': # <<==================== NOT IMPLEMENTING IN API YET
#print("creating group for GP")
group = obj.vertex_groups.new(name=f"SimpleLattice")
group.add(vert_mapping[obj.name], 1.0, "REPLACE")
group_mapping[obj.name] = group.name
if mode != obj.mode:
bpy.ops.object.editmode_toggle()
return group_mapping
def for_edit_mode(self, context):
active_object = context.view_layer.objects.active
# try:
# bpy.ops.transform.create_orientation(name="SimpleLattice_Orientation", use=False, overwrite=True)
# except:
# EXPERIMENTAL Solution for Normal orientation
if active_object.type == 'MESH':
if bpy.context.mode == 'EDIT_MESH':
# bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.transform.create_orientation(name="SimpleLattice_Orientation", use=False, overwrite=True)
if bpy.context.mode == 'OBJECT':
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.duplicate()
bpy.ops.mesh.edge_face_add()
bpy.ops.transform.create_orientation(name="SimpleLattice_Orientation", use=False, overwrite=True)
bpy.ops.mesh.delete(type='VERT')
bpy.ops.object.mode_set(mode='OBJECT')
if active_object.mode == 'EDIT':
objects_originals = context.selected_objects
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.empty_add()
bpy.ops.object.delete(use_global=False, confirm=False)
# selecting original objects with active
for obj in objects_originals:
obj.select_set(True)
context.view_layer.objects.active = active_object
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.ed.undo_push()