Blender V5.3
grease_pencil_edit.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_array.hh"
10#include "BLI_array_utils.hh"
11#include "BLI_assert.hh"
13#include "BLI_index_mask.hh"
14#include "BLI_index_range.hh"
15#include "BLI_listbase.hh"
16#include "BLI_math_base.hh"
17#include "BLI_math_matrix.hh"
18#include "BLI_math_vector.hh"
20#include "BLI_offset_indices.hh"
21#include "BLI_span.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_utildefines.hh"
25#include "BLI_vector.hh"
26
27#include "BLT_translation.hh"
28
29#include "DNA_anim_types.h"
31#include "DNA_layer_types.h"
32#include "DNA_material_types.h"
33#include "DNA_object_types.h"
34#include "DNA_scene_types.h"
35#include "DNA_view3d_types.h"
37
38#include "BKE_anim_data.hh"
39#include "BKE_animsys.hh"
40#include "BKE_attribute.hh"
42#include "BKE_context.hh"
43#include "BKE_curves_utils.hh"
44#include "BKE_customdata.hh"
45#include "BKE_deform.hh"
46#include "BKE_fcurve_driver.h"
47#include "BKE_grease_pencil.hh"
49#include "BKE_instances.hh"
50#include "BKE_lib_id.hh"
51#include "BKE_main.hh"
52#include "BKE_material.hh"
53#include "BKE_preview_image.hh"
54#include "BKE_report.hh"
55#include "BKE_scene.hh"
56
57#include "RNA_access.hh"
58#include "RNA_define.hh"
59#include "RNA_enum_types.hh"
60
61#include "DEG_depsgraph.hh"
63
64#include "ED_curves.hh"
65#include "ED_grease_pencil.hh"
66#include "ED_object.hh"
68#include "ED_view3d.hh"
69
71#include "GEO_fit_curves.hh"
74#include "GEO_reorder.hh"
76#include "GEO_set_curve_type.hh"
78#include "GEO_smooth_curves.hh"
80
81#include "UI_interface_c.hh"
83
84#include "UI_resources.hh"
85#include <limits>
86
87namespace blender {
88
89namespace ed::greasepencil {
90
91/* -------------------------------------------------------------------- */
94
96{
97 const Scene *scene = CTX_data_scene(C);
99 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
100
101 const int iterations = RNA_int_get(op->ptr, "iterations");
102 const float influence = RNA_float_get(op->ptr, "factor");
103 const bool keep_shape = RNA_boolean_get(op->ptr, "keep_shape");
104 const bool smooth_ends = RNA_boolean_get(op->ptr, "smooth_ends");
105
106 const bool smooth_position = RNA_boolean_get(op->ptr, "smooth_position");
107 const bool smooth_radius = RNA_boolean_get(op->ptr, "smooth_radius");
108 const bool smooth_opacity = RNA_boolean_get(op->ptr, "smooth_opacity");
109
110 if (!(smooth_position || smooth_radius || smooth_opacity)) {
111 /* There's nothing to be smoothed, return. */
112 return OPERATOR_FINISHED;
113 }
114
115 bool changed = false;
117 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
119 if (curves.is_empty()) {
120 return;
121 }
122
123 IndexMaskMemory memory;
125 *object, info.drawing, info.layer_index, memory);
126 if (strokes.is_empty()) {
127 return;
128 }
129
130 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
131 const OffsetIndices points_by_curve = curves.points_by_curve();
132 const VArray<bool> cyclic = curves.cyclic();
133 const VArray<bool> point_selection = *curves.attributes().lookup_or_default<bool>(
134 ".selection", bke::AttrDomain::Point, true);
135
136 if (smooth_position) {
137 bke::GSpanAttributeWriter positions = attributes.lookup_for_write_span("position");
139 points_by_curve,
140 point_selection,
141 cyclic,
142 iterations,
143 influence,
144 smooth_ends,
145 keep_shape,
146 positions.span);
147 positions.finish();
148 changed = true;
149 }
150 if (smooth_opacity && info.drawing.opacities().is_span()) {
151 bke::GSpanAttributeWriter opacities = attributes.lookup_for_write_span("opacity");
153 points_by_curve,
154 point_selection,
155 cyclic,
156 iterations,
157 influence,
158 smooth_ends,
159 false,
160 opacities.span);
161 opacities.finish();
162 changed = true;
163 }
164 if (smooth_radius && info.drawing.radii().is_span()) {
165 bke::GSpanAttributeWriter radii = attributes.lookup_for_write_span("radius");
167 points_by_curve,
168 point_selection,
169 cyclic,
170 iterations,
171 influence,
172 smooth_ends,
173 false,
174 radii.span);
175 radii.finish();
176 changed = true;
177 }
178 });
179
180 if (changed) {
183 }
184
185 return OPERATOR_FINISHED;
186}
187
189{
190 PropertyRNA *prop;
191
192 ot->name = "Smooth Stroke";
193 ot->idname = "GREASE_PENCIL_OT_stroke_smooth";
194 ot->description = "Smooth selected strokes";
195
198
200
201 prop = RNA_def_int(ot->srna, "iterations", 10, 1, 100, "Iterations", "", 1, 30);
203 RNA_def_float(ot->srna, "factor", 1.0f, 0.0f, 1.0f, "Factor", "", 0.0f, 1.0f);
204 RNA_def_boolean(ot->srna, "smooth_ends", false, "Smooth Endpoints", "");
205 RNA_def_boolean(ot->srna, "keep_shape", false, "Keep Shape", "");
206
207 RNA_def_boolean(ot->srna, "smooth_position", true, "Position", "");
208 RNA_def_boolean(ot->srna, "smooth_radius", true, "Radius", "");
209 RNA_def_boolean(ot->srna, "smooth_opacity", false, "Opacity", "");
210}
211
213
214/* -------------------------------------------------------------------- */
217
218enum class SimplifyMode {
219 FIXED = 0,
222 MERGE = 3,
223};
224
227 "FIXED",
228 0,
229 "Fixed",
230 "Delete alternating vertices in the stroke, except extremes"},
232 "ADAPTIVE",
233 0,
234 "Adaptive",
235 "Use a Ramer-Douglas-Peucker algorithm to simplify the stroke preserving main shape"},
237 "SAMPLE",
238 0,
239 "Sample",
240 "Re-sample the stroke with segments of the specified length"},
242 "MERGE",
243 0,
244 "Merge",
245 "Simplify the stroke by merging vertices closer than a given distance"},
246 {0, nullptr, 0, nullptr, nullptr},
247};
248
250 const int step,
251 const IndexMask &stroke_selection,
252 IndexMaskMemory &memory)
253{
254 const OffsetIndices points_by_curve = curves.points_by_curve();
255 const Array<int> point_to_curve_map = curves.point_to_curve_map();
256
257 const IndexMask selected_points = IndexMask::from_ranges(
258 points_by_curve, stroke_selection, memory);
259
260 /* Find points to keep among selected points. */
261 const IndexMask selected_to_keep = IndexMask::from_predicate(
262 selected_points, memory, [&](const int64_t i) {
263 const int curve_i = point_to_curve_map[i];
264 const IndexRange points = points_by_curve[curve_i];
265 if (points.size() <= 2) {
266 return true;
267 }
268 const int local_i = i - points.start();
269 return (local_i % int(math::pow(2.0f, float(step))) == 0) || points.last() == i;
270 });
271
272 /* All the points that are not selected are also kept. */
274 {selected_to_keep, selected_points.complement(curves.points_range(), memory)}, memory);
275}
276
278{
279 const Scene *scene = CTX_data_scene(C);
281 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
282
283 const SimplifyMode mode = SimplifyMode(RNA_enum_get(op->ptr, "mode"));
284
285 bool changed = false;
287 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
289 if (curves.is_empty()) {
290 return;
291 }
292
293 IndexMaskMemory memory;
295 *object, info.drawing, info.layer_index, memory);
296 if (strokes.is_empty()) {
297 return;
298 }
299
300 switch (mode) {
301 case SimplifyMode::FIXED: {
302 const int steps = RNA_int_get(op->ptr, "steps");
303 const IndexMask points_to_keep = simplify_fixed(curves, steps, strokes, memory);
304 if (points_to_keep.is_empty()) {
305 info.drawing.strokes_for_write() = {};
306 break;
307 }
308 if (points_to_keep.size() == curves.points_num()) {
309 break;
310 }
312 curves, points_to_keep, {});
314 changed = true;
315 break;
316 }
318 const float simplify_factor = RNA_float_get(op->ptr, "factor");
319 const IndexMask points_to_delete = geometry::simplify_curve_attribute(
320 curves.positions(),
321 strokes,
322 curves.points_by_curve(),
323 curves.cyclic(),
324 simplify_factor,
325 curves.positions(),
326 memory);
327 info.drawing.strokes_for_write().remove_points(points_to_delete, {});
329 changed = true;
330 break;
331 }
333 const float resample_length = RNA_float_get(op->ptr, "length");
334 /* Prevent division by zero. */
335 if (resample_length == 0.0f) {
336 break;
337 }
339 curves, strokes, VArray<float>::from_single(resample_length, curves.curves_num()), {});
341 changed = true;
342 break;
343 }
344 case SimplifyMode::MERGE: {
345 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
346 const Array<int> point_to_curve_map = curves.point_to_curve_map();
347 const float merge_distance = RNA_float_get(op->ptr, "distance");
348 const IndexMask selected_points = IndexMask::from_ranges(points_by_curve, strokes, memory);
349 const IndexMask filtered_points = IndexMask::from_predicate(
350 selected_points, memory, [&](const int64_t i) {
351 const int curve_i = point_to_curve_map[i];
352 const IndexRange points = points_by_curve[curve_i];
353 if (points.drop_front(1).drop_back(1).contains(i)) {
354 return true;
355 }
356 return false;
357 });
359 curves, merge_distance, filtered_points, {});
361 changed = true;
362 break;
363 }
364 default:
365 break;
366 }
367 });
368
369 if (changed) {
372 }
373 return OPERATOR_FINISHED;
374}
375
377{
378 ui::Layout &layout = *op->layout;
380
382
383 layout.use_property_split_set(true);
384 layout.use_property_decorate_set(false);
385
386 layout.prop(&ptr, "mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
387
388 const SimplifyMode mode = SimplifyMode(RNA_enum_get(op->ptr, "mode"));
389
390 switch (mode) {
392 layout.prop(&ptr, "steps", UI_ITEM_NONE, std::nullopt, ICON_NONE);
393 break;
395 layout.prop(&ptr, "factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
396 break;
398 layout.prop(&ptr, "length", UI_ITEM_NONE, std::nullopt, ICON_NONE);
399 break;
401 layout.prop(&ptr, "distance", UI_ITEM_NONE, std::nullopt, ICON_NONE);
402 break;
403 default:
404 break;
405 }
406}
407
409{
410 PropertyRNA *prop;
411
412 ot->name = "Simplify Stroke";
413 ot->idname = "GREASE_PENCIL_OT_stroke_simplify";
414 ot->description = "Simplify selected strokes";
415
418
420
422
423 prop = RNA_def_float(ot->srna, "factor", 0.01f, 0.0f, 100.0f, "Factor", "", 0.0f, 100.0f);
425 prop = RNA_def_float(ot->srna, "length", 0.05f, 0.0f, 100.0f, "Length", "", 0.01f, 1.0f);
427 prop = RNA_def_float(ot->srna, "distance", 0.01f, 0.0f, 100.0f, "Distance", "", 0.0f, 1.0f);
429 prop = RNA_def_int(ot->srna, "steps", 1, 0, 50, "Steps", "", 0.0f, 10);
431 prop = RNA_def_enum(ot->srna,
432 "mode",
434 0,
435 "Mode",
436 "Method used for simplifying stroke points");
438}
439
441
442/* -------------------------------------------------------------------- */
445
446enum class DeleteMode {
447 All = 0,
450};
451
454 const int layer_index,
455 const DeleteMode mode,
456 IndexMaskMemory &memory)
457{
458 const IndexMask editable_strokes = retrieve_editable_and_selected_strokes(
459 object, drawing, layer_index, memory);
460 if (editable_strokes.is_empty()) {
461 return false;
462 }
464 const bke::AttributeAccessor attributes = curves.attributes();
465 const VArray<int> fill_ids = *attributes.lookup_or_default<int>(
466 "fill_id", bke::AttrDomain::Curve, 0);
467 const VArray<bool> hidden_strokes = *attributes.lookup_or_default<bool>(
468 "hide_stroke", bke::AttrDomain::Curve, false);
469 const IndexMask strokes_to_delete = [&]() -> IndexMask {
470 if (mode == DeleteMode::OnlyStrokes) {
471 /* Only curves that are unfilled and have the stroke set. */
472 return IndexMask::from_predicate(editable_strokes, memory, [&](const int index) {
473 return fill_ids[index] == 0 && !hidden_strokes[index];
474 });
475 }
476 if (mode == DeleteMode::OnlyFills) {
477 /* Only curves that don't have the stroke set and are filled. */
478 return IndexMask::from_predicate(editable_strokes, memory, [&](const int index) {
479 return hidden_strokes[index] && fill_ids[index] != 0;
480 });
481 }
483 return {};
484 }();
485 if (!strokes_to_delete.is_empty()) {
486 curves.remove_curves(strokes_to_delete, {});
487 drawing.tag_topology_changed();
488 return true;
489 }
490 return false;
491}
492
495 const int layer_index,
496 const DeleteMode mode,
497 IndexMaskMemory &memory)
498{
500 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
501 if (!attributes.contains("fill_id")) {
502 return false;
503 }
504 const IndexMask editable_strokes = retrieve_editable_and_selected_strokes(
505 object, drawing, layer_index, memory);
506 const VArray<int> fill_ids = *attributes.lookup<int>("fill_id", bke::AttrDomain::Curve);
507 const VArray<bool> hidden_strokes = *attributes.lookup_or_default<bool>(
508 "hide_stroke", bke::AttrDomain::Curve, false);
509 const IndexMask fills_with_stroke = IndexMask::from_predicate(
510 editable_strokes, memory, [&](const int index) {
511 return fill_ids[index] != 0 && !hidden_strokes[index];
512 });
513 if (fills_with_stroke.is_empty()) {
514 return false;
515 }
516 if (mode == DeleteMode::OnlyStrokes) {
517 bke::SpanAttributeWriter<bool> hide_strokes = attributes.lookup_or_add_for_write_span<bool>(
518 "hide_stroke", bke::AttrDomain::Curve, bke::AttributeInitValue(false));
519 index_mask::masked_fill(hide_strokes.span, true, fills_with_stroke);
520 hide_strokes.finish();
521 }
522 else if (mode == DeleteMode::OnlyFills) {
523 bke::SpanAttributeWriter<int> fill_ids = attributes.lookup_for_write_span<int>("fill_id");
524 index_mask::masked_fill(fill_ids.span, 0, fills_with_stroke);
525 fill_ids.finish();
526 drawing.tag_fills_changed();
527 }
528 return true;
529}
530
532{
533 const Scene *scene = CTX_data_scene(C);
535 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
536
537 const auto mode = DeleteMode(RNA_enum_get(op->ptr, "mode"));
539 scene->toolsettings);
540
541 bool changed = false;
543 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
544 IndexMaskMemory memory;
545 if (mode == DeleteMode::All) {
546 const IndexMask elements_to_delete = retrieve_editable_and_selected_elements(
547 *object, info.drawing, info.layer_index, selection_domain, memory);
548 if (elements_to_delete.is_empty()) {
549 return;
550 }
552 if (selection_domain == bke::AttrDomain::Curve) {
553 curves.remove_curves(elements_to_delete, {});
555 changed = true;
556 }
557 else if (selection_domain == bke::AttrDomain::Point) {
560 changed = true;
561 }
562 return;
563 }
564
565 if (remove_curves_based_on_mode(*object, info.drawing, info.layer_index, mode, memory)) {
566 changed = true;
567 }
568
569 if (remove_stroke_or_fill_based_on_mode(*object, info.drawing, info.layer_index, mode, memory))
570 {
571 changed = true;
572 }
573 });
574
575 if (changed) {
578 }
579 return OPERATOR_FINISHED;
580}
581
583{
584 static const EnumPropertyItem prop_delete_modes[] = {
585 {int(DeleteMode::All), "ALL", 0, "All", "Delete all selected strokes or points"},
587 "STROKES",
588 0,
589 "Only Strokes",
590 "Delete only strokes and not fills"},
591 {int(DeleteMode::OnlyFills), "FILLS", 0, "Only Fills", "Delete only fills and not strokes"},
592 {0, nullptr, 0, nullptr, nullptr},
593 };
594
595 ot->name = "Delete";
596 ot->idname = "GREASE_PENCIL_OT_delete";
597 ot->description = "Delete selected strokes or points";
598
601
603
604 ot->prop = RNA_def_enum(
605 ot->srna, "mode", prop_delete_modes, 0, "Mode", "The kind of strokes or fills to delete");
607}
608
610
611/* -------------------------------------------------------------------- */
614
615enum class DissolveMode : int8_t {
622};
623
625 {int(DissolveMode::POINTS), "POINTS", 0, "Dissolve", "Dissolve selected points"},
627 "BETWEEN",
628 0,
629 "Dissolve Between",
630 "Dissolve points between selected points"},
632 "UNSELECT",
633 0,
634 "Dissolve Unselect",
635 "Dissolve all unselected points"},
636 {0, nullptr, 0, nullptr, nullptr},
637};
638
640 const IndexMask &mask,
641 const DissolveMode mode)
642{
643 Array<bool> points_to_dissolve(curves.points_num());
644 mask.to_bools(points_to_dissolve);
645
646 if (mode == DissolveMode::POINTS) {
647 return points_to_dissolve;
648 }
649
650 /* Both `between` and `unselect` have the unselected point being the ones dissolved so we need
651 * to invert. */
653
654 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
655 /* Because we are going to invert, these become the points to keep. */
656 MutableSpan<bool> points_to_keep = points_to_dissolve.as_mutable_span();
657
658 threading::parallel_for(curves.curves_range(), 128, [&](const IndexRange range) {
659 for (const int64_t curve_i : range) {
660 const IndexRange points = points_by_curve[curve_i];
661 const Span<bool> curve_selection = points_to_dissolve.as_span().slice(points);
662 /* The unselected curves should not be dissolved. */
663 if (!curve_selection.contains(true)) {
664 points_to_keep.slice(points).fill(true);
665 continue;
666 }
667
668 /* `between` is just `unselect` but with the first and last segments not getting
669 * dissolved. */
670 if (mode != DissolveMode::BETWEEN) {
671 continue;
672 }
673
674 const Vector<IndexRange> deselection_ranges = array_utils::find_all_ranges(curve_selection,
675 false);
676
677 if (deselection_ranges.size() != 0) {
678 const IndexRange first_range = deselection_ranges.first().shift(points.first());
679 const IndexRange last_range = deselection_ranges.last().shift(points.first());
680
681 /* Ranges should only be fill if the first/last point matches the start/end point
682 * of the segment. */
683 if (first_range.first() == points.first()) {
684 points_to_keep.slice(first_range).fill(true);
685 }
686 if (last_range.last() == points.last()) {
687 points_to_keep.slice(last_range).fill(true);
688 }
689 }
690 }
691 });
692
693 array_utils::invert_booleans(points_to_dissolve);
694
695 return points_to_dissolve;
696}
697
699{
700 const Scene *scene = CTX_data_scene(C);
702 View3D *v3d = CTX_wm_view3d(C);
703 const eHandleDisplay handle_display = view3d_handle_type_or_default(v3d);
704 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
705
706 const DissolveMode mode = DissolveMode(RNA_enum_get(op->ptr, "type"));
707
708 bool changed = false;
710 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
712 if (curves.is_empty()) {
713 return;
714 }
715
716 IndexMaskMemory memory;
718 *object, info.drawing, info.layer_index, handle_display, memory);
719 if (points.is_empty()) {
720 return;
721 }
722
723 const Array<bool> points_to_dissolve = get_points_to_dissolve(curves, points, mode);
724 if (points_to_dissolve.as_span().contains(true)) {
725 curves.remove_points(IndexMask::from_bools(points_to_dissolve, memory), {});
727 changed = true;
728 }
729 });
730
731 if (changed) {
734 }
735 return OPERATOR_FINISHED;
736}
737
739{
740 PropertyRNA *prop;
741
742 ot->name = "Dissolve";
743 ot->idname = "GREASE_PENCIL_OT_dissolve";
744 ot->description = "Delete selected points without splitting strokes";
745
749
751
752 ot->prop = prop = RNA_def_enum(ot->srna,
753 "type",
755 0,
756 "Type",
757 "Method used for dissolving stroke points");
760}
761
763
764/* -------------------------------------------------------------------- */
767
774
777 "ACTIVE_FRAME",
778 0,
779 "Active Frame",
780 "Deletes current frame in the active layer"},
782 "ALL_FRAMES",
783 0,
784 "All Active Frames",
785 "Delete active frames for all layers"},
786 {0, nullptr, 0, nullptr, nullptr},
787};
788
790{
791 const Scene *scene = CTX_data_scene(C);
793 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
794 const int current_frame = scene->r.cfra;
795
796 const DeleteFrameMode mode = DeleteFrameMode(RNA_enum_get(op->ptr, "type"));
797
798 bool changed = false;
799 if (mode == DeleteFrameMode::ACTIVE_FRAME && grease_pencil.has_active_layer()) {
800 bke::greasepencil::Layer &layer = *grease_pencil.get_active_layer();
801 if (layer.is_editable() && layer.start_frame_at(current_frame)) {
802 changed |= grease_pencil.remove_frames(layer, {*layer.start_frame_at(current_frame)});
803 }
804 }
805 else if (mode == DeleteFrameMode::ALL_FRAMES) {
806 for (bke::greasepencil::Layer *layer : grease_pencil.layers_for_write()) {
807 if (layer->is_editable() && layer->start_frame_at(current_frame)) {
808 changed |= grease_pencil.remove_frames(*layer, {*layer->start_frame_at(current_frame)});
809 }
810 }
811 }
812
813 if (changed) {
817 }
818
819 return OPERATOR_FINISHED;
820}
821
823{
824 PropertyRNA *prop;
825
826 ot->name = "Delete Frame";
827 ot->idname = "GREASE_PENCIL_OT_delete_frame";
828 ot->description = "Delete Grease Pencil Frame(s)";
829
833
835
836 ot->prop = prop = RNA_def_enum(ot->srna,
837 "type",
839 0,
840 "Type",
841 "Method used for deleting Grease Pencil frames");
843}
844
845
846/* -------------------------------------------------------------------- */
849
851{
852 Main *bmain = CTX_data_main(C);
853 const Scene *scene = CTX_data_scene(C);
855 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
856 Material *ma = nullptr;
857 char name[MAX_ID_NAME - 2];
858 RNA_string_get(op->ptr, "material", name);
859
860 int material_index = object->actcol - 1;
861
862 if (name[0] != '\0') {
863 ma = reinterpret_cast<Material *>(BKE_libblock_find_name(bmain, ID_MA, name));
864 if (ma == nullptr) {
865 BKE_reportf(op->reports, RPT_WARNING, TIP_("Material '%s' could not be found"), name);
866 return OPERATOR_CANCELLED;
867 }
868
869 /* Find slot index. */
870 material_index = BKE_object_material_index_get(object, ma);
871 }
872
873 if (material_index == -1) {
874 return OPERATOR_CANCELLED;
875 }
876
878 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
879 IndexMaskMemory memory;
881 *object, info.drawing, info.layer_index, memory);
882 if (strokes.is_empty()) {
883 return;
884 }
885
887 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
889 "material_index", bke::AttrDomain::Curve);
890
892 strokes, curves, bke::AttrDomain::Curve, memory);
893
894 index_mask::masked_fill(materials.span, material_index, fill_strokes);
895 materials.finish();
896 });
897
900
901 return OPERATOR_FINISHED;
902}
903
905{
906 ot->name = "Assign Material";
907 ot->idname = "GREASE_PENCIL_OT_stroke_material_set";
908 ot->description = "Assign the active material slot to the selected strokes";
909
912
914
915 ot->prop = RNA_def_string(
916 ot->srna, "material", nullptr, MAX_ID_NAME - 2, "Material", "Name of the material");
918}
919
920
921/* -------------------------------------------------------------------- */
924
925enum class CyclicalMode : int8_t {
927 CLOSE = 0,
929 OPEN = 1,
932};
933
935 {int(CyclicalMode::CLOSE), "CLOSE", 0, "Close All", ""},
936 {int(CyclicalMode::OPEN), "OPEN", 0, "Open All", ""},
937 {int(CyclicalMode::TOGGLE), "TOGGLE", 0, "Toggle", ""},
938 {0, nullptr, 0, nullptr, nullptr},
939};
940
942 const IndexMask &strokes)
943{
944 const VArray<bool> cyclic = curves.cyclic();
945 const Span<float3> positions = curves.positions();
946 curves.ensure_evaluated_lengths();
947
948 Array<int> use_cuts(curves.points_num(), 0);
949 const OffsetIndices points_by_curve = curves.points_by_curve();
950
951 strokes.foreach_index(
952 [&](const int curve_i) {
953 if (cyclic[curve_i]) {
954 const IndexRange points = points_by_curve[curve_i];
955 const float end_distance = math::distance(positions[points.first()],
956 positions[points.last()]);
957
958 /* Because the curve is already cyclical the last segment has to be subtracted. */
959 const float curve_length = curves.evaluated_length_total_for_curve(curve_i, true) -
960 end_distance;
961
962 /* Calculate cuts to match the average density. */
963 const float point_density = float(points.size()) / curve_length;
964 use_cuts[points.last()] = int(point_density * end_distance);
965 }
966 },
968
969 const VArray<int> cuts = VArray<int>::from_span(use_cuts.as_span());
970
971 return geometry::subdivide_curves(curves, strokes, cuts);
972}
973
975{
976 const Scene *scene = CTX_data_scene(C);
978 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
979
980 const CyclicalMode mode = CyclicalMode(RNA_enum_get(op->ptr, "type"));
981 const bool subdivide_cyclic_segment = RNA_boolean_get(op->ptr, "subdivide_cyclic_segment");
982
983 bool changed = false;
985 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
987 if (mode == CyclicalMode::OPEN && !curves.attributes().contains("cyclic")) {
988 /* Avoid creating unneeded attribute. */
989 return;
990 }
991
992 IndexMaskMemory memory;
994 *object, info.drawing, info.layer_index, memory);
995 if (strokes.is_empty()) {
996 return;
997 }
998
999 MutableSpan<bool> cyclic = curves.cyclic_for_write();
1000 switch (mode) {
1002 index_mask::masked_fill(cyclic, true, strokes);
1003 break;
1004 case CyclicalMode::OPEN:
1005 index_mask::masked_fill(cyclic, false, strokes);
1006 break;
1008 array_utils::invert_booleans(cyclic, strokes);
1009 break;
1010 }
1011
1012 /* Remove the attribute if it is empty. */
1013 if (mode != CyclicalMode::CLOSE) {
1015 curves.attributes_for_write().remove("cyclic");
1016 }
1017 }
1018
1019 if (subdivide_cyclic_segment) {
1020 /* Update to properly calculate the lengths. */
1021 curves.tag_topology_changed();
1022
1024 }
1025
1027 changed = true;
1028 });
1029
1030 if (changed) {
1033 }
1034
1035 return OPERATOR_FINISHED;
1036}
1037
1039{
1040 ot->name = "Set Cyclical State";
1041 ot->idname = "GREASE_PENCIL_OT_cyclical_set";
1042 ot->description = "Close or open the selected stroke adding a segment from last to first point";
1043
1044 ot->invoke = WM_menu_invoke;
1047
1049
1050 ot->prop = RNA_def_enum(
1051 ot->srna, "type", prop_cyclical_types, int(CyclicalMode::TOGGLE), "Type", "");
1052
1054 "subdivide_cyclic_segment",
1055 true,
1056 "Match Point Density",
1057 "Add point in the new segment to keep the same density");
1058}
1059
1061
1062/* -------------------------------------------------------------------- */
1065
1067{
1068 const Scene *scene = CTX_data_scene(C);
1069 Object *object = CTX_data_active_object(C);
1070 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1071
1072 if (object->totcol == 0) {
1073 return OPERATOR_CANCELLED;
1074 }
1075
1077 for (const MutableDrawingInfo &info : drawings) {
1078 IndexMaskMemory memory;
1080 *object, info.drawing, info.layer_index, memory);
1081 if (strokes.is_empty()) {
1082 continue;
1083 }
1084 bke::CurvesGeometry &curves = info.drawing.strokes_for_write();
1085
1086 const VArray<int> materials = *curves.attributes().lookup_or_default<int>(
1087 "material_index", bke::AttrDomain::Curve, 0);
1088 object->actcol = materials[strokes.first()] + 1;
1090 break;
1091 };
1092
1094
1095 return OPERATOR_FINISHED;
1096}
1097
1099{
1100 ot->name = "Set Active Material";
1101 ot->idname = "GREASE_PENCIL_OT_set_active_material";
1102 ot->description = "Set the selected stroke material as the active material";
1103
1106
1108}
1109
1110
1111/* -------------------------------------------------------------------- */
1114
1116{
1117 const Scene *scene = CTX_data_scene(C);
1118 Object *object = CTX_data_active_object(C);
1119 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1120
1121 /* Radius is half of the thickness. */
1122 const float radius = RNA_float_get(op->ptr, "thickness") * 0.5f;
1123
1124 bool changed = false;
1126 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1127 IndexMaskMemory memory;
1129 *object, info.drawing, info.layer_index, memory);
1130 if (strokes.is_empty()) {
1131 return;
1132 }
1134
1135 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1137 bke::curves::fill_points<float>(points_by_curve, strokes, radius, radii);
1138 changed = true;
1139 });
1140
1141 if (changed) {
1144 }
1145
1146 return OPERATOR_FINISHED;
1147}
1148
1150{
1151 ot->name = "Set Uniform Thickness";
1152 ot->idname = "GREASE_PENCIL_OT_set_uniform_thickness";
1153 ot->description = "Set all stroke points to same thickness";
1154
1157
1159
1160 ot->prop = RNA_def_float(
1161 ot->srna, "thickness", 0.1f, 0.0f, 1000.0f, "Thickness", "Thickness", 0.0f, 1000.0f);
1162}
1163
1165/* -------------------------------------------------------------------- */
1168
1170{
1171 using namespace blender::bke;
1172
1173 const Scene *scene = CTX_data_scene(C);
1174 Object *object = CTX_data_active_object(C);
1175 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1176
1177 const float opacity_stroke = RNA_float_get(op->ptr, "opacity_stroke");
1178 const float opacity_fill = RNA_float_get(op->ptr, "opacity_fill");
1179
1180 bool changed = false;
1182 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1183 IndexMaskMemory memory;
1185 *object, info.drawing, info.layer_index, memory);
1186 if (strokes.is_empty()) {
1187 return;
1188 }
1190 MutableAttributeAccessor attributes = curves.attributes_for_write();
1191 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1192
1194 bke::curves::fill_points<float>(points_by_curve, strokes, opacity_stroke, opacities);
1195
1196 if (SpanAttributeWriter<float> fill_opacities = attributes.lookup_or_add_for_write_span<float>(
1197 "fill_opacity", AttrDomain::Curve, bke::AttributeInitValue(1.0f)))
1198 {
1199 index_mask::masked_fill(fill_opacities.span, opacity_fill, strokes);
1200 fill_opacities.finish();
1201 }
1202
1203 changed = true;
1204 });
1205
1206 if (changed) {
1209 }
1210
1211 return OPERATOR_FINISHED;
1212}
1213
1215{
1216 ot->name = "Set Uniform Opacity";
1217 ot->idname = "GREASE_PENCIL_OT_set_uniform_opacity";
1218 ot->description = "Set all stroke points to same opacity";
1219
1222
1224
1225 /* Differentiate default opacities for stroke & fills so shapes with same stroke+fill colors will
1226 * be more readable. */
1227 RNA_def_float(ot->srna, "opacity_stroke", 1.0f, 0.0f, 1.0f, "Stroke Opacity", "", 0.0f, 1.0f);
1228 RNA_def_float(ot->srna, "opacity_fill", 0.5f, 0.0f, 1.0f, "Fill Opacity", "", 0.0f, 1.0f);
1229}
1230
1232
1233/* -------------------------------------------------------------------- */
1236
1238 wmOperator * /*op*/)
1239{
1240 const Scene *scene = CTX_data_scene(C);
1241 Object *object = CTX_data_active_object(C);
1242 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1243
1244 bool changed = false;
1246 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1247 IndexMaskMemory memory;
1249 *object, info.drawing, info.layer_index, memory);
1250 if (strokes.is_empty()) {
1251 return;
1252 }
1254
1255 /* Switch stroke direction. */
1256 curves.reverse_curves(strokes);
1257
1258 changed = true;
1259 });
1260
1261 if (changed) {
1264 }
1265
1266 return OPERATOR_FINISHED;
1267}
1268
1270{
1271 /* identifiers */
1272 ot->name = "Switch Direction";
1273 ot->idname = "GREASE_PENCIL_OT_stroke_switch_direction";
1274 ot->description = "Change direction of the points of the selected strokes";
1275
1276 /* Callbacks. */
1279
1281}
1282
1284/* -------------------------------------------------------------------- */
1288 const IndexMask &mask)
1289{
1290
1291 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1292 const VArray<bool> src_cyclic = curves.cyclic();
1293
1294 /* Early-return if no cyclic curves. */
1296 return curves;
1297 }
1298
1299 Array<bool> start_set_points(curves.points_num());
1300 mask.to_bools(start_set_points.as_mutable_span());
1301
1302 Array<int> dst_to_src_point(curves.points_num());
1303
1304 threading::parallel_for(curves.curves_range(), 1024, [&](const IndexRange range) {
1305 for (const int curve_i : range) {
1306 const IndexRange points = points_by_curve[curve_i];
1307 const Span<bool> curve_i_selected_points = start_set_points.as_span().slice(points);
1308 const int first_selected = curve_i_selected_points.first_index_try(true);
1309
1310 MutableSpan<int> dst_to_src_slice = dst_to_src_point.as_mutable_span().slice(points);
1311
1312 array_utils::fill_index_range<int>(dst_to_src_slice, points.start());
1313
1314 if (first_selected == -1 || src_cyclic[curve_i] == false) {
1315 continue;
1316 }
1317
1318 std::rotate(dst_to_src_slice.begin(),
1319 dst_to_src_slice.begin() + first_selected,
1320 dst_to_src_slice.end());
1321 }
1322 });
1323
1324 /* New CurvesGeometry to copy to. */
1325 bke::CurvesGeometry dst_curves(curves.points_num(), curves.curves_num());
1326 BKE_defgroup_copy_list(&dst_curves.vertex_group_names, &curves.vertex_group_names);
1327
1328 /* Copy offsets. */
1329 array_utils::copy(curves.offsets(), dst_curves.offsets_for_write());
1330
1331 /* Attribute accessors for copying. */
1332 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
1333 const bke::AttributeAccessor src_attributes = curves.attributes();
1334
1335 /* Copy curve attrs. */
1337 src_attributes, bke::AttrDomain::Curve, bke::AttrDomain::Curve, {}, dst_attributes);
1338 array_utils::copy(src_cyclic, dst_curves.cyclic_for_write());
1339
1340 /* Copy point attrs */
1341 gather_attributes(src_attributes,
1344 {},
1345 dst_to_src_point,
1346 dst_attributes);
1347
1348 dst_curves.update_curve_types();
1349 /* TODO: change to copying knots by point. */
1350 if (curves.nurbs_has_custom_knots()) {
1352 dst_curves.curves_range(), NURBS_KNOT_MODE_NORMAL, NURBS_KNOT_MODE_NORMAL, dst_curves);
1353 }
1354 return dst_curves;
1355}
1356
1358{
1359 using namespace bke::greasepencil;
1360 const Scene *scene = CTX_data_scene(C);
1361 Object *object = CTX_data_active_object(C);
1362 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1363
1364 std::atomic<bool> changed = false;
1366 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1367 IndexMaskMemory memory;
1369 *object, info.drawing, info.layer_index, memory);
1370 if (selection.is_empty()) {
1371 return;
1372 }
1373
1374 info.drawing.strokes_for_write() = set_start_point(info.drawing.strokes(), selection);
1375
1377 changed = true;
1378 });
1379
1380 if (changed) {
1383 }
1384 return OPERATOR_FINISHED;
1385}
1387{
1388 /* Identifiers */
1389 ot->name = "Set Start Point";
1390 ot->idname = "GREASE_PENCIL_OT_set_start_point";
1391 ot->description = "Select which point is the beginning of the curve";
1392
1393 /* Callbacks */
1396
1398}
1399
1400
1401/* -------------------------------------------------------------------- */
1404
1405enum class CapsMode : int8_t {
1407 FLAT = 0,
1411 END = 2,
1414};
1415
1416static void toggle_caps(MutableSpan<int8_t> caps, const IndexMask &strokes)
1417{
1418 strokes.foreach_index([&](const int stroke_i) {
1419 if (caps[stroke_i] == GP_STROKE_CAP_FLAT) {
1420 caps[stroke_i] = GP_STROKE_CAP_ROUND;
1421 }
1422 else {
1423 caps[stroke_i] = GP_STROKE_CAP_FLAT;
1424 }
1425 });
1426}
1427
1429{
1430 const Scene *scene = CTX_data_scene(C);
1431 Object *object = CTX_data_active_object(C);
1432 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1433
1434 const CapsMode mode = CapsMode(RNA_enum_get(op->ptr, "type"));
1435
1436 bool changed = false;
1438 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1440 IndexMaskMemory memory;
1442 *object, info.drawing, info.layer_index, memory);
1443 if (strokes.is_empty()) {
1444 return;
1445 }
1446
1447 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
1448
1449 if (ELEM(mode, CapsMode::ROUND, CapsMode::FLAT)) {
1450 const int8_t flag_set = (mode == CapsMode::ROUND) ? int8_t(GP_STROKE_CAP_TYPE_ROUND) :
1452 if (bke::SpanAttributeWriter<int8_t> start_caps =
1454 {
1455 index_mask::masked_fill(start_caps.span, flag_set, strokes);
1456 start_caps.finish();
1457 }
1460 {
1461 index_mask::masked_fill(end_caps.span, flag_set, strokes);
1462 end_caps.finish();
1463 }
1464 }
1465 else {
1466 switch (mode) {
1467 case CapsMode::START: {
1469 attributes.lookup_or_add_for_write_span<int8_t>("start_cap",
1471 {
1472 toggle_caps(caps.span, strokes);
1473 caps.finish();
1474 }
1475 break;
1476 }
1477 case CapsMode::END: {
1479 attributes.lookup_or_add_for_write_span<int8_t>("end_cap",
1481 {
1482 toggle_caps(caps.span, strokes);
1483 caps.finish();
1484 }
1485 break;
1486 }
1487 case CapsMode::ROUND:
1488 case CapsMode::FLAT:
1489 break;
1490 }
1491 }
1492
1493 changed = true;
1494 });
1495
1496 if (changed) {
1499 }
1500
1501 return OPERATOR_FINISHED;
1502}
1503
1505{
1506 static const EnumPropertyItem prop_caps_types[] = {
1507 {int(CapsMode::ROUND), "ROUND", ICON_GP_CAPS_ROUND, "Rounded", "Set as default rounded"},
1508 {int(CapsMode::FLAT), "FLAT", ICON_GP_CAPS_FLAT, "Flat", ""},
1510 {int(CapsMode::START), "START", 0, "Toggle Start", ""},
1511 {int(CapsMode::END), "END", 0, "Toggle End", ""},
1512 {0, nullptr, 0, nullptr, nullptr},
1513 };
1514
1515 ot->name = "Set Curve Caps";
1516 ot->idname = "GREASE_PENCIL_OT_caps_set";
1517 ot->description = "Change curve caps mode (rounded or flat)";
1518
1519 ot->invoke = WM_menu_invoke;
1522
1524
1525 ot->prop = RNA_def_enum(ot->srna, "type", prop_caps_types, int(CapsMode::ROUND), "Type", "");
1526}
1527
1529
1530/* -------------------------------------------------------------------- */
1533
1534/* Retry enum items with object materials. */
1536 PointerRNA * /*ptr*/,
1537 PropertyRNA * /*prop*/,
1538 bool *r_free)
1539{
1540 Object *ob = (C) ? CTX_data_active_object(C) : nullptr;
1541 EnumPropertyItem *item = nullptr, item_tmp = {0};
1542 int totitem = 0;
1543
1544 if (ob == nullptr) {
1546 }
1547
1548 /* Existing materials */
1549 for (const int i : IndexRange(ob->totcol)) {
1550 if (Material *ma = BKE_object_material_get(ob, i + 1)) {
1551 item_tmp.identifier = ma->id.name + 2;
1552 item_tmp.name = ma->id.name + 2;
1553 item_tmp.value = i + 1;
1554 item_tmp.icon = ma->preview ? ma->preview->runtime->icon_id : ICON_NONE;
1555
1556 RNA_enum_item_add(&item, &totitem, &item_tmp);
1557 }
1558 }
1559 RNA_enum_item_end(&item, &totitem);
1560 *r_free = true;
1561
1562 return item;
1563}
1564
1566{
1567 Object *object = CTX_data_active_object(C);
1568 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1569 const int slot = RNA_enum_get(op->ptr, "slot");
1570
1571 /* Try to get material slot. */
1572 if ((slot < 1) || (slot > object->totcol)) {
1573 return OPERATOR_CANCELLED;
1574 }
1575
1576 /* Set active material. */
1577 object->actcol = slot;
1578
1580
1581 return OPERATOR_FINISHED;
1582}
1583
1585{
1586 ot->name = "Set Active Material";
1587 ot->idname = "GREASE_PENCIL_OT_set_material";
1588 ot->description = "Set active material";
1589
1592
1594
1595 /* Material to use (dynamic enum) */
1596 ot->prop = RNA_def_enum(ot->srna, "slot", rna_enum_dummy_DEFAULT_items, 0, "Material Slot", "");
1598}
1599
1600
1601/* -------------------------------------------------------------------- */
1604
1606{
1607 const Scene *scene = CTX_data_scene(C);
1608 Object *object = CTX_data_active_object(C);
1609 View3D *v3d = CTX_wm_view3d(C);
1610 const eHandleDisplay handle_display = view3d_handle_type_or_default(v3d);
1611 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1612
1614 scene->toolsettings);
1615
1616 std::atomic<bool> changed = false;
1618 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1619 IndexMaskMemory memory;
1620
1622 const int num_old_curves = curves.curves_num();
1623 if (selection_domain == bke::AttrDomain::Curve) {
1625 *object, info.drawing, info.layer_index, memory);
1626 if (strokes.is_empty()) {
1627 return;
1628 }
1630 }
1631 else if (selection_domain == bke::AttrDomain::Point) {
1633 *object, info.drawing, info.layer_index, handle_display, memory);
1634 if (points.is_empty()) {
1635 return;
1636 }
1638 }
1639
1642 changed.store(true, std::memory_order_relaxed);
1643 });
1644
1645 if (changed) {
1648 }
1649 return OPERATOR_FINISHED;
1650}
1651
1653{
1654 ot->name = "Duplicate";
1655 ot->idname = "GREASE_PENCIL_OT_duplicate";
1656 ot->description = "Duplicate the selected points";
1657
1660
1662}
1663
1665{
1666 Object *object = CTX_data_active_object(C);
1667 Scene &scene = *CTX_data_scene(C);
1668 const int limit = RNA_int_get(op->ptr, "limit");
1669
1670 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1672
1673 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1675 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1676
1677 IndexMaskMemory memory;
1679 *object, info.drawing, info.layer_index, memory);
1680
1681 const IndexMask curves_to_delete = IndexMask::from_predicate(
1682 editable_strokes, memory, [&](const int i) { return points_by_curve[i].size() <= limit; });
1683
1684 curves.remove_curves(curves_to_delete, {});
1685 });
1686
1689
1690 return OPERATOR_FINISHED;
1691}
1692
1694 wmOperator *op,
1695 const wmEvent *event)
1696{
1698 C, op, event, IFACE_("Remove Loose Points"), IFACE_("Delete"));
1699}
1700
1702{
1703 ot->name = "Clean Loose Points";
1704 ot->idname = "GREASE_PENCIL_OT_clean_loose";
1705 ot->description = "Remove loose points";
1706
1710
1712
1713 RNA_def_int(ot->srna,
1714 "limit",
1715 1,
1716 1,
1717 INT_MAX,
1718 "Limit",
1719 "Number of points to consider stroke as loose",
1720 1,
1721 INT_MAX);
1722}
1723
1725
1726/* -------------------------------------------------------------------- */
1729
1731{
1732 const int cuts = RNA_int_get(op->ptr, "number_cuts");
1733 const bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
1734
1735 std::atomic<bool> changed = false;
1736
1737 const Scene *scene = CTX_data_scene(C);
1738 Object *object = CTX_data_active_object(C);
1739 View3D *v3d = CTX_wm_view3d(C);
1740 const eHandleDisplay handle_display = view3d_handle_type_or_default(v3d);
1741 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1743 scene->toolsettings);
1744
1746
1747 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1748 IndexMaskMemory memory;
1750 *object, info.drawing, info.layer_index, memory);
1751 if (strokes.is_empty()) {
1752 return;
1753 }
1755
1756 VArray<int> vcuts = {};
1757
1758 if (selection_domain == bke::AttrDomain::Curve || !only_selected) {
1759 /* Subdivide entire selected curve, every stroke subdivides to the same cut. */
1760 vcuts = VArray<int>::from_single(cuts, curves.points_num());
1761 }
1762 else if (selection_domain == bke::AttrDomain::Point) {
1763 /* Subdivide between selected points. Only cut between selected points.
1764 * Make the cut array the same length as point count for specifying
1765 * cut/uncut for each segment. */
1766 const VArray<bool> selection = *curves.attributes().lookup_or_default<bool>(
1767 ".selection", bke::AttrDomain::Point, true);
1768 const VArray<bool> selection_left = *curves.attributes().lookup_or_default<bool>(
1769 ".selection_handle_left", bke::AttrDomain::Point, true);
1770 const VArray<bool> selection_right = *curves.attributes().lookup_or_default<bool>(
1771 ".selection_handle_right", bke::AttrDomain::Point, true);
1772 const VArray<int8_t> curve_types = curves.curve_types();
1773
1774 auto is_selected = [&](const int point_i, const int curve_i) {
1775 if (selection[point_i]) {
1776 return true;
1777 }
1778 if (handle_display == CURVE_HANDLE_NONE) {
1779 return false;
1780 }
1781 if (curve_types[curve_i] == CURVE_TYPE_BEZIER) {
1782 return selection_left[point_i] || selection_right[point_i];
1783 }
1784 return false;
1785 };
1786
1787 const OffsetIndices points_by_curve = curves.points_by_curve();
1788 const VArray<bool> cyclic = curves.cyclic();
1789
1790 Array<int> use_cuts(curves.points_num(), 0);
1791
1792 /* The cut is after each point, so the last point selected wouldn't need to be registered. */
1793 for (const int curve : curves.curves_range()) {
1794 /* No need to loop to the last point since the cut is registered on the point before the
1795 * segment. */
1796 for (const int point : points_by_curve[curve].drop_back(1)) {
1797 /* The point itself should be selected. */
1798 if (!is_selected(point, curve)) {
1799 continue;
1800 }
1801 /* If the next point in the curve is selected, then cut this segment. */
1802 if (is_selected(point + 1, curve)) {
1803 use_cuts[point] = cuts;
1804 }
1805 }
1806 /* Check for cyclic and selection. */
1807 if (cyclic[curve]) {
1808 const int first_point = points_by_curve[curve].first();
1809 const int last_point = points_by_curve[curve].last();
1810 if (is_selected(first_point, curve) && is_selected(last_point, curve)) {
1811 use_cuts[last_point] = cuts;
1812 }
1813 }
1814 }
1815 vcuts = VArray<int>::from_container(std::move(use_cuts));
1816 }
1817
1818 curves = geometry::subdivide_curves(curves, strokes, vcuts);
1820 changed.store(true, std::memory_order_relaxed);
1821 });
1822
1823 if (changed) {
1826 }
1827
1828 return OPERATOR_FINISHED;
1829}
1830
1832{
1833 PropertyRNA *prop;
1834
1835 ot->name = "Subdivide Stroke";
1836 ot->idname = "GREASE_PENCIL_OT_stroke_subdivide";
1837 ot->description =
1838 "Subdivide between continuous selected points of the stroke adding a point half way "
1839 "between "
1840 "them";
1841
1844
1846
1847 prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 32, "Number of Cuts", "", 1, 5);
1848 /* Avoid re-using last var because it can cause _very_ high value and annoy users. */
1850
1852 "only_selected",
1853 true,
1854 "Selected Points",
1855 "Smooth only selected points in the stroke");
1856}
1857
1859
1860/* -------------------------------------------------------------------- */
1863
1866 TOP = 0,
1868 UP = 1,
1870 DOWN = 2,
1873};
1874
1876 const IndexMask &selected,
1877 const ReorderDirection direction)
1878{
1879 Array<int> indices(universe.size());
1880
1882 /* Initialize the indices. */
1884 }
1885
1887 /*
1888 * Take the selected indices and move them to the start for `Bottom` or the end for `Top`
1889 * And fill the reset with the unselected indices.
1890 *
1891 * Here's a diagram:
1892 *
1893 * Input
1894 * 0 1 2 3 4 5 6 7 8 9
1895 * ^ ^ ^
1896 *
1897 * Top
1898 * |-----A-----| |-B-|
1899 * 0 1 3 6 7 8 9 2 4 5
1900 * ^ ^ ^
1901 *
1902 * Bottom
1903 * |-A-| |-----B-----|
1904 * 2 4 5 0 1 3 6 7 8 9
1905 * ^ ^ ^
1906 */
1907
1908 IndexMaskMemory memory;
1909 const IndexMask unselected = selected.complement(universe, memory);
1910
1911 const IndexMask &A = (direction == ReorderDirection::BOTTOM) ? selected : unselected;
1912 const IndexMask &B = (direction == ReorderDirection::BOTTOM) ? unselected : selected;
1913
1914 A.to_indices(indices.as_mutable_span().take_front(A.size()));
1915 B.to_indices(indices.as_mutable_span().take_back(B.size()));
1916 }
1917 else if (direction == ReorderDirection::DOWN) {
1918 selected.foreach_index_optimized<int>([&](const int curve_i, const int pos) {
1919 /* Check if the curve index is touching the beginning without any gaps. */
1920 if (curve_i != pos) {
1921 /* Move a index down by flipping it with the one below it. */
1922 std::swap(indices[curve_i], indices[curve_i - 1]);
1923 }
1924 });
1925 }
1926 else if (direction == ReorderDirection::UP) {
1927 Array<int> selected_indices(selected.size());
1928 selected.to_indices(selected_indices.as_mutable_span());
1929
1930 /* Because each index is moving up we need to loop through the indices backwards,
1931 * starting at the largest. */
1932 for (const int i : selected_indices.index_range()) {
1933 const int pos = selected_indices.index_range().last(i);
1934 const int curve_i = selected_indices[pos];
1935
1936 /* Check if the curve index is touching the end without any gaps. */
1937 if (curve_i != universe.last(i)) {
1938 /* Move a index up by flipping it with the one above it. */
1939 std::swap(indices[curve_i], indices[curve_i + 1]);
1940 }
1941 }
1942 }
1943
1944 return indices;
1945}
1946
1948{
1949 const Scene *scene = CTX_data_scene(C);
1950 Object *object = CTX_data_active_object(C);
1951 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
1952
1953 const ReorderDirection direction = ReorderDirection(RNA_enum_get(op->ptr, "direction"));
1954
1955 std::atomic<bool> changed = false;
1957 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
1958 IndexMaskMemory memory;
1960 *object, info.drawing, info.layer_index, memory);
1961 if (strokes.is_empty()) {
1962 return;
1963 }
1965
1966 /* Return if everything is selected. */
1967 if (strokes.size() == curves.curves_num()) {
1968 return;
1969 }
1970
1971 const Array<int> indices = get_reordered_indices(curves.curves_range(), strokes, direction);
1972
1975 changed.store(true, std::memory_order_relaxed);
1976 });
1977
1978 if (changed) {
1981 }
1982
1983 return OPERATOR_FINISHED;
1984}
1985
1987{
1988 static const EnumPropertyItem prop_reorder_direction[] = {
1989 {int(ReorderDirection::TOP), "TOP", 0, "Bring to Front", ""},
1990 {int(ReorderDirection::UP), "UP", 0, "Bring Forward", ""},
1992 {int(ReorderDirection::DOWN), "DOWN", 0, "Send Backward", ""},
1993 {int(ReorderDirection::BOTTOM), "BOTTOM", 0, "Send to Back", ""},
1994 {0, nullptr, 0, nullptr, nullptr},
1995 };
1996
1997 ot->name = "Reorder";
1998 ot->idname = "GREASE_PENCIL_OT_reorder";
1999 ot->description = "Change the display order of the selected strokes";
2000
2003
2005
2006 ot->prop = RNA_def_enum(
2007 ot->srna, "direction", prop_reorder_direction, int(ReorderDirection::TOP), "Direction", "");
2008}
2009
2011
2012/* -------------------------------------------------------------------- */
2015
2017{
2018 using namespace bke::greasepencil;
2019 const Scene *scene = CTX_data_scene(C);
2020 bool changed = false;
2021
2022 Object *object = CTX_data_active_object(C);
2023 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
2024
2025 std::string target_layer_name = RNA_string_get(op->ptr, "target_layer_name");
2026 std::string target_group_name = RNA_string_get(op->ptr, "target_group_name");
2027 const bool add_new_layer = RNA_boolean_get(op->ptr, "add_new_layer");
2028 TreeNode *target_node = nullptr;
2029
2030 if (add_new_layer) {
2031 target_node = &grease_pencil.add_layer(target_layer_name).as_node();
2032
2033 if (!target_group_name.empty()) {
2034 TreeNode *group_node = grease_pencil.find_node_by_name(target_group_name);
2035
2036 if (group_node && group_node->is_group()) {
2037 grease_pencil.move_node_into(*target_node, group_node->as_group());
2038 }
2039 }
2040 }
2041 else {
2042 target_node = grease_pencil.find_node_by_name(target_layer_name);
2043 }
2044
2045 if (target_node == nullptr || !target_node->is_layer()) {
2046 BKE_reportf(op->reports, RPT_ERROR, "There is no layer '%s'", target_layer_name.c_str());
2047 return OPERATOR_CANCELLED;
2048 }
2049
2050 Layer &layer_dst = target_node->as_layer();
2051 if (layer_dst.is_locked()) {
2052 BKE_reportf(op->reports, RPT_ERROR, "'%s' Layer is locked", target_layer_name.c_str());
2053 return OPERATOR_CANCELLED;
2054 }
2055
2056 /* Iterate through all the drawings at current scene frame. */
2057 const Vector<MutableDrawingInfo> drawings_src = retrieve_editable_drawings(*scene,
2059 for (const MutableDrawingInfo &info : drawings_src) {
2060 bke::CurvesGeometry &curves_src = info.drawing.strokes_for_write();
2061 IndexMaskMemory memory;
2062 const IndexMask selected_strokes = ed::curves::retrieve_selected_curves(curves_src, memory);
2063 if (selected_strokes.is_empty()) {
2064 continue;
2065 }
2066
2067 bool is_key_inserted = false;
2068 bool has_active_key = false;
2069 if (layer_dst.frames().is_empty()) {
2070 /* If the target layer doesn't have any keyframes, insert a new key at the current frame. */
2071 grease_pencil.insert_frame(layer_dst, scene->r.cfra);
2072 is_key_inserted = true;
2073 has_active_key = true;
2074 }
2075 else {
2076 has_active_key = ensure_active_keyframe(
2077 *scene, grease_pencil, layer_dst, false, is_key_inserted);
2078 }
2079
2080 if (has_active_key && is_key_inserted) {
2081 /* If we inserted new key frame on target layer, then it means target layer/drawing was newly
2082 * created, so we move strokes to that drawing instead, otherwise insert to their original
2083 * frames. */
2084 const int frame_number_dst = is_key_inserted ? scene->r.cfra : info.frame_number;
2085 /* Move geometry to a new drawing in target layer. */
2086 Drawing &drawing_dst = *grease_pencil.get_drawing_at(layer_dst, frame_number_dst);
2088 curves_src, selected_strokes, {});
2089
2090 curves_src.remove_curves(selected_strokes, {});
2091 drawing_dst.tag_topology_changed();
2092 }
2093 else if (Drawing *drawing_dst = grease_pencil.get_drawing_at(layer_dst, info.frame_number)) {
2094 /* Append geometry to drawing in target layer. */
2096 curves_src, selected_strokes, {});
2097 Curves *selected_curves = bke::curves_new_nomain(std::move(selected_elems));
2098 Curves *layer_curves = bke::curves_new_nomain(std::move(drawing_dst->strokes_for_write()));
2099 std::array<bke::GeometrySet, 2> geometry_sets{
2100 bke::GeometrySet::from_curves(layer_curves),
2101 bke::GeometrySet::from_curves(selected_curves)};
2102 bke::GeometrySet joined = geometry::join_geometries(geometry_sets, {});
2103 drawing_dst->strokes_for_write() = std::move(joined.get_curves_for_write()->geometry.wrap());
2104
2105 curves_src.remove_curves(selected_strokes, {});
2106
2107 drawing_dst->tag_topology_changed();
2108 }
2109
2110 info.drawing.tag_topology_changed();
2111 changed = true;
2112 }
2113
2114 if (changed) {
2115 /* updates */
2118 }
2119
2120 return OPERATOR_FINISHED;
2121}
2122
2124 wmOperator *op,
2125 const wmEvent *event)
2126{
2127 const bool add_new_layer = RNA_boolean_get(op->ptr, "add_new_layer");
2128 if (add_new_layer) {
2129 Object *object = CTX_data_active_object(C);
2130 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
2131
2132 const std::string unique_name = grease_pencil.unique_layer_name("Layer");
2133 RNA_string_set(op->ptr, "target_layer_name", unique_name.c_str());
2134
2136 C, op, event, IFACE_("Move to New Layer"), IFACE_("Create"));
2137 }
2138
2139 /* Show the move menu if this operator is invoked from operator search without any property
2140 * pre-set. */
2141 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "target_layer_name");
2142 if (!RNA_property_is_set(op->ptr, prop)) {
2143 WM_menu_name_call(C, "GREASE_PENCIL_MT_move_to_layer", wm::OpCallContext::InvokeDefault);
2144 return OPERATOR_FINISHED;
2145 }
2146
2148}
2149
2151{
2152 PropertyRNA *prop;
2153
2154 ot->name = "Move to Layer";
2155 ot->idname = "GREASE_PENCIL_OT_move_to_layer";
2156 ot->description = "Move selected strokes to another layer";
2157
2161
2163
2164 prop = RNA_def_string(
2165 ot->srna, "target_layer_name", nullptr, INT16_MAX, "Name", "Target Grease Pencil Layer");
2167 prop = RNA_def_string(ot->srna,
2168 "target_group_name",
2169 nullptr,
2170 INT16_MAX,
2171 "Target Group",
2172 "Group to add the new layer to");
2174 prop = RNA_def_boolean(
2175 ot->srna, "add_new_layer", false, "New Layer", "Move selection to a new layer");
2177}
2178
2180
2181/* -------------------------------------------------------------------- */
2184
2185enum class SeparateMode : int8_t {
2186 /* Selected Points/Strokes. */
2188 /* By Material. */
2189 MATERIAL = 1,
2190 /* By each Layer. */
2192};
2193
2195 {int(SeparateMode::SELECTED), "SELECTED", 0, "Selection", "Separate selected geometry"},
2196 {int(SeparateMode::MATERIAL), "MATERIAL", 0, "By Material", "Separate by material"},
2197 {int(SeparateMode::LAYER), "LAYER", 0, "By Layer", "Separate by layer"},
2198 {0, nullptr, 0, nullptr, nullptr},
2199};
2200
2202 Scene *scene,
2203 ViewLayer *view_layer,
2204 Base *base_prev,
2205 const GreasePencil &grease_pencil_src)
2206{
2207 const eDupli_ID_Flags dupflag = eDupli_ID_Flags((U.dupflag & USER_DUP_GPENCIL) |
2208 (U.dupflag & USER_DUP_ACT));
2209 Base *base_new = object::add_duplicate(bmain, scene, view_layer, base_prev, dupflag);
2210 Object *object_dst = base_new->object;
2211 object_dst->mode = OB_MODE_OBJECT;
2212 GreasePencil *grease_pencil_dst = BKE_grease_pencil_add(bmain, grease_pencil_src.id.name + 2);
2213 BKE_grease_pencil_copy_parameters(grease_pencil_src, *grease_pencil_dst);
2214 object_dst->data = id_cast<ID *>(grease_pencil_dst);
2215
2216 return object_dst;
2217}
2218
2220 const int layer_index,
2221 const GreasePencil &grease_pencil_src,
2222 GreasePencil &grease_pencil_dst,
2223 Vector<int> &src_to_dst_layer_indices)
2224{
2225 using namespace bke::greasepencil;
2226
2227 /* This assumes that the index is valid. Will cause an assert if it is not. */
2228 const Layer &layer_src = grease_pencil_src.layer(layer_index);
2229 if (TreeNode *node = grease_pencil_dst.find_node_by_name(layer_src.name())) {
2230 return node->as_layer();
2231 }
2232
2233 /* If the layer can't be found in `grease_pencil_dst` by name add a new layer. */
2234 Layer &new_layer = grease_pencil_dst.add_layer(layer_src.name());
2235 BKE_grease_pencil_copy_layer_parameters(layer_src, new_layer);
2236 src_to_dst_layer_indices.append(layer_index);
2237
2238 return new_layer;
2239}
2240
2242 Main &bmain,
2243 Scene &scene,
2244 ViewLayer &view_layer,
2245 Base &base_prev,
2246 Object &object_src)
2247{
2248 using namespace bke::greasepencil;
2249 bool changed = false;
2250
2251 GreasePencil &grease_pencil_src = *id_cast<GreasePencil *>(object_src.data);
2253 &bmain, &scene, &view_layer, &base_prev, grease_pencil_src);
2254 GreasePencil &grease_pencil_dst = *id_cast<GreasePencil *>(object_dst->data);
2255
2256 /* Iterate through all the drawings at current scene frame. */
2257 const Vector<MutableDrawingInfo> drawings_src = retrieve_editable_drawings(scene,
2258 grease_pencil_src);
2259 Vector<int> src_to_dst_layer_indices;
2260 for (const MutableDrawingInfo &info : drawings_src) {
2261 bke::CurvesGeometry &curves_src = info.drawing.strokes_for_write();
2262 IndexMaskMemory memory;
2263 const IndexMask selected_points = ed::curves::retrieve_selected_points(curves_src, memory);
2264 if (selected_points.is_empty()) {
2265 continue;
2266 }
2267
2268 /* Insert Keyframe at current frame/layer. */
2270 info.layer_index, grease_pencil_src, grease_pencil_dst, src_to_dst_layer_indices);
2271
2272 Drawing *drawing_dst = grease_pencil_dst.insert_frame(layer_dst, info.frame_number);
2273 BLI_assert(drawing_dst != nullptr);
2274
2275 /* Copy strokes to new CurvesGeometry. */
2277 curves_src, selected_points, {});
2278 curves_src = geometry::grease_pencil_remove_points_and_split(curves_src, selected_points);
2279
2280 info.drawing.tag_topology_changed();
2281 drawing_dst->tag_topology_changed();
2282
2283 changed = true;
2284 }
2285
2286 if (changed) {
2287 /* Transfer layer attributes. */
2288 bke::gather_attributes(grease_pencil_src.attributes(),
2291 {},
2292 src_to_dst_layer_indices.as_span(),
2293 grease_pencil_dst.attributes_for_write());
2294
2295 /* Set the active layer in the target object. */
2296 if (grease_pencil_src.has_active_layer()) {
2297 const Layer &active_layer_src = *grease_pencil_src.get_active_layer();
2298 TreeNode *active_layer_dst = grease_pencil_dst.find_node_by_name(active_layer_src.name());
2299 if (active_layer_dst && active_layer_dst->is_layer()) {
2300 grease_pencil_dst.set_active_layer(&active_layer_dst->as_layer());
2301 }
2302 }
2303
2304 /* Add object materials to target object. */
2306 object_dst,
2307 BKE_object_material_array_p(&object_src),
2308 *BKE_object_material_len_p(&object_src),
2309 false);
2310
2311 BKE_object_material_remove_unused(&bmain, object_dst);
2312 DEG_id_tag_update(&grease_pencil_dst.id, ID_RECALC_GEOMETRY);
2313 WM_event_add_notifier(&C, NC_OBJECT | ND_DRAW, &grease_pencil_dst);
2314 }
2315 return changed;
2316}
2317
2319 Main &bmain,
2320 Scene &scene,
2321 ViewLayer &view_layer,
2322 Base &base_prev,
2323 Object &object_src)
2324{
2325 using namespace bke::greasepencil;
2326 bool changed = false;
2327
2328 GreasePencil &grease_pencil_src = *id_cast<GreasePencil *>(object_src.data);
2329
2330 /* Create a new object for each layer. */
2331 for (const int layer_i : grease_pencil_src.layers().index_range()) {
2332 Layer &layer_src = grease_pencil_src.layer(layer_i);
2333 if (layer_src.is_locked()) {
2334 continue;
2335 }
2336
2338 &bmain, &scene, &view_layer, &base_prev, grease_pencil_src);
2339 GreasePencil &grease_pencil_dst = *id_cast<GreasePencil *>(object_dst->data);
2340 Vector<int> src_to_dst_layer_indices;
2342 layer_i, grease_pencil_src, grease_pencil_dst, src_to_dst_layer_indices);
2343
2344 /* Iterate through all the drawings at current frame. */
2346 scene, grease_pencil_src, layer_src);
2347 for (const MutableDrawingInfo &info : drawings_src) {
2348 bke::CurvesGeometry &curves_src = info.drawing.strokes_for_write();
2349 IndexMaskMemory memory;
2350 const IndexMask strokes = retrieve_editable_strokes(
2351 object_src, info.drawing, info.layer_index, memory);
2352 if (strokes.is_empty()) {
2353 continue;
2354 }
2355
2356 /* Add object materials. */
2358 object_dst,
2359 BKE_object_material_array_p(&object_src),
2360 *BKE_object_material_len_p(&object_src),
2361 false);
2362
2363 /* Insert Keyframe at current frame/layer. */
2364 Drawing *drawing_dst = grease_pencil_dst.insert_frame(layer_dst, info.frame_number);
2365 /* TODO: Can we assume the insert never fails? */
2366 BLI_assert(drawing_dst != nullptr);
2367
2368 /* Copy strokes to new CurvesGeometry. */
2370 info.drawing.strokes(), strokes, {});
2371 curves_src.remove_curves(strokes, {});
2372
2373 info.drawing.tag_topology_changed();
2374 drawing_dst->tag_topology_changed();
2375
2376 changed = true;
2377 }
2378
2379 /* Transfer layer attributes. */
2380 bke::gather_attributes(grease_pencil_src.attributes(),
2383 {},
2384 src_to_dst_layer_indices.as_span(),
2385 grease_pencil_dst.attributes_for_write());
2386
2387 BKE_object_material_remove_unused(&bmain, object_dst);
2388
2389 DEG_id_tag_update(&grease_pencil_dst.id, ID_RECALC_GEOMETRY);
2390 WM_event_add_notifier(&C, NC_OBJECT | ND_DRAW, &grease_pencil_dst);
2391 }
2392
2393 return changed;
2394}
2395
2397 Main &bmain,
2398 Scene &scene,
2399 ViewLayer &view_layer,
2400 Base &base_prev,
2401 Object &object_src)
2402{
2403 using namespace blender::bke;
2404 using namespace bke::greasepencil;
2405 bool changed = false;
2406
2407 GreasePencil &grease_pencil_src = *id_cast<GreasePencil *>(object_src.data);
2408
2409 /* Create a new object for each material. */
2410 for (const int mat_i : IndexRange(object_src.totcol).drop_front(1)) {
2411 if (!BKE_object_material_slot_used(&object_src, mat_i + 1)) {
2412 continue;
2413 }
2414
2416 &bmain, &scene, &view_layer, &base_prev, grease_pencil_src);
2417 GreasePencil &grease_pencil_dst = *id_cast<GreasePencil *>(object_dst->data);
2418
2419 /* Add object materials. */
2421 object_dst,
2422 BKE_object_material_array_p(&object_src),
2423 *BKE_object_material_len_p(&object_src),
2424 false);
2425
2426 /* Iterate through all the drawings at current scene frame. */
2427 const Vector<MutableDrawingInfo> drawings_src = retrieve_editable_drawings(scene,
2428 grease_pencil_src);
2429 Vector<int> src_to_dst_layer_indices;
2430 for (const MutableDrawingInfo &info : drawings_src) {
2431 bke::CurvesGeometry &curves_src = info.drawing.strokes_for_write();
2432 IndexMaskMemory memory;
2434 object_src, info.drawing, mat_i, memory);
2435 if (strokes.is_empty()) {
2436 continue;
2437 }
2438
2439 /* Insert Keyframe at current frame/layer. */
2441 info.layer_index, grease_pencil_src, grease_pencil_dst, src_to_dst_layer_indices);
2442
2443 Drawing *drawing_dst = grease_pencil_dst.insert_frame(layer_dst, info.frame_number);
2444 /* TODO: Can we assume the insert never fails? */
2445 BLI_assert(drawing_dst != nullptr);
2446
2447 /* Copy strokes to new CurvesGeometry. */
2448 drawing_dst->strokes_for_write() = bke::curves_copy_curve_selection(curves_src, strokes, {});
2449 curves_src.remove_curves(strokes, {});
2450
2451 info.drawing.tag_topology_changed();
2452 drawing_dst->tag_topology_changed();
2453
2454 changed = true;
2455 }
2456
2457 /* Transfer layer attributes. */
2458 bke::gather_attributes(grease_pencil_src.attributes(),
2461 {},
2462 src_to_dst_layer_indices.as_span(),
2463 grease_pencil_dst.attributes_for_write());
2464
2465 BKE_object_material_remove_unused(&bmain, object_dst);
2466
2467 DEG_id_tag_update(&grease_pencil_dst.id, ID_RECALC_GEOMETRY);
2468 WM_event_add_notifier(&C, NC_OBJECT | ND_DRAW, &grease_pencil_dst);
2469 }
2470
2471 if (changed) {
2472 BKE_object_material_remove_unused(&bmain, &object_src);
2473 }
2474
2475 return changed;
2476}
2477
2479{
2480 using namespace bke::greasepencil;
2481 Main *bmain = CTX_data_main(C);
2482 Scene *scene = CTX_data_scene(C);
2483 ViewLayer *view_layer = CTX_data_view_layer(C);
2484 Base *base_prev = CTX_data_active_base(C);
2485 Object *object_src = CTX_data_active_object(C);
2486 GreasePencil &grease_pencil_src = *id_cast<GreasePencil *>(object_src->data);
2487
2488 const SeparateMode mode = SeparateMode(RNA_enum_get(op->ptr, "mode"));
2489 bool changed = false;
2490
2491 WM_cursor_wait(true);
2492
2493 switch (mode) {
2495 /* Cancel if nothing selected. */
2497 grease_pencil_src);
2498 const bool has_selection = std::any_of(
2499 drawings.begin(), drawings.end(), [&](const MutableDrawingInfo &info) {
2500 return ed::curves::has_anything_selected(info.drawing.strokes());
2501 });
2502 if (!has_selection) {
2503 BKE_report(op->reports, RPT_ERROR, "Nothing selected");
2504 WM_cursor_wait(false);
2505 return OPERATOR_CANCELLED;
2506 }
2507
2509 *C, *bmain, *scene, *view_layer, *base_prev, *object_src);
2510 break;
2511 }
2513 /* Cancel if the object only has one material. */
2514 if (object_src->totcol == 1) {
2515 BKE_report(op->reports, RPT_ERROR, "The object has only one material");
2516 WM_cursor_wait(false);
2517 return OPERATOR_CANCELLED;
2518 }
2519
2521 *C, *bmain, *scene, *view_layer, *base_prev, *object_src);
2522 break;
2523 }
2524 case SeparateMode::LAYER: {
2525 /* Cancel if the object only has one layer. */
2526 if (grease_pencil_src.layers().size() == 1) {
2527 BKE_report(op->reports, RPT_ERROR, "The object has only one layer");
2528 WM_cursor_wait(false);
2529 return OPERATOR_CANCELLED;
2530 }
2532 *C, *bmain, *scene, *view_layer, *base_prev, *object_src);
2533 break;
2534 }
2535 }
2536
2537 WM_cursor_wait(false);
2538
2539 if (changed) {
2540 DEG_id_tag_update(&grease_pencil_src.id, ID_RECALC_GEOMETRY);
2541 WM_event_add_notifier(C, NC_GEOM | ND_DATA | NA_EDITED, &grease_pencil_src);
2542 }
2543
2544 return OPERATOR_FINISHED;
2545}
2546
2548{
2549 ot->name = "Separate";
2550 ot->idname = "GREASE_PENCIL_OT_separate";
2551 ot->description = "Separate the selected geometry into a new Grease Pencil object";
2552
2553 ot->invoke = WM_menu_invoke;
2556
2558
2559 ot->prop = RNA_def_enum(
2560 ot->srna, "mode", prop_separate_modes, int(SeparateMode::SELECTED), "Mode", "");
2561}
2562
2564
2565/* -------------------------------------------------------------------- */
2568
2569/* Global clipboard for Grease Pencil curves. */
2570static struct Clipboard {
2572 /* Name of the layer. */
2573 std::string name;
2574 /* Curves for this layer. */
2576 };
2578 /* Object transform of stored curves. */
2580 /* We store the material uid's of the copied curves, so we can match those when pasting the
2581 * clipboard into another object. */
2584 ListBaseT<bDeformGroup> defnames;
2585} *grease_pencil_clipboard = nullptr;
2586
2589
2591{
2592 std::scoped_lock lock(grease_pencil_clipboard_lock);
2593
2594 if (grease_pencil_clipboard == nullptr) {
2595 grease_pencil_clipboard = MEM_new<Clipboard>(__func__);
2596 }
2598}
2599
2601{
2602 std::scoped_lock lock(grease_pencil_clipboard_lock);
2603
2605 MEM_delete(grease_pencil_clipboard);
2606 grease_pencil_clipboard = nullptr;
2607 }
2608}
2609
2611{
2612 using namespace blender::ed::greasepencil;
2613
2614 /* Get a list of all materials in the scene. */
2615 Map<uint, Material *> scene_materials;
2616 for (Material &material : bmain.materials) {
2617 scene_materials.add(material.id.session_uid, &material);
2618 }
2619
2620 const Clipboard &clipboard = ensure_grease_pencil_clipboard();
2621 Array<int> clipboard_material_remap(clipboard.materials_in_source_num, 0);
2622 for (const int i : clipboard.materials.index_range()) {
2623 /* Check if the material name exists in the scene. */
2624 int target_index;
2625 uint material_id = clipboard.materials[i].first;
2626 Material *material = scene_materials.lookup_default(material_id, nullptr);
2627 if (!material) {
2628 /* Material is removed, so create a new material. */
2629 BKE_grease_pencil_object_material_new(&bmain, &object, nullptr, &target_index);
2630 clipboard_material_remap[clipboard.materials[i].second] = target_index;
2631 continue;
2632 }
2633
2634 /* Find or add the material to the target object. */
2635 target_index = BKE_object_material_ensure(&bmain, &object, material);
2636 clipboard_material_remap[clipboard.materials[i].second] = target_index;
2637 }
2638
2639 return clipboard_material_remap;
2640}
2641
2643 const VArray<float4x4> &transforms)
2644{
2645 BLI_assert(geometries.size() == transforms.size());
2646
2647 auto instances = std::make_unique<bke::Instances>(geometries.size());
2648 transforms.materialize(instances->transforms_for_write());
2649 MutableSpan<int> handles = instances->reference_handles_for_write();
2650 for (const int i : geometries.index_range()) {
2651 handles[i] = instances->add_new_reference(bke::InstanceReference{geometries[i]});
2652 }
2653
2655 options.keep_original_ids = true;
2656 options.realize_instance_attributes = false;
2657 return realize_instances(bke::GeometrySet::from_instances(std::move(instances)), options)
2658 .geometry;
2659}
2666
2668{
2670
2671 const Scene *scene = CTX_data_scene(C);
2672 Object *object = CTX_data_active_object(C);
2673 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
2675 scene->toolsettings);
2676
2678
2679 int num_elements_copied = 0;
2680 Map<const Layer *, Vector<bke::GeometrySet>> copied_curves_per_layer;
2681
2682 /* Collect all selected strokes/points on all editable layers. */
2684 for (const MutableDrawingInfo &drawing_info : drawings) {
2685 const bke::CurvesGeometry &curves = drawing_info.drawing.strokes();
2686 const Layer &layer = grease_pencil.layer(drawing_info.layer_index);
2687
2688 if (curves.is_empty()) {
2689 continue;
2690 }
2692 continue;
2693 }
2694
2695 /* Get a copy of the selected geometry on this layer. */
2696 IndexMaskMemory memory;
2697 bke::CurvesGeometry copied_curves;
2698
2699 if (selection_domain == bke::AttrDomain::Curve) {
2700 const IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory);
2701 copied_curves = curves_copy_curve_selection(curves, selected_curves, {});
2702 num_elements_copied += copied_curves.curves_num();
2703 }
2704 else if (selection_domain == bke::AttrDomain::Point) {
2705 const IndexMask selected_points = ed::curves::retrieve_selected_points(curves, memory);
2707 curves, selected_points.complement(curves.points_range(), memory));
2708 num_elements_copied += copied_curves.points_num();
2709 }
2710
2711 /* Add the layer selection to the set of copied curves. */
2712 copied_curves_per_layer.lookup_or_add_default(&layer).append(
2713 bke::GeometrySet::from_curves(curves_new_nomain(std::move(copied_curves))));
2714 }
2715
2716 if (copied_curves_per_layer.is_empty()) {
2717 clipboard.layers.reinitialize(0);
2718 return OPERATOR_CANCELLED;
2719 }
2720
2721 clipboard.layers.reinitialize(copied_curves_per_layer.size());
2722
2723 int i = 0;
2724 for (auto const &[layer, geometries] : copied_curves_per_layer.items()) {
2725 const float4x4 layer_to_object = layer->to_object_space(*object);
2726 Clipboard::ClipboardLayer &cliplayer = clipboard.layers[i];
2727
2728 bke::GeometrySet joined_copied_curves = join_geometries_with_transform(geometries.as_span(),
2729 layer_to_object);
2730 cliplayer.curves = joined_copied_curves.get_curves()->geometry.wrap();
2731 cliplayer.name = layer->name();
2732 i++;
2733 }
2734 clipboard.object_to_world = object->object_to_world();
2735
2736 /* Store the session uid of the materials used by the curves in the clipboard. We use the uid to
2737 * remap the material indices when pasting. */
2738 clipboard.materials.clear();
2739 clipboard.materials_in_source_num = grease_pencil.material_array_num;
2740
2741 const auto is_material_index_used = [&](const int material_index) -> bool {
2742 for (const Clipboard::ClipboardLayer &layer : clipboard.layers) {
2743 const bke::AttributeAccessor attributes = layer.curves.attributes();
2744 const VArraySpan<int> material_indices = *attributes.lookup_or_default<int>(
2745 "material_index", bke::AttrDomain::Curve, 0);
2746 if (material_indices.contains(material_index)) {
2747 return true;
2748 }
2749 }
2750 return false;
2751 };
2752
2753 for (const int material_index : IndexRange(grease_pencil.material_array_num)) {
2754 if (!is_material_index_used(material_index)) {
2755 continue;
2756 }
2757 const Material *material = BKE_object_material_get(object, material_index + 1);
2758 clipboard.materials.append({material ? material->id.session_uid : 0, material_index});
2759 }
2760
2762
2763 /* Report the numbers. */
2764 if (selection_domain == bke::AttrDomain::Curve) {
2765 BKE_reportf(op->reports, RPT_INFO, "Copied %d selected curve(s)", num_elements_copied);
2766 }
2767 else if (selection_domain == bke::AttrDomain::Point) {
2768 BKE_reportf(op->reports, RPT_INFO, "Copied %d selected point(s)", num_elements_copied);
2769 }
2770
2771 return OPERATOR_FINISHED;
2772}
2773
2775{
2776 ot->name = "Copy Strokes";
2777 ot->idname = "GREASE_PENCIL_OT_copy";
2778 ot->description = "Copy the selected Grease Pencil points or strokes to the internal clipboard";
2779
2782
2783 ot->flag = OPTYPE_REGISTER;
2784}
2785
2787 Object &object,
2788 const bke::CurvesGeometry &curves_to_paste,
2789 const float4x4 &object_to_paste_layer,
2790 const float4x4 &clipboard_to_world,
2791 const bool keep_world_transform,
2792 const bool paste_back,
2794{
2795 /* Get a list of all materials in the scene. */
2796 const Array<int> clipboard_material_remap = ed::greasepencil::clipboard_materials_remap(bmain,
2797 object);
2798
2799 /* Get the index range of the pasted curves in the target layer. */
2800 const IndexRange pasted_curves_range = paste_back ? IndexRange(0, curves_to_paste.curves_num()) :
2801 IndexRange(drawing.strokes().curves_num(),
2802 curves_to_paste.curves_num());
2803
2804 /* Append the geometry from the clipboard to the target layer. */
2805 Curves *clipboard_id = bke::curves_new_nomain(curves_to_paste);
2806 Curves *target_id = curves_new_nomain(std::move(drawing.strokes_for_write()));
2807
2808 const Array<bke::GeometrySet> geometry_sets = {
2809 bke::GeometrySet::from_curves(paste_back ? clipboard_id : target_id),
2810 bke::GeometrySet::from_curves(paste_back ? target_id : clipboard_id)};
2811
2812 const float4x4 transform = object_to_paste_layer *
2813 (keep_world_transform ?
2814 object.world_to_object() * clipboard_to_world :
2816 const Array<float4x4> transforms = paste_back ? Span<float4x4>{transform, float4x4::identity()} :
2819 geometry_sets, VArray<float4x4>::from_container(transforms));
2820
2821 drawing.strokes_for_write() = std::move(joined_curves.get_curves_for_write()->geometry.wrap());
2822
2823 if (!clipboard_material_remap.is_empty()) {
2824 /* Remap the material indices of the pasted curves to the target object material indices. */
2826 bke::SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
2827 "material_index", bke::AttrDomain::Curve);
2828 if (material_indices) {
2829 for (const int i : pasted_curves_range) {
2830 material_indices.span[i] = clipboard_material_remap[material_indices.span[i]];
2831 }
2832 material_indices.finish();
2833 }
2834 }
2835
2836 drawing.tag_topology_changed();
2837
2838 return pasted_curves_range;
2839}
2840
2841enum class PasteType {
2844};
2845
2847{
2848 using namespace bke::greasepencil;
2849 Main *bmain = CTX_data_main(C);
2850 const Scene &scene = *CTX_data_scene(C);
2851 Object *object = CTX_data_active_object(C);
2853 scene.toolsettings);
2854 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
2855
2856 const PasteType type = PasteType(RNA_enum_get(op->ptr, "type"));
2857
2858 const bool keep_world_transform = RNA_boolean_get(op->ptr, "keep_world_transform");
2859 const bool paste_on_back = RNA_boolean_get(op->ptr, "paste_back");
2860
2862 if (clipboard.layers.is_empty()) {
2863 return OPERATOR_CANCELLED;
2864 }
2865
2866 /* Make sure everything on the clipboard is selected, in the correct selection domain. */
2868 bke::GSpanAttributeWriter selection = ed::curves::ensure_selection_attribute(
2869 layer.curves, selection_domain, bke::AttrType::Bool);
2870 selection.finish();
2871 });
2872
2873 if (type == PasteType::Active) {
2874 Layer *active_layer = grease_pencil.get_active_layer();
2875 if (!active_layer) {
2876 BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer to paste into");
2877 return OPERATOR_CANCELLED;
2878 }
2879 if (!active_layer->is_editable()) {
2880 BKE_report(op->reports, RPT_ERROR, "Active layer is not editable");
2881 return OPERATOR_CANCELLED;
2882 }
2883
2884 /* Deselect everything from editable drawings. The pasted strokes are the only ones then after
2885 * the paste. That's convenient for the user. */
2887 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
2889 info.drawing.strokes_for_write(), selection_domain, bke::AttrType::Bool);
2890 ed::curves::fill_selection_false(selection_in_target.span);
2891 selection_in_target.finish();
2892 });
2893
2894 const float4x4 object_to_layer = math::invert(active_layer->to_object_space(*object));
2895
2896 /* Ensure active keyframe. */
2897 bool inserted_keyframe = false;
2898 if (!ensure_active_keyframe(scene, grease_pencil, *active_layer, false, inserted_keyframe)) {
2899 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
2900 return OPERATOR_CANCELLED;
2901 }
2902
2903 Vector<MutableDrawingInfo> drawing_infos =
2905 scene, grease_pencil, *active_layer);
2906 for (const MutableDrawingInfo info : drawing_infos) {
2908 *bmain, *object, object_to_layer, keep_world_transform, paste_on_back, info.drawing);
2909 }
2910
2911 if (inserted_keyframe) {
2913 }
2914 }
2915 else if (type == PasteType::ByLayer) {
2916 Layer *active_layer = grease_pencil.get_active_layer();
2917 /* Find layers to paste strokes into. */
2918 Array<Layer *> layers_to_paste_into(clipboard.layers.size());
2919 for (const int clip_layer_i : clipboard.layers.index_range()) {
2920 const Clipboard::ClipboardLayer &layer = clipboard.layers[clip_layer_i];
2921 bke::greasepencil::TreeNode *node = grease_pencil.find_node_by_name(layer.name);
2922 const bool found_layer = node && node->is_layer() && node->as_layer().is_editable();
2923 if (found_layer) {
2924 layers_to_paste_into[clip_layer_i] = &node->as_layer();
2925 continue;
2926 }
2927 if (active_layer && active_layer->is_editable()) {
2928 /* Fall back to active layer. */
2929 BKE_report(
2930 op->reports, RPT_WARNING, "Couldn't find matching layer, pasting into active layer");
2931 layers_to_paste_into[clip_layer_i] = active_layer;
2932 continue;
2933 }
2934
2935 if (!active_layer) {
2936 BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer to paste into");
2937 }
2938 else if (!active_layer->is_editable()) {
2939 BKE_report(op->reports, RPT_ERROR, "Active layer is not editable");
2940 }
2941 return OPERATOR_CANCELLED;
2942 }
2943
2944 /* Deselect everything from editable drawings. The pasted strokes are the only ones then after
2945 * the paste. That's convenient for the user. */
2947 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
2949 info.drawing.strokes_for_write(), selection_domain, bke::AttrType::Bool);
2950 ed::curves::fill_selection_false(selection_in_target.span);
2951 selection_in_target.finish();
2952 });
2953
2954 for (const int clip_layer_i : clipboard.layers.index_range()) {
2955 const Clipboard::ClipboardLayer &clip_layer = clipboard.layers[clip_layer_i];
2956 const bke::CurvesGeometry &curves_to_paste = clip_layer.curves;
2957
2958 BLI_assert(layers_to_paste_into[clip_layer_i] != nullptr);
2959 Layer &paste_layer = *layers_to_paste_into[clip_layer_i];
2960 const float4x4 object_to_paste_layer = math::invert(paste_layer.to_object_space(*object));
2961
2962 /* Ensure active keyframe. */
2963 bool inserted_keyframe = false;
2964 if (!ensure_active_keyframe(scene, grease_pencil, paste_layer, false, inserted_keyframe)) {
2965 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
2966 return OPERATOR_CANCELLED;
2967 }
2968
2969 Vector<MutableDrawingInfo> drawing_infos =
2971 scene, grease_pencil, paste_layer);
2972 for (const MutableDrawingInfo info : drawing_infos) {
2974 *object,
2975 curves_to_paste,
2976 object_to_paste_layer,
2977 clipboard.object_to_world,
2978 keep_world_transform,
2979 paste_on_back,
2980 info.drawing);
2981 }
2982
2983 if (inserted_keyframe) {
2985 }
2986 }
2987 }
2988 else {
2990 }
2991
2992 if (!clipboard.defnames.is_empty()) {
2993 ListBaseT<bDeformGroup> *deflist = BKE_id_defgroup_list_get_mutable(object->data);
2994 for (const bDeformGroup &defgroup : clipboard.defnames) {
2995 if (BKE_defgroup_name_index(deflist, defgroup.name) != -1) {
2996 continue;
2997 }
2998 bDeformGroup *new_group = BKE_defgroup_duplicate(&defgroup);
2999 BLI_addtail(deflist, new_group);
3000 }
3001 clipboard.defnames.free_no_destruct();
3002 }
3003
3006
3007 return OPERATOR_FINISHED;
3008}
3009
3011{
3013 return false;
3014 }
3015
3016 std::scoped_lock lock(grease_pencil_clipboard_lock);
3017 /* Check for curves in the Grease Pencil clipboard. */
3019}
3020
3022{
3023 PropertyRNA *prop;
3024
3025 static const EnumPropertyItem rna_paste_items[] = {
3026 {int(PasteType::Active), "ACTIVE", 0, "Paste to Active", ""},
3027 {int(PasteType::ByLayer), "LAYER", 0, "Paste by Layer", ""},
3028 {0, nullptr, 0, nullptr, nullptr},
3029 };
3030
3031 ot->name = "Paste Strokes";
3032 ot->idname = "GREASE_PENCIL_OT_paste";
3033 ot->description =
3034 "Paste Grease Pencil points or strokes from the internal clipboard to the active layer";
3035
3038
3040
3041 ot->prop = RNA_def_enum(ot->srna, "type", rna_paste_items, int(PasteType::Active), "Type", "");
3042
3043 prop = RNA_def_boolean(
3044 ot->srna, "paste_back", false, "Paste on Back", "Add pasted strokes behind all strokes");
3046
3047 prop = RNA_def_boolean(ot->srna,
3048 "keep_world_transform",
3049 false,
3050 "Keep World Transform",
3051 "Keep the world transform of strokes from the clipboard unchanged");
3053}
3054
3056
3058 Object &object,
3059 const float4x4 &object_to_paste_layer,
3060 const bool keep_world_transform,
3061 const bool paste_back,
3063{
3065 if (clipboard.layers.is_empty()) {
3066 return {};
3067 }
3068
3069 Vector<bke::GeometrySet> geometries_to_join;
3070 for (Clipboard::ClipboardLayer &layer : clipboard.layers) {
3071 geometries_to_join.append(bke::GeometrySet::from_curves(curves_new_nomain(layer.curves)));
3072 }
3073 bke::GeometrySet joined_clipboard_set = geometry::join_geometries(geometries_to_join.as_span(),
3074 {});
3075 BLI_assert(joined_clipboard_set.has_curves());
3076 const bke::CurvesGeometry &joined_clipboard_curves =
3077 joined_clipboard_set.get_curves()->geometry.wrap();
3078
3079 return clipboard_paste_strokes_ex(bmain,
3080 object,
3081 joined_clipboard_curves,
3082 object_to_paste_layer,
3083 clipboard.object_to_world,
3084 keep_world_transform,
3085 paste_back,
3086 drawing);
3087}
3088
3089/* -------------------------------------------------------------------- */
3093{
3094 const Scene *scene = CTX_data_scene(C);
3095 Object *object = CTX_data_active_object(C);
3096 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
3097
3098 const float threshold = RNA_float_get(op->ptr, "threshold");
3099 const bool use_unselected = RNA_boolean_get(op->ptr, "use_unselected");
3100
3101 std::atomic<bool> changed = false;
3102
3104 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
3105 bke::greasepencil::Drawing &drawing = info.drawing;
3106 IndexMaskMemory memory;
3107 const IndexMask points = use_unselected ?
3109 *object, drawing, info.layer_index, memory) :
3111 *object, info.drawing, info.layer_index, memory);
3112 if (points.is_empty()) {
3113 return;
3114 }
3116 drawing.strokes(), threshold, points, {});
3117 drawing.tag_topology_changed();
3118 changed.store(true, std::memory_order_relaxed);
3119 });
3120 if (changed) {
3123 }
3124 return OPERATOR_FINISHED;
3125}
3126
3128{
3129 PropertyRNA *prop;
3130
3131 ot->name = "Merge by Distance";
3132 ot->idname = "GREASE_PENCIL_OT_stroke_merge_by_distance";
3133 ot->description = "Merge points by distance";
3134
3137
3139
3140 prop = RNA_def_float(ot->srna, "threshold", 0.001f, 0.0f, 100.0f, "Threshold", "", 0.0f, 100.0f);
3141 /* Avoid re-using last var. */
3143
3144 prop = RNA_def_boolean(ot->srna,
3145 "use_unselected",
3146 false,
3147 "Unselected",
3148 "Use whole stroke, not only selected points");
3150}
3151
3153
3154/* -------------------------------------------------------------------- */
3157
3159 const IndexMask &points_to_extrude)
3160{
3161 const OffsetIndices<int> points_by_curve = src.points_by_curve();
3162
3163 const int old_curves_num = src.curves_num();
3164 const int old_points_num = src.points_num();
3165
3166 Vector<int> dst_to_src_points(old_points_num);
3168
3169 Vector<int> dst_to_src_curves(old_curves_num);
3171
3172 Vector<bool> dst_selected(old_points_num, false);
3173
3174 Vector<int> dst_curve_counts(old_curves_num);
3176 points_by_curve, src.curves_range(), dst_curve_counts.as_mutable_span());
3177
3178 const VArray<bool> &src_cyclic = src.cyclic();
3179
3180 /* Point offset keeps track of the points inserted. */
3181 int point_offset = 0;
3182 for (const int curve_index : src.curves_range()) {
3183 const IndexRange curve_points = points_by_curve[curve_index];
3184 const IndexMask curve_points_to_extrude = points_to_extrude.slice_content(curve_points);
3185 const bool curve_cyclic = src_cyclic[curve_index];
3186
3187 curve_points_to_extrude.foreach_index([&](const int src_point_index) {
3188 if (!curve_cyclic && (src_point_index == curve_points.first())) {
3189 /* Start-point extruded, we insert a new point at the beginning of the curve.
3190 * NOTE: all points of a cyclic curve behave like an inner-point. */
3191 dst_to_src_points.insert(src_point_index + point_offset, src_point_index);
3192 dst_selected.insert(src_point_index + point_offset, true);
3193 ++dst_curve_counts[curve_index];
3194 ++point_offset;
3195 return;
3196 }
3197 if (!curve_cyclic && (src_point_index == curve_points.last())) {
3198 /* End-point extruded, we insert a new point at the end of the curve.
3199 * NOTE: all points of a cyclic curve behave like an inner-point. */
3200 dst_to_src_points.insert(src_point_index + point_offset + 1, src_point_index);
3201 dst_selected.insert(src_point_index + point_offset + 1, true);
3202 ++dst_curve_counts[curve_index];
3203 ++point_offset;
3204 return;
3205 }
3206
3207 /* Inner-point extruded: we create a new curve made of two points located at the same
3208 * position. Only one of them is selected so that the other one remains stuck to the curve.
3209 */
3210 dst_to_src_points.append(src_point_index);
3211 dst_selected.append(false);
3212 dst_to_src_points.append(src_point_index);
3213 dst_selected.append(true);
3214 dst_to_src_curves.append(curve_index);
3215 dst_curve_counts.append(2);
3216 });
3217 }
3218
3219 const int new_points_num = dst_to_src_points.size();
3220 const int new_curves_num = dst_to_src_curves.size();
3221
3222 bke::CurvesGeometry dst(new_points_num, new_curves_num);
3224
3225 /* Setup curve offsets, based on the number of points in each curve. */
3226 MutableSpan<int> new_curve_offsets = dst.offsets_for_write();
3227 array_utils::copy(dst_curve_counts.as_span(), new_curve_offsets.drop_back(1));
3229
3230 /* Attributes. */
3231 const bke::AttributeAccessor src_attributes = src.attributes();
3233
3234 /* Selection attribute. */
3235 /* Copy the value of control point selections to all selection attributes.
3236 *
3237 * This will lead to the extruded control point always having both handles selected, if it's a
3238 * bezier type stroke. This is to circumvent the issue of source curves handles not being
3239 * deselected when the user extrudes a bezier control point with both handles selected. */
3240 for (const StringRef selection_attribute_name :
3242 {
3244 dst, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
3245 selection.span.copy_from(dst_selected.as_span());
3246 selection.finish();
3247 }
3248
3249 bke::gather_attributes(src_attributes,
3252 {},
3253 dst_to_src_curves,
3254 dst_attributes);
3255
3256 /* Cyclic attribute : newly created curves cannot be cyclic. */
3257 dst.cyclic_for_write().drop_front(old_curves_num).fill(false);
3258
3259 bke::gather_attributes(src_attributes,
3263 {".selection", ".selection_handle_left", ".selection_handle_right"}),
3264 dst_to_src_points,
3265 dst_attributes);
3266
3267 dst.update_curve_types();
3268 if (src.nurbs_has_custom_knots()) {
3269 IndexMaskMemory memory;
3270 const VArray<int8_t> curve_types = src.curve_types();
3271 const VArray<int8_t> knot_modes = dst.nurbs_knots_modes();
3272 const OffsetIndices<int> dst_points_by_curve = dst.points_by_curve();
3273 const IndexMask include_curves = IndexMask::from_predicate(
3274 src.curves_range(), memory, [&](const int64_t curve_index) {
3275 return curve_types[curve_index] == CURVE_TYPE_NURBS &&
3276 knot_modes[curve_index] == NURBS_KNOT_MODE_CUSTOM &&
3277 points_by_curve[curve_index].size() == dst_points_by_curve[curve_index].size();
3278 });
3280 include_curves.complement(dst.curves_range(), memory),
3283 dst);
3284 bke::curves::nurbs::gather_custom_knots(src, include_curves, 0, dst);
3285 }
3286 return dst;
3287}
3288
3290{
3291 const Scene *scene = CTX_data_scene(C);
3292 Object *object = CTX_data_active_object(C);
3293 View3D *v3d = CTX_wm_view3d(C);
3294 const eHandleDisplay handle_display = view3d_handle_type_or_default(v3d);
3295
3296 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
3297
3298 std::atomic<bool> changed = false;
3300 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
3301 IndexMaskMemory memory;
3302 const IndexMask points_to_extrude =
3304 *object, info.drawing, info.layer_index, handle_display, memory);
3305 if (points_to_extrude.is_empty()) {
3306 return;
3307 }
3308
3309 const bke::CurvesGeometry &curves = info.drawing.strokes();
3310 info.drawing.strokes_for_write() = extrude_grease_pencil_curves(curves, points_to_extrude);
3311
3313 changed.store(true, std::memory_order_relaxed);
3314 });
3315
3316 if (changed) {
3319 }
3320
3321 return OPERATOR_FINISHED;
3322}
3323
3325{
3326 ot->name = "Extrude Stroke Points";
3327 ot->idname = "GREASE_PENCIL_OT_extrude";
3328 ot->description = "Extrude the selected points";
3329
3332
3334}
3335
3337
3338/* -------------------------------------------------------------------- */
3341
3342/* Determine how much the radius needs to be scaled to look the same from the view. */
3344 const float3 &old_pos,
3345 const float3 &new_pos)
3346{
3347 /* Don't scale the radius when the view is orthographic. */
3348 if (!rv3d->is_persp) {
3349 return 1.0f;
3350 }
3351
3352 const float3 view_center = float3(rv3d->viewinv[3]);
3353 return math::length(new_pos - view_center) / math::length(old_pos - view_center);
3354}
3355
3357{
3358 Scene &scene = *CTX_data_scene(C);
3359 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
3360
3361 View3D *v3d = CTX_wm_view3d(C);
3362 ARegion *region = CTX_wm_region(C);
3363
3364 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
3365
3366 const ReprojectMode mode = ReprojectMode(RNA_enum_get(op->ptr, "type"));
3367 const bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
3368
3369 Object *object = CTX_data_active_object(C);
3370 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
3371 const float offset = RNA_float_get(op->ptr, "offset");
3372
3373 /* Init snap context for geometry projection. */
3375 [&]() -> transform::SnapObjectContext * {
3376 if (mode == ReprojectMode::Surface) {
3378 }
3379 return nullptr;
3380 });
3381
3383 scene.toolsettings);
3384
3385 const int oldframe = int(DEG_get_ctime(depsgraph));
3386 if (keep_original) {
3388 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
3389 IndexMaskMemory memory;
3391 *object, info.drawing, info.layer_index, selection_domain, memory);
3392 if (elements.is_empty()) {
3393 return;
3394 }
3395
3397 if (selection_domain == bke::AttrDomain::Curve) {
3399 }
3400 else if (selection_domain == bke::AttrDomain::Point) {
3402 }
3404 });
3405 }
3406
3407 /* TODO: This can probably be optimized further for the non-Surface projection use case by
3408 * considering all drawings for the parallel loop instead of having to partition by frame number.
3409 */
3410 std::atomic<bool> changed = false;
3411 Array<Vector<MutableDrawingInfo>> drawings_per_frame =
3413 for (const Span<MutableDrawingInfo> drawings : drawings_per_frame) {
3414 if (drawings.is_empty()) {
3415 continue;
3416 }
3417 const int current_frame_number = drawings.first().frame_number;
3418
3419 if (mode == ReprojectMode::Surface) {
3420 scene.r.cfra = current_frame_number;
3422 }
3423
3424 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
3426 MutableSpan<float> radii = curves.radius_for_write();
3427
3428 IndexMaskMemory memory;
3429 const IndexMask editable_points = retrieve_editable_points(
3430 *object, info.drawing, info.layer_index, memory);
3431
3433 curves, CURVE_TYPE_BEZIER, memory);
3434
3435 for (const StringRef selection_name :
3437 {
3438 const IndexMask selected_points = ed::curves::retrieve_selected_points(
3439 curves, selection_name, bezier_points, memory);
3440 const IndexMask points_to_reproject = IndexMask::from_intersection(
3441 editable_points, selected_points, memory);
3442
3443 if (points_to_reproject.is_empty()) {
3444 return;
3445 }
3446
3447 MutableSpan<float3> positions = curves.positions_for_write();
3448 if (selection_name == ".selection_handle_left") {
3449 positions = curves.handle_positions_left_for_write();
3450 }
3451 else if (selection_name == ".selection_handle_right") {
3452 positions = curves.handle_positions_right_for_write();
3453 }
3454
3456 if (mode == ReprojectMode::Surface) {
3457 const float4x4 layer_space_to_world_space = layer.to_world_space(*object);
3458 const float4x4 world_space_to_layer_space = math::invert(layer_space_to_world_space);
3459 points_to_reproject.foreach_index(
3460 [&](const int point_i) {
3461 float3 &position = positions[point_i];
3462 const float3 world_pos = math::transform_point(layer_space_to_world_space,
3463 position);
3464 float2 screen_co;
3466 region, world_pos, screen_co, V3D_PROJ_TEST_NOP) !=
3468 {
3469 return;
3470 }
3471
3472 float3 ray_start, ray_direction;
3474 depsgraph, region, v3d, screen_co, ray_start, ray_direction, true))
3475 {
3476 return;
3477 }
3478
3479 float hit_depth = std::numeric_limits<float>::max();
3480 float3 hit_position(0.0f);
3481 float3 hit_normal(0.0f);
3482
3485 transform::SnapObjectContext *snap_context = thread_snap_contexts.local();
3486 if (transform::snap_object_project_ray(snap_context,
3487 depsgraph,
3488 v3d,
3489 &params,
3490 ray_start,
3491 ray_direction,
3492 &hit_depth,
3493 hit_position,
3494 hit_normal))
3495 {
3496 /* Apply offset over surface. */
3497 const float3 new_pos = math::transform_point(
3498 world_space_to_layer_space,
3499 hit_position + math::normalize(ray_start - hit_position) * offset);
3500
3501 if (selection_name == ".selection") {
3502 radii[point_i] *= calculate_radius_projection_factor(rv3d, position, new_pos);
3503 }
3504 position = new_pos;
3505 }
3506 },
3507 exec_mode::grain_size(4096));
3508 }
3509 else {
3510 const DrawingPlacement drawing_placement(
3511 scene, *region, *v3d, *object, &layer, mode, offset, nullptr);
3512 points_to_reproject.foreach_index(
3513 [&](const int point_i) {
3514 const float3 new_pos = drawing_placement.reproject(positions[point_i]);
3515 if (selection_name == ".selection") {
3516 radii[point_i] *= calculate_radius_projection_factor(
3517 rv3d, positions[point_i], new_pos);
3518 }
3519 positions[point_i] = new_pos;
3520 },
3521 exec_mode::grain_size(4096));
3522 }
3523
3525 changed.store(true, std::memory_order_relaxed);
3526 }
3527 });
3528 }
3529
3530 for (transform::SnapObjectContext *snap_context : thread_snap_contexts) {
3531 if (snap_context != nullptr) {
3533 }
3534 }
3535
3536 if (mode == ReprojectMode::Surface) {
3537 scene.r.cfra = oldframe;
3539 }
3540
3541 if (changed) {
3544 }
3545
3546 return OPERATOR_FINISHED;
3547}
3548
3550{
3551 ui::Layout &layout = *op->layout;
3552
3553 const ReprojectMode type = ReprojectMode(RNA_enum_get(op->ptr, "type"));
3554
3555 layout.use_property_split_set(true);
3556 layout.use_property_decorate_set(false);
3557
3558 layout.row(true).prop(op->ptr, "type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
3559
3560 if (type == ReprojectMode::Surface) {
3561 layout.row(true).prop(op->ptr, "offset", UI_ITEM_NONE, std::nullopt, ICON_NONE);
3562 }
3563 layout.row(true).prop(op->ptr, "keep_original", UI_ITEM_NONE, std::nullopt, ICON_NONE);
3564}
3565
3567{
3568 static const EnumPropertyItem reproject_type[] = {
3570 "FRONT",
3571 0,
3572 "Front",
3573 "Reproject the strokes using the X-Z plane"},
3574 {int(ReprojectMode::Side), "SIDE", 0, "Side", "Reproject the strokes using the Y-Z plane"},
3575 {int(ReprojectMode::Top), "TOP", 0, "Top", "Reproject the strokes using the X-Y plane"},
3576 {int(ReprojectMode::View),
3577 "VIEW",
3578 0,
3579 "View",
3580 "Reproject the strokes to end up on the same plane, as if drawn from the current "
3581 "viewpoint "
3582 "using 'Cursor' Stroke Placement"},
3584 "SURFACE",
3585 0,
3586 "Surface",
3587 "Reproject the strokes on to the scene geometry, as if drawn using 'Surface' placement"},
3589 "CURSOR",
3590 0,
3591 "Cursor",
3592 "Reproject the strokes using the orientation of 3D cursor"},
3593 {0, nullptr, 0, nullptr, nullptr},
3594 };
3595
3596 /* identifiers */
3597 ot->name = "Reproject Strokes";
3598 ot->idname = "GREASE_PENCIL_OT_reproject";
3599 ot->description =
3600 "Reproject the selected strokes from the current viewpoint as if they had been newly "
3601 "drawn "
3602 "(e.g. to fix problems from accidental 3D cursor movement or accidental viewport changes, "
3603 "or for matching deforming geometry)";
3604
3605 /* callbacks */
3606 ot->invoke = WM_menu_invoke;
3610
3611 /* flags */
3613
3614 /* properties */
3615 ot->prop = RNA_def_enum(
3616 ot->srna, "type", reproject_type, int(ReprojectMode::View), "Projection Type", "");
3617
3619 ot->srna,
3620 "keep_original",
3621 false,
3622 "Keep Original",
3623 "Keep original strokes and create a copy before reprojecting");
3625
3626 RNA_def_float(ot->srna, "offset", 0.0f, 0.0f, 10.0f, "Surface Offset", "", 0.0f, 10.0f);
3627}
3628
3630/* -------------------------------------------------------------------- */
3633
3634/* Poll callback for snap operators */
3635/* NOTE: For now, we only allow these in the 3D view, as other editors do not
3636 * define a cursor or grid-step which can be used.
3637 */
3639{
3641 return false;
3642 }
3643
3644 const ScrArea *area = CTX_wm_area(C);
3645 if (!(area && area->spacetype == SPACE_VIEW3D)) {
3646 return false;
3647 }
3648 const ARegion *region = CTX_wm_region(C);
3649 if (!(region && region->regiontype == RGN_TYPE_WINDOW)) {
3650 return false;
3651 }
3652
3653 return true;
3654}
3655
3657{
3659
3660 const Scene &scene = *CTX_data_scene(C);
3661 Object &object = *CTX_data_active_object(C);
3662 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
3663 const View3D &v3d = *CTX_wm_view3d(C);
3664 const ARegion &region = *CTX_wm_region(C);
3665 const float grid_size = ED_view3d_grid_view_scale(&scene, &v3d, &region, nullptr);
3666
3668 for (const MutableDrawingInfo &drawing_info : drawings) {
3669 bke::CurvesGeometry &curves = drawing_info.drawing.strokes_for_write();
3670 if (curves.is_empty()) {
3671 continue;
3672 }
3674 continue;
3675 }
3676
3677 IndexMaskMemory memory;
3679 curves, CURVE_TYPE_BEZIER, memory);
3680
3682 {
3683 const IndexMask selected_points = ed::curves::retrieve_selected_points(
3684 curves, selection_name, bezier_points, memory);
3685
3686 const Layer &layer = grease_pencil.layer(drawing_info.layer_index);
3687 const float4x4 layer_to_world = layer.to_world_space(object);
3688 const float4x4 world_to_layer = math::invert(layer_to_world);
3689
3690 MutableSpan<float3> positions = curves.positions_for_write();
3691 if (selection_name == ".selection_handle_left") {
3692 positions = curves.handle_positions_left_for_write();
3693 }
3694 else if (selection_name == ".selection_handle_right") {
3695 positions = curves.handle_positions_right_for_write();
3696 }
3697 selected_points.foreach_index(
3698 [&](const int point_i) {
3699 const float3 pos_world = math::transform_point(layer_to_world, positions[point_i]);
3700 const float3 pos_snapped = grid_size * math::floor(pos_world / grid_size + 0.5f);
3701 positions[point_i] = math::transform_point(world_to_layer, pos_snapped);
3702 },
3703 exec_mode::grain_size(4096));
3704 }
3705
3706 drawing_info.drawing.tag_positions_changed();
3711 }
3712
3713 return OPERATOR_FINISHED;
3714}
3715
3717{
3718 ot->name = "Snap Selection to Grid";
3719 ot->idname = "GREASE_PENCIL_OT_snap_to_grid";
3720 ot->description = "Snap selected points to the nearest grid points";
3721
3724
3726}
3727
3729
3730/* -------------------------------------------------------------------- */
3733
3735{
3737
3738 const Scene &scene = *CTX_data_scene(C);
3739 Object &object = *CTX_data_active_object(C);
3740 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
3741 const bool use_offset = RNA_boolean_get(op->ptr, "use_offset");
3742 const float3 cursor_world = scene.cursor.location;
3743
3745 for (const MutableDrawingInfo &drawing_info : drawings) {
3746 bke::CurvesGeometry &curves = drawing_info.drawing.strokes_for_write();
3747 if (curves.is_empty()) {
3748 continue;
3749 }
3751 continue;
3752 }
3753
3754 IndexMaskMemory selected_points_memory;
3756 selected_points_memory);
3757
3758 const Layer &layer = grease_pencil.layer(drawing_info.layer_index);
3759 const float4x4 layer_to_world = layer.to_world_space(object);
3760 const float4x4 world_to_layer = math::invert(layer_to_world);
3761 const float3 cursor_layer = math::transform_point(world_to_layer, cursor_world);
3762
3763 MutableSpan<float3> positions = curves.positions_for_write();
3764 if (use_offset) {
3765 const OffsetIndices points_by_curve = curves.points_by_curve();
3766 IndexMaskMemory selected_curves_memory;
3767 const IndexMask selected_curves = ed::curves::retrieve_selected_curves(
3768 curves, selected_curves_memory);
3769
3770 selected_curves.foreach_index(
3771 [&](const int curve_i) {
3772 const IndexRange points = points_by_curve[curve_i];
3773
3774 /* Offset from first point of the curve. */
3775 const float3 offset = cursor_layer - positions[points.first()];
3776 selected_points.slice_content(points).foreach_index_optimized<int>(
3777 [&](const int point_i) { positions[point_i] += offset; },
3778 exec_mode::grain_size(4096));
3779 },
3781 }
3782 else {
3783 /* Set all selected positions to the cursor location. */
3784 index_mask::masked_fill(positions, cursor_layer, selected_points);
3785 }
3786
3787 curves.calculate_bezier_auto_handles();
3788 drawing_info.drawing.tag_positions_changed();
3793 }
3794
3795 return OPERATOR_FINISHED;
3796}
3797
3799{
3800 /* identifiers */
3801 ot->name = "Snap Selection to Cursor";
3802 ot->idname = "GREASE_PENCIL_OT_snap_to_cursor";
3803 ot->description = "Snap selected points/strokes to the cursor";
3804
3805 /* callbacks */
3808
3809 /* flags */
3811
3812 /* props */
3813 ot->prop = RNA_def_boolean(ot->srna,
3814 "use_offset",
3815 true,
3816 "With Offset",
3817 "Offset the entire stroke instead of selected points only");
3818}
3819
3821
3822/* -------------------------------------------------------------------- */
3825
3827 const Object &object,
3829 float3 &r_centroid,
3830 float3 &r_min,
3831 float3 &r_max)
3832{
3834
3835 int num_selected = 0;
3836 r_centroid = float3(0.0f);
3837 r_min = float3(std::numeric_limits<float>::max());
3838 r_max = float3(std::numeric_limits<float>::lowest());
3839
3840 const Vector<DrawingInfo> drawings = retrieve_visible_drawings(scene, grease_pencil, false);
3841 for (const DrawingInfo &drawing_info : drawings) {
3842 const Layer &layer = grease_pencil.layer(drawing_info.layer_index);
3843 if (layer.is_locked()) {
3844 continue;
3845 }
3846 const bke::CurvesGeometry &curves = drawing_info.drawing.strokes();
3847 if (curves.is_empty()) {
3848 continue;
3849 }
3851 continue;
3852 }
3853
3854 IndexMaskMemory selected_points_memory;
3856 selected_points_memory);
3857 const float4x4 layer_to_world = layer.to_world_space(object);
3858
3859 Span<float3> positions = curves.positions();
3860 selected_points.foreach_index(
3861 [&](const int point_i) {
3862 const float3 pos_world = math::transform_point(layer_to_world, positions[point_i]);
3863 r_centroid += pos_world;
3864 math::min_max(pos_world, r_min, r_max);
3865 },
3866 exec_mode::grain_size(4096));
3867 num_selected += selected_points.size();
3868 }
3869 if (num_selected == 0) {
3870 r_min = r_max = float3(0.0f);
3871 return false;
3872 }
3873
3874 r_centroid /= num_selected;
3875 return true;
3876}
3877
3879{
3880 Scene &scene = *CTX_data_scene(C);
3881 const Object &object = *CTX_data_active_object(C);
3882 const GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
3883 float3 &cursor = reinterpret_cast<float3 &>(scene.cursor.location);
3884
3885 float3 centroid, points_min, points_max;
3887 scene, object, grease_pencil, centroid, points_min, points_max))
3888 {
3889 return OPERATOR_FINISHED;
3890 }
3891
3892 switch (scene.toolsettings->transform_pivot_point) {
3894 cursor = math::midpoint(points_min, points_max);
3895 break;
3897 case V3D_AROUND_CURSOR:
3899 case V3D_AROUND_ACTIVE:
3900 cursor = centroid;
3901 break;
3902 default:
3904 }
3905
3908
3909 return OPERATOR_FINISHED;
3910}
3911
3913{
3914 /* identifiers */
3915 ot->name = "Snap Cursor to Selected Points";
3916 ot->idname = "GREASE_PENCIL_OT_snap_cursor_to_selected";
3917 ot->description = "Snap cursor to center of selected points";
3918
3919 /* callbacks */
3922
3923 /* flags */
3925}
3926
3928{
3929 float4x3 strokemat4x3 = float4x3(strokemat);
3930
3931 /*
3932 * We need the diagonal of ones to start from the bottom right instead top left to properly
3933 * apply the two matrices.
3934 *
3935 * i.e.
3936 * # # # # # # # #
3937 * We need # # # # Instead of # # # #
3938 * 0 0 0 1 0 0 1 0
3939 *
3940 */
3941 strokemat4x3[2][2] = 0.0f;
3942 strokemat4x3[3][2] = 1.0f;
3943
3944 return strokemat4x3;
3945}
3946
3948{
3949 const Scene *scene = CTX_data_scene(C);
3950 Object *object = CTX_data_active_object(C);
3951 ARegion *region = CTX_wm_region(C);
3952 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
3953
3954 bool inserted_keyframe = false;
3955 const bool use_duplicate_previous_key = true;
3956 for (bke::greasepencil::Layer *layer : grease_pencil.layers_for_write()) {
3957 if (layer->is_editable()) {
3959 *scene, grease_pencil, *layer, use_duplicate_previous_key, inserted_keyframe);
3960 }
3961 }
3962
3963 std::atomic<bool> changed = inserted_keyframe;
3965 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
3966 IndexMaskMemory memory;
3968 *object, info.drawing, info.layer_index, memory);
3969 if (strokes.is_empty()) {
3970 return;
3971 }
3972
3974 const float4x4 layer_space_to_world_space = layer.to_world_space(*object);
3975
3976 /* Calculate screen space points. */
3977 const float2 screen_start(RNA_int_get(op->ptr, "xstart"), RNA_int_get(op->ptr, "ystart"));
3978 const float2 screen_end(RNA_int_get(op->ptr, "xend"), RNA_int_get(op->ptr, "yend"));
3979 const float2 screen_direction = screen_end - screen_start;
3980 const float2 screen_tangent = screen_start + float2(-screen_direction[1], screen_direction[0]);
3981
3982 const bke::CurvesGeometry &curves = info.drawing.strokes();
3983 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
3984 const Span<float3> positions = curves.positions();
3985 const Span<float3> normals = info.drawing.curve_plane_normals();
3986 const VArray<int> materials = *curves.attributes().lookup_or_default<int>(
3987 "material_index", bke::AttrDomain::Curve, 0);
3988
3989 Array<float4x2> texture_matrices(strokes.size());
3990
3991 strokes.foreach_index([&](const int curve_i, const int pos) {
3992 const int material_index = materials[curve_i];
3993
3994 const MaterialGPencilStyle *gp_style = BKE_gpencil_material_settings(object,
3995 material_index + 1);
3996 const bool is_radial = gp_style->gradient_type == GP_MATERIAL_GRADIENT_RADIAL;
3997
3998 const float texture_angle = gp_style->texture_angle;
3999 const float2 texture_scale = float2(gp_style->texture_scale);
4000 const float2 texture_offset = float2(gp_style->texture_offset);
4001
4002 const float2x2 texture_rotation = math::from_rotation<float2x2>(
4003 math::AngleRadian(texture_angle));
4004
4005 const float3 point = math::transform_point(layer_space_to_world_space,
4006 positions[points_by_curve[curve_i].first()]);
4007 const float3 normal = math::transform_direction(layer_space_to_world_space,
4008 normals[curve_i]);
4009
4010 const float4 plane = float4(normal, -math::dot(normal, point));
4011
4012 float3 start;
4013 float3 tangent;
4014 float3 end;
4015 ED_view3d_win_to_3d_on_plane(region, plane, screen_start, false, start);
4016 ED_view3d_win_to_3d_on_plane(region, plane, screen_tangent, false, tangent);
4017 ED_view3d_win_to_3d_on_plane(region, plane, screen_end, false, end);
4018
4019 const float3 origin = start;
4020 /* Invert the length by dividing by the length squared. */
4021 const float3 u_dir = (end - origin) / math::length_squared(end - origin);
4022 float3 v_dir = math::cross(u_dir, normal);
4023
4024 /* Flip the texture if need so that it is not mirrored. */
4025 if (math::dot(tangent - start, v_dir) < 0.0f) {
4026 v_dir = -v_dir;
4027 }
4028
4029 /* Calculate the texture space before the texture offset transformation. */
4030 const float4x2 base_texture_space = math::transpose(float2x4(
4031 float4(u_dir, -math::dot(u_dir, origin)), float4(v_dir, -math::dot(v_dir, origin))));
4032
4033 float3x2 offset_matrix = float3x2::identity();
4034
4035 if (is_radial) {
4036 /* Radial gradients are scaled down by a factor of 2 and have the center at 0.5 */
4037 offset_matrix *= 0.5f;
4038 offset_matrix[2] += float2(0.5f, 0.5f);
4039 }
4040
4041 /* For some reason 0.5 is added to the offset before being rendered, so remove it here. */
4042 offset_matrix[2] -= float2(0.5f, 0.5f);
4043
4044 offset_matrix = math::from_scale<float2x2>(texture_scale) * offset_matrix;
4045 offset_matrix = texture_rotation * offset_matrix;
4046 offset_matrix[2] -= texture_offset;
4047
4048 texture_matrices[pos] = (offset_matrix * expand_4x2_mat(base_texture_space)) *
4049 layer_space_to_world_space;
4050 });
4051
4052 info.drawing.set_texture_matrices(texture_matrices, strokes);
4053
4054 changed.store(true, std::memory_order_relaxed);
4055 });
4056
4057 if (changed) {
4060 }
4061
4062 return OPERATOR_FINISHED;
4063}
4064
4066 wmOperator *op,
4067 const wmEvent *event)
4068{
4070
4071 /* Check for mouse release. */
4072 if ((ret & OPERATOR_RUNNING_MODAL) != 0 && event->type == LEFTMOUSE && event->val == KM_RELEASE)
4073 {
4077 }
4078
4079 return ret;
4080}
4081
4083 wmOperator *op,
4084 const wmEvent *event)
4085{
4086 /* Invoke interactive line drawing (representing the gradient) in viewport. */
4088
4089 if ((ret & OPERATOR_RUNNING_MODAL) != 0) {
4090 ARegion *region = CTX_wm_region(C);
4091 if (region->regiontype == RGN_TYPE_WINDOW && event->type == LEFTMOUSE &&
4092 event->val == KM_PRESS)
4093 {
4094 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
4095 gesture->is_active = true;
4096 }
4097 }
4098
4099 return ret;
4100}
4101
4103{
4104 /* Identifiers. */
4105 ot->name = "Texture Gradient";
4106 ot->idname = "GREASE_PENCIL_OT_texture_gradient";
4107 ot->description = "Draw a line to set the fill material gradient for the selected strokes";
4108
4109 /* API callbacks. */
4115
4116 /* Flags. */
4118
4120}
4121
4123
4124/* -------------------------------------------------------------------- */
4127
4129{
4130 const Scene *scene = CTX_data_scene(C);
4131 Object *object = CTX_data_active_object(C);
4132 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4133
4134 const CurveType dst_type = CurveType(RNA_enum_get(op->ptr, "type"));
4135 const bool use_handles = RNA_boolean_get(op->ptr, "use_handles");
4136
4137 bool changed = false;
4139 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4141 IndexMaskMemory memory;
4143 *object, info.drawing, info.layer_index, memory);
4144 if (strokes.is_empty()) {
4145 return;
4146 }
4147
4149 options.convert_bezier_handles_to_poly_points = use_handles;
4150 options.convert_bezier_handles_to_catmull_rom_points = use_handles;
4151 options.keep_bezier_shape_as_nurbs = use_handles;
4152 options.keep_catmull_rom_shape_as_nurbs = use_handles;
4153
4154 curves = geometry::convert_curves(curves, strokes, dst_type, {}, options);
4156
4157 changed = true;
4158 });
4159
4160 if (changed) {
4163 }
4164
4165 return OPERATOR_FINISHED;
4166}
4167
4169{
4170 ot->name = "Set Curve Type";
4171 ot->idname = "GREASE_PENCIL_OT_set_curve_type";
4172 ot->description = "Set type of selected curves";
4173
4174 ot->invoke = WM_menu_invoke;
4177
4179
4180 ot->prop = RNA_def_enum(
4181 ot->srna, "type", rna_enum_curves_type_items, CURVE_TYPE_POLY, "Type", "Curve type");
4182
4184 "use_handles",
4185 false,
4186 "Handles",
4187 "Take handle information into account in the conversion");
4188}
4189
4191
4192/* -------------------------------------------------------------------- */
4195
4197{
4198 const Scene *scene = CTX_data_scene(C);
4199 Object *object = CTX_data_active_object(C);
4200 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4201
4202 using namespace ed::curves;
4203 const SetHandleType dst_type = SetHandleType(RNA_enum_get(op->ptr, "type"));
4204
4205 auto new_handle_type = [&](const int8_t handle_type) {
4206 switch (dst_type) {
4207 case SetHandleType::Free:
4208 return int8_t(BEZIER_HANDLE_FREE);
4209 case SetHandleType::Auto:
4210 return int8_t(BEZIER_HANDLE_AUTO);
4211 case SetHandleType::Vector:
4213 case SetHandleType::Align:
4215 case SetHandleType::Toggle:
4216 return int8_t(handle_type == BEZIER_HANDLE_FREE ? BEZIER_HANDLE_ALIGN :
4218 }
4220 return int8_t(0);
4221 };
4222
4223 bool changed = false;
4225 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4227 if (!curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
4228 return;
4229 }
4230 IndexMaskMemory memory;
4232 *object, info.drawing, info.layer_index, memory);
4233 const IndexMask bezier_curves = curves.indices_for_curve_type(
4234 CURVE_TYPE_BEZIER, editable_strokes, memory);
4235
4236 const bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
4237 const VArraySpan<bool> selection = *attributes.lookup_or_default<bool>(
4238 ".selection", bke::AttrDomain::Point, true);
4239 const VArraySpan<bool> selection_left = *attributes.lookup_or_default<bool>(
4240 ".selection_handle_left", bke::AttrDomain::Point, true);
4241 const VArraySpan<bool> selection_right = *attributes.lookup_or_default<bool>(
4242 ".selection_handle_right", bke::AttrDomain::Point, true);
4243
4244 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
4245 MutableSpan<int8_t> handle_types_left = curves.handle_types_left_for_write();
4246 MutableSpan<int8_t> handle_types_right = curves.handle_types_right_for_write();
4247 bezier_curves.foreach_index(
4248 [&](const int curve_i) {
4249 const IndexRange points = points_by_curve[curve_i];
4250 for (const int point_i : points) {
4251 if (selection_left[point_i] || selection[point_i]) {
4252 handle_types_left[point_i] = new_handle_type(handle_types_left[point_i]);
4253 }
4254 if (selection_right[point_i] || selection[point_i]) {
4255 handle_types_right[point_i] = new_handle_type(handle_types_right[point_i]);
4256 }
4257 }
4258 },
4260
4261 curves.calculate_bezier_auto_handles();
4262 curves.calculate_bezier_aligned_handles();
4263 curves.tag_topology_changed();
4265
4266 changed = true;
4267 });
4268
4269 if (changed) {
4272 }
4273
4274 return OPERATOR_FINISHED;
4275}
4276
4278{
4279 ot->name = "Set Handle Type";
4280 ot->idname = "GREASE_PENCIL_OT_set_handle_type";
4281 ot->description = "Set the handle type for Bézier curves";
4282
4283 ot->invoke = WM_menu_invoke;
4286
4288
4289 ot->prop = RNA_def_enum(ot->srna,
4290 "type",
4293 "Type",
4294 nullptr);
4295}
4296
4298
4299/* -------------------------------------------------------------------- */
4302
4304{
4305 const Scene *scene = CTX_data_scene(C);
4306 Object *object = CTX_data_active_object(C);
4307 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4308
4309 const int resolution = RNA_int_get(op->ptr, "resolution");
4310
4311 bool changed = false;
4313 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4315 IndexMaskMemory memory;
4317 *object, info.drawing, info.layer_index, memory);
4318 if (editable_strokes.is_empty()) {
4319 return;
4320 }
4321
4322 if (curves.is_single_type(CURVE_TYPE_POLY)) {
4323 return;
4324 }
4325
4326 index_mask::masked_fill(curves.resolution_for_write(), resolution, editable_strokes);
4328 changed = true;
4329 });
4330
4331 if (changed) {
4334 }
4335
4336 return OPERATOR_FINISHED;
4337}
4338
4340{
4341 ot->name = "Set Curve Resolution";
4342 ot->idname = "GREASE_PENCIL_OT_set_curve_resolution";
4343 ot->description = "Set resolution of selected curves";
4344
4347
4349
4350 RNA_def_int(ot->srna,
4351 "resolution",
4352 12,
4353 0,
4354 10000,
4355 "Resolution",
4356 "The resolution to use for each curve segment",
4357 1,
4358 64);
4359}
4360
4362
4363/* -------------------------------------------------------------------- */
4366
4368{
4369 const Scene *scene = CTX_data_scene(C);
4370 Object *object = CTX_data_active_object(C);
4371 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4372
4373 bool changed = false;
4375 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4377 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
4378 IndexMaskMemory memory;
4380 *object, info.drawing, info.layer_index, memory);
4381 if (editable_strokes.is_empty()) {
4382 return;
4383 }
4384
4385 if (attributes.contains("uv_rotation")) {
4386 if (editable_strokes.size() == curves.curves_num()) {
4387 attributes.remove("uv_rotation");
4388 }
4389 else {
4390 bke::SpanAttributeWriter<float> uv_rotations = attributes.lookup_for_write_span<float>(
4391 "uv_rotation");
4392 index_mask::masked_fill(uv_rotations.span, 0.0f, editable_strokes);
4393 uv_rotations.finish();
4394 }
4395 }
4396
4397 if (attributes.contains("uv_translation")) {
4398 if (editable_strokes.size() == curves.curves_num()) {
4399 attributes.remove("uv_translation");
4400 }
4401 else {
4402 bke::SpanAttributeWriter<float2> uv_translations =
4403 attributes.lookup_for_write_span<float2>("uv_translation");
4404 index_mask::masked_fill(uv_translations.span, float2(0.0f, 0.0f), editable_strokes);
4405 uv_translations.finish();
4406 }
4407 }
4408
4409 if (attributes.contains("uv_scale")) {
4410 if (editable_strokes.size() == curves.curves_num()) {
4411 attributes.remove("uv_scale");
4412 }
4413 else {
4415 "uv_scale");
4416 index_mask::masked_fill(uv_scales.span, float2(1.0f, 1.0f), editable_strokes);
4417 uv_scales.finish();
4418 }
4419 }
4420
4421 if (attributes.contains("uv_shear")) {
4422 if (editable_strokes.size() == curves.curves_num()) {
4423 attributes.remove("uv_shear");
4424 }
4425 else {
4426 bke::SpanAttributeWriter<float> uv_shears = attributes.lookup_for_write_span<float>(
4427 "uv_shear");
4428 index_mask::masked_fill(uv_shears.span, 0.0f, editable_strokes);
4429 uv_shears.finish();
4430 }
4431 }
4432
4434 changed = true;
4435 });
4436
4437 if (changed) {
4440 }
4441
4442 return OPERATOR_FINISHED;
4443}
4444
4446{
4447 /* Identifiers. */
4448 ot->name = "Reset UVs";
4449 ot->idname = "GREASE_PENCIL_OT_reset_uvs";
4450 ot->description = "Reset UV transformation to default values";
4451
4452 /* Callbacks. */
4455
4457}
4458
4460{
4461 const Scene &scene = *CTX_data_scene(C);
4462 Object &object = *CTX_data_active_object(C);
4463 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
4464 std::atomic<bool> changed = false;
4465
4467 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4468 IndexMaskMemory memory;
4470 object, info.drawing, info.layer_index, memory);
4471
4472 if (selected_points.is_empty()) {
4473 return;
4474 }
4475
4477 selected_points);
4479 changed.store(true, std::memory_order_relaxed);
4480 });
4481
4482 if (changed) {
4485 return OPERATOR_FINISHED;
4486 }
4487
4488 return OPERATOR_CANCELLED;
4489}
4490
4492{
4493 /* Identifiers. */
4494 ot->name = "Split stroke";
4495 ot->idname = "GREASE_PENCIL_OT_stroke_split";
4496 ot->description = "Split selected points to a new stroke";
4497
4498 /* Callbacks. */
4501
4503}
4504
4506
4507/* -------------------------------------------------------------------- */
4510
4512
4514{
4515 using namespace blender::bke::greasepencil;
4516 const Scene &scene = *CTX_data_scene(C);
4517 Object &object = *CTX_data_active_object(C);
4518 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
4519
4520 const RemoveFillGuidesMode mode = RemoveFillGuidesMode(RNA_enum_get(op->ptr, "mode"));
4521
4522 std::atomic<bool> changed = false;
4525 for (const int layer_i : grease_pencil.layers().index_range()) {
4526 const Layer &layer = grease_pencil.layer(layer_i);
4527 if (Drawing *drawing = grease_pencil.get_drawing_at(layer, scene.r.cfra)) {
4528 drawings.append({*drawing, layer_i, scene.r.cfra, 1.0f});
4529 }
4530 }
4531 }
4532 else if (mode == RemoveFillGuidesMode::AllFrames) {
4533 for (const int layer_i : grease_pencil.layers().index_range()) {
4534 const Layer &layer = grease_pencil.layer(layer_i);
4535 for (const auto [frame_number, frame] : layer.frames().items()) {
4536 if (Drawing *drawing = grease_pencil.get_drawing_at(layer, frame_number)) {
4537 drawings.append({*drawing, layer_i, frame_number, 1.0f});
4538 }
4539 }
4540 }
4541 }
4542 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4545 changed.store(true, std::memory_order_relaxed);
4546 }
4547 });
4548
4549 if (changed) {
4552 return OPERATOR_FINISHED;
4553 }
4554
4555 return OPERATOR_CANCELLED;
4556}
4557
4559{
4560 static const EnumPropertyItem rna_mode_items[] = {
4561 {int(RemoveFillGuidesMode::ActiveFrame), "ACTIVE_FRAME", 0, "Active Frame", ""},
4562 {int(RemoveFillGuidesMode::AllFrames), "ALL_FRAMES", 0, "All Frames", ""},
4563 {0, nullptr, 0, nullptr, nullptr},
4564 };
4565
4566 /* Identifiers. */
4567 ot->name = "Remove Fill Guides";
4568 ot->idname = "GREASE_PENCIL_OT_remove_fill_guides";
4569 ot->description = "Remove all the strokes that were created from the fill tool as guides";
4570
4571 /* Callbacks. */
4574
4576
4577 ot->prop = RNA_def_enum(
4578 ot->srna, "mode", rna_mode_items, int(RemoveFillGuidesMode::AllFrames), "Mode", "");
4579}
4580
4582
4583/* -------------------------------------------------------------------- */
4586
4587enum class OutlineMode : int8_t {
4588 View = 0,
4590 Side = 2,
4591 Top = 3,
4594};
4595
4597 {int(OutlineMode::View), "VIEW", 0, "View", ""},
4598 {int(OutlineMode::Front), "FRONT", 0, "Front", ""},
4599 {int(OutlineMode::Side), "SIDE", 0, "Side", ""},
4600 {int(OutlineMode::Top), "TOP", 0, "Top", ""},
4601 {int(OutlineMode::Cursor), "CURSOR", 0, "Cursor", ""},
4602 {int(OutlineMode::Camera), "CAMERA", 0, "Camera", ""},
4603 {0, nullptr, 0, nullptr, nullptr},
4604};
4605
4607{
4609
4610 const Scene *scene = CTX_data_scene(C);
4611 Object *object = CTX_data_active_object(C);
4612 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4613
4614 const float radius = RNA_float_get(op->ptr, "radius");
4615 const float offset_factor = RNA_float_get(op->ptr, "offset_factor");
4616 const int corner_subdivisions = RNA_int_get(op->ptr, "corner_subdivisions");
4617 const float outline_offset = radius * offset_factor;
4618 const int mat_nr = -1;
4619
4620 const OutlineMode mode = OutlineMode(RNA_enum_get(op->ptr, "type"));
4621
4622 float4x4 viewinv = float4x4::identity();
4623 switch (mode) {
4624 case OutlineMode::View: {
4625 if (RegionView3D *rv3d = CTX_wm_region_view3d(C)) {
4626 viewinv = float4x4(rv3d->viewmat);
4627 }
4628 break;
4629 }
4630 case OutlineMode::Front:
4631 viewinv = float4x4({1.0f, 0.0f, 0.0f, 0.0f},
4632 {0.0f, 0.0f, 1.0f, 0.0f},
4633 {0.0f, 1.0f, 0.0f, 0.0f},
4634 {0.0f, 0.0f, 0.0f, 1.0f});
4635 break;
4636 case OutlineMode::Side:
4637 viewinv = float4x4({0.0f, 0.0f, 1.0f, 0.0f},
4638 {0.0f, 1.0f, 0.0f, 0.0f},
4639 {1.0f, 0.0f, 0.0f, 0.0f},
4640 {0.0f, 0.0f, 0.0f, 1.0f});
4641 break;
4642 case OutlineMode::Top:
4643 viewinv = float4x4::identity();
4644 break;
4645 case OutlineMode::Cursor: {
4646 viewinv = scene->cursor.matrix<float4x4>();
4647 break;
4648 }
4650 if (scene->camera == nullptr) {
4651 BKE_report(op->reports, RPT_ERROR, "No camera in the scene");
4652 return OPERATOR_CANCELLED;
4653 }
4654 viewinv = scene->camera->world_to_object();
4655 break;
4656 default:
4658 break;
4659 }
4660
4661 bool changed = false;
4663 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4664 IndexMaskMemory memory;
4666 *object, info.drawing, info.layer_index, memory);
4667 if (editable_strokes.is_empty()) {
4668 return;
4669 }
4670
4671 const Layer &layer = grease_pencil.layer(info.layer_index);
4672 const float4x4 viewmat = viewinv * layer.to_world_space(*object);
4673
4675 editable_strokes,
4676 viewmat,
4677 corner_subdivisions,
4678 radius,
4679 outline_offset,
4680 mat_nr);
4681
4682 info.drawing.strokes_for_write().remove_curves(editable_strokes, {});
4683
4684 /* Join the outline stroke into the drawing. */
4685 Curves *strokes = bke::curves_new_nomain(std::move(outline));
4686
4687 Curves *other_curves = bke::curves_new_nomain(std::move(info.drawing.strokes_for_write()));
4688 const std::array<bke::GeometrySet, 2> geometry_sets = {
4690
4691 info.drawing.strokes_for_write() = std::move(
4692 geometry::join_geometries(geometry_sets, {}).get_curves_for_write()->geometry.wrap());
4693
4695 changed = true;
4696 });
4697
4698 if (changed) {
4701 }
4702
4703 return OPERATOR_FINISHED;
4704}
4705
4707{
4708 /* Identifiers. */
4709 ot->name = "Outline";
4710 ot->idname = "GREASE_PENCIL_OT_outline";
4711 ot->description = "Convert selected strokes to perimeter";
4712
4713 /* Callbacks. */
4716
4718
4719 /* Properties */
4720 ot->prop = RNA_def_enum(
4721 ot->srna, "type", prop_outline_modes, int(OutlineMode::View), "Projection Mode", "");
4722 RNA_def_float_distance(ot->srna, "radius", 0.01f, 0.0f, 10.0f, "Radius", "", 0.0f, 10.0f);
4724 ot->srna, "offset_factor", -1.0f, -1.0f, 1.0f, "Offset Factor", "", -1.0f, 1.0f);
4725 RNA_def_int(ot->srna, "corner_subdivisions", 2, 0, 10, "Corner Subdivisions", "", 0, 5);
4726}
4727
4729
4730/* -------------------------------------------------------------------- */
4733
4735 const IndexMask &selection,
4736 const float threshold)
4737{
4738 const VArray<float> thresholds = VArray<float>::from_single(threshold, curves.curves_num());
4739 /* TODO: Detect or manually provide corners. */
4740 const VArray<bool> corners = VArray<bool>::from_single(false, curves.points_num());
4742 curves, selection, thresholds, corners, geometry::FitMethod::Refit, {});
4743}
4744
4746 const IndexMask &selection,
4747 const float threshold)
4748{
4749 if (curves.is_single_type(CURVE_TYPE_CATMULL_ROM)) {
4750 return;
4751 }
4752 IndexMaskMemory memory;
4753 const IndexMask non_catmull_rom_curves_selection =
4754 curves.indices_for_curve_type(CURVE_TYPE_CATMULL_ROM, selection, memory)
4755 .complement(selection, memory);
4756 if (non_catmull_rom_curves_selection.is_empty()) {
4757 return;
4758 }
4759
4760 curves = geometry::resample_to_evaluated(curves, non_catmull_rom_curves_selection);
4761
4762 /* To avoid having too many control points, simplify the position attribute based on the
4763 * threshold. This doesn't replace an actual curve fitting (which would be better), but
4764 * is a decent approximation for the meantime. */
4765 const IndexMask points_to_remove = geometry::simplify_curve_attribute(
4766 curves.positions(),
4767 non_catmull_rom_curves_selection,
4768 curves.points_by_curve(),
4769 curves.cyclic(),
4770 threshold,
4771 curves.positions(),
4772 memory);
4773 curves.remove_points(points_to_remove, {});
4774
4776 options.convert_bezier_handles_to_poly_points = false;
4777 options.convert_bezier_handles_to_catmull_rom_points = false;
4778 options.keep_bezier_shape_as_nurbs = true;
4779 options.keep_catmull_rom_shape_as_nurbs = true;
4781 curves, non_catmull_rom_curves_selection, CURVE_TYPE_CATMULL_ROM, {}, options);
4782}
4783
4785{
4786 if (curves.is_single_type(CURVE_TYPE_POLY)) {
4787 return;
4788 }
4789 IndexMaskMemory memory;
4790 const IndexMask non_poly_curves_selection = curves
4791 .indices_for_curve_type(
4792 CURVE_TYPE_POLY, selection, memory)
4793 .complement(selection, memory);
4794 if (non_poly_curves_selection.is_empty()) {
4795 return;
4796 }
4797
4798 curves = geometry::resample_to_evaluated(curves, non_poly_curves_selection);
4799}
4800
4802 const IndexMask &selection,
4803 const float threshold)
4804{
4805 if (curves.is_single_type(CURVE_TYPE_BEZIER)) {
4806 return;
4807 }
4808 IndexMaskMemory memory;
4809 const IndexMask poly_curves_selection = curves.indices_for_curve_type(
4810 CURVE_TYPE_POLY, selection, memory);
4811 if (!poly_curves_selection.is_empty()) {
4812 curves = fit_poly_curves(curves, poly_curves_selection, threshold);
4813 }
4814
4816 options.convert_bezier_handles_to_poly_points = false;
4817 options.convert_bezier_handles_to_catmull_rom_points = false;
4818 options.keep_bezier_shape_as_nurbs = true;
4819 options.keep_catmull_rom_shape_as_nurbs = true;
4821}
4822
4824 const IndexMask &selection,
4825 const float threshold)
4826{
4827 if (curves.is_single_type(CURVE_TYPE_NURBS)) {
4828 return;
4829 }
4830
4831 IndexMaskMemory memory;
4832 const IndexMask poly_curves_selection = curves.indices_for_curve_type(
4833 CURVE_TYPE_POLY, selection, memory);
4834 if (!poly_curves_selection.is_empty()) {
4835 curves = fit_poly_curves(curves, poly_curves_selection, threshold);
4836 }
4837
4839 options.convert_bezier_handles_to_poly_points = false;
4840 options.convert_bezier_handles_to_catmull_rom_points = false;
4841 options.keep_bezier_shape_as_nurbs = true;
4842 options.keep_catmull_rom_shape_as_nurbs = true;
4844}
4845
4847{
4848 const Scene *scene = CTX_data_scene(C);
4849 Object *object = CTX_data_active_object(C);
4850 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4851
4852 const CurveType dst_type = CurveType(RNA_enum_get(op->ptr, "type"));
4853 const float threshold = RNA_float_get(op->ptr, "threshold");
4854
4855 std::atomic<bool> changed = false;
4857 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4859 IndexMaskMemory memory;
4861 *object, info.drawing, info.layer_index, memory);
4862 if (strokes.is_empty()) {
4863 return;
4864 }
4865
4866 switch (dst_type) {
4868 convert_to_catmull_rom(curves, strokes, threshold);
4869 break;
4870 case CURVE_TYPE_POLY:
4871 convert_to_poly(curves, strokes);
4872 break;
4873 case CURVE_TYPE_BEZIER:
4874 convert_to_bezier(curves, strokes, threshold);
4875 break;
4876 case CURVE_TYPE_NURBS:
4877 convert_to_nurbs(curves, strokes, threshold);
4878 break;
4879 }
4880
4882 changed.store(true, std::memory_order_relaxed);
4883 });
4884
4885 if (changed) {
4888 }
4889
4890 return OPERATOR_FINISHED;
4891}
4892
4894{
4895 ui::Layout &layout = *op->layout;
4897
4899
4900 layout.use_property_split_set(true);
4901 layout.use_property_decorate_set(false);
4902
4903 layout.prop(&ptr, "type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
4904
4905 const CurveType dst_type = CurveType(RNA_enum_get(op->ptr, "type"));
4906
4907 if (dst_type == CURVE_TYPE_POLY) {
4908 return;
4909 }
4910
4911 layout.prop(&ptr, "threshold", UI_ITEM_NONE, std::nullopt, ICON_NONE);
4912}
4913
4915{
4916 ot->name = "Convert Curve Type";
4917 ot->idname = "GREASE_PENCIL_OT_convert_curve_type";
4918 ot->description = "Convert type of selected curves";
4919
4920 ot->invoke = WM_menu_invoke;
4924
4926
4927 ot->prop = RNA_def_enum(
4928 ot->srna, "type", rna_enum_curves_type_items, CURVE_TYPE_POLY, "Type", "");
4930
4931 PropertyRNA *prop = RNA_def_float(
4932 ot->srna,
4933 "threshold",
4934 0.01f,
4935 0.0f,
4936 100.0f,
4937 "Threshold",
4938 "The distance that the resulting points are allowed to be within",
4939 0.0f,
4940 100.0f);
4942}
4943
4945
4946/* -------------------------------------------------------------------- */
4949
4950enum class CornerType : uint8_t {
4954};
4955
4957 {int(CornerType::Round), "ROUND", 0, "Round", ""},
4958 {int(CornerType::Bevel), "FLAT", 0, "Flat", ""},
4959 {int(CornerType::Miter), "SHARP", 0, "Sharp", ""},
4960 {0, nullptr, 0, nullptr, nullptr},
4961};
4962
4964{
4966
4967 const Scene *scene = CTX_data_scene(C);
4968 Object *object = CTX_data_active_object(C);
4969 View3D *v3d = CTX_wm_view3d(C);
4970 const eHandleDisplay handle_display = view3d_handle_type_or_default(v3d);
4971 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
4972
4973 const CornerType corner_type = CornerType(RNA_enum_get(op->ptr, "corner_type"));
4974 float miter_angle = RNA_float_get(op->ptr, "miter_angle");
4975
4976 if (corner_type == CornerType::Round) {
4977 miter_angle = GP_STROKE_MITER_ANGLE_ROUND;
4978 }
4979 else if (corner_type == CornerType::Bevel) {
4980 miter_angle = GP_STROKE_MITER_ANGLE_BEVEL;
4981 }
4982 else if (corner_type == CornerType::Miter) {
4983 /* Prevent the angle from being set to zero, and becoming the `Round` type. */
4984 if (miter_angle == 0.0f) {
4985 miter_angle = DEG2RADF(1.0f);
4986 }
4987 }
4988
4989 std::atomic<bool> changed = false;
4991 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
4992 IndexMaskMemory memory;
4994 *object, info.drawing, info.layer_index, handle_display, memory);
4995 if (selection.is_empty()) {
4996 return;
4997 }
4998
5000 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
5001
5002 /* Only create the attribute if we are not storing the default. */
5003 if (miter_angle == GP_STROKE_MITER_ANGLE_ROUND && !attributes.contains("miter_angle")) {
5004 return;
5005 }
5006
5007 /* Remove the attribute if we are storing all default. */
5008 if (miter_angle == GP_STROKE_MITER_ANGLE_ROUND && selection == curves.points_range()) {
5009 attributes.remove("miter_angle");
5010 changed.store(true, std::memory_order_relaxed);
5011 return;
5012 }
5013
5014 if (bke::SpanAttributeWriter<float> miter_angles =
5015 attributes.lookup_or_add_for_write_span<float>(
5016 "miter_angle",
5019 {
5020 index_mask::masked_fill(miter_angles.span, miter_angle, selection);
5021 miter_angles.finish();
5022 changed.store(true, std::memory_order_relaxed);
5023 }
5024 });
5025
5026 if (changed) {
5029 }
5030
5031 return OPERATOR_FINISHED;
5032}
5033
5035{
5036 ui::Layout &layout = *op->layout;
5038
5040
5041 layout.use_property_split_set(true);
5042 layout.use_property_decorate_set(false);
5043
5044 layout.prop(&ptr, "corner_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5045
5046 const CornerType corner_type = CornerType(RNA_enum_get(op->ptr, "corner_type"));
5047
5048 if (corner_type != CornerType::Miter) {
5049 return;
5050 }
5051
5052 layout.prop(&ptr, "miter_angle", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5053}
5054
5056{
5057 /* Identifiers. */
5058 ot->name = "Set Corner Type";
5059 ot->idname = "GREASE_PENCIL_OT_set_corner_type";
5060 ot->description = "Set the corner type of the selected points";
5061
5062 /* Callbacks. */
5066
5068
5069 /* Properties */
5070 ot->prop = RNA_def_enum(
5071 ot->srna, "corner_type", prop_corner_types, int(CornerType::Miter), "Corner Type", "");
5073 "miter_angle",
5074 DEG2RADF(45.0f),
5075 0.0f,
5076 M_PI,
5077 "Miter Cut Angle",
5078 "All corners sharper than the Miter angle will be cut flat",
5079 0.0f,
5080 M_PI);
5082}
5083
5085
5086/* -------------------------------------------------------------------- */
5089
5090namespace {
5091enum class StrokeType : int8_t { Stroke = 0, Fill = 1, Both = 2 };
5092}
5093
5095{
5096 const Scene *scene = CTX_data_scene(C);
5097 Object *object = CTX_data_active_object(C);
5098 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
5099
5100 const StrokeType type = StrokeType(RNA_enum_get(op->ptr, "type"));
5101 std::atomic<bool> changed = false;
5103 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
5104 IndexMaskMemory memory;
5106 *object, info.drawing, info.layer_index, memory);
5107 if (strokes.is_empty()) {
5108 return;
5109 }
5110
5112 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
5113
5114 bke::SpanAttributeWriter<bool> hide_stroke = attributes.lookup_or_add_for_write_span<bool>(
5115 "hide_stroke", bke::AttrDomain::Curve);
5117 "fill_id", bke::AttrDomain::Curve);
5118
5119 switch (type) {
5120 case StrokeType::Stroke: {
5121 index_mask::masked_fill(hide_stroke.span, false, strokes);
5122 index_mask::masked_fill(fill_ids.span, 0, strokes);
5123 break;
5124 }
5125 case StrokeType::Fill: {
5126 index_mask::masked_fill(hide_stroke.span, true, strokes);
5127 break;
5128 }
5129 case StrokeType::Both: {
5130 index_mask::masked_fill(hide_stroke.span, false, strokes);
5131 break;
5132 }
5133 }
5134
5135 if (ELEM(type, StrokeType::Fill, StrokeType::Both)) {
5136 const IndexMask selected_non_fill_strokes = IndexMask::from_predicate(
5137 strokes, memory, [&](const int64_t index) { return fill_ids.span[index] == 0; });
5139 fill_ids.span.varray(), selected_non_fill_strokes, fill_ids.span);
5140 bke::SpanAttributeWriter<float> fill_opacities =
5141 attributes.lookup_or_add_for_write_span<float>("fill_opacity", bke::AttrDomain::Curve);
5142
5143 /* Each non fill selected stroke becomes a new fill. */
5144 index_mask::masked_fill(fill_opacities.span, 1.0f, selected_non_fill_strokes);
5145 fill_opacities.finish();
5146 }
5147
5148 hide_stroke.finish();
5149 fill_ids.finish();
5150
5151 if (type == StrokeType::Stroke) {
5152 if (std::all_of(fill_ids.span.begin(),
5153 fill_ids.span.end(),
5154 [&](const int64_t fill_id) { return fill_id == 0; }))
5155 {
5156 /* Remove #fill_id attribute if there are no fills left. */
5157 attributes.remove("fill_id");
5158 }
5159 }
5160
5161 if (ELEM(type, StrokeType::Stroke, StrokeType::Both)) {
5163 hide_stroke.span.varray());
5164 if (hide_strokes_mix == array_utils::BooleanMix::AllFalse) {
5165 /* Remove #hide_stroke attribute if all strokes are visible. */
5166 attributes.remove("hide_stroke");
5167 }
5168 else {
5169 /* If some strokes got unhidden, make sure that we create the radius attribute if it
5170 * doesn't exist already. */
5171 attributes.add<float>("radius", bke::AttrDomain::Point, bke::AttributeInitValue(0.005f));
5172 }
5173 }
5174
5176 changed.store(true, std::memory_order_relaxed);
5177 });
5178
5179 if (changed) {
5182 }
5183
5184 return OPERATOR_FINISHED;
5185}
5186
5188{
5189 static const EnumPropertyItem prop_stroke_type_types[] = {
5190 {int(StrokeType::Stroke), "STROKE", ICON_GP_DRAW_STROKE, "Stroke", ""},
5191 {int(StrokeType::Fill), "FILL", ICON_GP_DRAW_FILL, "Fill", ""},
5192 {int(StrokeType::Both), "BOTH", ICON_GP_DRAW_BOTH, "Both", ""},
5193 {0, nullptr, 0, nullptr, nullptr},
5194 };
5195
5196 /* Identifiers. */
5197 ot->name = "Set Stroke Type";
5198 ot->idname = "GREASE_PENCIL_OT_set_stroke_type";
5199 ot->description = "Set the stroke type (stroke, fill, or both) of the selected strokes";
5200
5201 /* Callbacks. */
5204
5206
5207 ot->prop = RNA_def_enum(
5208 ot->srna, "type", prop_stroke_type_types, int(StrokeType::Stroke), "Type", "");
5210}
5211
5213
5214/* -------------------------------------------------------------------- */
5217
5218static Array<int> get_gapless_indices(const IndexRange &universe, const IndexMask &selected)
5219{
5220 const int first_curve = selected.first();
5221 Array<int> indices_data(universe.size());
5222 MutableSpan<int> indices = indices_data.as_mutable_span();
5223
5224 /*
5225 * Make the selected indices be in ascending order with the first indices staying at the same
5226 * place.
5227 *
5228 * Here's a diagram:
5229 *
5230 * Input
5231 * 0 1 2 3 4 5 6 7 8 9
5232 * ^ ^ ^
5233 *
5234 * |-A-| |-B-| |--C--|
5235 * 0 1 2 3 5 6 4 7 8 9
5236 * ^ ^ ^
5237 *
5238 * The `A` range gets filled with increasing indices starting at zero, ending at `first_curve`.
5239 * The `B` range gets filled with the selected indices starting at `first_curve`.
5240 * The `C` range gets filled with the unselected indices not including the `A` range.
5241 */
5242
5243 IndexMaskMemory memory;
5244 const IndexMask unselected = selected.complement(universe.drop_front(first_curve), memory);
5245
5246 /* Fill `A`. */
5247 array_utils::fill_index_range<int>(indices.take_front(first_curve));
5248 /* Fill `B`. */
5249 selected.to_indices(indices.drop_front(first_curve).take_front(selected.size()));
5250 /* Fill `C`. */
5251 unselected.to_indices(indices.take_back(unselected.size()));
5252
5253 return indices_data;
5254}
5255
5257{
5258 using namespace bke::greasepencil;
5259
5260 const Scene *scene = CTX_data_scene(C);
5261 Object *object = CTX_data_active_object(C);
5262 GreasePencil &grease_pencil = *blender::id_cast<GreasePencil *>(object->data);
5263
5264 const Vector<MutableDrawingInfo> drawings_src = retrieve_editable_drawings(*scene,
5266
5267 Drawing *drawing_dst;
5268 int num_drawings_selected = 0;
5269 int active_layer_index = 0;
5270 for (const MutableDrawingInfo &info : drawings_src) {
5271 IndexMaskMemory memory;
5272 const IndexMask base_selected_strokes =
5274 *object, info.drawing, info.layer_index, memory);
5275
5276 if (!base_selected_strokes.is_empty()) {
5277 drawing_dst = &info.drawing;
5278 active_layer_index = info.layer_index;
5279 num_drawings_selected++;
5280 }
5281 }
5282
5283 if (num_drawings_selected == 0) {
5284 return OPERATOR_FINISHED;
5285 }
5286
5287 /* Move the selected strokes to the active layer only when the stroke span across multiple
5288 * drawings. */
5289 if (num_drawings_selected > 1) {
5290 Layer *active_layer = grease_pencil.get_active_layer();
5291 if (!active_layer) {
5292 BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer");
5293 return OPERATOR_CANCELLED;
5294 }
5295 if (!active_layer->is_editable()) {
5296 BKE_report(op->reports, RPT_ERROR, "Active layer is not editable");
5297 return OPERATOR_CANCELLED;
5298 }
5299
5300 drawing_dst = grease_pencil.get_drawing_at(*active_layer, scene->r.cfra);
5301 int active_layer_index = *grease_pencil.get_layer_index(*active_layer);
5302
5303 for (const MutableDrawingInfo &info : drawings_src) {
5304 bke::CurvesGeometry &curves_src = info.drawing.strokes_for_write();
5305 IndexMaskMemory memory;
5306 const IndexMask base_selected_strokes =
5308 *object, info.drawing, info.layer_index, memory);
5309 if (base_selected_strokes.is_empty()) {
5310 continue;
5311 }
5312
5313 /* Skip moving the strokes already on the active layer. */
5314 if (info.layer_index == active_layer_index) {
5315 continue;
5316 }
5317
5319 base_selected_strokes, curves_src, bke::AttrDomain::Curve, memory);
5320
5321 bool is_key_inserted = false;
5322 bool has_active_key = false;
5323 if (active_layer->frames().is_empty()) {
5324 /* If the target layer doesn't have any keyframes, insert a new key at the current frame.
5325 */
5326 grease_pencil.insert_frame(*active_layer, scene->r.cfra);
5327 is_key_inserted = true;
5328 has_active_key = true;
5329 }
5330 else {
5331 has_active_key = ensure_active_keyframe(
5332 *scene, grease_pencil, *active_layer, false, is_key_inserted);
5333 }
5334
5335 if (has_active_key && is_key_inserted) {
5336 /* Move geometry to a new drawing in target layer. */
5337 Drawing &drawing_dst = *grease_pencil.get_drawing_at(*active_layer, info.frame_number);
5339 curves_src, selected_strokes, {});
5340
5341 curves_src.remove_curves(selected_strokes, {});
5342 drawing_dst.tag_topology_changed();
5343 }
5344 else if (Drawing *drawing_dst = grease_pencil.get_drawing_at(*active_layer,
5345 info.frame_number))
5346 {
5347 /* Append geometry to drawing in target layer. */
5349 curves_src, selected_strokes, {});
5350 Curves *selected_curves = bke::curves_new_nomain(std::move(selected_elems));
5351 Curves *layer_curves = bke::curves_new_nomain(std::move(drawing_dst->strokes_for_write()));
5352 std::array<bke::GeometrySet, 2> geometry_sets{
5353 bke::GeometrySet::from_curves(layer_curves),
5354 bke::GeometrySet::from_curves(selected_curves)};
5355 bke::GeometrySet joined = geometry::join_geometries(geometry_sets, {});
5356 drawing_dst->strokes_for_write() = std::move(
5357 joined.get_curves_for_write()->geometry.wrap());
5358
5359 curves_src.remove_curves(selected_strokes, {});
5360
5361 drawing_dst->tag_topology_changed();
5362 }
5363
5364 info.drawing.tag_topology_changed();
5365 }
5366 }
5367
5368 IndexMaskMemory memory;
5370 *object, *drawing_dst, active_layer_index, memory);
5371 if (base_selected_strokes.is_empty()) {
5372 return OPERATOR_CANCELLED;
5373 }
5375 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
5377 "fill_id", bke::AttrDomain::Curve);
5378
5380 base_selected_strokes, curves, bke::AttrDomain::Curve, memory);
5381
5382 /* Currently Grease Pencil does not have a active element, so instead just use the first. */
5383 const int active_curve = selected_strokes.first();
5384
5385 int fill_id_to_set = fill_ids.span[active_curve];
5386 if (fill_id_to_set == 0) {
5387 fill_id_to_set = bke::greasepencil::get_next_available_fill_id(fill_ids.span.varray());
5388 }
5389
5390 index_mask::masked_fill(fill_ids.span, fill_id_to_set, selected_strokes);
5391 fill_ids.finish();
5392
5393 Set<StringRef> attributes_to_set{{"material_index",
5394 "fill_color",
5395 "fill_opacity",
5396 "uv_rotation",
5397 "uv_translation",
5398 "uv_scale"}};
5399 /* Copy curve attributes from the active to all other selected curves. */
5400 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
5401 if (iter.domain != bke::AttrDomain::Curve) {
5402 return;
5403 }
5404 if (!attributes_to_set.contains(iter.name)) {
5405 return;
5406 }
5408 const CPPType &type = attribute.span.type();
5410 attribute.span[active_curve], attribute.span.data(), selected_strokes);
5411 attribute.finish();
5412 });
5413
5414 Array<int> indices = get_gapless_indices(curves.curves_range(), selected_strokes);
5416 drawing_dst->tag_topology_changed();
5417
5420
5421 return OPERATOR_FINISHED;
5422}
5423
5425{
5426 ot->name = "Join Fills";
5427 ot->idname = "GREASE_PENCIL_OT_join_fills";
5428 ot->description = "Join selected strokes into one fill to create holes";
5429
5432
5434}
5435
5437
5438/* -------------------------------------------------------------------- */
5441
5443{
5444 const Scene *scene = CTX_data_scene(C);
5445 Object *object = CTX_data_active_object(C);
5446 GreasePencil &grease_pencil = *blender::id_cast<GreasePencil *>(object->data);
5447
5448 const bool individual = RNA_boolean_get(op->ptr, "individual");
5449
5450 std::atomic<bool> changed = false;
5452 threading::parallel_for_each(drawings, [&](const MutableDrawingInfo &info) {
5453 IndexMaskMemory memory;
5455 *object, info.drawing, info.layer_index, memory);
5456 if (strokes.is_empty()) {
5457 return;
5458 }
5460 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
5461 bke::SpanAttributeWriter<int> fill_ids = attributes.lookup_for_write_span<int>("fill_id");
5462 if (!fill_ids) {
5463 return;
5464 }
5465
5466 /* Get the first id that does not already exist. Start at ID 1. */
5467 const int new_fill_id = std::max(
5468 *std::max_element(fill_ids.span.begin(), fill_ids.span.end()) + 1, 1);
5469 if (individual) {
5470 /* Each selected stroke becomes a new fill. */
5471 strokes.foreach_index_optimized<int>(
5472 [&](const int64_t i, const int64_t pos) { fill_ids.span[i] = pos + new_fill_id; },
5473 exec_mode::grain_size(4096));
5474 }
5475 else {
5476 /* All selected strokes become a new fill. */
5477 index_mask::masked_fill(fill_ids.span, new_fill_id, strokes);
5478 }
5479
5480 fill_ids.finish();
5482
5483 changed.store(true, std::memory_order_relaxed);
5484 });
5485
5486 if (changed) {
5489 }
5490
5491 return OPERATOR_FINISHED;
5492}
5493
5495{
5496 ot->name = "Separate Fills";
5497 ot->idname = "GREASE_PENCIL_OT_separate_fills";
5498 ot->description = "Separate the selected strokes from current fill";
5499
5502
5504
5506 "individual",
5507 true,
5508 "Individual",
5509 "Create a separate fill for each individual stroke");
5510}
5511
5513
5514} // namespace ed::greasepencil
5515
5517{
5518 using namespace blender::ed::greasepencil;
5562}
5563
5564/* -------------------------------------------------------------------- */
5567
5568namespace ed::greasepencil {
5569
5570/* Note: the `duplicate_layer` API would be nicer, but only supports duplicating groups from the
5571 * same datablock. */
5574 const bke::greasepencil::Layer &layer_src)
5575{
5576 using namespace blender::bke::greasepencil;
5577
5578 Layer &layer_dst = grease_pencil_dst.add_layer(group_dst, layer_src.name());
5579 BKE_grease_pencil_copy_layer_parameters(layer_src, layer_dst);
5580
5581 layer_dst.frames_for_write() = layer_src.frames();
5582 layer_dst.tag_frames_map_changed();
5583
5584 return layer_dst;
5585}
5586
5588 GreasePencil &grease_pencil_dst,
5590 const bke::greasepencil::LayerGroup &group_src,
5591 Map<StringRefNull, StringRefNull> &layer_name_map);
5592
5593static void copy_layer_group_content(GreasePencil &grease_pencil_dst,
5595 const bke::greasepencil::LayerGroup &group_src,
5596 Map<StringRefNull, StringRefNull> &layer_name_map)
5597{
5598 using namespace blender::bke::greasepencil;
5599
5600 for (GreasePencilLayerTreeNode &child : group_src.children) {
5601 switch (child.type) {
5602 case GP_LAYER_TREE_LEAF: {
5603 Layer &layer_src = reinterpret_cast<GreasePencilLayer *>(&child)->wrap();
5604 Layer &layer_dst = copy_layer(grease_pencil_dst, group_dst, layer_src);
5605 layer_name_map.add_new(layer_src.name(), layer_dst.name());
5606 break;
5607 }
5608 case GP_LAYER_TREE_GROUP: {
5609 LayerGroup &group_src = reinterpret_cast<GreasePencilLayerTreeGroup *>(&child)->wrap();
5610 copy_layer_group_recursive(grease_pencil_dst, group_dst, group_src, layer_name_map);
5611 break;
5612 }
5613 }
5614 }
5615}
5616
5618 GreasePencil &grease_pencil_dst,
5620 const bke::greasepencil::LayerGroup &group_src,
5621 Map<StringRefNull, StringRefNull> &layer_name_map)
5622{
5623 bke::greasepencil::LayerGroup &group_dst = grease_pencil_dst.add_layer_group(
5624 parent_dst, group_src.base.name);
5626
5627 copy_layer_group_content(grease_pencil_dst, group_dst, group_src, layer_name_map);
5628 return group_dst;
5629}
5630
5632{
5633 BLI_assert(object.type == OB_GREASE_PENCIL);
5634 Array<int> material_index_map(*BKE_object_material_len_p(&object));
5635 for (const int i : material_index_map.index_range()) {
5636 Material *material = BKE_object_material_get(&object, i + 1);
5637 if (material != nullptr) {
5638 material_index_map[i] = materials.index_of_or_add(material);
5639 }
5640 else {
5641 material_index_map[i] = 0;
5642 }
5643 }
5644 return material_index_map;
5645}
5646
5648 const Span<int> material_index_map)
5649{
5651 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
5652 /* Validate material indices and add missing materials. */
5653 bke::SpanAttributeWriter<int> material_writer = attributes.lookup_or_add_for_write_span<int>(
5654 "material_index", bke::AttrDomain::Curve);
5655 threading::parallel_for(curves.curves_range(), 1024, [&](const IndexRange range) {
5656 for (const int curve_i : range) {
5657 material_writer.span[curve_i] = material_index_map[material_writer.span[curve_i]];
5658 }
5659 });
5660 material_writer.finish();
5661}
5662
5664 Object &object, GreasePencil &grease_pencil, const ListBaseT<bDeformGroup> &vertex_group_names)
5665{
5666 Map<StringRefNull, StringRefNull> vertex_group_map;
5667 for (bDeformGroup &dg : vertex_group_names) {
5668 bDeformGroup *vgroup = MEM_dupalloc(&dg);
5669 BKE_object_defgroup_unique_name(vgroup, &object);
5670 BLI_addtail(&grease_pencil.vertex_group_names, vgroup);
5671 vertex_group_map.add_new(dg.name, vgroup->name);
5672 }
5673 return vertex_group_map;
5674}
5675
5677 const Map<StringRefNull, StringRefNull> &vertex_group_map)
5678{
5679 for (bDeformGroup &dg : drawing.strokes_for_write().vertex_group_names) {
5680 STRNCPY_UTF8(dg.name, vertex_group_map.lookup(dg.name).c_str());
5681 }
5682
5683 /* Indices in vertex weights remain valid, they are local to the drawing's vertex groups.
5684 * Only the names of the groups change. */
5685}
5686
5689 const int dst_size)
5690{
5692 const auto add_or_upgrade_types = [&](const bke::AttributeAccessor &attributes) {
5693 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
5694 new_types.add_or_modify(
5695 iter.name,
5696 [&](bke::AttrType *value) { *value = iter.data_type; },
5697 [&](bke::AttrType *value) {
5698 *value = bke::attribute_data_type_highest_complexity({*value, iter.data_type});
5699 });
5700 });
5701 };
5702 add_or_upgrade_types(a);
5703 add_or_upgrade_types(b);
5704 const int64_t domain_size_a = a.domain_size(bke::AttrDomain::Layer);
5705
5706 bke::AttributeStorage new_storage;
5707 for (const auto &[name, type] : new_types.items()) {
5708 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(type);
5709 auto new_data = bke::Attribute::ArrayData::from_uninitialized(cpp_type, dst_size);
5710
5711 const GVArray data_a = *a.lookup_or_default(name, bke::AttrDomain::Layer, type);
5712 data_a.materialize_to_uninitialized(new_data.data);
5713
5714 const GVArray data_b = *b.lookup_or_default(name, bke::AttrDomain::Layer, type);
5716 POINTER_OFFSET(new_data.data, cpp_type.size * domain_size_a));
5717
5718 new_storage.add(name, bke::AttrDomain::Layer, type, std::move(new_data));
5719 }
5720
5721 return new_storage;
5722}
5723
5725 Object &ob_src,
5726 Object &ob_dst,
5727 VectorSet<Material *> &materials)
5728{
5729 using namespace blender::bke::greasepencil;
5730
5731 /* Skip if the datablock is already used by the active object. */
5732 if (ob_src.data == ob_dst.data) {
5733 return;
5734 }
5735
5736 BLI_assert(ob_src.type == OB_GREASE_PENCIL);
5737 BLI_assert(ob_dst.type == OB_GREASE_PENCIL);
5738 GreasePencil &grease_pencil_src = *id_cast<GreasePencil *>(ob_src.data);
5739 GreasePencil &grease_pencil_dst = *id_cast<GreasePencil *>(ob_dst.data);
5740 /* Number of existing layers that don't need to be updated. */
5741 const int orig_layers_num = grease_pencil_dst.layers().size();
5742
5744 ob_dst, grease_pencil_dst, grease_pencil_src.vertex_group_names);
5745 const Array<int> material_index_map = add_materials_to_map(ob_src, materials);
5746
5747 /* Concatenate drawing arrays. Existing drawings in dst keep their position, new drawings are
5748 * mapped to the new index range. */
5749 const int new_drawing_array_num = grease_pencil_dst.drawing_array_num +
5750 grease_pencil_src.drawing_array_num;
5751 GreasePencilDrawingBase **new_drawing_array =
5752 MEM_new_array_uninitialized<GreasePencilDrawingBase *>(new_drawing_array_num, __func__);
5753 MutableSpan<GreasePencilDrawingBase *> new_drawings = {new_drawing_array, new_drawing_array_num};
5754 const IndexRange new_drawings_dst = IndexRange::from_begin_size(
5755 0, grease_pencil_dst.drawing_array_num);
5756 const IndexRange new_drawings_src = IndexRange::from_begin_size(
5757 grease_pencil_dst.drawing_array_num, grease_pencil_src.drawing_array_num);
5758
5759 copy_drawing_array(grease_pencil_dst.drawings(), new_drawings.slice(new_drawings_dst));
5760 copy_drawing_array(grease_pencil_src.drawings(), new_drawings.slice(new_drawings_src));
5761
5762 /* Free existing drawings array. */
5763 grease_pencil_dst.resize_drawings(0);
5764 grease_pencil_dst.drawing_array = new_drawing_array;
5765 grease_pencil_dst.drawing_array_num = new_drawing_array_num;
5766
5767 /* Maps original names of source layers to new unique layer names. */
5768 Map<StringRefNull, StringRefNull> layer_name_map;
5769 /* Only copy the content of the root group, not the root node itself. */
5770 copy_layer_group_content(grease_pencil_dst,
5771 grease_pencil_dst.root_group(),
5772 grease_pencil_src.root_group(),
5773 layer_name_map);
5774
5775 grease_pencil_dst.attribute_storage.wrap() = merge_attributes(grease_pencil_src.attributes(),
5776 grease_pencil_dst.attributes(),
5777 grease_pencil_dst.layers().size());
5778
5779 /* Fix names, indices and transforms to keep relationships valid. */
5780 for (const int layer_index : grease_pencil_dst.layers().index_range()) {
5781 Layer &layer = *grease_pencil_dst.layers_for_write()[layer_index];
5782 const bool is_orig_layer = (layer_index < orig_layers_num);
5783 const float4x4 old_layer_to_world = (is_orig_layer ? layer.to_world_space(ob_dst) :
5784 layer.to_world_space(ob_src));
5785
5786 /* Update newly added layers. */
5787 if (!is_orig_layer) {
5788 /* Update name references for masks. */
5789 for (GreasePencilLayerMask &dst_mask : layer.masks) {
5790 const StringRefNull *new_mask_name = layer_name_map.lookup_ptr(dst_mask.layer_name);
5791 if (new_mask_name) {
5792 MEM_SAFE_DELETE(dst_mask.layer_name);
5793 dst_mask.layer_name = BLI_strdup(new_mask_name->c_str());
5794 }
5795 }
5796 /* Shift drawing indices to match the new drawings array. */
5797 for (const int key : layer.frames_for_write().keys()) {
5798 int &drawing_index = layer.frames_for_write().lookup(key).drawing_index;
5799 drawing_index = new_drawings_src[drawing_index];
5800 }
5801 }
5802
5803 /* Layer parent object may become invalid. This can be an original layer pointing at the joined
5804 * object which gets destroyed, or a new layer that points at the target object which is now
5805 * its owner. */
5806 if (ELEM(layer.parent, &ob_dst, &ob_src)) {
5807 layer.parent = nullptr;
5808 }
5809
5810 /* Apply relative object transform to new drawings to keep world-space positions unchanged.
5811 * Be careful where the matrix is computed: changing the parent pointer (above) can affect
5812 * this! */
5813 const float4x4 new_layer_to_world = layer.to_world_space(ob_dst);
5814 for (const int key : layer.frames_for_write().keys()) {
5815 const int drawing_index = layer.frames_for_write().lookup(key).drawing_index;
5816 GreasePencilDrawingBase *drawing_base = grease_pencil_dst.drawings()[drawing_index];
5817 if (drawing_base->type != GP_DRAWING) {
5818 continue;
5819 }
5820 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base)->wrap();
5822 curves.transform(math::invert(new_layer_to_world) * old_layer_to_world);
5823
5824 if (!is_orig_layer) {
5825 remap_vertex_groups(drawing, vertex_group_map);
5826 remap_material_indices(drawing, material_index_map);
5827 }
5828 }
5829 }
5830
5831 /* Rename animation paths to layers. */
5832 BKE_fcurves_main_cb(&bmain, [&](ID *id, FCurve *fcu) {
5833 const StringRefNull rna_path = fcu->rna_path();
5834 if (id == &grease_pencil_src.id && strstr(rna_path.c_str(), "layers[")) {
5835 /* Have to use linear search, the layer name map only contains sub-strings of RNA paths. */
5836 for (auto [name_src, name_dst] : layer_name_map.items()) {
5837 if (name_dst != name_src) {
5838 if (char *new_path = BKE_animsys_fix_rna_path_rename(
5839 id, rna_path.c_str(), "layers", name_src, name_dst))
5840 {
5841 fcu->rna_path_set_move(new_path);
5842 /* Stop after first match. */
5843 break;
5844 }
5845 }
5846 }
5847 }
5848 /* Fix driver targets. */
5849 if (fcu->driver) {
5850 for (DriverVar &dvar : fcu->driver->variables) {
5851 /* Only change the used targets, since the others will need fixing manually anyway. */
5853 if (dtar->id != &grease_pencil_src.id) {
5854 continue;
5855 }
5856 dtar->id = &grease_pencil_dst.id;
5857
5858 if (dtar->rna_path && strstr(dtar->rna_path, "layers[")) {
5859 for (auto [name_src, name_dst] : layer_name_map.items()) {
5860 if (name_dst != name_src) {
5861 if (char *new_path = BKE_animsys_fix_rna_path_rename(
5862 id, dtar->rna_path, "layers", name_src, name_dst))
5863 {
5864 MEM_delete(dtar->rna_path);
5865 dtar->rna_path = new_path;
5866 break;
5867 }
5868 }
5869 }
5870 }
5871 }
5873 }
5874 }
5875 });
5876
5877 /* Merge animation data of objects and grease pencil datablocks. */
5878 if (ob_src.adt) {
5879 if (ob_dst.adt == nullptr) {
5880 ob_dst.adt = BKE_animdata_copy(&bmain, ob_src.adt, 0);
5881 }
5882 else {
5883 BKE_animdata_merge_copy(&bmain, &ob_dst.id, &ob_src.id, ADT_MERGECOPY_KEEP_DST, false);
5884 }
5885
5886 if (ob_dst.adt->action) {
5888 }
5889 }
5890 if (grease_pencil_src.adt) {
5891 if (grease_pencil_dst.adt == nullptr) {
5892 grease_pencil_dst.adt = BKE_animdata_copy(&bmain, grease_pencil_src.adt, 0);
5893 }
5894 else {
5896 &bmain, &grease_pencil_dst.id, &grease_pencil_src.id, ADT_MERGECOPY_KEEP_DST, false);
5897 }
5898
5899 if (grease_pencil_dst.adt->action) {
5900 DEG_id_tag_update(&grease_pencil_dst.adt->action->id, ID_RECALC_ANIMATION_NO_FLUSH);
5901 }
5902 }
5903}
5904
5905} // namespace ed::greasepencil
5906
5908{
5909 Main *bmain = CTX_data_main(C);
5910 Scene *scene = CTX_data_scene(C);
5911 Object *ob_active = CTX_data_active_object(C);
5912
5913 /* Ensure we're in right mode and that the active object is correct. */
5914 if (!ob_active || ob_active->type != OB_GREASE_PENCIL) {
5915 return OPERATOR_CANCELLED;
5916 }
5917
5918 bool ok = false;
5919 CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects) {
5920 if (ob_iter == ob_active) {
5921 ok = true;
5922 break;
5923 }
5924 }
5926 /* Active object must always selected. */
5927 if (ok == false) {
5928 BKE_report(op->reports, RPT_WARNING, "Active object is not a selected Grease Pencil");
5929 return OPERATOR_CANCELLED;
5930 }
5931
5932 Object *ob_dst = ob_active;
5933 GreasePencil *grease_pencil_dst = id_cast<GreasePencil *>(ob_dst->data);
5934
5935 VectorSet<Material *> materials;
5936 Array<int> material_index_map = ed::greasepencil::add_materials_to_map(*ob_dst, materials);
5937 /* Reassign material indices in the original layers, in case materials are deduplicated. */
5938 for (GreasePencilDrawingBase *drawing_base : grease_pencil_dst->drawings()) {
5939 if (drawing_base->type != GP_DRAWING) {
5940 continue;
5941 }
5943 reinterpret_cast<GreasePencilDrawing *>(drawing_base)->wrap();
5944 ed::greasepencil::remap_material_indices(drawing, material_index_map);
5945 }
5946
5947 /* Loop and join all data. */
5948 CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects) {
5949 if (ob_iter->type != OB_GREASE_PENCIL || ob_iter == ob_active) {
5950 continue;
5951 }
5952
5953 ed::greasepencil::join_object_with_active(*bmain, *ob_iter, *ob_dst, materials);
5954
5955 /* Free the old object. */
5956 ed::object::base_free_and_unlink(bmain, scene, ob_iter);
5957 }
5959
5960 /* Transfer material pointers. The material indices are updated for each drawing separately. */
5961 if (!materials.is_empty()) {
5962 /* Old C API, needs a const_cast but doesn't actually change anything. */
5963 Material **materials_ptr = const_cast<Material **>(materials.data());
5965 bmain, DEG_get_original(ob_dst), &materials_ptr, materials.size(), false);
5966 }
5967
5968 DEG_id_tag_update(&grease_pencil_dst->id, ID_RECALC_GEOMETRY);
5970
5973
5974 return OPERATOR_FINISHED;
5975}
5976
5978
5979} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
#define DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
#define DRIVER_TARGETS_LOOPER_END
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define INT16_MAX
#define DEG2RADF(_deg)
#define M_PI
#define STRNCPY_UTF8(dst, src)
#define TIP_(msgid)
#define BLT_I18NCONTEXT_ID_GPENCIL
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_MOVIECLIP
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define GP_STROKE_MITER_ANGLE_BEVEL
#define GP_STROKE_MITER_ANGLE_ROUND
Object is a sort of wrapper for general info.
#define RNA_ENUM_ITEM_SEPR
Definition RNA_types.hh:762
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DRAW
Definition WM_types.hh:502
#define ND_OB_ACTIVE
Definition WM_types.hh:459
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define ND_LAYER_CONTENT
Definition WM_types.hh:472
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
#define ND_SPACE_VIEW3D
Definition WM_types.hh:571
#define NC_OBJECT
Definition WM_types.hh:392
#define NC_SPACE
Definition WM_types.hh:405
volatile int lock
#define BLI_assert_unreachable()
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define A
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
bool is_empty() const
Definition BLI_array.hh:275
void fill_assign_indices(const void *value, void *dst, const IndexMask &mask) const
void copy_from(GSpan values)
void materialize_to_uninitialized(void *dst) const
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
static IndexMask from_ranges(OffsetIndices< T > offsets, const IndexMask &mask, LinearAllocator<> &memory)
static IndexMask from_union(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr int64_t start() const
constexpr bool contains(int64_t value) const
constexpr IndexRange drop_front(int64_t n) const
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
int64_t size() const
Definition BLI_map.hh:976
bool is_empty() const
Definition BLI_map.hh:986
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
Definition BLI_map.hh:481
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:624
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
void materialize(MutableSpan< T > r_span) const
static VArray from_single(T value, const int64_t size)
static VArray from_span(Span< T > values)
static VArray from_container(ContainerT container)
const Key * data() const
int64_t index_of_or_add(const Key &key)
int64_t size() const
int64_t size() const
void append(const T &value)
void insert(const int64_t insert_index, const T &value)
MutableSpan< T > as_mutable_span()
Span< T > as_span() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
Attribute & add(std::string name, bke::AttrDomain domain, bke::AttrType data_type, Attribute::DataVariant data)
VArray< int8_t > curve_types() const
void remove_points(const IndexMask &points_to_delete, const AttributeFilter &attribute_filter)
VArray< bool > cyclic() const
AttributeAccessor attributes() const
IndexRange curves_range() const
VArray< int8_t > nurbs_knots_modes() const
MutableSpan< int > offsets_for_write()
MutableSpan< bool > cyclic_for_write()
void remove_curves(const IndexMask &curves_to_delete, const AttributeFilter &attribute_filter)
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef name)
Span< float3 > curve_plane_normals() const
MutableSpan< float > opacities_for_write()
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void set_texture_matrices(Span< float4x2 > matrices, const IndexMask &selection)
const Map< FramesMapKeyT, GreasePencilFrame > & frames() const
float4x4 to_object_space(const Object &object) const
Map< FramesMapKeyT, GreasePencilFrame > & frames_for_write()
const LayerGroup & as_group() const
constexpr bool contains(const T &value) const
void foreach_index_optimized(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask slice_content(IndexRange range) const
void to_indices(MutableSpan< T > r_indices) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
CCL_NAMESPACE_BEGIN struct Options options
#define INT_MAX
#define FLAT(type, name)
#define position
#define attribute(slot)
#define layer
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define B
void invert_booleans(MutableSpan< bool > span)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
BooleanMix booleans_mix_calc(const VArray< bool > &varray, IndexRange range_to_check)
void fill_index_range(MutableSpan< T > span, const T start=0)
void gather_custom_knots(const bke::CurvesGeometry &src, const IndexMask &src_curves, int dst_curve_offset, bke::CurvesGeometry &dst)
void update_custom_knot_modes(const IndexMask &mask, const KnotsMode mode_for_regular, const KnotsMode mode_for_cyclic, bke::CurvesGeometry &curves)
IndexMask curve_type_point_selection(const bke::CurvesGeometry &curves, CurveType curve_type, LinearAllocator<> &memory)
void fill_points(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, GPointer value, GMutableSpan dst)
IndexMask selected_mask_to_fills(const IndexMask &selected_mask, const CurvesGeometry &curves, AttrDomain selection_domain, IndexMaskMemory &memory)
int get_next_available_fill_id(Span< int > fill_ids)
void gather_next_available_fill_ids(const VArray< int > &fill_ids, MutableSpan< int > r_new_fill_ids)
void separate_fill_ids(CurvesGeometry &curves, const IndexMask &strokes_to_keep)
void copy_drawing_array(Span< const GreasePencilDrawingBase * > src_drawings, MutableSpan< GreasePencilDrawingBase * > dst_drawings)
CurvesGeometry curves_copy_curve_selection(const CurvesGeometry &curves, const IndexMask &curves_to_copy, const AttributeFilter &attribute_filter)
CurvesGeometry curves_copy_point_selection(const CurvesGeometry &curves, const IndexMask &points_to_copy, const AttributeFilter &attribute_filter)
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
const CPPType & attribute_type_to_cpp_type(AttrType type)
Curves * curves_new_nomain(int points_num, int curves_num)
bool has_anything_selected(const VArray< bool > &varray, const IndexRange range_to_check)
const EnumPropertyItem rna_enum_set_handle_type_items[]
IndexMask retrieve_selected_points(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
void duplicate_curves(bke::CurvesGeometry &curves, const IndexMask &mask)
void duplicate_points(bke::CurvesGeometry &curves, const IndexMask &mask)
bke::CurvesGeometry split_points(const bke::CurvesGeometry &curves, const IndexMask &points_to_split)
void fill_selection_false(GMutableSpan selection)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
Span< StringRef > get_curves_selection_attribute_names(const bke::CurvesGeometry &curves)
bke::GSpanAttributeWriter ensure_selection_attribute(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, bke::AttrType create_type, StringRef attribute_name)
static void convert_to_nurbs(bke::CurvesGeometry &curves, const IndexMask &selection, const float threshold)
IndexMask retrieve_editable_and_selected_elements(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, const bke::AttrDomain selection_domain, IndexMaskMemory &memory)
static void GREASE_PENCIL_OT_extrude(wmOperatorType *ot)
static bke::GeometrySet join_geometries_with_transforms(Span< bke::GeometrySet > geometries, const VArray< float4x4 > &transforms)
static void GREASE_PENCIL_OT_texture_gradient(wmOperatorType *ot)
bool active_grease_pencil_poll(bContext *C)
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_set_corner_type_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_convert_curve_type_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_delete_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_set_active_material_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_caps_set(wmOperatorType *ot)
bke::CurvesGeometry curves_merge_by_distance(const bke::CurvesGeometry &src_curves, const float merge_distance, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
static bke::greasepencil::Layer & copy_layer(GreasePencil &grease_pencil_dst, bke::greasepencil::LayerGroup &group_dst, const bke::greasepencil::Layer &layer_src)
static void grease_pencil_reproject_ui(bContext *, wmOperator *op)
static bke::greasepencil::Layer & find_or_create_layer_in_dst_by_name(const int layer_index, const GreasePencil &grease_pencil_src, GreasePencil &grease_pencil_dst, Vector< int > &src_to_dst_layer_indices)
static wmOperatorStatus grease_pencil_stroke_material_set_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_stroke_simplify(wmOperatorType *ot)
static void convert_to_catmull_rom(bke::CurvesGeometry &curves, const IndexMask &selection, const float threshold)
static void GREASE_PENCIL_OT_separate_fills(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_set_curve_type_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_extrude_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_outline_exec(bContext *C, wmOperator *op)
static void join_object_with_active(Main &bmain, Object &ob_src, Object &ob_dst, VectorSet< Material * > &materials)
IndexMask retrieve_editable_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_stroke_smooth_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool ensure_active_keyframe(const Scene &scene, GreasePencil &grease_pencil, bke::greasepencil::Layer &layer, const bool duplicate_previous_key, bool &r_inserted_keyframe)
static void convert_to_bezier(bke::CurvesGeometry &curves, const IndexMask &selection, const float threshold)
static wmOperatorStatus grease_pencil_duplicate_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_set_stroke_type_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_dissolve_exec(bContext *C, wmOperator *op)
static void convert_to_poly(bke::CurvesGeometry &curves, const IndexMask &selection)
static bke::CurvesGeometry set_start_point(const bke::CurvesGeometry &curves, const IndexMask &mask)
static void GREASE_PENCIL_OT_set_stroke_type(wmOperatorType *ot)
static float calculate_radius_projection_factor(const RegionView3D *rv3d, const float3 &old_pos, const float3 &new_pos)
static wmOperatorStatus grease_pencil_separate_fills_exec(bContext *C, wmOperator *op)
static bke::AttributeStorage merge_attributes(const bke::AttributeAccessor &a, const bke::AttributeAccessor &b, const int dst_size)
static wmOperatorStatus grease_pencil_texture_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void GREASE_PENCIL_OT_set_curve_type(wmOperatorType *ot)
static bool remove_stroke_or_fill_based_on_mode(Object &object, bke::greasepencil::Drawing &drawing, const int layer_index, const DeleteMode mode, IndexMaskMemory &memory)
static void toggle_caps(MutableSpan< int8_t > caps, const IndexMask &strokes)
static void GREASE_PENCIL_OT_snap_cursor_to_selected(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_copy_strokes_exec(bContext *C, wmOperator *op)
static void grease_pencil_convert_curve_type_ui(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_outline_modes[]
static Array< int > clipboard_materials_remap(Main &bmain, Object &object)
static wmOperatorStatus grease_pencil_join_fills_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_stroke_merge_by_distance_exec(bContext *C, wmOperator *op)
static bool grease_pencil_paste_strokes_poll(bContext *C)
bool editable_grease_pencil_point_selection_poll(bContext *C)
static bke::CurvesGeometry extrude_grease_pencil_curves(const bke::CurvesGeometry &src, const IndexMask &points_to_extrude)
static void GREASE_PENCIL_OT_set_start_point(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_reproject_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_delete(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_stroke_switch_direction_exec(bContext *C, wmOperator *)
static IndexMask simplify_fixed(const bke::CurvesGeometry &curves, const int step, const IndexMask &stroke_selection, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings_from_layer(const Scene &scene, GreasePencil &grease_pencil, const bke::greasepencil::Layer &layer)
bool editable_grease_pencil_with_region_view3d_poll(bContext *C)
static wmOperatorStatus grease_pencil_snap_to_cursor_exec(bContext *C, wmOperator *op)
static bool grease_pencil_separate_material(bContext &C, Main &bmain, Scene &scene, ViewLayer &view_layer, Base &base_prev, Object &object_src)
static IndexRange clipboard_paste_strokes_ex(Main &bmain, Object &object, const bke::CurvesGeometry &curves_to_paste, const float4x4 &object_to_paste_layer, const float4x4 &clipboard_to_world, const bool keep_world_transform, const bool paste_back, bke::greasepencil::Drawing &drawing)
static wmOperatorStatus grease_pencil_set_handle_type_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_stroke_switch_direction(wmOperatorType *ot)
Vector< DrawingInfo > retrieve_visible_drawings(const Scene &scene, const GreasePencil &grease_pencil, const bool do_onion_skinning)
static void GREASE_PENCIL_OT_set_active_material(wmOperatorType *ot)
static Clipboard & ensure_grease_pencil_clipboard()
static void GREASE_PENCIL_OT_outline(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_set_material_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_simplify_modes[]
static void remap_material_indices(bke::greasepencil::Drawing &drawing, const Span< int > material_index_map)
IndexMask retrieve_editable_strokes_by_material(Object &object, const bke::greasepencil::Drawing &drawing, const int mat_i, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_reset_uvs_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_clean_loose_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_set_curve_resolution_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_separate_modes[]
static void GREASE_PENCIL_OT_copy(wmOperatorType *ot)
static void GREASE_PENCIL_OT_separate(wmOperatorType *ot)
static Array< int > get_gapless_indices(const IndexRange &universe, const IndexMask &selected)
static bool grease_pencil_separate_layer(bContext &C, Main &bmain, Scene &scene, ViewLayer &view_layer, Base &base_prev, Object &object_src)
static bool remove_curves_based_on_mode(Object &object, bke::greasepencil::Drawing &drawing, const int layer_index, const DeleteMode mode, IndexMaskMemory &memory)
static void GREASE_PENCIL_OT_delete_frame(wmOperatorType *ot)
static bke::greasepencil::LayerGroup & copy_layer_group_recursive(GreasePencil &grease_pencil_dst, bke::greasepencil::LayerGroup &parent_dst, const bke::greasepencil::LayerGroup &group_src, Map< StringRefNull, StringRefNull > &layer_name_map)
static void GREASE_PENCIL_OT_set_curve_resolution(wmOperatorType *ot)
static float4x3 expand_4x2_mat(float4x2 strokemat)
static wmOperatorStatus grease_pencil_cyclical_set_exec(bContext *C, wmOperator *op)
bool editable_grease_pencil_poll(bContext *C)
static void remap_vertex_groups(bke::greasepencil::Drawing &drawing, const Map< StringRefNull, StringRefNull > &vertex_group_map)
static const EnumPropertyItem prop_corner_types[]
static wmOperatorStatus grease_pencil_set_start_point_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_set_uniform_opacity(wmOperatorType *ot)
static Array< int > add_materials_to_map(Object &object, VectorSet< Material * > &materials)
static void GREASE_PENCIL_OT_move_to_layer(wmOperatorType *ot)
static Map< StringRefNull, StringRefNull > add_vertex_groups(Object &object, GreasePencil &grease_pencil, const ListBaseT< bDeformGroup > &vertex_group_names)
static void GREASE_PENCIL_OT_snap_to_grid(wmOperatorType *ot)
IndexMask retrieve_editable_and_all_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const eHandleDisplay handle_display, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_remove_fill_guides_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_clean_loose(wmOperatorType *ot)
static void GREASE_PENCIL_OT_stroke_smooth(wmOperatorType *ot)
IndexMask retrieve_editable_and_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_move_to_layer_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_remove_fill_guides(wmOperatorType *ot)
static void GREASE_PENCIL_OT_stroke_split(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_snap_cursor_to_sel_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_set_handle_type(wmOperatorType *ot)
static void GREASE_PENCIL_OT_stroke_reorder(wmOperatorType *ot)
static void GREASE_PENCIL_OT_stroke_merge_by_distance(wmOperatorType *ot)
static bool grease_pencil_snap_compute_centroid(const Scene &scene, const Object &object, const GreasePencil &grease_pencil, float3 &r_centroid, float3 &r_min, float3 &r_max)
static const EnumPropertyItem prop_dissolve_types[]
static wmOperatorStatus grease_pencil_set_uniform_opacity_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_clean_loose_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void GREASE_PENCIL_OT_stroke_subdivide(wmOperatorType *ot)
static bool grease_pencil_snap_poll(bContext *C)
static wmOperatorStatus grease_pencil_caps_set_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_set_corner_type(wmOperatorType *ot)
static wmOperatorStatus gpencil_stroke_subdivide_exec(bContext *C, wmOperator *op)
IndexRange paste_all_strokes_from_clipboard(Main &bmain, Object &object, const float4x4 &object_to_paste_layer, const bool keep_world_transform, const bool paste_back, bke::greasepencil::Drawing &drawing)
static wmOperatorStatus grease_pencil_paste_strokes_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_reproject(wmOperatorType *ot)
static bke::CurvesGeometry fit_poly_curves(bke::CurvesGeometry &curves, const IndexMask &selection, const float threshold)
static void copy_layer_group_content(GreasePencil &grease_pencil_dst, bke::greasepencil::LayerGroup &group_dst, const bke::greasepencil::LayerGroup &group_src, Map< StringRefNull, StringRefNull > &layer_name_map)
static wmOperatorStatus grease_pencil_texture_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void GREASE_PENCIL_OT_convert_curve_type(wmOperatorType *ot)
eHandleDisplay view3d_handle_type_or_default(const View3D *v3d)
static wmOperatorStatus grease_pencil_snap_to_grid_exec(bContext *C, wmOperator *)
static void GREASE_PENCIL_OT_snap_to_cursor(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_set_uniform_thickness_exec(bContext *C, wmOperator *op)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
bool remove_fill_guides(bke::CurvesGeometry &curves)
static wmOperatorStatus grease_pencil_stroke_split_exec(bContext *C, wmOperator *)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
static struct blender::ed::greasepencil::Clipboard * grease_pencil_clipboard
static void GREASE_PENCIL_OT_stroke_material_set(wmOperatorType *ot)
static void GREASE_PENCIL_OT_set_uniform_thickness(wmOperatorType *ot)
static void GREASE_PENCIL_OT_reset_uvs(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_texture_gradient_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_duplicate(wmOperatorType *ot)
static const EnumPropertyItem * material_enum_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
static Object * duplicate_grease_pencil_object(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_prev, const GreasePencil &grease_pencil_src)
static void GREASE_PENCIL_OT_paste(wmOperatorType *ot)
static bke::GeometrySet join_geometries_with_transform(Span< bke::GeometrySet > geometries, const float4x4 &transform)
static void GREASE_PENCIL_OT_cyclical_set(wmOperatorType *ot)
Array< Vector< MutableDrawingInfo > > retrieve_editable_drawings_grouped_per_frame(const Scene &scene, GreasePencil &grease_pencil)
static wmOperatorStatus grease_pencil_stroke_simplify_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_cyclical_types[]
static wmOperatorStatus grease_pencil_delete_frame_exec(bContext *C, wmOperator *op)
static void grease_pencil_set_corner_type_ui(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_dissolve(wmOperatorType *ot)
bool active_grease_pencil_layer_poll(bContext *C)
static wmOperatorStatus grease_pencil_separate_exec(bContext *C, wmOperator *op)
static bool grease_pencil_separate_selected(bContext &C, Main &bmain, Scene &scene, ViewLayer &view_layer, Base &base_prev, Object &object_src)
bke::CurvesGeometry create_curves_outline(const bke::greasepencil::Drawing &drawing, const IndexMask &strokes, const float4x4 &transform, const int corner_subdivisions, const float outline_radius, const float outline_offset, const int material_index)
static wmOperatorStatus grease_pencil_stroke_reorder_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_greasepencil_deleteframe_types[]
static void grease_pencil_simplify_ui(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_set_material(wmOperatorType *ot)
static Array< bool > get_points_to_dissolve(bke::CurvesGeometry &curves, const IndexMask &mask, const DissolveMode mode)
static Array< int > get_reordered_indices(const IndexRange universe, const IndexMask &selected, const ReorderDirection direction)
static bke::CurvesGeometry subdivide_last_segment(const bke::CurvesGeometry &curves, const IndexMask &strokes)
static void GREASE_PENCIL_OT_join_fills(wmOperatorType *ot)
Base * add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, eDupli_ID_Flags dupflag)
void base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
void snap_object_context_destroy(SnapObjectContext *sctx)
SnapObjectContext * snap_object_context_create()
bool snap_object_project_ray(SnapObjectContext *sctx, Depsgraph *depsgraph, const View3D *v3d, const SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, float r_co[3], float r_no[3])
constexpr ParallelGrainSize grain_size(int grain_size)
bke::GeometrySet join_geometries(Span< bke::GeometrySet > geometries, const bke::AttributeFilter &attribute_filter, const std::optional< Span< bke::GeometryComponent::Type > > &component_types_to_join=std::nullopt, bool allow_merging_instance_references=true)
bke::CurvesGeometry reorder_curves_geometry(const bke::CurvesGeometry &src_curves, Span< int > old_by_new_map, const bke::AttributeFilter &attribute_filter)
Definition reorder.cc:368
void smooth_curve_attribute(const IndexMask &curves_to_smooth, const OffsetIndices< int > points_by_curve, const VArray< bool > &point_selection, const VArray< bool > &cyclic, int iterations, float influence, bool smooth_ends, bool keep_shape, GMutableSpan attribute_data)
IndexMask simplify_curve_attribute(const Span< float3 > positions, const IndexMask &curves_selection, const OffsetIndices< int > points_by_curve, const VArray< bool > &cyclic, float epsilon, GSpan attribute_data, IndexMaskMemory &memory)
bke::CurvesGeometry grease_pencil_remove_points_and_split(const bke::CurvesGeometry &curves, const IndexMask &mask)
bke::CurvesGeometry fit_poly_to_bezier_curves(const bke::CurvesGeometry &src_curves, const IndexMask &curve_selection, const VArray< float > &thresholds, const VArray< bool > &corners, FitMethod method, const bke::AttributeFilter &attribute_filter)
Definition fit_curves.cc:25
CurvesGeometry resample_to_evaluated(const CurvesGeometry &src_curves, const IndexMask &selection, const ResampleCurvesOutputAttributeIDs &output_ids={})
CurvesGeometry resample_to_length(const CurvesGeometry &src_curves, const IndexMask &selection, const VArray< float > &sample_lengths, const ResampleCurvesOutputAttributeIDs &output_ids={}, bool keep_last_segment=false)
bke::CurvesGeometry subdivide_curves(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const VArray< int > &cuts, const bke::AttributeFilter &attribute_filter={})
bke::CurvesGeometry convert_curves(const bke::CurvesGeometry &src_curves, const IndexMask &selection, CurveType dst_type, const bke::AttributeFilter &attribute_filter, const ConvertCurvesOptions &options={})
BLI_INLINE Texture * wrap(MTLTexture *texture)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
AngleRadianBase< float > AngleRadian
T length_squared(const VecBase< T, Size > &a)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T pow(const T &x, const T &power)
T floor(const T &a)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
CartesianBasis invert(const CartesianBasis &basis)
T midpoint(const T &a, const T &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
void min_max(const T &value, T &min, T &max)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
MatT from_rotation(const RotationT &rotation)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void copy_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
void BKE_object_defgroup_unique_name(bDeformGroup *dg, Object *ob)
Definition deform.cc:758
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
ScrArea * CTX_wm_area(const bContext *C)
__int64 int64_t
Definition BKE_text.h:90
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)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
@ RPT_WARNING
Definition BKE_report.hh:40
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void GREASE_PENCIL_OT_stroke_carver(wmOperatorType *ot)
unsigned int uint
Definition BKE_text.h:55
int BKE_object_material_remove_unused(Main *bmain, Object *ob)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
short * BKE_object_material_len_p(Object *ob)
Material *** BKE_object_material_array_p(Object *ob)
wmOperatorStatus WM_gesture_straightline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Material * BKE_object_material_get(Object *ob, short act)
void BKE_grease_pencil_copy_layer_group_parameters(const bke::greasepencil::LayerGroup &src, bke::greasepencil::LayerGroup &dst)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
MatBase< float, 2, 2 > float2x2
wmOperatorStatus WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *event)
const EnumPropertyItem rna_enum_curves_type_items[]
Definition rna_curves.cc:26
const ListBaseT< bDeformGroup > * BKE_id_defgroup_list_get(const ID *id)
Definition deform.cc:472
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_LOCAL_ORIGINS
@ V3D_AROUND_CENTER_MEDIAN
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void GREASE_PENCIL_OT_stroke_trim(wmOperatorType *ot)
int RNA_int_get(PointerRNA *ptr, const char *name)
void BKE_object_material_active_index_sanitize(Object *ob)
void ED_operatortypes_grease_pencil_edit()
MatBase< float, 4, 4 > float4x4
MatBase< float, 2, 4 > float2x4
Base * CTX_data_active_base(const bContext *C)
int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material)
void BKE_grease_pencil_copy_layer_parameters(const bke::greasepencil::Layer &src, bke::greasepencil::Layer &dst)
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
GreasePencil * BKE_grease_pencil_add(Main *bmain, const char *name)
ViewLayer * CTX_data_view_layer(const bContext *C)
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
VecBase< float, 4 > float4
MaterialGPencilStyle * BKE_gpencil_material_settings(Object *ob, short act)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Material * BKE_grease_pencil_object_material_new(Main *bmain, Object *ob, const char *name, int *r_index)
VecBase< float, 2 > float2
bool BKE_object_material_slot_used(Object *object, short actcol)
wmOperatorStatus ED_grease_pencil_join_objects_exec(bContext *C, wmOperator *op)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
MatBase< float, 4, 2 > float4x2
bke::AttrDomain ED_grease_pencil_edit_selection_domain_get(const ToolSettings *tool_settings)
void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
unsigned char uint8_t
Definition BKE_text.h:79
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void BKE_grease_pencil_copy_parameters(const GreasePencil &src, GreasePencil &dst)
bDeformGroup * BKE_defgroup_duplicate(const bDeformGroup *ingroup)
Definition deform.cc:85
void WM_menu_name_call(bContext *C, const char *menu_name, wm::OpCallContext context)
float ED_view3d_grid_view_scale(const Scene *scene, const View3D *v3d, const ARegion *region, const char **r_grid_unit)
MatBase< float, 3, 2 > float3x2
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
const EnumPropertyItem rna_enum_dummy_DEFAULT_items[]
Definition rna_rna.cc:34
int BKE_object_material_index_get(Object *ob, const Material *ma)
MatBase< float, 4, 3 > float4x3
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_object_material_array_assign(Main *bmain, Object *ob, Material ***matar, int totcol, bool to_object_only)
void BKE_fcurves_main_cb(struct Main *bmain, FunctionRef< void(ID *, FCurve *)> func)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
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
@ WM_CURSOR_EDIT
Definition wm_cursors.hh:21
T * DEG_get_original(T *id)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ CURVE_TYPE_CATMULL_ROM
bool ED_view3d_win_to_ray_clipped(Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_normal[3], bool do_clip_planes)
std::mutex Mutex
Definition BLI_mutex.hh:51
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
AnimData * BKE_animdata_copy(Main *bmain, AnimData *adt, int flag)
Definition anim_data.cc:346
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
@ KM_RELEASE
Definition WM_types.hh:325
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
ListBaseT< bDeformGroup > * BKE_id_defgroup_list_get_mutable(ID *id)
Definition deform.cc:524
VecBase< float, 3 > float3
@ GP_MATERIAL_GRADIENT_RADIAL
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
@ ADT_MERGECOPY_KEEP_DST
@ PROP_DISTANCE
Definition RNA_types.hh:313
void WM_cursor_wait(bool val)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
float DEG_get_ctime(const Depsgraph *graph)
ARegion * CTX_wm_region(const bContext *C)
char * BKE_animsys_fix_rna_path_rename(ID *owner_id, const char *old_path, StringRef prefix, StringRefNull old_name, StringRefNull newName)
Definition anim_data.cc:882
void BKE_animdata_merge_copy(Main *bmain, ID *dst_id, ID *src_id, eAnimData_MergeCopy_Modes action_mode, bool fix_drivers)
Definition anim_data.cc:443
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
View3D * CTX_wm_view3d(const bContext *C)
signed char int8_t
Definition BKE_text.h:76
void WM_gesture_straightline_cancel(bContext *C, wmOperator *op)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
struct Object * object
ListBaseT< DriverVar > variables
ListBaseT< struct bDeformGroup > vertex_group_names
ChannelDriver * driver
ListBaseT< GreasePencilLayerTreeNode > children
ListBaseT< Material > materials
Definition BKE_main.hh:392
eMaterialGPencilStyle_GradientType gradient_type
struct AnimData * adt
struct Object * camera
View3DCursor cursor
struct RenderData r
struct ToolSettings * toolsettings
static ArrayData from_uninitialized(const CPPType &type, int64_t domain_size)
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
const Curves * get_curves() const
Vector< std::pair< uint, int > > materials
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & row(bool align)
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type