-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCONFIG_Modifiers.py
679 lines (523 loc) · 25 KB
/
DCONFIG_Modifiers.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
# ------------------------------------------------------------
# Copyright(c) 2018-2020 Jesse Yurkovich
# Licensed under the MIT License <http://opensource.org/licenses/MIT>.
# See the LICENSE file in the repo root for full license information.
# ------------------------------------------------------------
#
# Better modifiers
#
import math
import bpy
from mathutils import (Vector, Matrix)
from . import DCONFIG_Symmetry as symmetry
from . import DCONFIG_Utils as dc
class DCONFIG_MT_modifiers(bpy.types.Menu):
bl_label = "Modifiers"
@classmethod
def poll(cls, context):
return dc.active_object_available(context, {'MESH', 'LATTICE'})
def draw(self, context):
layout = self.layout
layout.operator_context = 'INVOKE_REGION_WIN'
dc.setup_op(layout, "dconfig.mod_mirror", 'MOD_MIRROR', "Local Mirror", local=True)
dc.setup_op(layout, "dconfig.mod_mirror", 'MOD_MIRROR', "World Mirror", local=False)
layout.separator()
dc.setup_op(layout, "dconfig.mod_linear_array", 'MOD_ARRAY', "Linear Array")
dc.setup_op(layout, "dconfig.mod_radial_array", 'MOD_ARRAY', "Radial Array")
dc.setup_op(layout, "dconfig.mod_bend", 'MOD_SIMPLEDEFORM', "Bend")
dc.setup_op(layout, "dconfig.mod_pillow", 'MOD_CLOTH', "Pillow")
layout.separator()
dc.setup_op(layout, "dconfig.mod_lattice", 'MESH_GRID', "FFD", resolution=2, only_base=True)
class DCONFIG_OT_mod_mirror(bpy.types.Operator):
bl_idname = "dconfig.mod_mirror"
bl_label = "DC Mirror"
bl_description = "Mirror mesh across an axis"
bl_options = {'UNDO'}
local: bpy.props.BoolProperty()
direction: bpy.props.EnumProperty(
items=(
('POSITIVE_X', "+X to -X", "+X to -X", 0),
('NEGATIVE_X', "-X to +X", "-X to +X", 1),
('POSITIVE_Y', "+Y to -Y", "+Y to -Y", 2),
('NEGATIVE_Y', "-Y to +Y", "-Y to +Y", 3),
('POSITIVE_Z', "+Z to -Z", "+Z to -Z", 4),
('NEGATIVE_Z', "-Z to +Z", "-Z to +Z", 5),
),
name="Direction",
description="which side to copy from",
default='POSITIVE_X',
options={'ANIMATABLE'},
update=None,
get=None,
set=None)
dc_uses_symmetry_gizmo = True
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def execute(self, context):
dc.trace_enter(self)
target = context.active_object
mirror_object = self.create_mirror_obj(context)
self.create_mirror_mod(target, mirror_object)
symmetry.DCONFIG_GGT_symmetry_gizmo.destroy(context)
return dc.trace_exit(self)
def invoke(self, context, event):
dc.trace_enter(self)
if context.space_data.type == 'VIEW_3D':
symmetry.DCONFIG_GGT_symmetry_gizmo.create(context, self.local)
return dc.trace_exit(self)
def create_mirror_obj(self, context):
mirror_object = None
if not self.local:
# Use a special collection
helpers_collection = dc.get_helpers_collection(context)
world_origin_name = "dc_world_origin"
if world_origin_name in helpers_collection.all_objects:
dc.trace(1, "Using existing world-origin empty")
mirror_object = helpers_collection.objects[world_origin_name]
else:
dc.trace(1, "Creating new world-origin empty")
original_object = context.active_object
original_mode = context.active_object.mode
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.object.empty_add(type='PLAIN_AXES', radius=0.25, align='WORLD', location=(0, 0, 0))
mirror_object = context.active_object
mirror_object.name = world_origin_name
mirror_object.select_set(state=False)
mirror_object.hide_viewport = True
mirror_object_collection = dc.find_collection(context, mirror_object)
helpers_collection.objects.link(mirror_object)
mirror_object_collection.objects.unlink(mirror_object)
dc.make_active_object(context, original_object)
bpy.ops.object.mode_set(mode=original_mode, toggle=False)
return mirror_object
def create_mirror_mod(self, target, mirror_object):
dc.trace(1, "Adding {} mirror modifier to {}", "local" if self.local else "world", dc.full_name(target))
if self.local:
mod = target.modifiers.new("dc_local_mirror", 'MIRROR')
mod.use_clip = True
else:
mod = target.modifiers.new("dc_world_mirror", 'MIRROR')
mod.use_clip = True
mod.mirror_object = mirror_object
axis = 0 if self.direction in {'POSITIVE_X', 'NEGATIVE_X'} else 1 if self.direction in {'POSITIVE_Y', 'NEGATIVE_Y'} else 2
should_bisect = self.local
should_flip = should_bisect and self.direction.startswith('NEGATIVE')
mod.use_axis = (False, False, False)
mod.use_axis[axis] = True
mod.use_bisect_axis[axis] = should_bisect
mod.use_bisect_flip_axis[axis] = should_flip
mod.show_on_cage = True
mod.show_expanded = False
# Local mirrors go before World and after Booleans...
if self.local:
mod_index = len(target.modifiers) - 1
while mod_index > 0 and target.modifiers[mod_index - 1].type != 'BOOLEAN' and not target.modifiers[mod_index - 1].name.startswith("dc_local_mirror"):
bpy.ops.object.modifier_move_up(modifier=mod.name)
mod_index -= 1
class DCONFIG_OT_mod_linear_array(bpy.types.Operator):
bl_idname = "dconfig.mod_linear_array"
bl_label = "DC Linear Array"
bl_description = "Array mesh in a linear fashion"
bl_options = {'UNDO'}
count: bpy.props.IntProperty(name="count", default=3, min=1, max=360)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mouse_x = None
self.array_mod = None
self.axis = 0
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def invoke(self, context, event):
dc.trace_enter(self)
self.execute_core(context, False)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
dc.trace_enter(self)
self.execute_core(context, True)
return dc.trace_exit(self)
def execute_core(self, context, is_execute):
target = context.active_object
self.create_array_mod(target)
def modal(self, context, event):
if event.type == 'MOUSEMOVE' and event.ctrl:
if self.mouse_x is not None:
scale = 100 if event.shift else 20
delta = (event.mouse_x - self.mouse_x) / scale
self.array_mod.relative_offset_displace[self.axis] += delta
self.mouse_x = event.mouse_x
elif not event.ctrl:
self.mouse_x = None
if event.type == 'WHEELUPMOUSE':
self.array_mod.count += 1
elif event.type == 'WHEELDOWNMOUSE':
if self.array_mod.count > 1:
self.array_mod.count -= 1
elif event.type in {'X', 'Y', 'Z'} and event.value == 'RELEASE':
current = self.array_mod.relative_offset_displace[self.axis]
self.axis = 0 if event.type == 'X' else 1 if event.type == 'Y' else 2
for a in (0, 1, 2):
if a == self.axis:
self.array_mod.relative_offset_displace[a] = current
else:
self.array_mod.relative_offset_displace[a] = 0
elif event.type == 'LEFTMOUSE':
return dc.trace_exit(self)
elif event.type in {'RIGHTMOUSE', 'ESC'}:
bpy.ops.object.modifier_remove(modifier=self.array_mod.name)
return dc.user_canceled(self)
return {'RUNNING_MODAL'}
def create_array_mod(self, target):
dc.trace(1, "Adding array modifier to {}", dc.full_name(target))
self.array_mod = target.modifiers.new("dc_array", 'ARRAY')
self.array_mod.fit_type = 'FIXED_COUNT'
self.array_mod.count = self.count
self.array_mod.use_relative_offset = True
self.array_mod.use_merge_vertices = True
self.array_mod.use_merge_vertices_cap = True
self.array_mod.merge_threshold = 0.001
self.array_mod.show_on_cage = False
self.array_mod.show_expanded = False
def adjust_array_mod(self, delta, init=False):
dc.trace(1, "Delta: {}", delta)
self.array_mod.count += delta
class DCONFIG_OT_mod_radial_array(bpy.types.Operator):
bl_idname = "dconfig.mod_radial_array"
bl_label = "DC Radial Array"
bl_description = "Array mesh in a radial fashion"
bl_options = {'REGISTER', 'UNDO'}
count: bpy.props.IntProperty(name="count", default=3, min=1, max=360)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mouse_x = None
self.offset_mod = None
self.radial_mod = None
self.radial_object = None
self.existing_strength = None
self.existing_direction = None
self.existing_count = None
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def invoke(self, context, event):
dc.trace_enter(self)
self.execute_core(context, False)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
dc.trace_enter(self)
self.execute_core(context, True)
self.radial_object.hide_viewport = True
return dc.trace_exit(self)
def execute_core(self, context, is_execute):
target = context.active_object
if not self.init_from_existing(target):
self.create_radial_obj(context, target)
self.create_radial_mod(target)
self.align_objects(context, target)
self.adjust_radial_mod(0, init=True)
elif is_execute and self.count != self.radial_mod.count:
self.adjust_radial_mod(self.count - self.radial_mod.count)
def modal(self, context, event):
if event.type == 'MOUSEMOVE' and event.ctrl:
if self.mouse_x is not None:
scale = 100 if event.shift else 10
displace_delta = (event.mouse_x - self.mouse_x) / scale
self.offset_mod.strength += displace_delta
self.mouse_x = event.mouse_x
elif not event.ctrl:
self.mouse_x = None
if event.type == 'WHEELUPMOUSE':
self.adjust_radial_mod(1)
elif event.type == 'WHEELDOWNMOUSE':
if self.radial_mod.count > 1:
self.adjust_radial_mod(-1)
elif event.type in {'X', 'Y', 'Z'} and event.value == 'RELEASE':
self.offset_mod.direction = event.type
elif event.type == 'LEFTMOUSE':
self.radial_object.hide_viewport = True
return dc.trace_exit(self)
elif event.type in {'RIGHTMOUSE', 'ESC'}:
if self.existing_strength is None:
self.radial_object.parent.select_set(True)
context.view_layer.objects.active = self.radial_object.parent
bpy.ops.object.modifier_remove(modifier=self.radial_mod.name)
bpy.ops.object.modifier_remove(modifier=self.offset_mod.name)
bpy.data.objects.remove(self.radial_object)
else:
self.offset_mod.strength = self.existing_strength
self.offset_mod.direction = self.existing_direction
self.adjust_radial_mod(self.existing_count - self.radial_mod.count)
self.radial_object.hide_viewport = True
return dc.user_canceled(self)
return {'RUNNING_MODAL'}
def create_radial_obj(self, context, target):
dc.trace(1, "Creating new radial empty")
prev_cursor_location = tuple(context.scene.cursor.location)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.empty_add(type='PLAIN_AXES', radius=0.25, align='WORLD')
self.radial_object = context.active_object
self.radial_object.name = target.name + "_world_radial"
self.radial_object.select_set(state=True)
# Place empty in a helpers collection
radial_object_collection = dc.find_collection(context, self.radial_object)
helpers_collection = dc.get_helpers_collection(context)
helpers_collection.objects.link(self.radial_object)
radial_object_collection.objects.unlink(self.radial_object)
# Parent empty to the target
self.radial_object.parent = target
self.radial_object.matrix_parent_inverse = target.matrix_world.inverted()
context.scene.cursor.location = prev_cursor_location
def create_radial_mod(self, target):
dc.trace(1, "Adding array modifier to {}", dc.full_name(target))
self.offset_mod = target.modifiers.new("dc_offset", 'DISPLACE')
self.offset_mod.strength = 0
self.offset_mod.direction = 'Y'
self.offset_mod.show_in_editmode = True
self.offset_mod.show_on_cage = False
self.offset_mod.show_expanded = False
self.radial_mod = target.modifiers.new("dc_radial", 'ARRAY')
self.radial_mod.fit_type = 'FIXED_COUNT'
self.radial_mod.count = self.count
self.radial_mod.offset_object = self.radial_object
self.radial_mod.use_relative_offset = False
self.radial_mod.use_object_offset = True
self.radial_mod.use_merge_vertices = True
self.radial_mod.use_merge_vertices_cap = True
self.radial_mod.merge_threshold = 0.001
self.radial_mod.show_on_cage = False
self.radial_mod.show_expanded = False
def align_objects(self, context, target):
is_ortho = not context.space_data.region_3d.is_perspective
if is_ortho:
view = dc.get_view_orientation_from_quaternion(context.space_data.region_3d.view_rotation)
dc.trace(1, "Using Ortho axis: {}", view)
if view == 'TOP' or view == 'BOTTOM':
self.radial_object["dc_axis"] = 'Z'
elif view == 'LEFT' or view == 'RIGHT':
self.radial_object["dc_axis"] = 'X'
elif view == 'FRONT' or view == 'BACK':
self.radial_object["dc_axis"] = 'Y'
else:
dc.trace(1, "Using Global-Z axis")
self.radial_object["dc_axis"] = None
# Ensure the proper object is active/selected for further bpy.ops
bpy.ops.object.select_all(action='DESELECT')
dc.make_active_object(context, self.radial_object)
def adjust_radial_mod(self, delta, init=False):
dc.trace(1, "Delta: {}", delta)
if init:
current_rotation = 0
else:
current_rotation = math.radians(360 / self.radial_mod.count)
self.radial_mod.count += delta
required_rotation = math.radians(360 / self.radial_mod.count)
actual_rotation = current_rotation - required_rotation
if self.radial_object["dc_axis"] is None:
bpy.ops.transform.rotate(value=actual_rotation, constraint_axis=(False, False, True), orient_type='LOCAL')
else:
bpy.ops.transform.rotate(value=actual_rotation, orient_axis=self.radial_object["dc_axis"], orient_type='GLOBAL',)
def init_from_existing(self, target):
self.offset_mod = next((mod for mod in reversed(target.modifiers) if mod.name.startswith("dc_offset")), None)
self.radial_mod = next((mod for mod in reversed(target.modifiers) if mod.name.startswith("dc_radial")), None)
if self.radial_mod is not None:
dc.trace(1, "Found existing modifier: {}", self.radial_mod.name)
self.radial_object = self.radial_mod.offset_object
self.radial_object.hide_viewport = False
self.existing_strength = self.offset_mod.strength
self.existing_direction = self.offset_mod.direction
self.existing_count = self.radial_mod.count
target.select_set(state=False)
self.radial_object.select_set(state=True)
return self.radial_mod is not None
class DCONFIG_OT_mod_lattice(bpy.types.Operator):
bl_idname = "dconfig.mod_lattice"
bl_label = "DC Add Lattice"
bl_description = "Add pre-configured lattice surrounding the selected geometry"
bl_options = {'REGISTER', 'UNDO'}
resolution: bpy.props.IntProperty(name="Resolution", default=2, min=2, max=6)
only_base: bpy.props.BoolProperty(name="Only Base Object", default=True)
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.target = None
self.lattice = None
self.mod = None
def invoke(self, context, event):
dc.trace_enter(self)
self.execute_core(context)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
dc.trace_enter(self)
self.target = context.active_object
self.execute_core(context)
self.make_lattice_active(context)
return dc.trace_exit(self)
def execute_core(self, context):
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
self.target = context.active_object
self.create_lattice_obj(context)
self.create_lattice_mod()
def modal(self, context, event):
if event.type == 'WHEELUPMOUSE':
self.resolution += 1
self.execute_core(context)
elif event.type == 'WHEELDOWNMOUSE':
self.resolution -= 1
self.execute_core(context)
elif event.type in {'B'} and event.value == 'RELEASE':
self.only_base = not self.only_base
self.execute_core(context)
elif event.type == 'LEFTMOUSE':
self.make_lattice_active(context)
return dc.trace_exit(self)
elif event.type in {'RIGHTMOUSE', 'ESC'}:
return dc.user_canceled(self)
return {'RUNNING_MODAL'}
def make_lattice_active(self, context):
dc.make_active_object(context, self.lattice)
self.target.select_set(False)
def create_lattice_obj(self, context):
# Create lattice
if self.lattice is None:
lattice_data = bpy.data.lattices.new('dc_lattice')
self.lattice = bpy.data.objects.new('dc_lattice', lattice_data)
self.lattice.data.interpolation_type_u = 'KEY_LINEAR'
self.lattice.data.interpolation_type_v = 'KEY_LINEAR'
self.lattice.data.interpolation_type_w = 'KEY_LINEAR'
self.lattice.data.use_outside = False
# Place in a special collection
helpers_collection = dc.get_helpers_collection(context)
helpers_collection.objects.link(self.lattice)
# Ensure the lattice is added to local view...
if context.space_data.local_view is not None:
self.lattice.local_view_set(context.space_data, True)
# Parent target to the lattice...
context.view_layer.update()
self.lattice.parent = self.target
self.lattice.matrix_parent_inverse = self.target.matrix_world.inverted()
# Position + Orientation (resolution is affected for 0 dimensions)
self.set_transforms()
def create_lattice_mod(self):
if self.mod is None:
self.mod = self.target.modifiers.new(self.lattice.name, "LATTICE")
self.mod.object = self.lattice
self.mod.show_expanded = False
# Place just after Booleans (or at end)...
if self.only_base:
mod_index = next((i for i, v in enumerate(self.target.modifiers) if v.name == self.mod.name), -1)
while mod_index > 0 and self.target.modifiers[mod_index - 1].type != 'BOOLEAN':
bpy.ops.object.modifier_move_up(modifier=self.mod.name)
mod_index -= 1
else:
while self.target.modifiers[-1].name != self.mod.name:
bpy.ops.object.modifier_move_down(modifier=self.mod.name)
def set_transforms(self):
if self.only_base:
bbox_min, bbox_max = dc.calculate_bbox(map(lambda v: v.co, self.target.data.vertices))
vert_avg = sum(map(lambda v: v.co, self.target.data.vertices), Vector()) / len(self.target.data.vertices)
box_center = ((bbox_min + vert_avg) + (bbox_max - vert_avg)) / 2
box_dims = bbox_max - bbox_min
else:
box_center = sum(map(Vector, self.target.bound_box), Vector()) / 8
box_dims = self.target.dimensions
target_loc, target_rot, _ = self.target.matrix_world.decompose()
self.lattice.matrix_world = (Matrix.Translation(target_loc) @ target_rot.to_matrix().to_4x4() @ Matrix.Translation(box_center))
self.lattice.dimensions = [max(0.01, d * 1.01) for d in box_dims]
safe_res = [self.resolution if d > 0.0 else 1 for d in box_dims]
self.lattice.data.points_u = safe_res[0]
self.lattice.data.points_v = safe_res[1]
self.lattice.data.points_w = safe_res[2]
class DCONFIG_OT_mod_bend(bpy.types.Operator):
bl_idname = "dconfig.mod_bend"
bl_label = "DC Bend"
bl_description = "Bend mesh"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def execute(self, context):
dc.trace_enter(self)
target = context.active_object
bend_object = self.create_bend_obj(context, target)
self.create_bend_mod(target, bend_object)
bend_object.select_set(state=False)
bend_object.hide_viewport = True
dc.make_active_object(context, target)
return dc.trace_exit(self)
def create_bend_obj(self, context, target):
dc.trace(1, "Creating new bend empty")
prev_cursor_location = tuple(context.scene.cursor.location)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.empty_add(type='PLAIN_AXES', radius=0.25, align='WORLD')
bend_object = context.active_object
bend_object.name = target.name + "_bender"
bend_object.select_set(state=True)
# Place empty in a helpers collection
bend_object_collection = dc.find_collection(context, bend_object)
helpers_collection = dc.get_helpers_collection(context)
helpers_collection.objects.link(bend_object)
bend_object_collection.objects.unlink(bend_object)
# Parent empty to the target
bend_object.parent = target
bend_object.matrix_parent_inverse = target.matrix_world.inverted()
context.scene.cursor.location = prev_cursor_location
return bend_object
def create_bend_mod(self, target, bend_object):
bend_mod = target.modifiers.new("dc_bend", 'SIMPLE_DEFORM')
bend_mod.deform_method = 'BEND'
bend_mod.deform_axis = 'Z'
bend_mod.angle = math.radians(120)
bend_mod.origin = bend_object
class DCONFIG_OT_mod_pillow(bpy.types.Operator):
bl_idname = "dconfig.mod_pillow"
bl_label = "DC Add Cloth Pillow"
bl_description = "Add pre-configured cloth pillow to selected object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def execute(self, context):
dc.trace_enter(self)
target = context.active_object
vgroup = self.create_vgroup(context, target)
self.create_mod_cloth(target, vgroup)
return dc.trace_exit(self)
def create_vgroup(self, context, target):
if context.mode == 'OBJECT':
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.region_to_loop()
bpy.ops.object.vertex_group_assign_new()
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
else:
bpy.ops.object.vertex_group_assign_new()
vgroup = target.vertex_groups.active
vgroup.name = "dc_cloth_pin"
return vgroup
def create_mod_cloth(self, target, vgroup):
mod = target.modifiers.new("dc_cloth", "CLOTH")
mod.settings.vertex_group_mass = vgroup.name
mod.settings.quality = 5
mod.settings.mass = 0.150
mod.settings.tension_stiffness = 5
mod.settings.compression_stiffness = 5
mod.settings.shear_stiffness = 5
mod.settings.bending_stiffness = 0.05
mod.settings.tension_damping = 0
mod.settings.compression_damping = 0
mod.settings.shear_damping = 0
mod.settings.air_damping = 1
mod.settings.use_pressure = True
mod.settings.uniform_pressure_force = 3
mod.settings.shrink_min = -0.15
mod.settings.effector_weights.gravity = 0