Blender V4.5
grease_pencil_utils.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
10#include "BKE_attribute.hh"
11#include "BKE_brush.hh"
12#include "BKE_colortools.hh"
13#include "BKE_context.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_material.hh"
17#include "BKE_paint.hh"
18#include "BKE_report.hh"
19#include "BKE_scene.hh"
20
21#include "BLI_array_utils.hh"
22#include "BLI_bounds.hh"
23#include "BLI_listbase.h"
24#include "BLI_math_geom.h"
25#include "BLI_math_numbers.hh"
26#include "BLI_math_vector.hh"
27#include "BLI_vector_set.hh"
28
29#include "DNA_brush_types.h"
30#include "DNA_material_types.h"
31#include "DNA_object_types.h"
32#include "DNA_scene_types.h"
33#include "DNA_view3d_types.h"
34
36
37#include "GEO_merge_layers.hh"
38
39#include "RNA_prototypes.hh"
40
41#include "ED_curves.hh"
42#include "ED_grease_pencil.hh"
43#include "ED_view3d.hh"
44
46
48 const ARegion &region,
49 const View3D &view3d,
50 const Object &eval_object,
51 const bke::greasepencil::Layer *layer)
52 : region_(&region), view3d_(&view3d)
53{
54 layer_space_to_world_space_ = (layer != nullptr) ? layer->to_world_space(eval_object) :
55 eval_object.object_to_world();
56 world_space_to_layer_space_ = math::invert(layer_space_to_world_space_);
57 /* Initialize DrawingPlacementPlane from toolsettings. */
59 case GP_LOCKAXIS_VIEW:
60 plane_ = DrawingPlacementPlane::View;
61 break;
62 case GP_LOCKAXIS_Y:
63 plane_ = DrawingPlacementPlane::Front;
64 placement_normal_ = float3(0, 1, 0);
65 break;
66 case GP_LOCKAXIS_X:
67 plane_ = DrawingPlacementPlane::Side;
68 placement_normal_ = float3(1, 0, 0);
69 break;
70 case GP_LOCKAXIS_Z:
71 plane_ = DrawingPlacementPlane::Top;
72 placement_normal_ = float3(0, 0, 1);
73 break;
74 case GP_LOCKAXIS_CURSOR: {
75 plane_ = DrawingPlacementPlane::Cursor;
76 placement_normal_ = scene.cursor.matrix<float3x3>() * float3(0, 0, 1);
77 break;
78 }
79 }
80
81 /* Account for layer transform. */
82 if (!ELEM(scene.toolsettings->gp_sculpt.lock_axis, GP_LOCKAXIS_VIEW, GP_LOCKAXIS_CURSOR)) {
83 /* Use the transpose inverse for normal. */
84 placement_normal_ = math::transform_direction(math::transpose(world_space_to_layer_space_),
85 placement_normal_);
86 }
87
88 /* Initialize DrawingPlacementDepth from toolsettings. */
89 const char align_flag = scene.toolsettings->gpencil_v3d_align;
90 if (align_flag & GP_PROJECT_VIEWSPACE) {
91 if (align_flag & GP_PROJECT_CURSOR) {
93 surface_offset_ = 0.0f;
94 placement_loc_ = float3(scene.cursor.location);
95 }
96 else if (align_flag & GP_PROJECT_DEPTH_VIEW) {
98 if (align_flag & GP_PROJECT_DEPTH_ONLY_SELECTED) {
99 use_project_only_selected_ = true;
100 }
101 surface_offset_ = scene.toolsettings->gpencil_surface_offset;
102 /* Default to view placement with the object origin if we don't hit a surface. */
103 placement_loc_ = layer_space_to_world_space_.location();
104 }
105 else if (align_flag & GP_PROJECT_DEPTH_STROKE) {
107 surface_offset_ = 0.0f;
108 /* Default to view placement with the object origin if we don't hit a stroke. */
109 placement_loc_ = layer_space_to_world_space_.location();
110 }
111 else {
113 surface_offset_ = 0.0f;
114 placement_loc_ = layer_space_to_world_space_.location();
115 }
116 }
117 else {
119 surface_offset_ = 0.0f;
120 placement_loc_ = float3(0.0f);
121 }
122
123 if (plane_ != DrawingPlacementPlane::View) {
124 placement_plane_ = float4();
125 plane_from_point_normal_v3(*placement_plane_, placement_loc_, placement_normal_);
126 }
127}
128
130 const ARegion &region,
131 const View3D &view3d,
132 const Object &eval_object,
133 const bke::greasepencil::Layer *layer,
134 const ReprojectMode reproject_mode,
135 const float surface_offset,
136 ViewDepths *view_depths)
137 : region_(&region),
138 view3d_(&view3d),
139 depth_cache_(view_depths),
140 surface_offset_(surface_offset)
141{
142 layer_space_to_world_space_ = (layer != nullptr) ? layer->to_world_space(eval_object) :
143 eval_object.object_to_world();
144 world_space_to_layer_space_ = math::invert(layer_space_to_world_space_);
145 /* Initialize DrawingPlacementPlane from mode. */
146 switch (reproject_mode) {
149 break;
152 placement_normal_ = float3(0, 1, 0);
153 break;
156 placement_normal_ = float3(1, 0, 0);
157 break;
160 placement_normal_ = float3(0, 0, 1);
161 break;
164 placement_normal_ = scene.cursor.matrix<float3x3>() * float3(0, 0, 1);
165 break;
166 }
167 default:
168 break;
169 }
170
171 /* Account for layer transform. */
172 if (!ELEM(reproject_mode, ReprojectMode::View, ReprojectMode::Cursor)) {
173 /* Use the transpose inverse for normal. */
174 placement_normal_ = math::transform_direction(math::transpose(world_space_to_layer_space_),
175 placement_normal_);
176 }
177
178 /* Initialize DrawingPlacementDepth from mode. */
179 switch (reproject_mode) {
182 surface_offset_ = 0.0f;
183 placement_loc_ = float3(scene.cursor.location);
184 break;
187 surface_offset_ = 0.0f;
188 placement_loc_ = layer_space_to_world_space_.location();
189 break;
192 placement_loc_ = layer_space_to_world_space_.location();
193 break;
194 default:
196 surface_offset_ = 0.0f;
197 placement_loc_ = layer_space_to_world_space_.location();
198 break;
199 }
200
201 if (plane_ != DrawingPlacementPlane::View) {
202 placement_plane_ = float4();
203 plane_from_point_normal_v3(*placement_plane_, placement_loc_, placement_normal_);
204 }
205}
206
208{
209 region_ = other.region_;
210 view3d_ = other.view3d_;
211
212 depth_ = other.depth_;
213 plane_ = other.plane_;
214
215 if (other.depth_cache_ != nullptr) {
216 depth_cache_ = static_cast<ViewDepths *>(MEM_dupallocN(other.depth_cache_));
217 depth_cache_->depths = static_cast<float *>(MEM_dupallocN(other.depth_cache_->depths));
218 }
219 use_project_only_selected_ = other.use_project_only_selected_;
220
221 surface_offset_ = other.surface_offset_;
222
223 placement_loc_ = other.placement_loc_;
224 placement_normal_ = other.placement_normal_;
225 placement_plane_ = other.placement_plane_;
226
227 layer_space_to_world_space_ = other.layer_space_to_world_space_;
228 world_space_to_layer_space_ = other.world_space_to_layer_space_;
229}
230
232{
233 region_ = other.region_;
234 view3d_ = other.view3d_;
235
236 depth_ = other.depth_;
237 plane_ = other.plane_;
238
239 std::swap(depth_cache_, other.depth_cache_);
240 use_project_only_selected_ = other.use_project_only_selected_;
241
242 surface_offset_ = other.surface_offset_;
243
244 placement_loc_ = other.placement_loc_;
245 placement_normal_ = other.placement_normal_;
246 placement_plane_ = other.placement_plane_;
247
248 layer_space_to_world_space_ = other.layer_space_to_world_space_;
249 world_space_to_layer_space_ = other.world_space_to_layer_space_;
250}
251
253{
254 if (this == &other) {
255 return *this;
256 }
257 std::destroy_at(this);
258 new (this) DrawingPlacement(other);
259 return *this;
260}
261
263{
264 if (this == &other) {
265 return *this;
266 }
267 std::destroy_at(this);
268 new (this) DrawingPlacement(std::move(other));
269 return *this;
270}
271
273{
274 if (depth_cache_ != nullptr) {
275 ED_view3d_depths_free(depth_cache_);
276 }
277}
278
283
288
290{
291 const short previous_gp_flag = view3d->gp_flag;
293
295 if (use_project_only_selected_) {
297 }
298 else {
300 }
301 }
302 if (use_project_to_stroke()) {
303 /* Enforce render engine to use 3D stroke order, otherwise depth buffer values are not in 3D
304 * space. */
306 }
307
308 ED_view3d_depth_override(depsgraph, region, view3d, nullptr, mode, false, &this->depth_cache_);
309
310 view3d->gp_flag = previous_gp_flag;
311}
312
313std::optional<float3> DrawingPlacement::project_depth(const float2 co) const
314{
315 std::optional<float> depth = get_depth(co);
316 if (!depth) {
317 return std::nullopt;
318 }
319
320 float3 proj_point;
321 if (ED_view3d_depth_unproject_v3(region_, int2(co), *depth, proj_point)) {
322 float3 view_normal;
323 ED_view3d_win_to_vector(region_, co, view_normal);
324 proj_point -= view_normal * surface_offset_;
325 return proj_point;
326 }
327 return std::nullopt;
328}
329
330std::optional<float> DrawingPlacement::get_depth(float2 co) const
331{
332 float depth;
333 if (depth_cache_ != nullptr && ED_view3d_depth_read_cached(depth_cache_, int2(co), 4, &depth)) {
334 return depth;
335 }
336 return std::nullopt;
337}
338
339float3 DrawingPlacement::try_project_depth(const float2 co) const
340{
341 if (std::optional<float3> proj_point = this->project_depth(co)) {
342 return *proj_point;
343 }
344
345 float3 proj_point;
346 /* Fall back to `View` placement. */
347 ED_view3d_win_to_3d(view3d_, region_, placement_loc_, co, proj_point);
348 return proj_point;
349}
350
351float3 DrawingPlacement::project(const float2 co, bool &r_clipped) const
352{
353 float3 proj_point;
354 if (depth_ == DrawingPlacementDepth::Surface) {
355 /* Project using the viewport depth cache. */
356 proj_point = this->try_project_depth(co);
357 r_clipped = false;
358 }
359 else {
360 if (placement_plane_) {
361 r_clipped = !ED_view3d_win_to_3d_on_plane(region_, *placement_plane_, co, true, proj_point);
362 }
363 else {
364 ED_view3d_win_to_3d(view3d_, region_, placement_loc_, co, proj_point);
365 r_clipped = false;
366 }
367 }
368 return math::transform_point(world_space_to_layer_space_, proj_point);
369}
371{
372 [[maybe_unused]] bool clipped_unused;
373 return this->project(co, clipped_unused);
374}
375
377{
378 float3 proj_point;
379 if (depth_ == DrawingPlacementDepth::Surface) {
380 /* Project using the viewport depth cache. */
381 proj_point = this->try_project_depth(co);
382 }
383 else {
384 if (placement_plane_) {
385 ED_view3d_win_to_3d_on_plane(region_, *placement_plane_, co, false, proj_point);
386 }
387 else {
388 ED_view3d_win_to_3d_with_shift(view3d_, region_, placement_loc_, co, proj_point);
389 }
390 }
391 return math::transform_point(world_space_to_layer_space_, proj_point);
392}
393
395{
396 threading::parallel_for(src.index_range(), 1024, [&](const IndexRange range) {
397 for (const int i : range) {
398 dst[i] = this->project(src[i]);
399 }
400 });
401}
402
403float3 DrawingPlacement::place(const float2 co, const float depth) const
404{
405 float3 loc;
406 ED_view3d_unproject_v3(region_, co.x, co.y, depth, loc);
407 return math::transform_point(world_space_to_layer_space_, loc);
408}
409
411{
412 const float3 world_pos = math::transform_point(layer_space_to_world_space_, pos);
413 float3 proj_point;
414 if (depth_ == DrawingPlacementDepth::Surface) {
415 /* First project the position into view space. */
416 float2 co;
417 if (ED_view3d_project_float_global(region_, world_pos, co, V3D_PROJ_TEST_NOP)) {
418 /* Can't reproject the point. */
419 return pos;
420 }
421 /* Project using the viewport depth cache. */
422 proj_point = this->try_project_depth(co);
423 }
424 else {
425 /* Reproject the point onto the `placement_plane_` from the current view. */
426 RegionView3D *rv3d = static_cast<RegionView3D *>(region_->regiondata);
427
428 float3 ray_no;
429 if (rv3d->is_persp) {
430 ray_no = math::normalize(world_pos - float3(rv3d->viewinv[3]));
431 }
432 else {
433 ray_no = -float3(rv3d->viewinv[2]);
434 }
435 float4 plane;
436 if (placement_plane_) {
437 plane = *placement_plane_;
438 }
439 else {
440 plane_from_point_normal_v3(plane, placement_loc_, rv3d->viewinv[2]);
441 }
442
443 float lambda;
444 if (isect_ray_plane_v3(world_pos, ray_no, plane, &lambda, false)) {
445 proj_point = world_pos + ray_no * lambda;
446 }
447 else {
448 return pos;
449 }
450 }
451 return math::transform_point(world_space_to_layer_space_, proj_point);
452}
453
455{
456 threading::parallel_for(src.index_range(), 1024, [&](const IndexRange range) {
457 for (const int i : range) {
458 dst[i] = this->reproject(src[i]);
459 }
460 });
461}
462
464{
465 return layer_space_to_world_space_;
466}
467
468static float get_frame_falloff(const bool use_multi_frame_falloff,
469 const int frame_number,
470 const int active_frame,
471 const std::optional<Bounds<int>> frame_bounds,
472 const CurveMapping *falloff_curve)
473{
474 if (!use_multi_frame_falloff || !frame_bounds.has_value() || falloff_curve == nullptr) {
475 return 1.0f;
476 }
477
478 const int min_frame = frame_bounds->min;
479 const int max_frame = frame_bounds->max;
480
481 /* Frame right of the center frame. */
482 if (frame_number < active_frame) {
483 const float frame_factor = 0.5f * float(frame_number - min_frame) / (active_frame - min_frame);
484 return BKE_curvemapping_evaluateF(falloff_curve, 0, frame_factor);
485 }
486 /* Frame left of the center frame. */
487 if (frame_number > active_frame) {
488 const float frame_factor = 0.5f * float(frame_number - active_frame) /
489 (max_frame - active_frame);
490 return BKE_curvemapping_evaluateF(falloff_curve, 0, frame_factor + 0.5f);
491 }
492 /* Frame at center. */
493 return BKE_curvemapping_evaluateF(falloff_curve, 0, 0.5f);
494}
495
496static std::optional<Bounds<int>> get_selected_frame_number_bounds(
497 const bke::greasepencil::Layer &layer)
498{
499 using namespace blender::bke::greasepencil;
500 if (!layer.is_editable()) {
501 return {};
502 }
503 Vector<int> frame_numbers;
504 for (const auto [frame_number, frame] : layer.frames().items()) {
505 if (frame.is_selected()) {
506 frame_numbers.append(frame_number);
507 }
508 }
509 return bounds::min_max<int>(frame_numbers);
510}
511
513 const std::optional<Bounds<int>> frame_bounds,
514 const int current_frame)
515{
516 std::optional<int> current_start_frame = layer.start_frame_at(current_frame);
517 if (!current_start_frame && frame_bounds) {
518 return math::clamp(current_frame, frame_bounds->min, frame_bounds->max);
519 }
520 return *current_start_frame;
521}
522
523static std::optional<int> get_frame_id(const bke::greasepencil::Layer &layer,
524 const GreasePencilFrame &frame,
525 const int frame_number,
526 const int frame_index,
527 const int current_frame,
528 const int current_frame_index,
529 const int last_frame,
530 const int last_frame_index,
531 const bool use_multi_frame_editing,
532 const bool do_onion_skinning,
533 const bool is_before_first,
534 const GreasePencilOnionSkinningSettings onion_settings)
535{
536 if (use_multi_frame_editing) {
537 if (frame.is_selected()) {
538 if (do_onion_skinning) {
539 return (frame_number < current_frame) ? -1 : 1;
540 }
541 return 0;
542 }
543 return {};
544 }
545 if (do_onion_skinning && layer.use_onion_skinning()) {
546 /* Keyframe type filter. */
547 if (onion_settings.filter != 0 && (onion_settings.filter & (1 << frame.type)) == 0) {
548 return {};
549 }
550 /* Selected mode filter. */
551 if (onion_settings.mode == GP_ONION_SKINNING_MODE_SELECTED && !frame.is_selected()) {
552 return {};
553 }
554
555 int delta = 0;
556 if (onion_settings.mode == GP_ONION_SKINNING_MODE_ABSOLUTE) {
557 delta = frame_number - current_frame;
558 }
559 else {
560 delta = frame_index - current_frame_index;
561 }
562
563 if (is_before_first) {
564 delta++;
565 }
566 if ((onion_settings.flag & GP_ONION_SKINNING_SHOW_LOOP) != 0 &&
567 (-delta > onion_settings.num_frames_before || delta > onion_settings.num_frames_after))
568 {
569 /* We wrap the value using the last frame and 0 as reference. */
570 /* FIXME: This might not be good for animations not starting at 0. */
571 int shift = 0;
572 if (onion_settings.mode == GP_ONION_SKINNING_MODE_ABSOLUTE) {
573 shift = last_frame;
574 }
575 else {
576 shift = last_frame_index;
577 }
578 delta += (delta < 0) ? (shift + 1) : -(shift + 1);
579 }
580 /* Frame range filter. */
581 if (ELEM(onion_settings.mode,
584 (-delta > onion_settings.num_frames_before || delta > onion_settings.num_frames_after))
585 {
586 return {};
587 }
588
589 return delta;
590 }
591 return {};
592}
593
595 const GreasePencil &grease_pencil,
596 const bke::greasepencil::Layer &layer,
597 const int current_frame,
598 const bool use_multi_frame_editing,
599 const bool do_onion_skinning)
600{
601 GreasePencilOnionSkinningSettings onion_settings = grease_pencil.onion_skinning_settings;
602 Vector<std::pair<int, int>> frame_numbers;
603 const Span<int> sorted_keys = layer.sorted_keys();
604 if (sorted_keys.is_empty()) {
605 return {};
606 }
607 const int current_frame_index = std::max(layer.sorted_keys_index_at(current_frame), 0);
608 const int last_frame = sorted_keys.last();
609 const int last_frame_index = sorted_keys.index_range().last();
610 const bool is_before_first = (current_frame < sorted_keys.first());
611 const std::optional<int> current_start_frame = layer.start_frame_at(current_frame);
612 for (const int frame_i : sorted_keys.index_range()) {
613 const int frame_number = sorted_keys[frame_i];
614 if (current_start_frame && *current_start_frame == frame_number) {
615 continue;
616 }
617 const GreasePencilFrame &frame = layer.frames().lookup(frame_number);
618 const std::optional<int> frame_id = get_frame_id(layer,
619 frame,
620 frame_number,
621 frame_i,
622 current_frame,
623 current_frame_index,
624 last_frame,
625 last_frame_index,
626 use_multi_frame_editing,
627 do_onion_skinning,
628 is_before_first,
629 onion_settings);
630 if (!frame_id.has_value()) {
631 /* Drawing on this frame is not visible. */
632 continue;
633 }
634
635 frame_numbers.append({frame_number, *frame_id});
636 }
637
638 frame_numbers.append({current_frame, 0});
639
640 return frame_numbers.as_span();
641}
642
644 const bke::greasepencil::Layer &layer,
645 const int current_frame,
646 const bool use_multi_frame_editing)
647{
648 using namespace blender::bke::greasepencil;
649 Vector<int> frame_numbers;
650 Set<const Drawing *> added_drawings;
651 if (use_multi_frame_editing) {
652 const Drawing *current_drawing = grease_pencil.get_drawing_at(layer, current_frame);
653 for (const auto [frame_number, frame] : layer.frames().items()) {
654 if (!frame.is_selected()) {
655 continue;
656 }
657 frame_numbers.append(frame_number);
658 added_drawings.add(grease_pencil.get_drawing_at(layer, frame_number));
659 }
660 if (added_drawings.contains(current_drawing)) {
661 return frame_numbers.as_span();
662 }
663 }
664
665 frame_numbers.append(current_frame);
666 return frame_numbers.as_span();
667}
668
670 GreasePencil &grease_pencil)
671{
672 using namespace blender::bke::greasepencil;
673 const int current_frame = scene.r.cfra;
674 const ToolSettings *toolsettings = scene.toolsettings;
675 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
677
678 Vector<MutableDrawingInfo> editable_drawings;
679 Span<const Layer *> layers = grease_pencil.layers();
680 for (const int layer_i : layers.index_range()) {
681 const Layer &layer = *layers[layer_i];
682 if (!layer.is_editable()) {
683 continue;
684 }
685 const Array<int> frame_numbers = get_editable_frames_for_layer(
686 grease_pencil, layer, current_frame, use_multi_frame_editing);
687 for (const int frame_number : frame_numbers) {
688 if (Drawing *drawing = grease_pencil.get_editable_drawing_at(layer, frame_number)) {
689 editable_drawings.append({*drawing, layer_i, frame_number, 1.0f});
690 }
691 }
692 }
693
694 return editable_drawings;
695}
696
698 GreasePencil &grease_pencil)
699{
700 using namespace blender::bke::greasepencil;
701 const int current_frame = scene.r.cfra;
702 const ToolSettings *toolsettings = scene.toolsettings;
703 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
705 const bool use_multi_frame_falloff = use_multi_frame_editing &&
706 (toolsettings->gp_sculpt.flag &
708 if (use_multi_frame_falloff) {
710 }
711
712 Vector<MutableDrawingInfo> editable_drawings;
713 Span<const Layer *> layers = grease_pencil.layers();
714 for (const int layer_i : layers.index_range()) {
715 const Layer &layer = *layers[layer_i];
716 if (!layer.is_editable()) {
717 continue;
718 }
719 const std::optional<Bounds<int>> frame_bounds = get_selected_frame_number_bounds(layer);
720 const int active_frame = get_active_frame_for_falloff(layer, frame_bounds, current_frame);
721 const Array<int> frame_numbers = get_editable_frames_for_layer(
722 grease_pencil, layer, current_frame, use_multi_frame_editing);
723 for (const int frame_number : frame_numbers) {
724 if (Drawing *drawing = grease_pencil.get_editable_drawing_at(layer, frame_number)) {
725 const float falloff = get_frame_falloff(use_multi_frame_falloff,
726 frame_number,
727 active_frame,
728 frame_bounds,
729 toolsettings->gp_sculpt.cur_falloff);
730 editable_drawings.append({*drawing, layer_i, frame_number, falloff});
731 }
732 }
733 }
734
735 return editable_drawings;
736}
737
739 const Scene &scene, GreasePencil &grease_pencil)
740{
741 using namespace blender::bke::greasepencil;
742 int current_frame = scene.r.cfra;
743 const ToolSettings *toolsettings = scene.toolsettings;
744 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
746 const bool use_multi_frame_falloff = use_multi_frame_editing &&
747 (toolsettings->gp_sculpt.flag &
749 if (use_multi_frame_falloff) {
751 }
752
753 /* Get a set of unique frame numbers with editable drawings on them. */
754 VectorSet<int> selected_frames;
755 Span<const Layer *> layers = grease_pencil.layers();
756 if (use_multi_frame_editing) {
757 for (const int layer_i : layers.index_range()) {
758 const Layer &layer = *layers[layer_i];
759 if (!layer.is_editable()) {
760 continue;
761 }
762 for (const auto [frame_number, frame] : layer.frames().items()) {
763 if (frame_number != current_frame && frame.is_selected()) {
764 selected_frames.add(frame_number);
765 }
766 }
767 }
768 }
769 selected_frames.add(current_frame);
770
771 /* Get drawings grouped per frame. */
772 Array<Vector<MutableDrawingInfo>> drawings_grouped_per_frame(selected_frames.size());
773 Set<const Drawing *> added_drawings;
774 for (const int layer_i : layers.index_range()) {
775 const Layer &layer = *layers[layer_i];
776 if (!layer.is_editable()) {
777 continue;
778 }
779 const std::optional<Bounds<int>> frame_bounds = get_selected_frame_number_bounds(layer);
780 const int active_frame = get_active_frame_for_falloff(layer, frame_bounds, current_frame);
781
782 /* In multi frame editing mode, add drawings at selected frames. */
783 if (use_multi_frame_editing) {
784 for (const auto [frame_number, frame] : layer.frames().items()) {
785 Drawing *drawing = grease_pencil.get_editable_drawing_at(layer, frame_number);
786 if (!frame.is_selected() || drawing == nullptr || added_drawings.contains(drawing)) {
787 continue;
788 }
789 const float falloff = get_frame_falloff(use_multi_frame_falloff,
790 frame_number,
791 active_frame,
792 frame_bounds,
793 toolsettings->gp_sculpt.cur_falloff);
794 const int frame_group = selected_frames.index_of(frame_number);
795 drawings_grouped_per_frame[frame_group].append({*drawing, layer_i, frame_number, falloff});
796 added_drawings.add_new(drawing);
797 }
798 }
799
800 /* Add drawing at current frame. */
801 Drawing *current_drawing = grease_pencil.get_drawing_at(layer, current_frame);
802 if (current_drawing != nullptr && !added_drawings.contains(current_drawing)) {
803 const float falloff = get_frame_falloff(use_multi_frame_falloff,
804 current_frame,
805 active_frame,
806 frame_bounds,
807 toolsettings->gp_sculpt.cur_falloff);
808 const int frame_group = selected_frames.index_of(current_frame);
809 drawings_grouped_per_frame[frame_group].append(
810 {*current_drawing, layer_i, current_frame, falloff});
811 added_drawings.add_new(current_drawing);
812 }
813 }
814
815 return drawings_grouped_per_frame;
816}
817
819 const Scene &scene,
820 GreasePencil &grease_pencil,
822{
823 using namespace blender::bke::greasepencil;
824 const int current_frame = scene.r.cfra;
825 const ToolSettings *toolsettings = scene.toolsettings;
826 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
828 const int layer_index = *grease_pencil.get_layer_index(layer);
829
830 Vector<MutableDrawingInfo> editable_drawings;
831 const Array<int> frame_numbers = get_editable_frames_for_layer(
832 grease_pencil, layer, current_frame, use_multi_frame_editing);
833 for (const int frame_number : frame_numbers) {
834 if (Drawing *drawing = grease_pencil.get_editable_drawing_at(layer, frame_number)) {
835 editable_drawings.append({*drawing, layer_index, frame_number, 1.0f});
836 }
837 }
838
839 return editable_drawings;
840}
841
843 const Scene &scene,
844 GreasePencil &grease_pencil,
846{
847 using namespace blender::bke::greasepencil;
848 const int current_frame = scene.r.cfra;
849 const ToolSettings *toolsettings = scene.toolsettings;
850 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
852 const bool use_multi_frame_falloff = use_multi_frame_editing &&
853 (toolsettings->gp_sculpt.flag &
855 const int layer_index = *grease_pencil.get_layer_index(layer);
856 std::optional<Bounds<int>> frame_bounds;
857 if (use_multi_frame_falloff) {
859 frame_bounds = get_selected_frame_number_bounds(layer);
860 }
861
862 const int active_frame = get_active_frame_for_falloff(layer, frame_bounds, current_frame);
863
864 Vector<MutableDrawingInfo> editable_drawings;
865 const Array<int> frame_numbers = get_editable_frames_for_layer(
866 grease_pencil, layer, current_frame, use_multi_frame_editing);
867 for (const int frame_number : frame_numbers) {
868 if (Drawing *drawing = grease_pencil.get_editable_drawing_at(layer, frame_number)) {
869 const float falloff = get_frame_falloff(use_multi_frame_falloff,
870 frame_number,
871 active_frame,
872 frame_bounds,
873 toolsettings->gp_sculpt.cur_falloff);
874 editable_drawings.append({*drawing, layer_index, frame_number, falloff});
875 }
876 }
877
878 return editable_drawings;
879}
880
882 const GreasePencil &grease_pencil,
883 const bool do_onion_skinning)
884{
885 using namespace blender::bke::greasepencil;
886 const int current_frame = BKE_scene_ctime_get(&scene);
887 const ToolSettings *toolsettings = scene.toolsettings;
888 const bool use_multi_frame_editing = (toolsettings->gpencil_flags &
890
891 Vector<DrawingInfo> visible_drawings;
892 Span<const Layer *> layers = grease_pencil.layers();
893 for (const int layer_i : layers.index_range()) {
894 const Layer &layer = *layers[layer_i];
895 if (!layer.is_visible()) {
896 continue;
897 }
899 grease_pencil, layer, current_frame, use_multi_frame_editing, do_onion_skinning);
900 for (const auto &[frame_number, onion_id] : frames) {
901 if (const Drawing *drawing = grease_pencil.get_drawing_at(layer, frame_number)) {
902 visible_drawings.append({*drawing, layer_i, frame_number, onion_id});
903 }
904 }
905 }
906
907 return visible_drawings;
908}
909
911{
912 BLI_assert(object.type == OB_GREASE_PENCIL);
913 VectorSet<int> locked_material_indices;
914 for (const int mat_i : IndexRange(object.totcol)) {
915 Material *material = BKE_object_material_get(&object, mat_i + 1);
916 /* The editable materials are unlocked and not hidden. */
917 if (material != nullptr && material->gp_style != nullptr &&
918 ((material->gp_style->flag & GP_MATERIAL_LOCKED) != 0 ||
919 (material->gp_style->flag & GP_MATERIAL_HIDE) != 0))
920 {
921 locked_material_indices.add_new(mat_i);
922 }
923 }
924 return locked_material_indices;
925}
926
928{
929 BLI_assert(object.type == OB_GREASE_PENCIL);
930 VectorSet<int> hidden_material_indices;
931 for (const int mat_i : IndexRange(object.totcol)) {
932 Material *material = BKE_object_material_get(&object, mat_i + 1);
933 if (material != nullptr && material->gp_style != nullptr &&
934 (material->gp_style->flag & GP_MATERIAL_HIDE) != 0)
935 {
936 hidden_material_indices.add_new(mat_i);
937 }
938 }
939 return hidden_material_indices;
940}
941
943{
944 BLI_assert(object.type == OB_GREASE_PENCIL);
945 VectorSet<int> fill_material_indices;
946 for (const int mat_i : IndexRange(object.totcol)) {
947 Material *material = BKE_object_material_get(&object, mat_i + 1);
948 if (material != nullptr && material->gp_style != nullptr &&
949 (material->gp_style->flag & GP_MATERIAL_FILL_SHOW) != 0)
950 {
951 fill_material_indices.add_new(mat_i);
952 }
953 }
954 return fill_material_indices;
955}
956
958 const bke::greasepencil::Drawing &drawing,
959 int layer_index,
960 IndexMaskMemory &memory)
961{
962 using namespace blender;
963 const bke::CurvesGeometry &curves = drawing.strokes();
964 const IndexRange curves_range = curves.curves_range();
965
966 if (object.totcol == 0) {
967 return IndexMask(curves_range);
968 }
969
970 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object.data);
971 const bke::greasepencil::Layer &layer = *grease_pencil.layers()[layer_index];
972
973 /* If we're not using material locking, the entire curves range is editable. */
974 if (layer.ignore_locked_materials()) {
975 return IndexMask(curves_range);
976 }
977
978 /* Get all the editable material indices */
979 VectorSet<int> locked_material_indices = get_locked_material_indices(object);
980 if (locked_material_indices.is_empty()) {
981 return curves_range;
982 }
983
984 const bke::AttributeAccessor attributes = curves.attributes();
985 const VArray<int> materials = *attributes.lookup_or_default<int>(
986 "material_index", bke::AttrDomain::Curve, 0);
987 if (!materials) {
988 /* If the attribute does not exist then the default is the first material. */
989 if (locked_material_indices.contains(0)) {
990 return {};
991 }
992 return curves_range;
993 }
994 /* Get all the strokes that have their material unlocked. */
996 curves_range, GrainSize(4096), memory, [&](const int64_t curve_i) {
997 return !locked_material_indices.contains(materials[curve_i]);
998 });
999}
1000
1002 const bke::greasepencil::Drawing &drawing,
1003 int layer_index,
1004 IndexMaskMemory &memory)
1005{
1006 using namespace blender;
1007 const IndexMask editable_strokes = retrieve_editable_strokes(
1008 object, drawing, layer_index, memory);
1009 if (editable_strokes.is_empty()) {
1010 return {};
1011 }
1012
1013 const bke::CurvesGeometry &curves = drawing.strokes();
1014 const IndexRange curves_range = curves.curves_range();
1015
1016 const bke::AttributeAccessor attributes = curves.attributes();
1017 const VArray<int> materials = *attributes.lookup_or_default<int>(
1018 "material_index", bke::AttrDomain::Curve, 0);
1019 const VectorSet<int> fill_material_indices = get_fill_material_indices(object);
1020 if (!materials) {
1021 /* If the attribute does not exist then the default is the first material. */
1022 if (editable_strokes.contains(0) && fill_material_indices.contains(0)) {
1023 return curves_range;
1024 }
1025 return {};
1026 }
1028 curves_range, GrainSize(4096), memory, [&](const int64_t curve_i) {
1029 const int material_index = materials[curve_i];
1030 return fill_material_indices.contains(material_index);
1031 });
1032 return IndexMask::from_intersection(editable_strokes, fill_strokes, memory);
1033}
1034
1036 const bke::greasepencil::Drawing &drawing,
1037 const int mat_i,
1038 IndexMaskMemory &memory)
1039{
1040 using namespace blender;
1041
1042 const bke::CurvesGeometry &curves = drawing.strokes();
1043 const IndexRange curves_range = curves.curves_range();
1044
1045 /* Get all the editable material indices */
1046 VectorSet<int> locked_material_indices = get_locked_material_indices(object);
1047
1048 const bke::AttributeAccessor attributes = curves.attributes();
1049
1050 const VArray<int> materials = *attributes.lookup_or_default<int>(
1051 "material_index", bke::AttrDomain::Curve, 0);
1052 if (!materials) {
1053 /* If the attribute does not exist then the default is the first material. */
1054 if (locked_material_indices.contains(0)) {
1055 return {};
1056 }
1057 return curves_range;
1058 }
1059 /* Get all the strokes that share the same material and have it unlocked. */
1061 curves_range, GrainSize(4096), memory, [&](const int64_t curve_i) {
1062 const int material_index = materials[curve_i];
1063 if (material_index == mat_i) {
1064 return !locked_material_indices.contains(material_index);
1065 }
1066 return false;
1067 });
1068}
1069
1071 const bke::greasepencil::Drawing &drawing,
1072 int layer_index,
1073 IndexMaskMemory &memory)
1074{
1075 const bke::CurvesGeometry &curves = drawing.strokes();
1076 const IndexRange points_range = curves.points_range();
1077
1078 if (object.totcol == 0) {
1079 return IndexMask(points_range);
1080 }
1081
1082 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object.data);
1083 const bke::greasepencil::Layer &layer = *grease_pencil.layers()[layer_index];
1084
1085 /* If we're not using material locking, the entire points range is editable. */
1086 if (layer.ignore_locked_materials()) {
1087 return IndexMask(points_range);
1088 }
1089
1090 /* Get all the editable material indices */
1091 VectorSet<int> locked_material_indices = get_locked_material_indices(object);
1092 if (locked_material_indices.is_empty()) {
1093 return points_range;
1094 }
1095
1096 /* Propagate the material index to the points. */
1097 const bke::AttributeAccessor attributes = curves.attributes();
1098 const VArray<int> materials = *attributes.lookup_or_default<int>(
1099 "material_index", bke::AttrDomain::Point, 0);
1100 if (!materials) {
1101 /* If the attribute does not exist then the default is the first material. */
1102 if (locked_material_indices.contains(0)) {
1103 return {};
1104 }
1105 return points_range;
1106 }
1107 /* Get all the points that are part of a stroke with an unlocked material. */
1109 points_range, GrainSize(4096), memory, [&](const int64_t point_i) {
1110 return !locked_material_indices.contains(materials[point_i]);
1111 });
1112}
1113
1115 const MutableDrawingInfo &info,
1116 const bke::AttrDomain selection_domain,
1117 IndexMaskMemory &memory)
1118{
1119
1120 const bke::greasepencil::Drawing &drawing = info.drawing;
1121 if (selection_domain == bke::AttrDomain::Curve) {
1122 return ed::greasepencil::retrieve_editable_strokes(object, drawing, info.layer_index, memory);
1123 }
1124 if (selection_domain == bke::AttrDomain::Point) {
1125 return ed::greasepencil::retrieve_editable_points(object, drawing, info.layer_index, memory);
1126 }
1127 return {};
1128}
1129
1131 const bke::greasepencil::Drawing &drawing,
1132 IndexMaskMemory &memory)
1133{
1134 using namespace blender;
1135
1136 /* Get all the hidden material indices. */
1137 VectorSet<int> hidden_material_indices = get_hidden_material_indices(object);
1138
1139 if (hidden_material_indices.is_empty()) {
1140 return drawing.strokes().curves_range();
1141 }
1142
1143 const bke::CurvesGeometry &curves = drawing.strokes();
1144 const IndexRange curves_range = drawing.strokes().curves_range();
1145 const bke::AttributeAccessor attributes = curves.attributes();
1146
1147 /* Get all the strokes that have their material visible. */
1148 const VArray<int> materials = *attributes.lookup_or_default<int>(
1149 "material_index", bke::AttrDomain::Curve, 0);
1151 curves_range, GrainSize(4096), memory, [&](const int64_t curve_i) {
1152 const int material_index = materials[curve_i];
1153 return !hidden_material_indices.contains(material_index);
1154 });
1155}
1156
1158 const bke::greasepencil::Drawing &drawing,
1159 IndexMaskMemory &memory)
1160{
1161 /* Get all the hidden material indices. */
1162 VectorSet<int> hidden_material_indices = get_hidden_material_indices(object);
1163
1164 if (hidden_material_indices.is_empty()) {
1165 return drawing.strokes().points_range();
1166 }
1167
1168 const bke::CurvesGeometry &curves = drawing.strokes();
1169 const IndexRange points_range = curves.points_range();
1170 const bke::AttributeAccessor attributes = curves.attributes();
1171
1172 /* Propagate the material index to the points. */
1173 const VArray<int> materials = *attributes.lookup_or_default<int>(
1174 "material_index", bke::AttrDomain::Point, 0);
1175 if (const std::optional<int> single_material = materials.get_if_single()) {
1176 if (!hidden_material_indices.contains(*single_material)) {
1177 return points_range;
1178 }
1179 return {};
1180 }
1181
1182 /* Get all the points that are part of a stroke with a visible material. */
1184 points_range, GrainSize(4096), memory, [&](const int64_t point_i) {
1185 const int material_index = materials[point_i];
1186 return !hidden_material_indices.contains(material_index);
1187 });
1188}
1189
1191 const bke::greasepencil::Drawing &drawing,
1192 const int layer_index,
1193 IndexMaskMemory &memory)
1194{
1195 const bke::CurvesGeometry &curves = drawing.strokes();
1196
1197 if (!curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
1198 return IndexMask(0);
1199 }
1200
1201 /* Make sure that the handle position attributes exists. */
1202 if (curves.handle_positions_left().is_empty() || curves.handle_positions_right().is_empty()) {
1203 return IndexMask(0);
1204 }
1205
1206 const Array<int> point_to_curve_map = curves.point_to_curve_map();
1207 const VArray<int8_t> types = curves.curve_types();
1208
1209 const VArray<bool> selected_point = *curves.attributes().lookup_or_default<bool>(
1210 ".selection", bke::AttrDomain::Point, true);
1211 const VArray<bool> selected_left = *curves.attributes().lookup_or_default<bool>(
1212 ".selection_handle_left", bke::AttrDomain::Point, true);
1213 const VArray<bool> selected_right = *curves.attributes().lookup_or_default<bool>(
1214 ".selection_handle_right", bke::AttrDomain::Point, true);
1215
1217 object, drawing, layer_index, memory);
1218
1219 const IndexMask selected_points = IndexMask::from_predicate(
1220 curves.points_range(), GrainSize(4096), memory, [&](const int64_t point_i) {
1221 const bool is_selected = selected_point[point_i] || selected_left[point_i] ||
1222 selected_right[point_i];
1223 const bool is_bezier = types[point_to_curve_map[point_i]] == CURVE_TYPE_BEZIER;
1224 return is_selected && is_bezier;
1225 });
1226
1227 return IndexMask::from_intersection(editable_points, selected_points, memory);
1228}
1229
1231 const bke::greasepencil::Drawing &drawing,
1232 const int layer_index,
1233 const bke::AttrDomain selection_domain,
1234 IndexMaskMemory &memory)
1235{
1236 if (selection_domain == bke::AttrDomain::Curve) {
1238 object, drawing, layer_index, memory);
1239 }
1240 if (selection_domain == bke::AttrDomain::Point) {
1242 object, drawing, layer_index, memory);
1243 }
1244 return {};
1245}
1246
1248 const bke::greasepencil::Drawing &drawing,
1249 int layer_index,
1250 IndexMaskMemory &memory)
1251{
1252 using namespace blender;
1253 const bke::CurvesGeometry &curves = drawing.strokes();
1254
1255 const IndexMask editable_strokes = retrieve_editable_strokes(
1256 object, drawing, layer_index, memory);
1257 const IndexMask selected_strokes = ed::curves::retrieve_selected_curves(curves, memory);
1258
1259 return IndexMask::from_intersection(editable_strokes, selected_strokes, memory);
1260}
1261
1263 const bke::greasepencil::Drawing &drawing,
1264 int layer_index,
1265 IndexMaskMemory &memory)
1266{
1267 using namespace blender;
1268 const bke::CurvesGeometry &curves = drawing.strokes();
1269
1270 const IndexMask editable_strokes = retrieve_editable_fill_strokes(
1271 object, drawing, layer_index, memory);
1272 const IndexMask selected_strokes = ed::curves::retrieve_selected_curves(curves, memory);
1273
1274 return IndexMask::from_intersection(editable_strokes, selected_strokes, memory);
1275}
1276
1278 const bke::greasepencil::Drawing &drawing,
1279 int layer_index,
1280 IndexMaskMemory &memory)
1281{
1282 const bke::CurvesGeometry &curves = drawing.strokes();
1283
1284 const IndexMask editable_points = retrieve_editable_points(object, drawing, layer_index, memory);
1285 const IndexMask selected_points = ed::curves::retrieve_selected_points(curves, memory);
1286
1287 return IndexMask::from_intersection(editable_points, selected_points, memory);
1288}
1289
1291 const bke::greasepencil::Drawing &drawing,
1292 int layer_index,
1293 const bke::AttrDomain selection_domain,
1294 IndexMaskMemory &memory)
1295{
1296 if (selection_domain == bke::AttrDomain::Curve) {
1298 object, drawing, layer_index, memory);
1299 }
1300 if (selection_domain == bke::AttrDomain::Point) {
1302 object, drawing, layer_index, memory);
1303 }
1304 return {};
1305}
1306
1307bool has_editable_layer(const GreasePencil &grease_pencil)
1308{
1309 using namespace blender::bke::greasepencil;
1310 for (const Layer *layer : grease_pencil.layers()) {
1311 if (layer->is_editable()) {
1312 return true;
1313 }
1314 }
1315 return false;
1316}
1317
1319 const bke::CurvesGeometry &src,
1321 const Span<Vector<PointTransferData>> src_to_dst_points,
1322 const bool keep_caps)
1323{
1324 const int src_curves_num = src.curves_num();
1325 const OffsetIndices<int> src_points_by_curve = src.points_by_curve();
1326 const VArray<bool> src_cyclic = src.cyclic();
1327
1328 int dst_points_num = 0;
1329 for (const Vector<PointTransferData> &src_transfer_data : src_to_dst_points) {
1330 dst_points_num += src_transfer_data.size();
1331 }
1332 if (dst_points_num == 0) {
1333 dst.resize(0, 0);
1334 return Array<PointTransferData>(0);
1335 }
1336
1337 /* Set the intersection parameters in the destination domain : a pair of int and float
1338 * numbers for which the integer is the index of the corresponding segment in the
1339 * source curves, and the float part is the (0,1) factor representing its position in
1340 * the segment.
1341 */
1342 Array<PointTransferData> dst_transfer_data(dst_points_num);
1343
1344 Array<int> src_pivot_point(src_curves_num, -1);
1345 Array<int> dst_interm_curves_offsets(src_curves_num + 1, 0);
1346 int dst_point = -1;
1347 for (const int src_curve : src.curves_range()) {
1348 const IndexRange src_points = src_points_by_curve[src_curve];
1349
1350 for (const int src_point : src_points) {
1351 for (const PointTransferData &dst_point_transfer : src_to_dst_points[src_point]) {
1352 if (dst_point_transfer.is_src_point) {
1353 dst_transfer_data[++dst_point] = dst_point_transfer;
1354 continue;
1355 }
1356
1357 /* Add an intersection with the eraser and mark it as a cut. */
1358 dst_transfer_data[++dst_point] = dst_point_transfer;
1359
1360 /* For cyclic curves, mark the pivot point as the last intersection with the eraser
1361 * that starts a new segment in the destination.
1362 */
1363 if (src_cyclic[src_curve] && dst_point_transfer.is_cut) {
1364 src_pivot_point[src_curve] = dst_point;
1365 }
1366 }
1367 }
1368 /* We store intermediate curve offsets represent an intermediate state of the
1369 * destination curves before cutting the curves at eraser's intersection. Thus, it
1370 * contains the same number of curves than in the source, but the offsets are
1371 * different, because points may have been added or removed. */
1372 dst_interm_curves_offsets[src_curve + 1] = dst_point + 1;
1373 }
1374
1375 /* Cyclic curves. */
1376 Array<bool> src_now_cyclic(src_curves_num);
1377 threading::parallel_for(src.curves_range(), 4096, [&](const IndexRange src_curves) {
1378 for (const int src_curve : src_curves) {
1379 const int pivot_point = src_pivot_point[src_curve];
1380
1381 if (pivot_point == -1) {
1382 /* Either the curve was not cyclic or it wasn't cut : no need to change it. */
1383 src_now_cyclic[src_curve] = src_cyclic[src_curve];
1384 continue;
1385 }
1386
1387 /* A cyclic curve was cut :
1388 * - this curve is not cyclic anymore,
1389 * - and we have to shift points to keep the closing segment.
1390 */
1391 src_now_cyclic[src_curve] = false;
1392
1393 const int dst_interm_first = dst_interm_curves_offsets[src_curve];
1394 const int dst_interm_last = dst_interm_curves_offsets[src_curve + 1];
1395 std::rotate(dst_transfer_data.begin() + dst_interm_first,
1396 dst_transfer_data.begin() + pivot_point,
1397 dst_transfer_data.begin() + dst_interm_last);
1398 }
1399 });
1400
1401 /* Compute the destination curve offsets. */
1402 Vector<int> dst_curves_offset;
1403 Vector<int> dst_to_src_curve;
1404 dst_curves_offset.append(0);
1405 for (int src_curve : src.curves_range()) {
1406 const IndexRange dst_points(dst_interm_curves_offsets[src_curve],
1407 dst_interm_curves_offsets[src_curve + 1] -
1408 dst_interm_curves_offsets[src_curve]);
1409 int length_of_current = 0;
1410
1411 for (int dst_point : dst_points) {
1412
1413 if ((length_of_current > 0) && dst_transfer_data[dst_point].is_cut) {
1414 /* This is the new first point of a curve. */
1415 dst_curves_offset.append(dst_point);
1416 dst_to_src_curve.append(src_curve);
1417 length_of_current = 0;
1418 }
1419 ++length_of_current;
1420 }
1421
1422 if (length_of_current != 0) {
1423 /* End of a source curve. */
1424 dst_curves_offset.append(dst_points.one_after_last());
1425 dst_to_src_curve.append(src_curve);
1426 }
1427 }
1428 const int dst_curves_num = dst_curves_offset.size() - 1;
1429 if (dst_curves_num == 0) {
1430 dst.resize(0, 0);
1431 return dst_transfer_data;
1432 }
1433
1434 /* Build destination curves geometry. */
1435 dst.resize(dst_points_num, dst_curves_num);
1436 array_utils::copy(dst_curves_offset.as_span(), dst.offsets_for_write());
1437 const OffsetIndices<int> dst_points_by_curve = dst.points_by_curve();
1438
1439 /* Attributes. */
1440 const bke::AttributeAccessor src_attributes = src.attributes();
1441 bke::MutableAttributeAccessor dst_attributes = dst.attributes_for_write();
1442
1443 /* Copy curves attributes. */
1444 bke::gather_attributes(src_attributes,
1448 dst_to_src_curve,
1449 dst_attributes);
1450 if (src_cyclic.get_if_single().value_or(true)) {
1452 src_now_cyclic.as_span(), dst_to_src_curve.as_span(), dst.cyclic_for_write());
1453 }
1454
1455 dst.update_curve_types();
1456
1457 /* Display intersections with flat caps. */
1458 if (!keep_caps) {
1459 bke::SpanAttributeWriter<int8_t> dst_start_caps =
1460 dst_attributes.lookup_or_add_for_write_span<int8_t>("start_cap", bke::AttrDomain::Curve);
1461 bke::SpanAttributeWriter<int8_t> dst_end_caps =
1463
1464 threading::parallel_for(dst.curves_range(), 4096, [&](const IndexRange dst_curves) {
1465 for (const int dst_curve : dst_curves) {
1466 const IndexRange dst_curve_points = dst_points_by_curve[dst_curve];
1467 const PointTransferData &start_point_transfer =
1468 dst_transfer_data[dst_curve_points.first()];
1469 const PointTransferData &end_point_transfer = dst_transfer_data[dst_curve_points.last()];
1470
1471 if (dst_start_caps && start_point_transfer.is_cut) {
1472 dst_start_caps.span[dst_curve] = GP_STROKE_CAP_TYPE_FLAT;
1473 }
1474 /* The is_cut flag does not work for end points, but any end point that isn't the source
1475 * point must also be a cut. */
1476 if (dst_end_caps && !end_point_transfer.is_src_end_point()) {
1477 dst_end_caps.span[dst_curve] = GP_STROKE_CAP_TYPE_FLAT;
1478 }
1479 }
1480 });
1481
1482 dst_start_caps.finish();
1483 dst_end_caps.finish();
1484 }
1485
1486 /* Copy/Interpolate point attributes. */
1487 for (bke::AttributeTransferData &attribute : bke::retrieve_attributes_for_transfer(
1488 src_attributes, dst_attributes, ATTR_DOMAIN_MASK_POINT))
1489 {
1490 bke::attribute_math::convert_to_static_type(attribute.dst.span.type(), [&](auto dummy) {
1491 using T = decltype(dummy);
1492 auto src_attr = attribute.src.typed<T>();
1493 auto dst_attr = attribute.dst.span.typed<T>();
1494
1495 threading::parallel_for(dst.points_range(), 4096, [&](const IndexRange dst_points) {
1496 for (const int dst_point : dst_points) {
1497 const PointTransferData &point_transfer = dst_transfer_data[dst_point];
1498 if (point_transfer.is_src_point) {
1499 dst_attr[dst_point] = src_attr[point_transfer.src_point];
1500 }
1501 else {
1502 dst_attr[dst_point] = bke::attribute_math::mix2<T>(
1503 point_transfer.factor,
1504 src_attr[point_transfer.src_point],
1505 src_attr[point_transfer.src_next_point]);
1506 }
1507 }
1508 });
1509
1510 attribute.dst.finish();
1511 });
1512 }
1513
1514 return dst_transfer_data;
1515}
1516
1518 const ARegion *region,
1519 const float3 center,
1520 const float4x4 to_world,
1521 const float pixel_radius)
1522{
1523 const float2 xy_delta = float2(pixel_radius, 0.0f);
1524 const float3 loc = math::transform_point(to_world, center);
1525
1526 const float zfac = ED_view3d_calc_zfac(rv3d, loc);
1527 float3 delta;
1528 ED_view3d_win_to_delta(region, xy_delta, zfac, delta);
1529
1530 const float scale = math::length(
1532
1533 return math::safe_divide(math::length(delta), scale);
1534}
1535
1537 const ARegion *region,
1538 const Brush *brush,
1539 const float3 location,
1540 const float4x4 to_world)
1541{
1542 if ((brush->flag & BRUSH_LOCK_SIZE) == 0) {
1543 return pixel_radius_to_world_space_radius(rv3d, region, location, to_world, brush->size);
1544 }
1545 return brush->unprojected_radius;
1546}
1547
1549 const ARegion *region,
1550 const Brush *brush,
1551 const float pressure,
1552 const float3 location,
1553 const float4x4 to_world,
1554 const BrushGpencilSettings *settings)
1555{
1556 float radius = brush_radius_at_location(rv3d, region, brush, location, to_world);
1558 radius *= BKE_curvemapping_evaluateF(settings->curve_sensitivity, 0, pressure);
1559 }
1560 return radius;
1561}
1562
1563float opacity_from_input_sample(const float pressure,
1564 const Brush *brush,
1565 const BrushGpencilSettings *settings)
1566{
1567 float opacity = brush->alpha;
1569 opacity *= BKE_curvemapping_evaluateF(settings->curve_strength, 0, pressure);
1570 }
1571 return opacity;
1572}
1573
1575 wmOperator *op,
1576 const bool use_duplicate_previous_key)
1577{
1578 const Scene *scene = CTX_data_scene(C);
1579 const Object *object = CTX_data_active_object(C);
1580 if (!object || object->type != OB_GREASE_PENCIL) {
1581 return OPERATOR_CANCELLED;
1582 }
1583
1584 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object->data);
1585 if (!grease_pencil.has_active_layer()) {
1586 BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer");
1587 return OPERATOR_CANCELLED;
1588 }
1589
1592 if (brush == nullptr) {
1593 return OPERATOR_CANCELLED;
1594 }
1595
1596 bke::greasepencil::Layer &active_layer = *grease_pencil.get_active_layer();
1597
1598 if (!active_layer.is_editable()) {
1599 BKE_report(op->reports, RPT_ERROR, "Active layer is locked or hidden");
1600 return OPERATOR_CANCELLED;
1601 }
1602
1603 /* Ensure a drawing at the current keyframe. */
1604 bool inserted_keyframe = false;
1606 *scene, grease_pencil, active_layer, use_duplicate_previous_key, inserted_keyframe))
1607 {
1608 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
1609 return OPERATOR_CANCELLED;
1610 }
1611 if (inserted_keyframe) {
1613 }
1615}
1616
1618 const ARegion *region,
1619 const float2 &mouse,
1620 const DrawingPlacement &placement)
1621{
1622 float3 u_dir;
1623 float3 v_dir;
1624 /* Set the texture space origin to be the first point. */
1625 float3 origin = placement.project(mouse);
1626 /* Align texture with the drawing plane. */
1627 switch (scene->toolsettings->gp_sculpt.lock_axis) {
1628 case GP_LOCKAXIS_VIEW:
1629 u_dir = math::normalize(placement.project(float2(region->winx, 0.0f) + mouse) - origin);
1630 v_dir = math::normalize(placement.project(float2(0.0f, region->winy) + mouse) - origin);
1631 break;
1632 case GP_LOCKAXIS_Y:
1633 u_dir = float3(1.0f, 0.0f, 0.0f);
1634 v_dir = float3(0.0f, 0.0f, 1.0f);
1635 break;
1636 case GP_LOCKAXIS_X:
1637 u_dir = float3(0.0f, 1.0f, 0.0f);
1638 v_dir = float3(0.0f, 0.0f, 1.0f);
1639 break;
1640 case GP_LOCKAXIS_Z:
1641 u_dir = float3(1.0f, 0.0f, 0.0f);
1642 v_dir = float3(0.0f, 1.0f, 0.0f);
1643 break;
1644 case GP_LOCKAXIS_CURSOR: {
1645 const float3x3 mat = scene->cursor.matrix<float3x3>();
1646 u_dir = mat * float3(1.0f, 0.0f, 0.0f);
1647 v_dir = mat * float3(0.0f, 1.0f, 0.0f);
1648 origin = float3(scene->cursor.location);
1649 break;
1650 }
1651 }
1652
1653 return math::transpose(float2x4(float4(u_dir, -math::dot(u_dir, origin)),
1654 float4(v_dir, -math::dot(v_dir, origin))));
1655}
1656
1658{
1659 GreasePencil *grease_pencil = static_cast<GreasePencil *>(
1660 CTX_data_pointer_get_type(&C, "grease_pencil", &RNA_GreasePencilv3).data);
1661
1662 if (grease_pencil == nullptr) {
1663 Object *object = CTX_data_active_object(&C);
1664 if (object && object->type == OB_GREASE_PENCIL) {
1665 grease_pencil = static_cast<GreasePencil *>(object->data);
1666 }
1667 }
1668 return grease_pencil;
1669}
1670
1672{
1673 if (at_end) {
1674 const int num_old_points = curves.points_num();
1675 curves.resize(curves.points_num() + 1, curves.curves_num() + 1);
1676 curves.offsets_for_write().last(1) = num_old_points;
1677 return;
1678 }
1679
1680 curves.resize(curves.points_num() + 1, curves.curves_num() + 1);
1681 MutableSpan<int> offsets = curves.offsets_for_write();
1682 offsets.first() = 0;
1683
1684 /* Loop through backwards to not overwrite the data. */
1685 for (int i = curves.curves_num() - 2; i >= 0; i--) {
1686 offsets[i + 1] = offsets[i] + 1;
1687 }
1688
1689 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
1690
1691 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1693 GMutableSpan attribute_data = dst.span;
1694
1695 bke::attribute_math::convert_to_static_type(attribute_data.type(), [&](auto dummy) {
1696 using T = decltype(dummy);
1697 MutableSpan<T> span_data = attribute_data.typed<T>();
1698
1699 /* Loop through backwards to not overwrite the data. */
1700 for (int i = span_data.size() - 2; i >= 0; i--) {
1701 span_data[i + 1] = span_data[i];
1702 }
1703 });
1704 dst.finish();
1705 });
1706}
1707
1708void resize_single_curve(bke::CurvesGeometry &curves, const bool at_end, const int new_points_num)
1709{
1710 BLI_assert(new_points_num >= 0);
1711 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1712 const int curve_index = at_end ? curves.curves_range().last() : 0;
1713 const int current_points_num = points_by_curve[curve_index].size();
1714 if (new_points_num == current_points_num) {
1715 return;
1716 }
1717
1718 if (at_end) {
1719 const int diff_points_num = new_points_num - current_points_num;
1720 curves.resize(curves.points_num() + diff_points_num, curves.curves_num());
1721 curves.offsets_for_write().last() = curves.points_num();
1722 return;
1723 }
1724
1725 if (current_points_num < new_points_num) {
1726 const int last_active_point = points_by_curve[0].last();
1727
1728 const int added_points_num = new_points_num - current_points_num;
1729
1730 curves.resize(curves.points_num() + added_points_num, curves.curves_num());
1731 MutableSpan<int> offsets = curves.offsets_for_write();
1732 for (const int src_curve : curves.curves_range().drop_front(1)) {
1733 offsets[src_curve] = offsets[src_curve] + added_points_num;
1734 }
1735 offsets.last() = curves.points_num();
1736
1737 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
1738 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1739 if (iter.domain != bke::AttrDomain::Point) {
1740 return;
1741 }
1742
1744 GMutableSpan attribute_data = dst.span;
1745
1746 bke::attribute_math::convert_to_static_type(attribute_data.type(), [&](auto dummy) {
1747 using T = decltype(dummy);
1748 MutableSpan<T> span_data = attribute_data.typed<T>();
1749
1750 /* Loop through backwards to not overwrite the data. */
1751 for (int i = span_data.size() - 1 - added_points_num; i >= last_active_point; i--) {
1752 span_data[i + added_points_num] = span_data[i];
1753 }
1754 });
1755 dst.finish();
1756 });
1757 }
1758 else {
1759 /* First move the attribute data, then resize. */
1760 const int removed_points_num = current_points_num - new_points_num;
1761 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
1762 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1763 if (iter.domain != bke::AttrDomain::Point) {
1764 return;
1765 }
1766
1768 GMutableSpan attribute_data = dst.span;
1769
1770 bke::attribute_math::convert_to_static_type(attribute_data.type(), [&](auto dummy) {
1771 using T = decltype(dummy);
1772 MutableSpan<T> span_data = attribute_data.typed<T>();
1773
1774 for (const int i :
1775 span_data.index_range().drop_front(new_points_num).drop_back(removed_points_num))
1776 {
1777 span_data[i] = span_data[i + removed_points_num];
1778 }
1779 });
1780 dst.finish();
1781 });
1782
1783 curves.resize(curves.points_num() - removed_points_num, curves.curves_num());
1784 MutableSpan<int> offsets = curves.offsets_for_write();
1785 for (const int src_curve : curves.curves_range().drop_front(1)) {
1786 offsets[src_curve] = offsets[src_curve] - removed_points_num;
1787 }
1788 offsets.last() = curves.points_num();
1789 }
1790}
1791
1792void apply_eval_grease_pencil_data(const GreasePencil &eval_grease_pencil,
1793 const int eval_frame,
1794 const IndexMask &orig_layers,
1795 GreasePencil &orig_grease_pencil)
1796{
1797 using namespace bke;
1798 using namespace bke::greasepencil;
1799 /* Build a set of pointers to the layers that we want to apply. */
1800 Set<const Layer *> orig_layers_to_apply;
1801 orig_layers.foreach_index([&](const int layer_i) {
1802 const Layer &layer = orig_grease_pencil.layer(layer_i);
1803 orig_layers_to_apply.add(&layer);
1804 });
1805
1806 /* Ensure that the layer names are unique by merging layers with the same name. */
1807 const int old_layers_num = eval_grease_pencil.layers().size();
1808 Vector<Vector<int>> layers_map;
1809 Map<StringRef, int> new_layer_index_by_name;
1810 for (const int layer_i : IndexRange(old_layers_num)) {
1811 const Layer &layer = eval_grease_pencil.layer(layer_i);
1812 const int new_layer_index = new_layer_index_by_name.lookup_or_add_cb(
1813 layer.name(), [&]() { return layers_map.append_and_get_index_as(); });
1814 layers_map[new_layer_index].append(layer_i);
1815 }
1816 GreasePencil &merged_layers_grease_pencil = *geometry::merge_layers(
1817 eval_grease_pencil, layers_map, {});
1818
1819 Map<const Layer *, const Layer *> eval_to_orig_layer_map;
1820 {
1821 /* Set of orig layers that require the drawing on `eval_frame` to be cleared. These are layers
1822 * that existed in original geometry but were removed in the evaluated data. */
1823 Set<Layer *> orig_layers_to_clear;
1824 for (Layer *layer : orig_grease_pencil.layers_for_write()) {
1825 /* Only allow clearing a layer if it is visible. */
1826 if (layer->is_visible()) {
1827 orig_layers_to_clear.add(layer);
1828 }
1829 }
1830 for (const TreeNode *node_eval : merged_layers_grease_pencil.nodes()) {
1831 /* Check if the original geometry has a layer with the same name. */
1832 TreeNode *node_orig = orig_grease_pencil.find_node_by_name(node_eval->name());
1833
1834 BLI_assert(node_eval != nullptr);
1835 if (!node_eval->is_layer()) {
1836 continue;
1837 }
1838 /* If the orig layer isn't valid then a new layer with a unique name will be generated. */
1839 const bool has_valid_orig_layer = (node_orig != nullptr && node_orig->is_layer());
1840 if (!has_valid_orig_layer) {
1841 /* Note: This name might be empty! This has to be resolved at a later stage! */
1842 Layer &layer_orig = orig_grease_pencil.add_layer(node_eval->name(), true);
1843 orig_layers_to_apply.add(&layer_orig);
1844 /* Make sure to add a new keyframe with a new drawing. */
1845 orig_grease_pencil.insert_frame(layer_orig, eval_frame);
1846 node_orig = &layer_orig.as_node();
1847 }
1848 BLI_assert(node_orig != nullptr);
1849 Layer &layer_orig = node_orig->as_layer();
1850 /* This layer has a matching evaluated layer, so don't clear its keyframe. */
1851 orig_layers_to_clear.remove(&layer_orig);
1852 /* Only map layers in `eval_to_orig_layer_map` that we want to apply. */
1853 if (orig_layers_to_apply.contains(&layer_orig)) {
1854 /* Copy layer properties to original geometry. */
1855 const Layer &layer_eval = node_eval->as_layer();
1856 layer_orig.opacity = layer_eval.opacity;
1857 layer_orig.set_local_transform(layer_eval.local_transform());
1858
1859 /* Add new mapping for `layer_eval` -> `layer_orig`. */
1860 eval_to_orig_layer_map.add_new(&layer_eval, &layer_orig);
1861 }
1862 }
1863
1864 /* Clear the keyframe of all the original layers that don't have a matching evaluated layer,
1865 * e.g. the ones that were "deleted" in the evaluated data. */
1866 for (Layer *layer_orig : orig_layers_to_clear) {
1867 /* Try inserting a frame. */
1868 Drawing *drawing_orig = orig_grease_pencil.insert_frame(*layer_orig, eval_frame);
1869 if (drawing_orig == nullptr) {
1870 /* If that fails, get the drawing for this frame. */
1871 drawing_orig = orig_grease_pencil.get_drawing_at(*layer_orig, eval_frame);
1872 }
1873 /* Clear the existing drawing. */
1874 drawing_orig->strokes_for_write() = {};
1875 drawing_orig->tag_topology_changed();
1876 }
1877 }
1878
1879 /* Gather the original vertex group names. */
1880 Set<StringRef> orig_vgroup_names;
1881 LISTBASE_FOREACH (bDeformGroup *, dg, &orig_grease_pencil.vertex_group_names) {
1882 orig_vgroup_names.add(dg->name);
1883 }
1884
1885 /* Update the drawings. */
1886 VectorSet<Drawing *> all_updated_drawings;
1887
1888 Set<StringRef> new_vgroup_names;
1889 for (auto [layer_eval, layer_orig] : eval_to_orig_layer_map.items()) {
1890 Drawing *drawing_eval = merged_layers_grease_pencil.get_drawing_at(*layer_eval, eval_frame);
1891 Drawing *drawing_orig = orig_grease_pencil.get_drawing_at(*layer_orig, eval_frame);
1892
1893 if (drawing_orig && drawing_eval) {
1894 CurvesGeometry &eval_strokes = drawing_eval->strokes_for_write();
1895
1896 /* Check for new vertex groups in CurvesGeometry. */
1897 LISTBASE_FOREACH (bDeformGroup *, dg, &eval_strokes.vertex_group_names) {
1898 if (!orig_vgroup_names.contains(dg->name)) {
1899 new_vgroup_names.add(dg->name);
1900 }
1901 }
1902
1903 /* Write the data to the original drawing. */
1904 drawing_orig->strokes_for_write() = std::move(eval_strokes);
1905 /* Anonymous attributes shouldn't be available on original geometry. */
1907 drawing_orig->tag_topology_changed();
1908 all_updated_drawings.add_new(drawing_orig);
1909 }
1910 }
1911
1912 /* Add new vertex groups to GreasePencil object. */
1913 for (StringRef new_vgroup_name : new_vgroup_names) {
1914 bDeformGroup *dst = MEM_callocN<bDeformGroup>(__func__);
1915 new_vgroup_name.copy_utf8_truncated(dst->name);
1916 BLI_addtail(&orig_grease_pencil.vertex_group_names, dst);
1917 }
1918
1919 /* Get the original material pointers from the result geometry. */
1920 VectorSet<Material *> original_materials;
1921 const Span<Material *> eval_materials = Span{eval_grease_pencil.material_array,
1922 eval_grease_pencil.material_array_num};
1923 for (Material *eval_material : eval_materials) {
1924 if (!eval_material) {
1925 return;
1926 }
1927 original_materials.add(DEG_get_original(eval_material));
1928 }
1929
1930 /* Build material indices mapping. This maps the materials indices on the original geometry to
1931 * the material indices used in the result geometry. The material indices for the drawings in
1932 * the result geometry are already correct, but this might not be the case for all drawings in
1933 * the original geometry (like for drawings that are not visible on the frame that the data is
1934 * being applied on). */
1935 const IndexRange orig_material_indices = IndexRange(orig_grease_pencil.material_array_num);
1936 Array<int> material_indices_map(orig_grease_pencil.material_array_num, -1);
1937 for (const int mat_i : orig_material_indices) {
1938 Material *material = orig_grease_pencil.material_array[mat_i];
1939 const int map_index = original_materials.index_of_try(material);
1940 if (map_index != -1) {
1941 material_indices_map[mat_i] = map_index;
1942 }
1943 }
1944
1945 /* Remap material indices for all other drawings. */
1946 if (!material_indices_map.is_empty() &&
1947 !array_utils::indices_are_range(material_indices_map, orig_material_indices))
1948 {
1949 for (GreasePencilDrawingBase *base : orig_grease_pencil.drawings()) {
1950 if (base->type != GP_DRAWING) {
1951 continue;
1952 }
1953 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
1954 if (all_updated_drawings.contains(&drawing)) {
1955 /* Skip remapping drawings that already have been updated. */
1956 continue;
1957 }
1959 if (!attributes.contains("material_index")) {
1960 continue;
1961 }
1962 SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
1963 "material_index", AttrDomain::Curve);
1964 for (int &material_index : material_indices.span) {
1965 if (material_indices_map.index_range().contains(material_index)) {
1966 material_index = material_indices_map[material_index];
1967 }
1968 }
1969 material_indices.finish();
1970 }
1971 }
1972
1973 /* Convert the layer map into an index mapping. */
1974 Map<int, int> eval_to_orig_layer_indices_map;
1975 for (const int layer_eval_i : merged_layers_grease_pencil.layers().index_range()) {
1976 const Layer *layer_eval = &merged_layers_grease_pencil.layer(layer_eval_i);
1977 if (eval_to_orig_layer_map.contains(layer_eval)) {
1978 const Layer *layer_orig = eval_to_orig_layer_map.lookup(layer_eval);
1979 const int layer_orig_index = *orig_grease_pencil.get_layer_index(*layer_orig);
1980 eval_to_orig_layer_indices_map.add(layer_eval_i, layer_orig_index);
1981 }
1982 }
1983
1984 /* Propagate layer attributes. */
1985 AttributeAccessor src_attributes = merged_layers_grease_pencil.attributes();
1986 MutableAttributeAccessor dst_attributes = orig_grease_pencil.attributes_for_write();
1987 src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1988 /* Anonymous attributes shouldn't be available on original geometry. */
1990 return;
1991 }
1992 if (iter.data_type == CD_PROP_STRING) {
1993 return;
1994 }
1995 const GVArraySpan src = *iter.get(AttrDomain::Layer);
1997 iter.name, AttrDomain::Layer, iter.data_type);
1998 if (!dst) {
1999 return;
2000 }
2001 attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
2002 using T = decltype(dummy);
2003 Span<T> src_span = src.typed<T>();
2004 MutableSpan<T> dst_span = dst.span.typed<T>();
2005 for (const auto [src_i, dst_i] : eval_to_orig_layer_indices_map.items()) {
2006 dst_span[dst_i] = src_span[src_i];
2007 }
2008 });
2009 dst.finish();
2010 });
2011
2012 /* Free temporary grease pencil struct. */
2013 BKE_id_free(nullptr, &merged_layers_grease_pencil);
2014}
2015
2017{
2018 if (!curves.attributes().contains(".is_fill_guide")) {
2019 return false;
2020 }
2021
2022 const bke::AttributeAccessor attributes = curves.attributes();
2023 const VArray<bool> is_fill_guide = *attributes.lookup<bool>(".is_fill_guide",
2025
2026 IndexMaskMemory memory;
2027 const IndexMask fill_guides = IndexMask::from_bools(is_fill_guide, memory);
2028 curves.remove_curves(fill_guides, {});
2029
2030 curves.attributes_for_write().remove(".is_fill_guide");
2031
2032 return true;
2033}
2034
2035} // namespace blender::ed::greasepencil
@ ATTR_DOMAIN_MASK_POINT
bool BKE_brush_use_alpha_pressure(const Brush *brush)
Definition brush.cc:1231
bool BKE_brush_use_size_pressure(const Brush *brush)
Definition brush.cc:1226
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_init(CurveMapping *cumap)
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Object * CTX_data_active_object(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Low-level operations for grease pencil.
void BKE_id_free(Main *bmain, void *idv)
General operations, lookup, etc. for materials.
Material * BKE_object_material_get(Object *ob, short act)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:641
Paint * BKE_paint_get_active_from_context(const bContext *C)
Definition paint.cc:467
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2367
#define BLI_assert(a)
Definition BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:217
bool isect_ray_plane_v3(const float ray_origin[3], const float ray_direction[3], const float plane[4], float *r_lambda, bool clip)
#define ELEM(...)
T * DEG_get_original(T *id)
@ BRUSH_LOCK_SIZE
@ CURVE_TYPE_BEZIER
@ CD_PROP_STRING
@ GP_ONION_SKINNING_MODE_ABSOLUTE
@ GP_ONION_SKINNING_MODE_SELECTED
@ GP_ONION_SKINNING_MODE_RELATIVE
@ GP_ONION_SKINNING_SHOW_LOOP
@ GP_MATERIAL_LOCKED
@ GP_MATERIAL_HIDE
@ GP_MATERIAL_FILL_SHOW
Object is a sort of wrapper for general info.
@ OB_GREASE_PENCIL
@ GP_SCULPT_SETT_FLAG_FRAME_FALLOFF
@ GP_LOCKAXIS_X
@ GP_LOCKAXIS_VIEW
@ GP_LOCKAXIS_Y
@ GP_LOCKAXIS_Z
@ GP_LOCKAXIS_CURSOR
@ GP_PROJECT_VIEWSPACE
@ GP_PROJECT_DEPTH_VIEW
@ GP_PROJECT_CURSOR
@ GP_PROJECT_DEPTH_STROKE
@ GP_PROJECT_DEPTH_ONLY_SELECTED
@ GP_USE_MULTI_FRAME_EDITING
@ V3D_GP_FORCE_STROKE_ORDER_3D
@ OPERATOR_CANCELLED
@ OPERATOR_RUNNING_MODAL
bool ED_view3d_depth_read_cached(const ViewDepths *vd, const int mval[2], int margin, float *r_depth)
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:279
void ED_view3d_depth_override(Depsgraph *depsgraph, ARegion *region, View3D *v3d, Object *obact, eV3DDepthOverrideMode mode, bool use_overlay, ViewDepths **r_depths)
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3])
bool ED_view3d_unproject_v3(const ARegion *region, float regionx, float regiony, float regionz, float world[3])
void ED_view3d_win_to_3d_with_shift(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
void ED_view3d_depths_free(ViewDepths *depths)
eV3DDepthOverrideMode
Definition ED_view3d.hh:188
@ V3D_DEPTH_SELECTED_ONLY
Definition ED_view3d.hh:198
@ V3D_DEPTH_NO_GPENCIL
Definition ED_view3d.hh:192
@ V3D_DEPTH_GPENCIL_ONLY
Definition ED_view3d.hh:194
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])
void ED_view3d_win_to_vector(const ARegion *region, const float mval[2], float r_out[3])
bool ED_view3d_depth_unproject_v3(const ARegion *region, const int mval[2], double depth, float r_location_world[3])
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:581
#define NC_GPENCIL
Definition WM_types.hh:396
switch((BMIterType) itype)
BPy_StructRNA * depsgraph
long long int int64_t
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:689
IndexRange index_range() const
Definition BLI_array.hh:349
bool is_empty() const
Definition BLI_array.hh:253
const CPPType & type() const
const CPPType & type() const
static IndexMask from_predicate(const IndexMask &universe, GrainSize grain_size, IndexMaskMemory &memory, Fn &&predicate)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, IndexMaskMemory &memory)
static IndexMask from_bools(Span< bool > bools, IndexMaskMemory &memory)
constexpr int64_t last(const int64_t n=0) const
constexpr bool contains(int64_t value) const
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
constexpr T & first() const
Definition BLI_span.hh:679
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:689
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void add_new(const Key &key)
Definition BLI_set.hh:233
bool remove(const Key &key)
Definition BLI_set.hh:385
constexpr const T & first() const
Definition BLI_span.hh:315
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:325
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
std::optional< T > get_if_single() const
int64_t index_of_try(const Key &key) const
bool add(const Key &key)
int64_t size() const
int64_t index_of(const Key &key) const
bool contains(const Key &key) const
void add_new(const Key &key)
int64_t size() const
void append(const T &value)
Span< T > as_span() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef attribute_id) const
GAttributeReader lookup(const StringRef attribute_id) const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
GAttributeReader get() const
OffsetIndices< int > points_by_curve() const
IndexRange curves_range() const
MutableAttributeAccessor attributes_for_write()
IndexRange points_range() const
void resize(int points_num, int curves_num)
VArray< bool > cyclic() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type)
GSpanAttributeWriter lookup_for_write_span(StringRef attribute_id)
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
int sorted_keys_index_at(int frame_number) const
float4x4 to_world_space(const Object &object) const
void set_local_transform(const float4x4 &transform)
const Map< FramesMapKeyT, GreasePencilFrame > & frames() const
std::optional< int > start_frame_at(int frame_number) const
Span< FramesMapKeyT > sorted_keys() const
DrawingPlacement & operator=(const DrawingPlacement &other)
std::optional< float > get_depth(float2 co) const
void cache_viewport_depths(Depsgraph *depsgraph, ARegion *region, View3D *view3d)
float3 place(float2 co, float depth) const
std::optional< float3 > project_depth(float2 co) const
float3 project(float2 co, bool &clipped) const
bool contains(int64_t query_index) const
void foreach_index(Fn &&fn) const
uint pos
static char ** types
Definition makesdna.cc:71
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void copy(const GVArray &src, GMutableSpan dst, int64_t grain_size=4096)
bool indices_are_range(Span< int > indices, IndexRange range)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, int64_t grain_size=4096)
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
bool attribute_name_is_anonymous(const StringRef name)
Vector< AttributeTransferData > retrieve_attributes_for_transfer(const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes, AttrDomainMask domain_mask, const AttributeFilter &attribute_filter={})
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:55
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
IndexMask retrieve_selected_points(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
IndexMask retrieve_editable_and_selected_elements(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, const bke::AttrDomain selection_domain, IndexMaskMemory &memory)
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static Array< int > get_editable_frames_for_layer(const GreasePencil &grease_pencil, const bke::greasepencil::Layer &layer, const int current_frame, const bool use_multi_frame_editing)
void resize_single_curve(bke::CurvesGeometry &curves, const bool at_end, const int new_points_num)
static float get_frame_falloff(const bool use_multi_frame_falloff, const int frame_number, const int active_frame, const std::optional< Bounds< int > > frame_bounds, const CurveMapping *falloff_curve)
static std::optional< int > get_frame_id(const bke::greasepencil::Layer &layer, const GreasePencilFrame &frame, const int frame_number, const int frame_index, const int current_frame, const int current_frame_index, const int last_frame, const int last_frame_index, const bool use_multi_frame_editing, const bool do_onion_skinning, const bool is_before_first, const GreasePencilOnionSkinningSettings onion_settings)
IndexMask retrieve_editable_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
bool ensure_active_keyframe(const Scene &scene, GreasePencil &grease_pencil, bke::greasepencil::Layer &layer, const bool duplicate_previous_key, bool &r_inserted_keyframe)
GreasePencil * from_context(bContext &C)
IndexMask retrieve_editable_fill_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
float opacity_from_input_sample(const float pressure, const Brush *brush, const BrushGpencilSettings *settings)
IndexMask retrieve_editable_elements(Object &object, const MutableDrawingInfo &info, const bke::AttrDomain selection_domain, IndexMaskMemory &memory)
IndexMask retrieve_visible_bezier_handle_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, IndexMaskMemory &memory)
void add_single_curve(bke::CurvesGeometry &curves, const bool at_end)
bke::CurvesGeometry fill_strokes(const ViewContext &view_context, const Brush &brush, const Scene &scene, const bke::greasepencil::Layer &layer, const VArray< bool > &boundary_layers, Span< DrawingInfo > src_drawings, bool invert, const std::optional< float > alpha_threshold, const float2 &fill_point, const ExtensionData &extensions, FillToolFitMethod fit_method, int stroke_material_index, bool keep_images)
IndexMask retrieve_visible_points(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
Vector< DrawingInfo > retrieve_visible_drawings(const Scene &scene, const GreasePencil &grease_pencil, const bool do_onion_skinning)
float radius_from_input_sample(const RegionView3D *rv3d, const ARegion *region, const Brush *brush, const float pressure, const float3 location, const float4x4 to_world, const BrushGpencilSettings *settings)
IndexMask retrieve_editable_strokes_by_material(Object &object, const bke::greasepencil::Drawing &drawing, const int mat_i, IndexMaskMemory &memory)
IndexMask retrieve_editable_and_selected_fill_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
bool has_editable_layer(const GreasePencil &grease_pencil)
static float pixel_radius_to_world_space_radius(const RegionView3D *rv3d, const ARegion *region, const float3 center, const float4x4 to_world, const float pixel_radius)
static float brush_radius_at_location(const RegionView3D *rv3d, const ARegion *region, const Brush *brush, const float3 location, const float4x4 to_world)
static Array< std::pair< int, int > > get_visible_frames_for_layer(const GreasePencil &grease_pencil, const bke::greasepencil::Layer &layer, const int current_frame, const bool use_multi_frame_editing, const bool do_onion_skinning)
Array< PointTransferData > compute_topology_change(const bke::CurvesGeometry &src, bke::CurvesGeometry &dst, const Span< Vector< PointTransferData > > src_to_dst_points, const bool keep_caps)
static VectorSet< int > get_locked_material_indices(Object &object)
IndexMask retrieve_editable_and_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static std::optional< Bounds< int > > get_selected_frame_number_bounds(const bke::greasepencil::Layer &layer)
static VectorSet< int > get_fill_material_indices(Object &object)
static VectorSet< int > get_hidden_material_indices(Object &object)
static int get_active_frame_for_falloff(const bke::greasepencil::Layer &layer, const std::optional< Bounds< int > > frame_bounds, const int current_frame)
Vector< MutableDrawingInfo > retrieve_editable_drawings_from_layer(const Scene &scene, GreasePencil &grease_pencil, const blender::bke::greasepencil::Layer &layer)
IndexMask retrieve_visible_strokes(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings_from_layer_with_falloff(const Scene &scene, GreasePencil &grease_pencil, const blender::bke::greasepencil::Layer &layer)
IndexMask retrieve_visible_bezier_handle_elements(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const bke::AttrDomain selection_domain, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings_with_falloff(const Scene &scene, GreasePencil &grease_pencil)
wmOperatorStatus grease_pencil_draw_operator_invoke(bContext *C, wmOperator *op, const bool use_duplicate_previous_key)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
bool remove_fill_guides(bke::CurvesGeometry &curves)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
float4x2 calculate_texture_space(const Scene *scene, const ARegion *region, const float2 &mouse, const DrawingPlacement &placement)
Array< Vector< MutableDrawingInfo > > retrieve_editable_drawings_grouped_per_frame(const Scene &scene, GreasePencil &grease_pencil)
void apply_eval_grease_pencil_data(const GreasePencil &eval_grease_pencil, const int eval_frame, const IndexMask &orig_layers, GreasePencil &orig_grease_pencil)
GreasePencil * merge_layers(const GreasePencil &src_grease_pencil, Span< Vector< int > > layers_to_merge, const bke::AttributeFilter &attribute_filter)
constexpr double inv_sqrt3
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T clamp(const T &a, const T &min, const T &max)
T safe_divide(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)
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)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
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:93
MatBase< float, 4, 4 > float4x4
MatBase< float, 2, 4 > float2x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
MatBase< float, 4, 2 > float4x2
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
float wrap(float value, float max, float min)
Definition node_math.h:71
struct CurveMapping * curve_sensitivity
struct CurveMapping * curve_strength
ListBase vertex_group_names
struct CurveMapping * cur_falloff
struct Material ** material_array
GreasePencilOnionSkinningSettings onion_skinning_settings
struct MaterialGPencilStyle * gp_style
void * data
Definition RNA_types.hh:53
float viewinv[4][4]
struct ToolSettings * toolsettings
struct RenderData r
View3DCursor cursor
struct GP_Sculpt_Settings gp_sculpt
float * depths
Definition ED_view3d.hh:89
struct ReportList * reports
i
Definition text_draw.cc:230
void WM_event_add_notifier(const bContext *C, uint type, void *reference)