Blender V5.3
grease_pencil_layers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_assert.hh"
10#include "BLI_listbase.hh"
11#include "BLI_math_matrix.hh"
12#include "BLI_math_matrix_c.hh"
13#include "BLI_string.hh"
14
15#include "BKE_attribute_math.hh"
16#include "BKE_context.hh"
17#include "BKE_grease_pencil.hh"
18#include "BKE_object.hh"
19#include "BKE_report.hh"
20
21#include "BLT_translation.hh"
22
23#include "DEG_depsgraph.hh"
24
25#include "ED_grease_pencil.hh"
26
27#include "RNA_access.hh"
28#include "RNA_define.hh"
29
30#include "UI_interface.hh"
31
32#include "DNA_scene_types.h"
33
34#include "WM_api.hh"
35#include "WM_message.hh"
36
37namespace blender {
38
39namespace ed::greasepencil {
40
41/* This utility function is modified from `BKE_object_get_parent_matrix()`. */
42static float4x4 get_bone_mat(const Object *parent, const char *parsubstr)
43{
44 if (parent->type != OB_ARMATURE) {
45 return float4x4::identity();
46 }
47
48 const bPoseChannel *pchan = BKE_pose_channel_find_name(parent->pose, parsubstr);
49 if (!pchan) {
50 return float4x4::identity();
51 }
52
53 const Bone *bone = pchan->bone_get(*parent);
54 if (!bone) {
56 return float4x4::identity();
57 }
58
59 if (bone->flag & BONE_RELATIVE_PARENTING) {
60 return float4x4(pchan->chan_mat);
61 }
62 return float4x4(pchan->pose_mat);
63}
64
66 Object *parent,
67 StringRefNull bone,
68 const bool keep_transform)
69{
70 if (keep_transform) {
71 /* TODO apply current transform to geometry. */
72 }
73
74 layer.parent = parent;
75 layer.parsubstr = BLI_strdup_null(bone.c_str());
76 /* Calculate inverse parent matrix. */
77 if (parent) {
78 float4x4 inverse = parent->world_to_object();
79 if (layer.parsubstr) {
80 const float4x4 bone_mat = get_bone_mat(parent, layer.parsubstr);
81 inverse = math::invert(bone_mat) * inverse;
82 }
83 copy_m4_m4(layer.parentinv, inverse.ptr());
84 }
85 else {
86 unit_m4(layer.parentinv);
87 }
88
89 return true;
90}
91
93{
94 if (layer.parent == nullptr) {
95 return;
96 }
97 if (keep_transform) {
98 /* TODO apply current transform to geometry. */
99 }
100
101 layer.parent = nullptr;
102 MEM_SAFE_DELETE(layer.parsubstr);
103
104 copy_m4_m4(layer.parentinv, float4x4::identity().ptr());
105}
106
108{
109 using namespace blender::bke::greasepencil;
110
111 if (layer != nullptr) {
112 layer->set_selected(true);
113 }
114
115 if (grease_pencil.get_active_layer() != layer) {
116 grease_pencil.set_active_layer(layer);
118 }
119}
120
122{
123 using namespace blender::bke::greasepencil;
124 Scene *scene = CTX_data_scene(C);
126
127 std::string new_layer_name = RNA_string_get(op->ptr, "new_layer_name");
128 Layer &new_layer = grease_pencil.add_layer(new_layer_name);
131
132 if (grease_pencil.has_active_layer()) {
133 grease_pencil.move_node_after(new_layer.as_node(),
134 grease_pencil.get_active_layer()->as_node());
135 }
136 else if (grease_pencil.has_active_group()) {
137 grease_pencil.move_node_into(new_layer.as_node(), *grease_pencil.get_active_group());
139 &grease_pencil.id,
141 GreasePencilv3LayerGroup,
142 active);
143 }
144
145 grease_pencil.set_active_layer(&new_layer);
147 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3Layers, active);
148
149 grease_pencil.insert_frame(new_layer, scene->r.cfra);
150
153
154 return OPERATOR_FINISHED;
155}
156
158 wmOperator *op,
159 const wmEvent *event)
160{
162 op,
163 event,
164 IFACE_("Add New Grease Pencil Layer"),
166}
167
169{
170 /* identifiers */
171 ot->name = "Add New Layer";
172 ot->idname = "GREASE_PENCIL_OT_layer_add";
173 ot->description = "Add a new Grease Pencil layer in the active object";
174
175 /* callbacks */
179
181
183 ot->srna, "new_layer_name", "Layer", INT16_MAX, "Name", "Name of the new layer");
185 ot->prop = prop;
186}
187
209
211{
212 /* identifiers */
213 ot->name = "Remove Layer";
214 ot->idname = "GREASE_PENCIL_OT_layer_remove";
215 ot->description = "Remove the active Grease Pencil layer";
216
217 /* callbacks */
220
222}
223
224enum class LayerMoveDirection : int8_t { Up = -1, Down = 1 };
225
227 {int(LayerMoveDirection::Up), "UP", 0, "Up", ""},
228 {int(LayerMoveDirection::Down), "DOWN", 0, "Down", ""},
229 {0, nullptr, 0, nullptr, nullptr},
230};
231
233{
234 using namespace blender::bke::greasepencil;
236 return false;
237 }
238
240 const TreeNode *active_node = grease_pencil.get_active_node();
241
242 if (active_node == nullptr) {
243 return false;
244 }
245
246 const LayerGroup *parent = active_node->parent_group();
247
248 if (parent == nullptr || parent->num_direct_nodes() < 2) {
249 return false;
250 }
251
252 return true;
253}
254
256{
257 using namespace blender::bke::greasepencil;
259
260 const LayerMoveDirection direction = LayerMoveDirection(RNA_enum_get(op->ptr, "direction"));
261
262 TreeNode &active_node = *grease_pencil.get_active_node();
263
264 if (direction == LayerMoveDirection::Up) {
265 grease_pencil.move_node_up(active_node);
266 }
267 else if (direction == LayerMoveDirection::Down) {
268 grease_pencil.move_node_down(active_node);
269 }
270
274
277
278 return OPERATOR_FINISHED;
279}
280
282{
283 /* identifiers */
284 ot->name = "Reorder Layer";
285 ot->idname = "GREASE_PENCIL_OT_layer_move";
286 ot->description = "Move the active Grease Pencil layer or Group";
287
288 /* callbacks */
291
293
294 ot->prop = RNA_def_enum(ot->srna, "direction", enum_layer_move_direction, 0, "Direction", "");
295}
296
298{
299 using namespace blender::bke::greasepencil;
301 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
302 int layer_index = RNA_int_get(op->ptr, "layer");
303
304 if (!grease_pencil.layers().index_range().contains(layer_index)) {
305 return OPERATOR_CANCELLED;
306 }
307
308 Layer &layer = grease_pencil.layer(layer_index);
309 if (grease_pencil.is_layer_active(&layer)) {
310 return OPERATOR_CANCELLED;
311 }
312
313 if (grease_pencil.has_active_group()) {
315 &grease_pencil.id,
317 GreasePencilv3LayerGroup,
318 active);
319 }
320 grease_pencil.set_active_layer(&layer);
321
323 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3Layers, active);
324
326
327 return OPERATOR_FINISHED;
328}
329
331{
332 /* identifiers */
333 ot->name = "Set Active Layer";
334 ot->idname = "GREASE_PENCIL_OT_layer_active";
335 ot->description = "Set the active Grease Pencil layer";
336
337 /* callbacks */
340
342
343 PropertyRNA *prop = RNA_def_int(
344 ot->srna, "layer", 0, 0, INT_MAX, "Grease Pencil Layer", "", 0, INT_MAX);
346}
347
349{
350 using namespace blender::bke::greasepencil;
352
353 std::string new_layer_group_name = RNA_string_get(op->ptr, "new_layer_group_name");
354
355 LayerGroup &new_group = grease_pencil.add_layer_group(new_layer_group_name);
358
359 if (grease_pencil.has_active_layer()) {
360 grease_pencil.move_node_after(new_group.as_node(),
361 grease_pencil.get_active_layer()->as_node());
363 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3Layers, active);
364 }
365 else if (grease_pencil.has_active_group()) {
366 grease_pencil.move_node_into(new_group.as_node(), *grease_pencil.get_active_group());
368 &grease_pencil.id,
370 GreasePencilv3LayerGroup,
371 active);
372 }
373
374 grease_pencil.set_active_node(&new_group.as_node());
375
377 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3LayerGroup, active);
378
382
383 return OPERATOR_FINISHED;
384}
385
387{
388 /* identifiers */
389 ot->name = "Add New Layer Group";
390 ot->idname = "GREASE_PENCIL_OT_layer_group_add";
391 ot->description = "Add a new Grease Pencil layer group in the active object";
392
393 /* callbacks */
396
398
400 ot->srna, "new_layer_group_name", nullptr, INT16_MAX, "Name", "Name of the new layer group");
402 ot->prop = prop;
403}
404
406{
407 using namespace blender::bke::greasepencil;
408 const bool keep_children = RNA_boolean_get(op->ptr, "keep_children");
410
411 if (!grease_pencil.has_active_group()) {
412 return OPERATOR_CANCELLED;
413 }
414
415 grease_pencil.remove_group(*grease_pencil.get_active_group(), keep_children);
416
419
421 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3LayerGroup, active);
424
425 return OPERATOR_FINISHED;
426}
427
429{
430 /* identifiers */
431 ot->name = "Remove Layer Group";
432 ot->idname = "GREASE_PENCIL_OT_layer_group_remove";
433 ot->description = "Remove Grease Pencil layer group in the active object";
434
435 /* callbacks */
438
440
442 "keep_children",
443 false,
444 "Keep children nodes",
445 "Keep the children nodes of the group and only delete the group itself");
446}
447
449{
450 using namespace blender::bke::greasepencil;
452 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
453
454 TreeNode *active_node = grease_pencil.get_active_node();
455
456 if (!active_node) {
457 return OPERATOR_CANCELLED;
458 }
459
460 if (unselected) {
461 /* If active node is a layer group, only show parent layer groups and child nodes.
462 * If active node is a layer, only show parent layer groups and active node. */
463
464 for (TreeNode *node : grease_pencil.nodes_for_write()) {
465 bool should_be_visible = false;
466
467 if (active_node->is_group()) {
468 should_be_visible = node->is_child_of(active_node->as_group());
469 if (node->is_group()) {
470 should_be_visible |= active_node->is_child_of(node->as_group());
471 }
472 }
473 else if (node->is_group()) {
474 should_be_visible = active_node->is_child_of(node->as_group());
475 }
476
477 node->set_visible(should_be_visible);
478 }
479 active_node->set_visible(true);
480 }
481 else {
482 /* hide selected/active */
483 active_node->set_visible(false);
484 }
485
486 /* notifiers */
490
491 return OPERATOR_FINISHED;
492}
493
495{
496 /* identifiers */
497 ot->name = "Hide Layer(s)";
498 ot->idname = "GREASE_PENCIL_OT_layer_hide";
499 ot->description = "Hide selected/unselected Grease Pencil layers";
500
501 /* callbacks */
504
505 /* flags */
507
508 /* props */
510 ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected layers");
512 ot->prop = prop;
513}
514
516{
517 using namespace blender::bke::greasepencil;
519
520 if (!grease_pencil.get_active_node()) {
521 return OPERATOR_CANCELLED;
522 }
523
524 for (TreeNode *node : grease_pencil.nodes_for_write()) {
525 node->set_visible(true);
526 }
527
528 /* notifiers */
532
533 return OPERATOR_FINISHED;
534}
535
537{
538 /* identifiers */
539 ot->name = "Show All Layers";
540 ot->idname = "GREASE_PENCIL_OT_layer_reveal";
541 ot->description = "Show all Grease Pencil layers";
542
543 /* callbacks */
546
547 /* flags */
549}
550
552{
553 using namespace blender::bke::greasepencil;
555 const int affect_visibility = RNA_boolean_get(op->ptr, "affect_visibility");
556 bool isolate = false;
557
558 for (const Layer *layer : grease_pencil.layers()) {
559 if (grease_pencil.is_layer_active(layer)) {
560 continue;
561 }
562 if ((affect_visibility && layer->is_visible()) || !layer->is_locked()) {
563 isolate = true;
564 break;
565 }
566 }
567
568 for (Layer *layer : grease_pencil.layers_for_write()) {
569 if (grease_pencil.is_layer_active(layer) || !isolate) {
570 layer->set_locked(false);
571 if (affect_visibility) {
572 layer->set_visible(true);
573 }
574 }
575 else {
576 layer->set_locked(true);
577 if (affect_visibility) {
578 layer->set_visible(false);
579 }
580 }
581 }
582
586
587 return OPERATOR_FINISHED;
588}
589
591{
592 /* identifiers */
593 ot->name = "Isolate Layers";
594 ot->idname = "GREASE_PENCIL_OT_layer_isolate";
595 ot->description = "Make only active layer visible/editable";
596
597 /* callbacks */
600
601 /* flags */
603
604 /* properties */
606 ot->srna, "affect_visibility", false, "Affect Visibility", "Also affect the visibility");
607}
608
610{
611 using namespace blender::bke::greasepencil;
613 const bool lock_value = RNA_boolean_get(op->ptr, "lock");
614
615 if (grease_pencil.nodes().is_empty()) {
616 return OPERATOR_CANCELLED;
617 }
618
619 for (TreeNode *node : grease_pencil.nodes_for_write()) {
620 node->set_locked(lock_value);
621 }
622
626
627 return OPERATOR_FINISHED;
628}
629
631{
632 /* identifiers */
633 ot->name = "Lock All Layers";
634 ot->idname = "GREASE_PENCIL_OT_layer_lock_all";
635 ot->description =
636 "Lock all Grease Pencil layers to prevent them from being accidentally modified";
637
638 /* callbacks */
641
642 /* flags */
644
645 /* properties */
646 RNA_def_boolean(ot->srna, "lock", true, "Lock Value", "Lock/Unlock all layers");
647}
648
650{
651 using namespace blender::bke::greasepencil;
653 const bool empty_keyframes = RNA_boolean_get(op->ptr, "empty_keyframes");
654
655 if (!grease_pencil.has_active_layer()) {
656 BKE_reportf(op->reports, RPT_ERROR, "No active layer to duplicate");
657 return OPERATOR_CANCELLED;
658 }
659
660 /* Duplicate layer. */
661 Layer &active_layer = *grease_pencil.get_active_layer();
662 const bool duplicate_frames = true;
663 const bool duplicate_drawings = !empty_keyframes;
664 Layer &new_layer = grease_pencil.duplicate_layer(
665 active_layer, duplicate_frames, duplicate_drawings);
666
669
670 grease_pencil.move_node_after(new_layer.as_node(), active_layer.as_node());
671 grease_pencil.set_active_layer(&new_layer);
672
675
677 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3Layers, active);
678
679 return OPERATOR_FINISHED;
680}
681
683{
684 /* identifiers */
685 ot->name = "Duplicate Layer";
686 ot->idname = "GREASE_PENCIL_OT_layer_duplicate";
687 ot->description = "Make a copy of the active Grease Pencil layer";
688
689 /* callbacks */
692
693 /* flags */
695
696 /* properties */
697 RNA_def_boolean(ot->srna, "empty_keyframes", false, "Empty Keyframes", "Add Empty Keyframes");
698}
699
700enum class MergeMode : int8_t {
701 Down = 0,
702 Group = 1,
703 All = 2,
704};
705
707{
708 using namespace blender::bke::greasepencil;
709 Main *bmain = CTX_data_main(C);
711 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
712 const MergeMode mode = MergeMode(RNA_enum_get(op->ptr, "mode"));
713
714 Vector<Vector<int>> src_layer_indices_by_dst_layer;
715 std::string merged_layer_name;
716 if (mode == MergeMode::Down) {
717 if (!grease_pencil.has_active_layer()) {
718 BKE_report(op->reports, RPT_ERROR, "No active layer");
719 return OPERATOR_CANCELLED;
720 }
721 const Layer &active_layer = *grease_pencil.get_active_layer();
722 GreasePencilLayerTreeNode *prev_node = active_layer.as_node().prev;
723 if (prev_node == nullptr || !prev_node->wrap().is_layer()) {
724 /* No layer below the active one. */
725 return OPERATOR_CANCELLED;
726 }
727 const Layer &prev_layer = prev_node->wrap().as_layer();
728 /* Get the indices of the two layers to be merged. */
729 const int prev_layer_index = *grease_pencil.get_layer_index(prev_layer);
730 const int active_layer_index = *grease_pencil.get_layer_index(active_layer);
731
732 /* Map all the other layers to their own index. */
733 const Span<const Layer *> layers = grease_pencil.layers();
734 for (const int layer_i : layers.index_range()) {
735 if (layer_i == active_layer_index) {
736 /* Active layer is merged into previous, skip. */
737 }
738 else if (layer_i == prev_layer_index) {
739 /* Previous layer merges itself and the active layer. */
740 src_layer_indices_by_dst_layer.append({prev_layer_index, active_layer_index});
741 }
742 else {
743 /* Other layers remain unchanged. */
744 src_layer_indices_by_dst_layer.append({layer_i});
745 }
746 }
747
748 /* Store the name of the current active layer as the name of the merged layer. */
749 merged_layer_name = grease_pencil.layer(prev_layer_index).name();
750 }
751 else if (mode == MergeMode::Group) {
752 if (!grease_pencil.has_active_group()) {
753 BKE_report(op->reports, RPT_ERROR, "No active group");
754 return OPERATOR_CANCELLED;
755 }
756 LayerGroup &active_group = *grease_pencil.get_active_group();
757
758 if (active_group.layers().is_empty()) {
759 BKE_report(op->reports, RPT_INFO, "No child layers to merge");
760 return OPERATOR_CANCELLED;
761 }
762
763 /* Remove all sub groups of the active group since they won't be needed anymore, but keep the
764 * layers. */
765 Array<LayerGroup *> groups = active_group.groups_for_write();
766 for (LayerGroup *group : groups) {
767 grease_pencil.remove_group(*group, true);
768 }
769
770 const Span<const Layer *> layers = grease_pencil.layers();
771 Vector<int> indices;
772 for (const int layer_i : layers.index_range()) {
773 const Layer &layer = grease_pencil.layer(layer_i);
774 if (!layer.is_child_of(active_group)) {
775 src_layer_indices_by_dst_layer.append({layer_i});
776 }
777 else {
778 indices.append(layer_i);
779 }
780 }
781 src_layer_indices_by_dst_layer.append(indices);
782
783 /* Store the name of the group as the name of the merged layer. */
784 merged_layer_name = active_group.name();
785
786 /* Remove the active group. */
787 grease_pencil.remove_group(active_group, true);
789 &grease_pencil.id,
791 GreasePencilv3LayerGroup,
792 active);
793
794 /* Rename the first node so that the merged layer will have the name of the group. */
795 grease_pencil.rename_node(
796 *bmain, grease_pencil.layer(indices[0]).as_node(), merged_layer_name);
797 }
798 else if (mode == MergeMode::All) {
799 if (grease_pencil.layers().is_empty()) {
800 return OPERATOR_CANCELLED;
801 }
802 /* Remove all groups, keep the layers. */
803 Array<LayerGroup *> groups = grease_pencil.layer_groups_for_write();
804 for (LayerGroup *group : groups) {
805 grease_pencil.remove_group(*group, true);
806 }
807
808 Vector<int> indices;
809 for (const int layer_i : grease_pencil.layers().index_range()) {
810 indices.append(layer_i);
811 }
812 src_layer_indices_by_dst_layer.append(indices);
813
814 merged_layer_name = N_("Layer");
815 grease_pencil.rename_node(
816 *bmain, grease_pencil.layer(indices[0]).as_node(), merged_layer_name);
817 }
818 else {
820 }
821
822 GreasePencil *merged_grease_pencil = BKE_grease_pencil_new_nomain();
823 BKE_grease_pencil_copy_parameters(grease_pencil, *merged_grease_pencil);
825 grease_pencil, src_layer_indices_by_dst_layer, *merged_grease_pencil);
827
830
831 /* Try to set the active (merged) layer. */
832 TreeNode *node = grease_pencil.find_node_by_name(merged_layer_name);
833 if (node && node->is_layer()) {
834 Layer &layer = node->as_layer();
835 grease_pencil.set_active_layer(&layer);
836
838 CTX_wm_message_bus(C), &grease_pencil.id, &grease_pencil, GreasePencilv3Layers, active);
839 }
840
843
844 return OPERATOR_FINISHED;
845}
846
848{
849 static const EnumPropertyItem merge_modes[] = {
850 {int(MergeMode::Down),
851 "ACTIVE",
852 0,
853 "Active",
854 "Combine the active layer with the layer just below (if it exists)"},
855 {int(MergeMode::Group),
856 "GROUP",
857 0,
858 "Group",
859 "Combine layers in the active group into a single layer"},
860 {int(MergeMode::All), "ALL", 0, "All", "Combine all layers into a single layer"},
861 {0, nullptr, 0, nullptr, nullptr},
862 };
863
864 ot->name = "Merge";
865 ot->idname = "GREASE_PENCIL_OT_layer_merge";
866 ot->description = "Combine layers based on the mode into one layer";
867
870
872
873 ot->prop = RNA_def_enum(ot->srna, "mode", merge_modes, int(MergeMode::Down), "Mode", "");
874}
875
877{
878 using namespace blender::bke::greasepencil;
880
881 if (!grease_pencil.has_active_layer()) {
882 return OPERATOR_CANCELLED;
883 }
884 Layer &active_layer = *grease_pencil.get_active_layer();
885
886 std::string mask_name = RNA_string_get(op->ptr, "name");
887
888 if (TreeNode *node = grease_pencil.find_node_by_name(mask_name)) {
889 if (grease_pencil.is_layer_active(&node->as_layer())) {
890 BKE_report(op->reports, RPT_ERROR, "Cannot add active layer as mask");
891 return OPERATOR_CANCELLED;
892 }
893
894 if (BLI_findstring_ptr(&active_layer.masks,
895 mask_name.c_str(),
896 offsetof(GreasePencilLayerMask, layer_name)) != nullptr)
897 {
898 BKE_report(op->reports, RPT_ERROR, "Layer already added");
899 return OPERATOR_CANCELLED;
900 }
901
902 LayerMask *new_mask = MEM_new<LayerMask>(__func__, mask_name);
903 BLI_addtail(&active_layer.masks, reinterpret_cast<GreasePencilLayerMask *>(new_mask));
904 /* Make the newly added mask active. */
905 active_layer.active_mask_index = active_layer.masks.count() - 1;
906 }
907 else {
908 BKE_report(op->reports, RPT_ERROR, "Unable to find layer to add");
909 return OPERATOR_CANCELLED;
910 }
911
914
915 return OPERATOR_FINISHED;
916}
917
919{
920 /* identifiers */
921 ot->name = "Add New Mask Layer";
922 ot->idname = "GREASE_PENCIL_OT_layer_mask_add";
923 ot->description = "Add new layer as masking";
924
926
927 /* callbacks */
930
931 /* properties */
932 RNA_def_string(ot->srna, "name", nullptr, 0, "Layer", "Name of the layer");
933}
934
936{
937 using namespace blender::bke::greasepencil;
939 return false;
940 }
941
943 Layer &active_layer = *grease_pencil.get_active_layer();
944
945 return !active_layer.masks.is_empty();
946}
947
949{
950 using namespace blender::bke::greasepencil;
952
953 if (!grease_pencil.has_active_layer()) {
954 return OPERATOR_CANCELLED;
955 }
956
957 Layer &active_layer = *grease_pencil.get_active_layer();
958 if (GreasePencilLayerMask *mask = reinterpret_cast<GreasePencilLayerMask *>(
959 BLI_findlink(&active_layer.masks, active_layer.active_mask_index)))
960 {
961 BLI_remlink(&active_layer.masks, mask);
962 MEM_delete(reinterpret_cast<LayerMask *>(mask));
963 active_layer.active_mask_index = std::max(active_layer.active_mask_index - 1, 0);
964 }
965 else {
966 return OPERATOR_CANCELLED;
967 }
968
971
972 return OPERATOR_FINISHED;
973}
974
976{
977 /* identifiers */
978 ot->name = "Remove Mask Layer";
979 ot->idname = "GREASE_PENCIL_OT_layer_mask_remove";
980 ot->description = "Remove Layer Mask";
981
983
984 /* callbacks */
987}
988
990{
991 using namespace blender::bke::greasepencil;
993 return false;
994 }
995
997 Layer &active_layer = *grease_pencil.get_active_layer();
998
999 return active_layer.masks.count() > 1;
1000}
1001
1003{
1004 using namespace blender::bke::greasepencil;
1006
1007 if (!grease_pencil.has_active_layer()) {
1008 return OPERATOR_CANCELLED;
1009 }
1010 Layer &active_layer = *grease_pencil.get_active_layer();
1011 const int direction = RNA_enum_get(op->ptr, "direction");
1012
1013 bool changed = false;
1014 if (GreasePencilLayerMask *mask = reinterpret_cast<GreasePencilLayerMask *>(
1015 BLI_findlink(&active_layer.masks, active_layer.active_mask_index)))
1016 {
1017 if (BLI_listbase_link_move(&active_layer.masks, mask, direction)) {
1018 active_layer.active_mask_index = std::max(active_layer.active_mask_index + direction, 0);
1019 changed = true;
1020 }
1021 }
1022 else {
1023 return OPERATOR_CANCELLED;
1024 }
1025
1026 if (changed) {
1029 }
1030
1031 return OPERATOR_FINISHED;
1032}
1033
1035{
1036 /* identifiers */
1037 ot->name = "Reorder Grease Pencil Layer Mask";
1038 ot->idname = "GREASE_PENCIL_OT_layer_mask_reorder";
1039 ot->description = "Reorder the active Grease Pencil mask layer up/down in the list";
1040
1041 /* API callbacks. */
1044
1045 /* flags */
1047
1048 ot->prop = RNA_def_enum(ot->srna, "direction", enum_layer_move_direction, 0, "Direction", "");
1049}
1050
1052 {LAYERGROUP_COLOR_NONE, "NONE", ICON_X, "Set Default icon", ""},
1053 {LAYERGROUP_COLOR_01, "COLOR1", ICON_LAYERGROUP_COLOR_01, "Color tag 1", ""},
1054 {LAYERGROUP_COLOR_02, "COLOR2", ICON_LAYERGROUP_COLOR_02, "Color tag 2", ""},
1055 {LAYERGROUP_COLOR_03, "COLOR3", ICON_LAYERGROUP_COLOR_03, "Color tag 3", ""},
1056 {LAYERGROUP_COLOR_04, "COLOR4", ICON_LAYERGROUP_COLOR_04, "Color tag 4", ""},
1057 {LAYERGROUP_COLOR_05, "COLOR5", ICON_LAYERGROUP_COLOR_05, "Color tag 5", ""},
1058 {LAYERGROUP_COLOR_06, "COLOR6", ICON_LAYERGROUP_COLOR_06, "Color tag 6", ""},
1059 {LAYERGROUP_COLOR_07, "COLOR7", ICON_LAYERGROUP_COLOR_07, "Color tag 7", ""},
1060 {LAYERGROUP_COLOR_08, "COLOR8", ICON_LAYERGROUP_COLOR_08, "Color tag 8", ""},
1061 {0, nullptr, 0, nullptr, nullptr},
1062};
1063
1065{
1066 using namespace blender::bke::greasepencil;
1068
1069 const GroupColorTag color_tag = GroupColorTag(RNA_enum_get(op->ptr, "color_tag"));
1070 LayerGroup *active_group = grease_pencil.get_active_group();
1071 active_group->color_tag = color_tag;
1072
1075
1076 return OPERATOR_FINISHED;
1077}
1078
1080{
1081 /* identifiers */
1082 ot->name = "Grease Pencil Group Color Tag";
1083 ot->idname = "GREASE_PENCIL_OT_layer_group_color_tag";
1084 ot->description = "Change layer group icon";
1085
1088
1089 ot->flag = OPTYPE_UNDO;
1090
1091 ot->prop = RNA_def_enum(ot->srna, "color_tag", enum_layergroup_color_items, 0, "Color Tag", "");
1092}
1093
1095 All = 0,
1097};
1098
1100 const GreasePencil &src_grease_pencil,
1101 const bke::greasepencil::Layer &src_layer,
1102 const DuplicateCopyMode copy_frame_mode,
1103 const int current_frame)
1104{
1105 using namespace blender::bke::greasepencil;
1106 BLI_assert(&src_grease_pencil != &dst_grease_pencil);
1107
1108 /* When copying from another object a new layer is created and all drawings are copied. */
1109 const int src_layer_index = *src_grease_pencil.get_layer_index(src_layer);
1110
1111 Layer &dst_layer = dst_grease_pencil.add_layer(src_layer.name());
1112 const int dst_layer_index = dst_grease_pencil.layers().size() - 1;
1113
1114 BKE_grease_pencil_copy_layer_parameters(src_layer, dst_layer);
1115
1116 const bke::AttributeAccessor src_attributes = src_grease_pencil.attributes();
1117 bke::MutableAttributeAccessor dst_attributes = dst_grease_pencil.attributes_for_write();
1118 src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1119 if (iter.domain != bke::AttrDomain::Layer) {
1120 return;
1121 }
1122 bke::GAttributeReader reader = src_attributes.lookup(iter.name, iter.domain, iter.data_type);
1123 BLI_assert(reader);
1124 bke::GAttributeWriter writer = dst_attributes.lookup_or_add_for_write(
1125 iter.name, iter.domain, iter.data_type);
1126 if (writer) {
1127 const CPPType &cpptype = bke::attribute_type_to_cpp_type(iter.data_type);
1128 BUFFER_FOR_CPP_TYPE_VALUE(cpptype, buffer);
1129 reader.varray.get(src_layer_index, buffer);
1130 writer.varray.set_by_copy(dst_layer_index, buffer);
1131 }
1132 writer.finish();
1133 });
1134
1135 std::optional<int> frame_select = std::nullopt;
1136 if (copy_frame_mode == DuplicateCopyMode::Active) {
1137 frame_select = current_frame;
1138 }
1139 dst_grease_pencil.copy_frames_from_layer(dst_layer, src_grease_pencil, src_layer, frame_select);
1140}
1141
1143{
1144 using namespace blender::bke::greasepencil;
1145 Object *src_object = CTX_data_active_object(C);
1146 const Scene *scene = CTX_data_scene(C);
1147 const int current_frame = scene->r.cfra;
1148 const GreasePencil &src_grease_pencil = *id_cast<GreasePencil *>(src_object->data);
1149 const bool only_active = RNA_boolean_get(op->ptr, "only_active");
1150 const DuplicateCopyMode copy_frame_mode = DuplicateCopyMode(RNA_enum_get(op->ptr, "mode"));
1151
1152 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
1153 if (ob == src_object || ob->type != OB_GREASE_PENCIL) {
1154 continue;
1155 }
1156 GreasePencil &dst_grease_pencil = *id_cast<GreasePencil *>(ob->data);
1157
1158 if (only_active) {
1159 const Layer &active_layer = *src_grease_pencil.get_active_layer();
1161 dst_grease_pencil, src_grease_pencil, active_layer, copy_frame_mode, current_frame);
1162 }
1163 else {
1164 for (const Layer *layer : src_grease_pencil.layers()) {
1166 dst_grease_pencil, src_grease_pencil, *layer, copy_frame_mode, current_frame);
1167 }
1168 }
1169
1170 DEG_id_tag_update(&dst_grease_pencil.id, ID_RECALC_GEOMETRY);
1172 }
1174
1175 return OPERATOR_FINISHED;
1176}
1177
1179{
1180 /* identifiers */
1181 ot->name = "Duplicate Layer to New Object";
1182 ot->idname = "GREASE_PENCIL_OT_layer_duplicate_object";
1183 ot->description = "Make a copy of the active Grease Pencil layer to selected object";
1184
1185 /* API callbacks. */
1188
1189 /* flags */
1191
1193 "only_active",
1194 true,
1195 "Only Active",
1196 "Copy only active Layer, uncheck to append all layers");
1197
1198 static const EnumPropertyItem copy_mode[] = {
1199 {int(DuplicateCopyMode::All), "ALL", 0, "All Frames", ""},
1200 {int(DuplicateCopyMode::Active), "ACTIVE", 0, "Active Frame", ""},
1201 {0, nullptr, 0, nullptr, nullptr},
1202 };
1203
1204 ot->prop = RNA_def_enum(ot->srna, "mode", copy_mode, 0, "Mode", "");
1205}
1206
1207} // namespace ed::greasepencil
1208
1232
1233} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for grease pencil.
General operations, lookup, etc. for blender objects.
#define INT16_MAX
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
#define NA_SELECTED
Definition WM_types.hh:639
#define BLI_assert_unreachable()
void get(int64_t index, void *r_value) const
void set_by_copy(int64_t index, const void *value)
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
void append(const T &value)
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef name) const
GAttributeWriter lookup_or_add_for_write(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
Span< const Layer * > layers() const
const LayerGroup & as_group() const
const LayerGroup * parent_group() const
#define offsetof(t, d)
#define INT_MAX
#define layer
MatBase< C, R > inverse(MatBase< C, R >) RET
const CPPType & attribute_type_to_cpp_type(AttrType type)
static void GREASE_PENCIL_OT_layer_lock_all(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_mask_reorder_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_layer_mask_remove_exec(bContext *C, wmOperator *)
static bool grease_pencil_layer_mask_reorder_poll(bContext *C)
static wmOperatorStatus grease_pencil_layer_move_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem enum_layer_move_direction[]
static wmOperatorStatus grease_pencil_layer_hide_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_layer_add_invoke(bContext *C, wmOperator *op, const wmEvent *event)
GreasePencil * from_context(bContext &C)
static void GREASE_PENCIL_OT_layer_group_add(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_group_add_exec(bContext *C, wmOperator *op)
void grease_pencil_layer_parent_clear(bke::greasepencil::Layer &layer, const bool keep_transform)
static wmOperatorStatus grease_pencil_merge_layer_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_layer_active_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_layer_group_color_tag(wmOperatorType *ot)
static float4x4 get_bone_mat(const Object *parent, const char *parsubstr)
static void GREASE_PENCIL_OT_layer_remove(wmOperatorType *ot)
static void GREASE_PENCIL_OT_layer_isolate(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_layer_hide(wmOperatorType *ot)
static void GREASE_PENCIL_OT_layer_merge(wmOperatorType *ot)
static void copy_layer_and_frames_to_target_object(GreasePencil &dst_grease_pencil, const GreasePencil &src_grease_pencil, const bke::greasepencil::Layer &src_layer, const DuplicateCopyMode copy_frame_mode, const int current_frame)
static void GREASE_PENCIL_OT_layer_add(wmOperatorType *ot)
static bool grease_pencil_layer_mask_poll(bContext *C)
static wmOperatorStatus grease_pencil_layer_add_exec(bContext *C, wmOperator *op)
bool editable_grease_pencil_poll(bContext *C)
static void GREASE_PENCIL_OT_layer_mask_add(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_mask_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_layer_remove_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_layer_active(wmOperatorType *ot)
const EnumPropertyItem enum_layergroup_color_items[]
static void GREASE_PENCIL_OT_layer_move(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_lock_all_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_layer_duplicate_object(wmOperatorType *ot)
static void GREASE_PENCIL_OT_layer_group_remove(wmOperatorType *ot)
static void GREASE_PENCIL_OT_layer_duplicate(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_group_remove_exec(bContext *C, wmOperator *op)
bool grease_pencil_layer_parent_set(bke::greasepencil::Layer &layer, Object *parent, StringRefNull bone, const bool keep_transform)
bool grease_pencil_context_poll(bContext *C)
static wmOperatorStatus grease_pencil_layer_duplicate_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_layer_mask_reorder(wmOperatorType *ot)
void merge_layers(const GreasePencil &src_grease_pencil, const Span< Vector< int > > src_layer_indices_by_dst_layer, GreasePencil &dst_grease_pencil)
bool active_grease_pencil_layer_group_poll(bContext *C)
static wmOperatorStatus grease_pencil_layer_group_color_tag_exec(bContext *C, wmOperator *op)
void select_layer_channel(GreasePencil &grease_pencil, bke::greasepencil::Layer *layer)
static void GREASE_PENCIL_OT_layer_reveal(wmOperatorType *ot)
bool active_grease_pencil_layer_poll(bContext *C)
static wmOperatorStatus grease_pencil_layer_reveal_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_layer_mask_remove(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_layer_isolate_exec(bContext *C, wmOperator *op)
static bool grease_pencil_layer_move_poll(bContext *C)
CartesianBasis invert(const CartesianBasis &basis)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
MatBase< float, 4, 4 > float4x4
void BKE_grease_pencil_copy_layer_parameters(const bke::greasepencil::Layer &src, bke::greasepencil::Layer &dst)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void BKE_grease_pencil_nomain_to_grease_pencil(GreasePencil *grease_pencil_src, GreasePencil *grease_pencil_dst)
void * BLI_findstring_ptr(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:658
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void ED_operatortypes_grease_pencil_layers()
void BKE_grease_pencil_copy_parameters(const GreasePencil &src, GreasePencil &dst)
void unit_m4(float m[4][4])
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmMsgBus * CTX_wm_message_bus(const bContext *C)
wmOperatorStatus WM_operator_props_popup_confirm_ex(bContext *C, wmOperator *op, const wmEvent *, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Scene * CTX_data_scene(const bContext *C)
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
Object * CTX_data_active_object(const bContext *C)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
GreasePencil * BKE_grease_pencil_new_nomain()
signed char int8_t
Definition BKE_text.h:76
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:443
struct GreasePencilLayerTreeNode * prev
ListBaseT< GreasePencilLayerMask > masks
struct bPose * pose
struct RenderData r
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports
#define N_(msgid)
#define WM_msg_publish_rna_prop(mbus, id_, data_, type_, prop_)