-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
760 lines (609 loc) · 26.4 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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# 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 3 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
# MERCHANTIBILITY 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, see <http://www.gnu.org/licenses/>.
bl_info = {
"name": "GP clipboard",
"description": "Copy/Cut/Paste Grease Pencil strokes to/from OS clipboard across layers and blends",
"author": "Samuel Bernou",
"version": (1, 3, 3),
"blender": (2, 83, 0),
"location": "View3D > Toolbar > Gpencil > GP clipboard",
"warning": "",
"doc_url": "https://github.com/Pullusb/GP_clipboard",
"tracker_url": "https://github.com/Pullusb/GP_clipboard/issues",
"category": "Object" }
import bpy
import os
import mathutils
from mathutils import Vector
import json
from time import time
from operator import itemgetter
from itertools import groupby
# from pprint import pprint
def convertAttr(Attr):
'''Convert given value to a Json serializable format'''
if isinstance(Attr, (mathutils.Vector,mathutils.Color)):
return Attr[:]
elif isinstance(Attr, mathutils.Matrix):
return [v[:] for v in Attr]
elif isinstance(Attr,bpy.types.bpy_prop_array):
return [Attr[i] for i in range(0,len(Attr))]
else:
return(Attr)
def getMatrix (layer) :
matrix = mathutils.Matrix.Identity(4)
if layer.is_parented:
if layer.parent_type == 'BONE':
object = layer.parent
bone = object.pose.bones[layer.parent_bone]
matrix = bone.matrix @ object.matrix_world
matrix = matrix.copy() @ layer.matrix_inverse
else :
matrix = layer.parent.matrix_world @ layer.matrix_inverse
return matrix.copy()
default_pt_uv_fill = Vector((0.5, 0.5))
def dump_gp_point(p, l, obj):
'''add properties of a given points to a dic and return it'''
pdic = {}
#point_attr_list = ('co', 'pressure', 'select', 'strength') #select#'rna_type'
#for att in point_attr_list:
# pdic[att] = convertAttr(getattr(p, att))
if l.parent:
mat = getMatrix(l)
pdic['co'] = convertAttr(obj.matrix_world @ mat @ getattr(p,'co'))
else:
pdic['co'] = convertAttr(obj.matrix_world @ getattr(p,'co'))
pdic['pressure'] = convertAttr(getattr(p,'pressure'))
# pdic['select'] = convertAttr(getattr(p,'select'))# need selection ?
pdic['strength'] = convertAttr(getattr(p,'strength'))
## get vertex color (long...)
if p.vertex_color[:] != (0.0, 0.0, 0.0, 0.0):
pdic['vertex_color'] = convertAttr(p.vertex_color)
## et UV attr)
if p.uv_fill != default_pt_uv_fill:
pdic['uv_fill'] = convertAttr(p.uv_fill)
if p.uv_factor != 0.0:
pdic['uv_factor'] = convertAttr(p.uv_factor)
if p.uv_rotation != 0.0:
pdic['uv_rotation'] = convertAttr(p.uv_rotation)
return pdic
def dump_gp_stroke_range(s, sid, l, obj):
'''Get a grease pencil stroke and return a dic with attribute
(points attribute being a dic of dics to store points and their attributes)
'''
sdic = {}
stroke_attr_list = ('line_width',) #'select'#read-only: 'triangles'
for att in stroke_attr_list:
sdic[att] = getattr(s, att)
## Dump following these value only if they are non default
if s.material_index != 0:
sdic['material_index'] = s.material_index
if getattr(s, 'draw_cyclic', None):# pre-2.92
sdic['draw_cyclic'] = s.draw_cyclic
if getattr(s, 'use_cyclic', None):# from 2.92
sdic['use_cyclic'] = s.use_cyclic
if s.uv_scale != 1.0:
sdic['uv_scale'] = s.uv_scale
if s.uv_rotation != 0.0:
sdic['uv_rotation'] = s.uv_rotation
if s.hardness != 1.0:
sdic['hardness'] = s.hardness
if s.uv_translation != Vector((0.0, 0.0)):
sdic['uv_translation'] = convertAttr(s.uv_translation)
if s.vertex_color_fill[:] != (0,0,0,0):
sdic['vertex_color_fill'] = convertAttr(s.vertex_color_fill)
points = []
if sid is None:#no ids, just full points...
for p in s.points:
points.append(dump_gp_point(p,l,obj))
else:
for pid in sid:
points.append(dump_gp_point(s.points[pid],l,obj))
sdic['points'] = points
return sdic
def copycut_strokes(layers=None, copy=True, keep_empty=True):# (mayber allow filter)
'''
copy all visibles selected strokes on active frame
layers can be None, a single layer object or list of layer object as filter
if keep_empty is False the frame is deleted when all strokes are cutted
'''
t0 = time()
### must iterate in all layers ! (since all layers are selectable / visible !)
obj = bpy.context.object
gp = obj.data
gpl = gp.layers
# if not color:#get active color name
# color = gp.palettes.active.colors.active.name
if not layers:
#by default all visible layers
layers = [l for l in gpl if not l.hide and not l.lock]#[]
if not isinstance(layers, list):
#if a single layer object is send put in a list
layers = [layers]
stroke_list = []#one stroke list for all layers.
for l in layers:
f = l.active_frame
if f:#active frame can be None
if not copy:
staylist = []#init part of strokes that must survive on this layer
for s in f.strokes:
if s.select:
# separate in multiple stroke if parts of the strokes a selected.
sel = [i for i, p in enumerate(s.points) if p.select]
substrokes = []# list of list containing isolated selection
for k, g in groupby(enumerate(sel), lambda x:x[0]-x[1]):# continuity stroke have same substract result between point index and enumerator
group = list(map(itemgetter(1), g))
substrokes.append(group)
for ss in substrokes:
if len(ss) > 1:#avoid copy isolated points
stroke_list.append(dump_gp_stroke_range(s,ss,l,obj))
#Cutting operation
if not copy:
maxindex = len(s.points)-1
if len(substrokes) == maxindex+1:#si un seul substroke, c'est le stroke entier
f.strokes.remove(s)
else:
neg = [i for i, p in enumerate(s.points) if not p.select]
staying = []
for k, g in groupby(enumerate(neg), lambda x:x[0]-x[1]):
group = list(map(itemgetter(1), g))
#extend group to avoid gap when cut, a bit dirty
if group[0] > 0:
group.insert(0,group[0]-1)
if group[-1] < maxindex:
group.append(group[-1]+1)
staying.append(group)
for ns in staying:
if len(ns) > 1:
staylist.append(dump_gp_stroke_range(s,ns,l,obj))
#make a negative list containing all last index
'''#full stroke version
# if s.colorname == color: #line for future filters
stroke_list.append(dump_gp_stroke(s,l))
#delete stroke on the fly
if not copy:
f.strokes.remove(s)
'''
if not copy:
# delete all selected strokes...
for s in f.strokes:
if s.select:
f.strokes.remove(s)
# ...recreate these uncutted ones
#pprint(staylist)
if staylist:
add_multiple_strokes(staylist, l)
#for ns in staylist:#weirdly recreate the stroke twice !
# add_stroke(ns, f, l)
#if nothing left on the frame choose to leave an empty frame or delete it (let previous frame appear)
if not copy and not keep_empty:#
if not len(f.strokes):
l.frames.remove(f)
print(len(stroke_list), 'strokes copied in', time()-t0, 'seconds')
#print(stroke_list)
return stroke_list
"""# Unused
def copy_all_strokes(layers=None):
'''
copy all stroke, not affected by selection on active frame
layers can be None, a single layer object or list of layer object as filter
if keep_empty is False the frame is deleted when all strokes are cutted
'''
t0 = time()
scene = bpy.context.scene
obj = bpy.context.object
gp = obj.data
gpl = gp.layers
if not layers:
# by default all visible layers
layers = [l for l in gpl if not l.hide and not l.lock]# include locked ?
if not isinstance(layers, list):
# if a single layer object is send put in a list
layers = [layers]
stroke_list = []# one stroke list for all layers.
for l in layers:
f = l.active_frame
if not f:
continue# active frame can be None
for s in f.strokes:
## full stroke version
# if s.select:
stroke_list.append(dump_gp_stroke_range(s, None, l, obj))
print(len(stroke_list), 'strokes copied in', time()-t0, 'seconds')
#print(stroke_list)
return stroke_list
"""
def copy_all_strokes_in_frame(frame=None, layers=None, obj=None):
'''
copy all stroke, not affected by selection on active frame
layers can be None, a single layer object or list of layer object as filter
if keep_empty is False the frame is deleted when all strokes are cutted
'''
t0 = time()
scene = bpy.context.scene
obj = bpy.context.object
gp = obj.data
gpl = gp.layers
if not frame or not obj:
return
if not layers:
# by default all visible layers
layers = [l for l in gpl if not l.hide and not l.lock]# include locked ?
if not isinstance(layers, list):
# if a single layer object is send put in a list
layers = [layers]
stroke_list = []
for l in layers:
f = l.active_frame
if not f:
continue# active frame can be None
for s in f.strokes:
## full stroke version
# if s.select:
# send index of all points to get the whole stroke with "range"
stroke_list.append( dump_gp_stroke_range(s, [i for i in range(len(s.points))], l, obj) )
print(len(stroke_list), 'strokes copied in', time()-t0, 'seconds')
#print(stroke_list)
return stroke_list
def add_stroke(s, frame, layer, obj):
'''add stroke on a given frame, (layer is for parentage setting)'''
# print(3*'-',s)
ns = frame.strokes.new()
for att, val in s.items():
if att not in ('points'):
setattr(ns, att, val)
pts_to_add = len(s['points'])
# print(pts_to_add, 'points')#dbg
ns.points.add(pts_to_add)
ob_mat_inv = obj.matrix_world.inverted()
## patch pressure 1
# pressure_flat_list = [di['pressure'] for di in s['points']] #get all pressure flatened
if layer.is_parented:
mat = getMatrix(layer).inverted()
for i, pt in enumerate(s['points']):
for k, v in pt.items():
if k == 'co':
setattr(ns.points[i], k, v)
ns.points[i].co = ob_mat_inv @ mat @ ns.points[i].co# invert of object * invert of layer * coordinate
else:
setattr(ns.points[i], k, v)
else:
for i, pt in enumerate(s['points']):
for k, v in pt.items():
if k == 'co':
setattr(ns.points[i], k, v)
ns.points[i].co = ob_mat_inv @ ns.points[i].co# invert of object * coordinate
else:
setattr(ns.points[i], k, v)
## Trigger update (starting 2.93, fix drawing problem for fills and UVs)
ns.points.update()
## patch pressure 2
# ns.points.foreach_set('pressure', pressure_flat_list)
def add_multiple_strokes(stroke_list, layer=None, use_current_frame=True):
'''
add a list of strokes to active frame of given layer
if no layer specified, active layer is used
if use_current_frame is True, a new frame will be created only if needed
'''
scene = bpy.context.scene
obj = bpy.context.object
gp = obj.data
gpl = gp.layers
#default: active
if not layer:
layer = gpl.active
fnum = scene.frame_current
target_frame = False
act = layer.active_frame
for s in stroke_list:
if act:
if use_current_frame or act.frame_number == fnum:
#work on current frame if exists
# use current frame anyway if one key exist at this scene.frame
target_frame = act
if not target_frame:
#no active frame
#or active exists but not aligned scene.current with use_current_frame disabled
target_frame = layer.frames.new(fnum)
add_stroke(s, target_frame, layer, obj)
'''
for s in stroke_data:
add_stroke(s, target_frame)
'''
print(len(stroke_list), 'strokes pasted')
### OPERATORS
class GPCLIP_OT_copy_strokes(bpy.types.Operator):
bl_idname = "gp.copy_strokes"
bl_label = "GP Copy strokes"
bl_description = "Copy strokes to str in paperclip"
bl_options = {"REGISTER"}
#copy = bpy.props.BoolProperty(default=True)
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
def execute(self, context):
# if not context.object or not context.object.type == 'GPENCIL':
# self.report({'ERROR'},'No GP object selected')
# return {"CANCELLED"}
t0 = time()
#ct = check_pressure()
strokelist = copycut_strokes(copy=True, keep_empty=True)
if not strokelist:
self.report({'ERROR'},'rien a copier')
return {"CANCELLED"}
bpy.context.window_manager.clipboard = json.dumps(strokelist)#copy=self.copy
#if ct:
# self.report({'ERROR'}, "Copie OK\n{} points ont une épaisseur supérieure a 1.0 (max = {:.2f})\nCes épaisseurs seront plafonnées à 1 au 'coller'".format(ct[0], ct[1]))
self.report({'INFO'}, f'Copied (time : {time() - t0:.4f})')
# print('copy total time:', time() - t0)
return {"FINISHED"}
class GPCLIP_OT_cut_strokes(bpy.types.Operator):
bl_idname = "gp.cut_strokes"
bl_label = "GP Cut strokes"
bl_description = "Cut strokes to str in paperclip"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
def execute(self, context):
# if not context.object or not context.object.type == 'GPENCIL':
# self.report({'ERROR'},'No GP object selected')
# return {"CANCELLED"}
t0 = time()
strokelist = copycut_strokes(copy=False, keep_empty=True)#ct = check_pressure()
if not strokelist:
self.report({'ERROR'},'Nothing to cut')
return {"CANCELLED"}
bpy.context.window_manager.clipboard = json.dumps(strokelist)
self.report({'INFO'}, f'Cutted (time : {time() - t0:.4f})')
return {"FINISHED"}
class GPCLIP_OT_paste_strokes(bpy.types.Operator):
bl_idname = "gp.paste_strokes"
bl_label = "GP Paste strokes"
bl_description = "paste stroke from paperclip"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
def execute(self, context):
# if not context.object or not context.object.type == 'GPENCIL':
# self.report({'ERROR'},'No GP object selected to paste on')
# return {"CANCELLED"}
t0 = time()
#add a validity check por the content of the paperclip (check if not data.startswith('[{') ? )
try:
data = json.loads(bpy.context.window_manager.clipboard)
except:
mess = 'Clipboard does not contain drawing data (load error)'
self.report({'ERROR'}, mess)
return {"CANCELLED"}
print('data loaded', time() - t0)
add_multiple_strokes(data, use_current_frame=True)
print('total_time', time() - t0)
return {"FINISHED"}
### --- multi copy
class GPCLIP_OT_copy_multi_strokes(bpy.types.Operator):
bl_idname = "gp.copy_multi_strokes"
bl_label = "GP Copy multi strokes"
bl_description = "Copy multiple layers>frames>strokes (unlocked and unhided ones) to str in paperclip"
bl_options = {"REGISTER"}
#copy = bpy.props.BoolProperty(default=True)
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
def execute(self, context):
bake_moves = True
skip_empty_frame = False
org_frame = context.scene.frame_current
obj = context.object
gpl = obj.data.layers
t0 = time()
#ct = check_pressure()
layerdic = {}
layerpool = [l for l in gpl if not l.hide and l.select]# and not l.lock
if not layerpool:
self.report({'ERROR'}, 'No layers selected in GP dopesheet (needs to be visible and selected to be copied)\nHint: Changing active layer reset selection to active only')
return {"CANCELLED"}
if not bake_moves:# copy only drawed frames as is.
for l in layerpool:
if not l.frames:
continue# skip empty layers
frame_dic = {}
for f in l.frames:
if skip_empty_frame and not len(f.strokes):
continue
context.scene.frame_set(f.frame_number)#use matrix of this frame
strokelist = copy_all_strokes_in_frame(frame=f, layers=l, obj=obj)
frame_dic[f.frame_number] = strokelist
layerdic[l.info] = frame_dic
else:# bake position: copy frame where object as moved even if frame is unchanged
for l in layerpool:
if not l.frames:
continue# skip empty layers
frame_dic = {}
fnums_dic = {f.frame_number: f for f in l.frames}
context.scene.frame_set(context.scene.frame_start)
curmat = prevmat = obj.matrix_world.copy()
for i in range(context.scene.frame_start, context.scene.frame_end):
context.scene.frame_set(i)#use matrix of this frame
curmat = obj.matrix_world.copy()
# if object has moved or current time is on a draw key
if prevmat != curmat or i in fnums_dic.keys():
# get the current used frame
for j in fnums_dic.keys():
if j >= i:
f = fnums_dic[j]
break
## skip empty frame if specified
if skip_empty_frame and not len(f.strokes):
continue
strokelist = copy_all_strokes_in_frame(frame=f, layers=l, obj=obj)
frame_dic[i] = strokelist
prevmat = curmat
layerdic[l.info] = frame_dic
## All to clipboard manager
bpy.context.window_manager.clipboard = json.dumps(layerdic)
# reset original frame.
context.scene.frame_set(org_frame)
self.report({'INFO'}, f'Copied layers (time : {time() - t0:.4f})')
# print('copy total time:', time() - t0)
return {"FINISHED"}
class GPCLIP_OT_paste_multi_strokes(bpy.types.Operator):
bl_idname = "gp.paste_multi_strokes"
bl_label = "GP paste multi strokes"
bl_description = "Paste multiple layers>frames>strokes from paperclip"
bl_options = {"REGISTER"}
#copy = bpy.props.BoolProperty(default=True)
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GPENCIL'
def execute(self, context):
org_frame = context.scene.frame_current
obj = context.object
gpl = obj.data.layers
t0 = time()
#add a validity check por the content of the paperclip (check if not data.startswith('[{') ? )
try:
data = json.loads(bpy.context.window_manager.clipboard)
except:
mess = 'Clipboard does not contain drawing data (load error)'
self.report({'ERROR'}, mess)
return {"CANCELLED"}
print('data loaded', time() - t0)
# add layers (or merge with existing names ?)
### structure
# {layername :
# {1: [strokelist of frame 1], 3: [strokelist of frame 3]}
# }
for layname, allframes in data.items():
layer = gpl.get(layname)
if not layer:
layer = gpl.new(layname)
for fnum, fstrokes in allframes.items():
context.scene.frame_set(int(fnum))#use matrix of this frame for copying (maybe just evaluate depsgraph for object
add_multiple_strokes(fstrokes, use_current_frame=False)#create a new frame at each encoutered
print('total_time', time() - t0)
# reset original frame.
context.scene.frame_set(org_frame)
self.report({'INFO'}, f'Copied layers (time : {time() - t0:.4f})')
# print('copy total time:', time() - t0)
return {"FINISHED"}
##--PANEL
class GPCLIP_PT_clipboard_ui(bpy.types.Panel):
# bl_idname = "gp_clipboard_panel"
bl_label = "GP Clipboard"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Gpencil"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.operator('gp.copy_strokes', text='Copy strokes', icon='COPYDOWN')
row.operator('gp.cut_strokes', text='Cut strokes', icon='PASTEFLIPUP')
layout.operator('gp.paste_strokes', text='Paste strokes', icon='PASTEDOWN')
layout.separator()
layout.operator('gp.copy_multi_strokes', text='Copy layers', icon='COPYDOWN')
layout.operator('gp.paste_multi_strokes', text='Paste layers', icon='PASTEDOWN')
## Addons Preferences Update Panel
def update_panel(self, context):
try:
bpy.utils.unregister_class(GPCLIP_PT_clipboard_ui)
except:
pass
GPCLIP_PT_clipboard_ui.bl_category = get_addon_prefs().category
bpy.utils.register_class(GPCLIP_PT_clipboard_ui)
class GPCLIP_addon_prefs(bpy.types.AddonPreferences):
bl_idname = __name__ # os.path.splitext(__name__)[0]
category : bpy.props.StringProperty(
name="Category",
description="Choose a name for the category of the panel",
default="Gpencil",
update=update_panel)
def draw(self, context):
layout = self.layout
## TAB CATEGORY
box = layout.box()
row = box.row(align=True)
row.label(text="Panel Category:")
row.prop(self, "category", text="")
def get_addon_prefs():
import os
addon_name = os.path.splitext(__name__)[0]
addon_prefs = bpy.context.preferences.addons[addon_name].preferences
return (addon_prefs)
###---TEST zone
"""
##main defs
def copy_strokes_to_paperclip():
bpy.context.window_manager.clipboard = json.dumps(copycut_strokes(copy=True, keep_empty=True))#default layers are visible one
def cut_strokes_to_paperclip():
bpy.context.window_manager.clipboard = json.dumps(copycut_strokes(copy=False, keep_empty=True))
def paste_strokes_from_paperclip():
#add condition to detect if clipboard contains loadable values
add_multiple_strokes(json.loads(bpy.context.window_manager.clipboard), use_current_frame=True)#layer= layers.active
#copy_strokes_to_paperclip()
#paste_strokes_from_paperclip()
#test direct
#li = copycut_strokes(copy=True)
#add_multiple_strokes(li, bpy.context.scene.grease_pencil.layers['correct'])
"""
#use directly operator idname in shortcut settings :
# gp.copy_strokes
# gp.cut_strokes
# gp.paste_strokes
# gp.copy_multi_strokes
# gp.paste_multi_strokes
###---REGISTER + copy cut paste keymapping
addon_keymaps = []
def register_keymaps():
addon = bpy.context.window_manager.keyconfigs.addon
km = addon.keymaps.new(name = "Grease Pencil", space_type = "EMPTY", region_type='WINDOW')# in Grease context
# km = addon.keymaps.new(name = "3D View", space_type = "VIEW_3D")# in 3D context
# km = addon.keymaps.new(name = "Window", space_type = "EMPTY")# from everywhere
kmi = km.keymap_items.new("gp.copy_strokes", type = "C", value = "PRESS", ctrl=True, shift=True)
kmi.repeat = False
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new("gp.cut_strokes", type = "X", value = "PRESS", ctrl=True, shift=True)
kmi.repeat = False
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new("gp.paste_strokes", type = "V", value = "PRESS", ctrl=True, shift=True)
kmi.repeat = False
addon_keymaps.append((km, kmi))
def unregister_keymaps():
wm = bpy.context.window_manager
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
classes = (
GPCLIP_OT_copy_strokes,
GPCLIP_OT_cut_strokes,
GPCLIP_OT_paste_strokes,
GPCLIP_OT_copy_multi_strokes,
GPCLIP_OT_paste_multi_strokes,
GPCLIP_PT_clipboard_ui,
GPCLIP_addon_prefs,
)
def register():
for cl in classes:
bpy.utils.register_class(cl)
## update tab name with update in pref file (passing addon_prefs)
update_panel(get_addon_prefs(), bpy.context)
## make scene propery for empty key preservation and bake movement for layers...
register_keymaps()
def unregister():
for cl in reversed(classes):
bpy.utils.unregister_class(cl)
unregister_keymaps()
if __name__ == "__main__":
register()