forked from Pullusb/storytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OP_gp_objects.py
710 lines (560 loc) · 26.5 KB
/
OP_gp_objects.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
import bpy
# from .fn import get_addon_prefs
from math import pi
from mathutils import Vector, Matrix, Quaternion
from bpy.types import Operator, Panel, PropertyGroup
from bpy.props import CollectionProperty, PointerProperty
from . import fn
from .fn import get_addon_prefs
from .constants import LAYERMAT_PREFIX
## Bonus
# - load guides if there are any
## possible modal cursor set
# ('DEFAULT', 'NONE', 'WAIT', 'CROSSHAIR', 'MOVE_X', 'MOVE_Y', 'KNIFE', 'TEXT',
# 'PAINT_BRUSH', 'PAINT_CROSS', 'DOT', 'ERASER', 'HAND',
# 'SCROLL_X', 'SCROLL_Y', 'SCROLL_XY', 'EYEDROPPER', 'PICK_AREA',
# 'STOP', 'COPY', 'CROSS', 'MUTE', 'ZOOM_IN', 'ZOOM_OUT')
## Object transform
# on_cam : bpy.props.BoolProperty(
# default=False, options={'SKIP_SAVE'})
# if self.on_cam:
# self.ob = context.scene.camera
# if not self.ob:
# return {'CANCELLED'}
# ## Take view center when in viewcam
# if context.space_data.region_3d.view_perspective == 'CAMERA':
# self.init_world_loc = fn.get_cam_frame_world_center(self.ob)
# else:
# self.init_world_loc = self.ob.matrix_world.to_translation()
# else:
# self.ob = context.object
# self.init_world_loc = self.ob.matrix_world.to_translation()
class STORYTOOLS_OT_object_depth_move(Operator):
bl_idname = "storytools.object_depth_move"
bl_label = "Object Depth Move"
bl_description = "Move object Forward/backward (Slide left-right)\
\n+ Ctrl : Adjust Scale (Retain same size in camera framing)"
bl_options = {"REGISTER", "INTERNAL", "UNDO"}
@classmethod
def poll(cls, context):
if not context.object:
cls.poll_message_set("No active object")
return False
if context.object.type == 'CAMERA' and len(context.selected_objects) < 2:
cls.poll_message_set("Cannot move camera object")
return False
return True
# return context.object and context.object.type != 'CAMERA'
def invoke(self, context, event):
self.init_mouse_x = event.mouse_x
self.cam = bpy.context.scene.camera
if not self.cam:
self.report({'ERROR'}, 'No active camera')
return {"CANCELLED"}
if any(context.object.lock_location):
self.report({'ERROR'}, "Active object's location is locked")
return {'CANCELLED'}
self.cam_pos = self.cam.matrix_world.translation
self.mode = 'distance'
## Consider all selected only in object mode
if context.mode == 'OBJECT':
self.objects = [o for o in context.selected_objects if o.type != 'CAMERA']
else:
self.objects = [context.object]
# Filter locked objects
self.objects = [o for o in self.objects if not any(o.lock_location)]
if not self.objects:
self.report({'ERROR'}, "Object is locked")
return {'CANCELLED'}
self.init_mats = [o.matrix_world.copy() for o in self.objects]
if self.cam.data.type == 'ORTHO':
context.area.header_text_set(f'Move factor: 0.00')
# distance is view vector based
self.view_vector = Vector((0,0,-1))
self.view_vector.rotate(self.cam.matrix_world)
else:
self.init_vecs = [o.matrix_world.translation - self.cam_pos for o in self.objects]
self.init_dists = [v.length for v in self.init_vecs]
context.area.header_text_set(
f'Move factor: 0.00 | Mode: {self.mode} (M to switch) | Ctrl: Adjust scale | Shift: Slow')
context.window.cursor_set("SCROLL_X")
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def stop(self, context):
context.area.header_text_set(None)
context.window.cursor_set("DEFAULT")
def modal(self, context, event):
if self.mode == 'distance':
factor = 0.1
if event.shift:
factor = 0.01
else:
# Smaller factor for proportional dist
factor = 0.01
if event.shift:
factor = 0.001
if event.type in {'MOUSEMOVE'}:
diff = (event.mouse_x - self.init_mouse_x) * factor
if self.cam.data.type == 'ORTHO':
# just push in view vector direction
context.area.header_text_set(f'Move factor: {diff:.2f}')
for i, obj in enumerate(self.objects):
new_vec = self.init_mats[i].translation + (self.view_vector * diff)
obj.matrix_world.translation = new_vec
else:
# Push from camera point and scale accordingly
context.area.header_text_set(
f'Move factor: {diff:.2f} | Mode: {self.mode} (M to switch) | Ctrl: Adjust scale | Shift: Slow')
for i, obj in enumerate(self.objects):
if self.mode == 'distance':
## move with the same length for everyone
new_vec = self.init_vecs[i] + (self.init_vecs[i].normalized() * diff)
else:
## move with proportional factor from individual distance vector to camera
new_vec = self.init_vecs[i] + (self.init_vecs[i] * diff)
obj.matrix_world.translation = self.cam_pos + new_vec
if event.ctrl: # Adjust scale only if Ctrl is pressed
dist_percentage = new_vec.length / self.init_dists[i]
obj.scale = self.init_mats[i].to_scale() * dist_percentage
else:
obj.scale = self.init_mats[i].to_scale() # reset to initial size
if event.type in {'M'} and event.value == 'PRESS':
# Switch mode
self.mode = 'distance' if self.mode == 'proportional' else 'proportional'
# cancel on release
if event.type in {'LEFTMOUSE'}: # and event.value == 'PRESS'
self.stop(context)
## Key objects
for o in self.objects:
fn.key_object(o, use_autokey=True)
return {"FINISHED"}
if event.type in {'RIGHTMOUSE', 'ESC'} and event.value == 'PRESS':
for i, obj in enumerate(self.objects):
obj.matrix_world = self.init_mats[i]
self.stop(context)
return {"CANCELLED"}
return {"RUNNING_MODAL"}
class STORYTOOLS_OT_object_pan(Operator):
bl_idname = "storytools.object_pan"
bl_label = 'Object Pan Translate'
bl_description = "Translate active object"
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
return context.object
def invoke(self, context, event):
# TODO bonus : add multiselection support like depth move (only on object mode)
self.ob = context.object
if any(context.object.lock_location):
self.report({'ERROR'}, "Active object's location is locked")
return {'CANCELLED'}
self.init_world_loc = self.ob.matrix_world.to_translation()
self.init_pos = self.ob.location.copy() # to restore if cancelled
self.init_mouse_x = event.mouse_x
self.init_mouse_y = event.mouse_y
self.init_mouse = Vector((event.mouse_x, event.mouse_y))
self.init_vector = fn.region_to_location(self.init_mouse, self.init_world_loc)
context.window.cursor_set("SCROLL_XY")
context.window_manager.modal_handler_add(self)
self.update_position(context, event)
return {'RUNNING_MODAL'}
def update_position(self, context, event):
mouse_co = Vector((event.mouse_x, event.mouse_y))
# delta_x = event.mouse_x - self.init_mouse_x
# context.object.location = self.init_pos * (1 + delta_x * 0.01)
vec = fn.region_to_location(mouse_co, self.init_world_loc)
move_vec = vec - self.init_vector
## move by vector (recompose matrix)
self.ob.matrix_world.translation = self.init_world_loc + move_vec
def modal(self, context, event):
self.update_position(context, event)
if event.type == 'LEFTMOUSE': # and event.value == 'RELEASE'
context.window.cursor_set("DEFAULT")
fn.key_object(self.ob, use_autokey=True)
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
self.ob.location = self.init_pos
context.window.cursor_set("DEFAULT")
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def execute(self, context):
return {"FINISHED"}
class STORYTOOLS_OT_object_scale(Operator):
bl_idname = "storytools.object_scale"
bl_label = 'Object Scale'
bl_description = "Scale object by going left-right"
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
return context.object
def invoke(self, context, event):
if any(context.object.lock_scale):
self.report({'ERROR'}, "Active object's scale is locked")
return {'CANCELLED'}
self.init_scale = context.object.scale.copy()
self.init_mouse_x = event.mouse_x
context.window.cursor_set("SCROLL_X")
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def modal(self, context, event):
delta = event.mouse_x - self.init_mouse_x
context.object.scale = self.init_scale * (1 + delta * 0.01)
if event.type == 'LEFTMOUSE':
context.window.cursor_set("DEFAULT")
## Key all transforms
fn.key_object(context.object, use_autokey=True)
## Key only scale
# fn.key_object(context.object, loc=False, rot=False, use_autokey=True)
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
context.object.scale = self.init_scale
context.window.cursor_set("DEFAULT")
return {'CANCELLED'}
return {'RUNNING_MODAL'}
# def execute(self, context):
# with context.temp_override(selected_objects=[context.object]):
# bpy.ops.transform.resize('INVOKE_DEFAULT')
# bpy.ops.transform.resize('INVOKE_DEFAULT')
# return {"FINISHED"}
## Create object
def distance_selection_update(self, context):
# print('self: ', dir(self))
if self.place_from_cam and context.scene.camera:
self.init_dist = fn.coord_distance_from_cam(context=context)
else:
self.init_dist = fn.coord_distance_from_view(context=context)
class STORYTOOLS_OT_create_object(Operator):
bl_idname = "storytools.create_object"
bl_label = "Create New Drawing"
bl_description = "Create a new grease pencil object"
bl_options = {"REGISTER"} # , "INTERNAL"
name : bpy.props.StringProperty(
name='Name',
description="Name of Grease pencil object")
parented : bpy.props.BoolProperty(
name='Attached To Camera',
description="When Creating the object, Attach it to the camera",
default=False)
init_dist : bpy.props.FloatProperty(
name="Distance", description="Initial distance of new grease pencil object",
default=8.0, min=0.0, max=999, step=3, precision=3,
subtype='DISTANCE')
place_from_cam : bpy.props.BoolProperty(
name='Use Active Camera',
description="Create the object facing camera, else create from your current view",
default=False, update=distance_selection_update)
at_cursor : bpy.props.BoolProperty(
name='At Cursor',
description="Create object at cursor location, else centered position at cursor 'distance' facing view",
default=False)
def invoke(self, context, event):
## Suggest a numbered default name for quick use
gp_ct = len([o for o in context.scene.objects if o.type == 'GPENCIL'])
self.name = f'Drawing_{gp_ct+1:03d}'
settings = context.scene.storytools_settings
## Calculate distance to 3D cursor
# self.init_dist = settings.initial_distance # overwritten by dist from cusor
# self.view_distance_from_cursor = fn.coord_distance_from_view(context=context)
# self.cam_distance_from_cursor = None
# if context.scene.camera:
# self.cam_distance_from_cursor = fn.coord_distance_from_cam(context=context)
distance_selection_update(self, context)
self.parented = settings.initial_parented
return context.window_manager.invoke_props_dialog(self, width=250)
def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.prop(self, 'name')
layout.prop(self, 'parented')
layout.prop(self, 'at_cursor')
row = layout.row()
row.prop(self, 'init_dist')
row.active = not self.at_cursor # enabled
if context.space_data.region_3d.view_perspective != 'CAMERA':
col=layout.column()
col.label(text='Not in camera', icon='ERROR')
col.prop(self, 'place_from_cam', text='Face Active Camera')
# if self.init_dist <= 0: (FIXME init_dist always positive, need futher check)
# viewpoint ='camera' if self.place_from_cam else 'view'
# col.label(text=f'Cursor is behind {viewpoint}', icon='ERROR')
def execute(self, context):
prefs = get_addon_prefs()
if context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
# for o in context.scene.objects:o.select_set(False)
r3d = context.space_data.region_3d
if r3d.view_perspective != 'CAMERA' and self.place_from_cam:
view_matrix = context.scene.camera.matrix_world
else:
view_matrix = r3d.view_matrix.inverted()
if self.at_cursor:
loc = context.scene.cursor.location
else:
loc = view_matrix @ Vector((0.0, 0.0, -self.init_dist))
## Create GP object
# TODO bonus : maybe check if want to use same data as another drawing
gp = bpy.data.grease_pencils.new(self.name)
ob_name = self.name
ob = bpy.data.objects.new(ob_name, gp)
## Set collection
draw_col = bpy.data.collections.get('Drawings')
if not draw_col:
draw_col = bpy.data.collections.new('Drawings')
bpy.context.scene.collection.children.link(draw_col)
draw_col.objects.link(ob)
if self.parented:
ob.parent = context.scene.camera
## Place
_ref_loc, ref_rot, _ref_scale = view_matrix.decompose()
rot_mat = ref_rot.to_matrix().to_4x4() @ Matrix.Rotation(-pi/2, 4, 'X')
loc_mat = Matrix.Translation(loc)
new_mat = loc_mat @ rot_mat @ fn.get_scale_matrix((1,1,1))
ob.matrix_world = new_mat
## Make active and selected
context.view_layer.objects.active = ob
ob.select_set(True)
## Configure
# TODO: Set Active palette (Need a selectable loader)
# fn.load_palette(path_to_palette)
fn.load_default_palette(ob=ob)
gp.edit_line_color[3] = prefs.default_edit_line_opacity # 0.2 # Bl default is 0.5
for l_name in reversed(['Sketch', 'Line', 'Color']):
layer = gp.layers.new(l_name)
# CHOICE: new frame creation -> Usual default is frame_current
layer.frames.new(context.scene.frame_start)
layer.use_lights = False # Can be a project prefs
## Set default association
## TODO: Set default name as string in prefs ?
if l_name in ['Line', 'Sketch']:
fn.set_material_association(ob, layer, 'line')
elif l_name == 'Color':
fn.set_material_association(ob, layer, 'fill_white')
# Enter Draw mode
bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
fn.reset_draw_settings(context=context)
return {"FINISHED"}
class STORYTOOLS_OT_align_with_view(Operator):
bl_idname = "storytools.align_with_view"
bl_label = "Align With View"
bl_description = "Align object with view\
\nCtrl + Click align but keep object Z axis pointing up"
bl_options = {"REGISTER", "UNDO"} # "INTERNAL"
@classmethod
def poll(cls, context):
return context.object # and context.object.type == 'GPENCIL'
keep_z_up : bpy.props.BoolProperty(name='Keep Z Up', default=False)
def invoke(self, context, event):
self.keep_z_up = event.ctrl
return self.execute(context)
def execute(self, context):
r3d = context.space_data.region_3d
for ob in context.selected_objects:
## skip camera object
if ob.type == 'CAMERA':
continue
## skip active camera if selected and IN cam view
# if context.scene.camera \
# and ob == context.scene.camera \
# and context.space_data.region_3d.view_perspective == 'CAMERA':
# continue
if self.keep_z_up:
## Align to view but keep world Up
Z_up_vec = Vector((0.0, 0.0, 1.0))
aim = r3d.view_rotation @ Z_up_vec
world_aligned_mat = aim.to_track_quat('Z','Y').to_matrix().to_4x4() # Track Up
fn.assign_rotation_from_ref_matrix(ob, world_aligned_mat)
else:
## Align to view
view_matrix = r3d.view_matrix.inverted()
fn.assign_rotation_from_ref_matrix(ob, view_matrix)
return {"FINISHED"}
## ---
## Object Property groups and UIlist
## ---
class STORYTOOLS_OT_visibility_toggle(Operator):
bl_idname = "storytools.visibility_toggle"
bl_label = 'Toggle Visibility'
bl_description = "Toggle and synchronize viewlayer visibility, viewport and render visibility"
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
@classmethod
def poll(cls, context):
return True
# def invoke(self, context, event):
# return self.execute(context)
name : bpy.props.StringProperty()
def execute(self, context):
if not self.name:
return {"CANCELLED"}
ob = context.scene.objects.get(self.name)
if not ob:
return {"CANCELLED"}
# hide = not ob.hide_viewport
hide = ob.visible_get() # Already inversed
ob.hide_viewport = hide
ob.hide_render = hide
# Set viewlayer visibility
ob.hide_set(hide)
return {"FINISHED"}
## Property groups
def update_object_change(self, context):
ob = context.scene.objects[self.index]
# print('Switch to object', ob.name)
if ob.type != 'GPENCIL' or context.object is ob:
return
prev_mode = context.mode
possible_gp_mods = ('OBJECT',
'EDIT_GPENCIL', 'SCULPT_GPENCIL', 'PAINT_GPENCIL',
'WEIGHT_GPENCIL', 'VERTEX_GPENCIL')
if prev_mode not in possible_gp_mods:
prev_mode = None
mode_swap = False
## TODO optional: Option to stop mode sync ?
## Set in same mode as previous object
if context.scene.tool_settings.lock_object_mode:
if context.mode != 'OBJECT':
mode_swap = True
bpy.ops.object.mode_set(mode='OBJECT')
# set active
context.view_layer.objects.active = ob
## keep same mode accross objects
if mode_swap and prev_mode is not None:
bpy.ops.object.mode_set(mode=prev_mode)
else:
## keep same mode accross objects
context.view_layer.objects.active = ob
if context.mode != prev_mode is not None:
bpy.ops.object.mode_set(mode=prev_mode)
if context.mode != 'OBJECT':
for o in [o for o in context.scene.objects if o.type == 'GPENCIL']:
o.select_set(o == ob) # select only active (when not in object mode)
else:
ob.select_set(True) # Select only active in object mode
# ob.select_set(True)
class CUSTOM_object_collection(PropertyGroup):
# need an index for the native object list
index : bpy.props.IntProperty(default=-1, update=update_object_change)
# point_prop : PointerProperty(
# name="Object",
# type=bpy.types.Object)
class STORYTOOLS_UL_gp_objects_list(bpy.types.UIList):
# Constants (flags)
# Be careful not to shadow FILTER_ITEM (i.e. UIList().bitflag_filter_item)!
# E.g. VGROUP_EMPTY = 1 << 0
# Custom properties, saved with .blend file. E.g.
# use_filter_empty: bpy.props.BoolProperty(
# name="Filter Empty", default=False, options=set(),
# description="Whether to filter empty vertex groups",
# )
# The draw_item function is called for each item of the collection that is visible in the list.
# data is the RNA object containing the collection,
# item is the current drawn item of the collection,
# icon is the "computed" icon for the item (as an integer, because some objects like materials or textures
# have custom icons ID, which are not available as enum items).
# active_data is the RNA object containing the active property for the collection (i.e. integer pointing to the
# active item of the collection).
# active_propname is the name of the active property (use 'getattr(active_data, active_propname)').
# index is index of the current item in the collection.
# flt_flag is the result of the filtering process for this item.
# Note: as index and flt_flag are optional arguments, you do not have to use/declare them here if you don't
# need them.
# Called for each drawn item.
## active (draw) : GREASEPENCIL
## sculpt : SCULPTMODE_HLT
## active edit : EDITMODE_HLT
## others : OUTLINER_DATA_GREASEPENCIL
# hide_ico = 'OUTLINER_OB_GREASEPENCIL' if item.active else 'HIDE_OFF'
# source_ico = 'NETWORK_DRIVE' if item.is_project else 'USER' # BLANK1
# row.label(text='', icon=source_ico)
# row.prop(item, 'hide', text='', icon=hide_ico, invert_checkbox=True)
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): # , flt_flag
# layout.label(text=item.name)
row = layout.row()
if item == context.view_layer.objects.active:
# if context.mode == 'PAINT'
icon = 'GREASEPENCIL'
# row.label(text='', icon='GREASEPENCIL')
else:
icon = 'OUTLINER_OB_GREASEPENCIL'
# row.label(text='', icon='OUTLINER_DATA_GREASEPENCIL')
# row.label(text=item.name, icon=icon)
row.prop(item, 'name', icon=icon, text='',emboss=False)
if item.data.users > 1:
row.template_ID(item, "data")
else:
row.label(text='', icon='BLANK1')
if item.parent:
row.label(text='', icon='DECORATE_LINKED')
else:
row.label(text='', icon='BLANK1')
## Clickable toggle, set and sync hide from viewlayer, viewport and render
## (Can lead to confusion with blender model... but heh !)
# if item.hide_viewport:
if item.visible_get():
# row.label(text='', icon='HIDE_OFF')
row.operator('storytools.visibility_toggle', text='', icon='HIDE_OFF', emboss=False).name = item.name
else:
# row.label(text='', icon='HIDE_ON')
row.operator('storytools.visibility_toggle', text='', icon='HIDE_ON', emboss=False).name = item.name
# Called once to draw filtering/reordering options.
# def draw_filter(self, context, layout):
# pass
# Called once to filter/reorder items.
def filter_items(self, context, data, propname):
# This function gets the collection property (as the usual tuple (data, propname)), and must return two lists:
# * The first one is for filtering, it must contain 32bit integers were self.bitflag_filter_item marks the
# matching item as filtered (i.e. to be shown), and 31 other bits are free for custom needs. Here we use the
# first one to mark VGROUP_EMPTY.
# * The second one is for reordering, it must return a list containing the new indices of the items (which
# gives us a mapping org_idx -> new_idx).
# Please note that the default UI_UL_list defines helper functions for common tasks (see its doc for more info).
# If you do not make filtering and/or ordering, return empty list(s) (this will be more efficient than
# returning full lists doing nothing!).
# Default return values.
flt_flags = []
flt_neworder = []
#### Do filtering/reordering here...
## data : scene struct -> propname: 'objects' string
objs = getattr(data, propname)
# objs: scene objects collection
helper_funcs = bpy.types.UI_UL_list
flt_flags = [self.bitflag_filter_item if o.type == 'GPENCIL' else 0 for o in objs]
## By name
# flt_flags = helper_funcs.filter_items_by_name(self.filter_name, self.bitflag_filter_item, objs, "name", reverse=False)
## BONUS option: By distance to camera ? (need to be computed OTF... possible ?)
return flt_flags, flt_neworder
class STORYTOOLS_OT_object_key_transform(Operator):
bl_idname = "storytools.object_key_transform"
bl_label = 'Key Object Transforms'
bl_description = "Key active object Loc / Rot / Scale"
bl_options = {'REGISTER', 'INTERNAL'}
@classmethod
def poll(cls, context):
return context.object
def execute(self, context):
ret = fn.key_object(context.object)
if ret:
self.report({'INFO'}, ret)
return {"FINISHED"}
## to test -> bl_options = {'HIDE_HEADER'}
classes=(
STORYTOOLS_OT_create_object,
STORYTOOLS_OT_align_with_view,
STORYTOOLS_OT_object_pan,
STORYTOOLS_OT_object_scale,
STORYTOOLS_OT_object_depth_move,
STORYTOOLS_OT_visibility_toggle,
STORYTOOLS_OT_object_key_transform,
CUSTOM_object_collection, ## Test all bugged
STORYTOOLS_UL_gp_objects_list,
)
def register():
# bpy.types.Scene.index_constant = -1
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.gp_object_props = bpy.props.PointerProperty(type=CUSTOM_object_collection)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
# del bpy.types.Scene.index_constant
del bpy.types.Scene.gp_object_props