Blender V4.5
grease_pencil.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
9#include <iostream>
10#include <optional>
11
12#include "BKE_action.hh"
13#include "BKE_anim_data.hh"
14#include "BKE_animsys.h"
15#include "BKE_asset_edit.hh"
20#include "BKE_curves.hh"
21#include "BKE_customdata.hh"
22#include "BKE_deform.hh"
23#include "BKE_fcurve.hh"
24#include "BKE_geometry_set.hh"
25#include "BKE_grease_pencil.h"
26#include "BKE_grease_pencil.hh"
27#include "BKE_idtype.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_lib_query.hh"
30#include "BKE_main.hh"
31#include "BKE_material.hh"
32#include "BKE_modifier.hh"
33#include "BKE_object.hh"
34#include "BKE_object_types.hh"
35
36#include "BLI_array_utils.hh"
37#include "BLI_bounds.hh"
39#include "BLI_listbase.h"
40#include "BLI_map.hh"
42#include "BLI_math_geom.h"
43#include "BLI_math_matrix.h"
44#include "BLI_math_matrix.hh"
47#include "BLI_memarena.h"
48#include "BLI_memory_utils.hh"
49#include "BLI_polyfill_2d.h"
50#include "BLI_resource_scope.hh"
51#include "BLI_span.hh"
52#include "BLI_stack.hh"
53#include "BLI_string.h"
54#include "BLI_string_ref.hh"
55#include "BLI_string_utils.hh"
56#include "BLI_utildefines.h"
57#include "BLI_vector_set.hh"
58#include "BLI_virtual_array.hh"
59
60#include "BLO_read_write.hh"
61
62#include "BLT_translation.hh"
63
64#include "DNA_ID.h"
65#include "DNA_ID_enums.h"
66#include "DNA_brush_types.h"
67#include "DNA_defaults.h"
69#include "DNA_material_types.h"
70#include "DNA_modifier_types.h"
71
72#include "DEG_depsgraph.hh"
74
75#include "RNA_access.hh"
76#include "RNA_path.hh"
77#include "RNA_prototypes.hh"
78
79#include "MEM_guardedalloc.h"
80
81#include "CLG_log.h"
82
83static CLG_LogRef LOG = {"geom.grease_pencil"};
84
85using blender::float3;
86using blender::int3;
87using blender::Span;
89
90static const char *ATTR_POSITION = "position";
91
92/* Forward declarations. */
93static void read_drawing_array(GreasePencil &grease_pencil, BlendDataReader *reader);
94static void write_drawing_array(GreasePencil &grease_pencil,
96 BlendWriter *writer);
97static void free_drawing_array(GreasePencil &grease_pencil);
98
99static void read_layer_tree(GreasePencil &grease_pencil, BlendDataReader *reader);
100static void write_layer_tree(GreasePencil &grease_pencil, BlendWriter *writer);
101
103{
104 using namespace blender::bke;
105
106 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
107 BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(grease_pencil, id));
108
110
111 grease_pencil->root_group_ptr = MEM_new<greasepencil::LayerGroup>(__func__);
112 grease_pencil->set_active_node(nullptr);
113
114 CustomData_reset(&grease_pencil->layers_data);
115 new (&grease_pencil->attribute_storage.wrap()) blender::bke::AttributeStorage();
116
117 grease_pencil->runtime = MEM_new<GreasePencilRuntime>(__func__);
118}
119
120/* See if the layer visibility is animated. This is determined whenever a copy is made, so that
121 * this happens in the "create evaluation copy" node of the depsgraph. */
122static void grease_pencil_set_runtime_visibilities(ID &id_dst, GreasePencil &grease_pencil)
123{
124 using namespace blender::bke;
125
126 if (!DEG_is_evaluated(&id_dst) || !grease_pencil.adt) {
127 return;
128 }
129
130 PropertyRNA *layer_hide_prop = RNA_struct_type_find_property(&RNA_GreasePencilLayer, "hide");
131 BLI_assert_msg(layer_hide_prop,
132 "RNA struct GreasePencilLayer is expected to have a 'hide' property.");
133 PropertyRNA *group_hide_prop = RNA_struct_type_find_property(&RNA_GreasePencilLayerGroup,
134 "hide");
135 BLI_assert_msg(group_hide_prop,
136 "RNA struct GreasePencilLayerGroup is expected to have a 'hide' property.");
137
138 for (greasepencil::LayerGroup *layer_group : grease_pencil.layer_groups_for_write()) {
140 &id_dst, &RNA_GreasePencilLayerGroup, layer_group);
141 std::optional<std::string> rna_path = RNA_path_from_ID_to_property(&layer_ptr,
142 group_hide_prop);
144 rna_path,
145 "It should be possible to construct the RNA path of a grease pencil layer group.");
146
147 layer_group->runtime->is_visibility_animated_ = animdata::prop_is_animated(
148 grease_pencil.adt, rna_path->c_str(), 0);
149 }
150
151 std::function<bool(greasepencil::LayerGroup &)> parent_group_visibility_animated =
152 [&](greasepencil::LayerGroup &parent) {
153 if (parent.runtime->is_visibility_animated_) {
154 return true;
155 }
156 greasepencil::LayerGroup *parent_group = parent.as_node().parent_group();
157 if (parent_group) {
158 return parent_group_visibility_animated(*parent_group);
159 }
160 return false;
161 };
162
163 for (greasepencil::Layer *layer : grease_pencil.layers_for_write()) {
164 if (parent_group_visibility_animated(layer->parent_group())) {
165 layer->runtime->is_visibility_animated_ = true;
166 continue;
167 }
168 PointerRNA layer_ptr = RNA_pointer_create_discrete(&id_dst, &RNA_GreasePencilLayer, layer);
169 std::optional<std::string> rna_path = RNA_path_from_ID_to_property(&layer_ptr,
170 layer_hide_prop);
171 BLI_assert_msg(rna_path,
172 "It should be possible to construct the RNA path of a grease pencil layer.");
173 layer->runtime->is_visibility_animated_ = animdata::prop_is_animated(
174 grease_pencil.adt, rna_path.value(), 0);
175 }
176}
177
186{
187 using namespace blender::bke::greasepencil;
188 int empty_drawing_users = 0;
189 const int empty_drawing_index = grease_pencil.drawings().size();
190 for (Layer *layer : grease_pencil.layers_for_write()) {
191 for (GreasePencilFrame &frame : layer->frames_for_write().values()) {
192 if (frame.is_end()) {
193 empty_drawing_users++;
194 frame.drawing_index = empty_drawing_index;
196 layer->tag_frames_map_keys_changed();
197 }
198 }
199 }
200 if (empty_drawing_users > 0) {
201 grease_pencil.add_empty_drawings(1);
202 BLI_assert(grease_pencil.drawings().index_range().contains(empty_drawing_index));
203
204 CLOG_WARN(&LOG,
205 "Converted %d end frames to regular frames using an empty drawing in \"%s\"",
206 empty_drawing_users,
207 BKE_id_name(grease_pencil.id));
208 }
209}
210
212{
213 using namespace blender::bke::greasepencil;
214 /* Unknown types have no runtime data, and only the base struct was read. */
215 for (const GreasePencilDrawingBase *drawing_base : grease_pencil.drawings()) {
216 if (!ELEM(drawing_base->type, GP_DRAWING, GP_DRAWING_REFERENCE)) {
217 return false;
218 }
219 }
220 for (const Layer *layer : grease_pencil.layers()) {
221 for (const GreasePencilFrame &value : layer->frames().values()) {
222 /* End frames have no drawing. */
223 if (value.is_end()) {
224 continue;
225 }
226 if (!grease_pencil.drawings().index_range().contains(value.drawing_index)) {
227 return false;
228 }
229 }
230 }
231 return true;
232}
233
234static void grease_pencil_copy_data(Main * /*bmain*/,
235 std::optional<Library *> /*owner_library*/,
236 ID *id_dst,
237 const ID *id_src,
238 const int /*flag*/)
239{
240 using namespace blender;
241
242 GreasePencil *grease_pencil_dst = reinterpret_cast<GreasePencil *>(id_dst);
243 const GreasePencil *grease_pencil_src = reinterpret_cast<const GreasePencil *>(id_src);
244
245 /* Duplicate material array. */
246 grease_pencil_dst->material_array = static_cast<Material **>(
247 MEM_dupallocN(grease_pencil_src->material_array));
248
249 BKE_grease_pencil_duplicate_drawing_array(grease_pencil_src, grease_pencil_dst);
250
251 /* Duplicate layer tree. */
252 grease_pencil_dst->root_group_ptr = MEM_new<bke::greasepencil::LayerGroup>(
253 __func__, grease_pencil_src->root_group());
254
255 /* Set active node. */
256 if (grease_pencil_src->get_active_node()) {
257 bke::greasepencil::TreeNode *active_node = grease_pencil_dst->find_node_by_name(
258 grease_pencil_src->get_active_node()->name());
259 BLI_assert(active_node);
260 grease_pencil_dst->set_active_node(active_node);
261 }
262
263 CustomData_init_from(&grease_pencil_src->layers_data,
264 &grease_pencil_dst->layers_data,
266 grease_pencil_dst->layers().size());
267 new (&grease_pencil_dst->attribute_storage.wrap())
268 blender::bke::AttributeStorage(grease_pencil_src->attribute_storage.wrap());
269
270 BKE_defgroup_copy_list(&grease_pencil_dst->vertex_group_names,
271 &grease_pencil_src->vertex_group_names);
272
273 /* Make sure the runtime pointer exists. */
274 grease_pencil_dst->runtime = MEM_new<bke::GreasePencilRuntime>(__func__);
275
276 if (grease_pencil_src->runtime->bake_materials) {
277 grease_pencil_dst->runtime->bake_materials = std::make_unique<bke::bake::BakeMaterialsList>(
278 *grease_pencil_src->runtime->bake_materials);
279 }
280
281 grease_pencil_set_runtime_visibilities(*id_dst, *grease_pencil_dst);
282}
283
285{
286 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
287 BKE_animdata_free(&grease_pencil->id, false);
288
289 MEM_SAFE_FREE(grease_pencil->material_array);
290
291 CustomData_free(&grease_pencil->layers_data);
292 grease_pencil->attribute_storage.wrap().~AttributeStorage();
293
294 free_drawing_array(*grease_pencil);
295 MEM_delete(&grease_pencil->root_group());
296
297 BLI_freelistN(&grease_pencil->vertex_group_names);
298
300
301 MEM_delete(grease_pencil->runtime);
302 grease_pencil->runtime = nullptr;
303}
304
306{
307 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
308 for (int i = 0; i < grease_pencil->material_array_num; i++) {
310 }
311 for (GreasePencilDrawingBase *drawing_base : grease_pencil->drawings()) {
312 if (drawing_base->type == GP_DRAWING_REFERENCE) {
313 GreasePencilDrawingReference *drawing_reference =
314 reinterpret_cast<GreasePencilDrawingReference *>(drawing_base);
316 }
317 }
318 for (const blender::bke::greasepencil::Layer *layer : grease_pencil->layers()) {
319 if (layer->parent) {
321 }
322 }
323}
324
325static void grease_pencil_blend_write(BlendWriter *writer, ID *id, const void *id_address)
326{
327 using namespace blender;
328 using namespace blender::bke;
329 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
330
332
333 blender::Vector<CustomDataLayer, 16> layers_data_layers;
335 attribute_storage_blend_write_prepare(grease_pencil->attribute_storage.wrap(),
336 {{AttrDomain::Layer, &layers_data_layers}},
337 attribute_data);
338 CustomData_blend_write_prepare(grease_pencil->layers_data,
339 AttrDomain::Layer,
340 grease_pencil->layers().size(),
341 layers_data_layers,
342 attribute_data);
343 grease_pencil->attribute_storage.dna_attributes = attribute_data.attributes.data();
344 grease_pencil->attribute_storage.dna_attributes_num = attribute_data.attributes.size();
345
346 /* Write LibData */
347 BLO_write_id_struct(writer, GreasePencil, id_address, &grease_pencil->id);
348 BKE_id_blend_write(writer, &grease_pencil->id);
349
351 &grease_pencil->layers_data,
352 layers_data_layers,
353 grease_pencil->layers().size(),
355 id);
356 grease_pencil->attribute_storage.wrap().blend_write(*writer, attribute_data);
357
358 /* Write drawings. */
359 write_drawing_array(*grease_pencil, scope, writer);
360 /* Write layer tree. */
361 write_layer_tree(*grease_pencil, writer);
362
363 /* Write materials. */
365 writer, grease_pencil->material_array_num, grease_pencil->material_array);
366 /* Write vertex group names. */
367 BKE_defbase_blend_write(writer, &grease_pencil->vertex_group_names);
368}
369
371{
372 using namespace blender::bke::greasepencil;
373 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
374
375 /* Read drawings. */
376 read_drawing_array(*grease_pencil, reader);
377 /* Read layer tree. */
378 read_layer_tree(*grease_pencil, reader);
379
380 CustomData_blend_read(reader, &grease_pencil->layers_data, grease_pencil->layers().size());
381 grease_pencil->attribute_storage.wrap().blend_read(*reader);
382
383 /* Forward compatibility. To be removed when runtime format changes. */
385
386 /* Read materials. */
388 grease_pencil->material_array_num,
389 reinterpret_cast<void **>(&grease_pencil->material_array));
390 /* Read vertex group names. */
391 BLO_read_struct_list(reader, bDeformGroup, &grease_pencil->vertex_group_names);
392
393 grease_pencil->runtime = MEM_new<blender::bke::GreasePencilRuntime>(__func__);
394
395 /* End-frames used to be 'encoded' using an invalid drawing index (`-1`), until Blender 5.3. But
396 * most of GP code does not handle them correctly.
397 *
398 * As backward-compatible solution, they are converted into regular frames pointing at an empty
399 * drawing. This will give the same visual result (for rendering e.g.), until proper
400 * implementation of endframes is reworked for Blender 5.4. */
402
403 /* Validate drawing data, both in drawings themselves, and the frames referencing them.
404 *
405 * Failure means the file is badly corrupted. Done last, because
406 * the ID gets freed after an invalidated read, which requires all of it to be initialized. */
407 if (!grease_pencil_validate_drawing_data(*grease_pencil)) {
408 BLO_read_data_invalidate(reader, "Invalid Grease Pencil data");
409 }
410}
411
413 /*id_code*/ GreasePencil::id_type,
414 /*id_filter*/ FILTER_ID_GP,
415 /*dependencies_id_types*/ FILTER_ID_GP | FILTER_ID_MA | FILTER_ID_OB,
416 /*main_listbase_index*/ INDEX_ID_GP,
417 /*struct_size*/ sizeof(GreasePencil),
418 /*name*/ "GreasePencil",
419 /*name_plural*/ N_("grease_pencils_v3"),
420 /*translation_context*/ BLT_I18NCONTEXT_ID_GPENCIL,
422 /*asset_type_info*/ nullptr,
423
424 /*init_data*/ grease_pencil_init_data,
425 /*copy_data*/ grease_pencil_copy_data,
426 /*free_data*/ grease_pencil_free_data,
427 /*make_local*/ nullptr,
428 /*foreach_id*/ grease_pencil_foreach_id,
429 /*foreach_cache*/ nullptr,
430 /*foreach_path*/ nullptr,
431 /*owner_pointer_get*/ nullptr,
432
433 /*blend_write*/ grease_pencil_blend_write,
434 /*blend_read_data*/ grease_pencil_blend_read_data,
435 /*blend_read_after_liblink*/ nullptr,
436
437 /*blend_read_undo_preserve*/ nullptr,
438
439 /*lib_override_apply_post*/ nullptr,
440};
441
443
444constexpr StringRef ATTR_RADIUS = "radius";
445constexpr StringRef ATTR_OPACITY = "opacity";
446constexpr StringRef ATTR_VERTEX_COLOR = "vertex_color";
447constexpr StringRef ATTR_FILL_COLOR = "fill_color";
448
449/* Curves attributes getters */
450static int domain_num(const CurvesGeometry &curves, const AttrDomain domain)
451{
452 return domain == AttrDomain::Point ? curves.points_num() : curves.curves_num();
453}
455{
456 return domain == AttrDomain::Point ? curves.point_data : curves.curve_data;
457}
458template<typename T>
460 const AttrDomain domain,
461 const StringRef name,
462 const T default_value = T())
463{
464 const int num = domain_num(curves, domain);
465 if (num <= 0) {
466 return {};
467 }
469 CustomData &custom_data = domain_custom_data(curves, domain);
470
471 T *data = (T *)CustomData_get_layer_named_for_write(&custom_data, type, name, num);
472 if (data != nullptr) {
473 return {data, num};
474 }
475 data = (T *)CustomData_add_layer_named(&custom_data, type, CD_SET_DEFAULT, num, name);
476 MutableSpan<T> span = {data, num};
477 if (span.first() != default_value) {
478 span.fill(default_value);
479 }
480 return span;
481}
482
484{
485 this->base.type = GP_DRAWING;
486 this->base.flag = 0;
487
488 new (&this->geometry) bke::CurvesGeometry();
489 /* Initialize runtime data. */
490 this->runtime = MEM_new<bke::greasepencil::DrawingRuntime>(__func__);
491}
492
494{
495 this->base.type = GP_DRAWING;
496 this->base.flag = other.base.flag;
497
498 new (&this->geometry) bke::CurvesGeometry(other.strokes());
499 /* Initialize runtime data. */
500 this->runtime = MEM_new<bke::greasepencil::DrawingRuntime>(__func__);
501
502 this->runtime->triangle_offsets_cache = other.runtime->triangle_offsets_cache;
503 this->runtime->triangles_cache = other.runtime->triangles_cache;
504 this->runtime->curve_plane_normals_cache = other.runtime->curve_plane_normals_cache;
505 this->runtime->curve_texture_matrices = other.runtime->curve_texture_matrices;
506}
507
509{
510 this->base.type = GP_DRAWING;
511 other.base.type = GP_DRAWING;
512 this->base.flag = other.base.flag;
513 other.base.flag = 0;
514
515 new (&this->geometry) bke::CurvesGeometry(std::move(other.geometry.wrap()));
516
517 this->runtime = other.runtime;
518 other.runtime = nullptr;
519}
520
522{
523 if (this == &other) {
524 return *this;
525 }
526 std::destroy_at(this);
527 new (this) Drawing(other);
528 return *this;
529}
530
532{
533 if (this == &other) {
534 return *this;
535 }
536 std::destroy_at(this);
537 new (this) Drawing(std::move(other));
538 return *this;
539}
540
542{
543 this->strokes().~CurvesGeometry();
544 MEM_delete(this->runtime);
545 this->runtime = nullptr;
546}
547
548OffsetIndices<int> Drawing::triangle_offsets() const
549{
550 this->runtime->triangle_offsets_cache.ensure([&](Vector<int> &r_offsets) {
551 const CurvesGeometry &curves = this->strokes();
552 const OffsetIndices<int> points_by_curve = curves.evaluated_points_by_curve();
553
554 int offset = 0;
555 r_offsets.reinitialize(curves.curves_num() + 1);
556 for (const int curve_i : points_by_curve.index_range()) {
557 const IndexRange points = points_by_curve[curve_i];
558 r_offsets[curve_i] = offset;
559 offset += std::max(int(points.size() - 2), 0);
560 }
561 r_offsets.last() = offset;
562 });
563 return this->runtime->triangle_offsets_cache.data().as_span();
564}
565
566static void update_triangle_cache(const Span<float3> positions,
567 const Span<float3> normals,
568 const OffsetIndices<int> points_by_curve,
569 const OffsetIndices<int> triangle_offsets,
570 const IndexMask &curve_mask,
571 MutableSpan<int3> triangles)
572{
573 struct LocalMemArena {
574 MemArena *pf_arena = nullptr;
575 LocalMemArena() : pf_arena(BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "Drawing::triangles")) {}
576
577 ~LocalMemArena()
578 {
579 if (pf_arena != nullptr) {
580 BLI_memarena_free(pf_arena);
581 }
582 }
583 };
585 curve_mask.foreach_segment(GrainSize(32), [&](const IndexMaskSegment mask_segment) {
586 MemArena *pf_arena = all_local_mem_arenas.local().pf_arena;
587 for (const int curve_i : mask_segment) {
588 const IndexRange points = points_by_curve[curve_i];
589 if (points.size() < 3) {
590 continue;
591 }
592 MutableSpan<int3> r_tris = triangles.slice(triangle_offsets[curve_i]);
593
594 float(*projverts)[2] = static_cast<float(*)[2]>(
595 BLI_memarena_alloc(pf_arena, sizeof(*projverts) * size_t(points.size())));
596
597 float3x3 axis_mat;
598 axis_dominant_v3_to_m3(axis_mat.ptr(), normals[curve_i]);
599
600 for (const int i : IndexRange(points.size())) {
601 mul_v2_m3v3(projverts[i], axis_mat.ptr(), positions[points[i]]);
602 }
603
605 projverts, points.size(), 0, reinterpret_cast<uint32_t(*)[3]>(r_tris.data()), pf_arena);
606 BLI_memarena_clear(pf_arena);
607 }
608 });
609}
610
612{
613 const CurvesGeometry &curves = this->strokes();
614 const OffsetIndices<int> triangle_offsets = this->triangle_offsets();
615 this->runtime->triangles_cache.ensure([&](Vector<int3> &r_data) {
616 const int total_triangles = triangle_offsets.total_size();
617 r_data.resize(total_triangles);
618
620 this->curve_plane_normals(),
622 triangle_offsets,
623 curves.curves_range(),
624 r_data.as_mutable_span());
625 });
626
627 return this->runtime->triangles_cache.data().as_span();
628}
629
631 const OffsetIndices<int> points_by_curve,
632 const IndexMask &curve_mask,
634{
635 curve_mask.foreach_index(GrainSize(512), [&](const int curve_i) {
636 const IndexRange points = points_by_curve[curve_i];
637 if (points.size() < 2) {
638 normals[curve_i] = float3(1.0f, 0.0f, 0.0f);
639 return;
640 }
641
642 /* Calculate normal using Newell's method. */
643 float3 normal(0.0f);
644 float3 prev_point = positions[points.last()];
645 for (const int point_i : points) {
646 const float3 curr_point = positions[point_i];
647 add_newell_cross_v3_v3v3(normal, prev_point, curr_point);
648 prev_point = curr_point;
649 }
650
651 float length;
652 normal = math::normalize_and_get_length(normal, length);
653 /* Check for degenerate case where the points are on a line. */
654 if (math::is_zero(length)) {
655 for (const int point_i : points.drop_back(1)) {
656 float3 segment_vec = positions[point_i] - positions[point_i + 1];
657 if (math::length_squared(segment_vec) != 0.0f) {
658 normal = math::normalize(float3(segment_vec.y, -segment_vec.x, 0.0f));
659 break;
660 }
661 }
662 }
663
664 normals[curve_i] = normal;
665 });
666}
667
669{
670 this->runtime->curve_plane_normals_cache.ensure([&](Vector<float3> &r_data) {
671 const CurvesGeometry &curves = this->strokes();
672 r_data.reinitialize(curves.curves_num());
674 curves.points_by_curve(),
675 curves.curves_range(),
676 r_data.as_mutable_span());
677 });
678 return this->runtime->curve_plane_normals_cache.data().as_span();
679}
680
681/*
682 * Returns the matrix that transforms from a 3D point in layer-space to a 2D point in
683 * stroke-space for the stroke `curve_i`
684 */
685static float4x2 get_local_to_stroke_matrix(const Span<float3> positions, const float3 normal)
686{
687 using namespace blender::math;
688
689 if (positions.size() <= 2) {
690 return float4x2::identity();
691 }
692
693 const float3 point_0 = positions[0];
694 const float3 point_1 = positions[1];
695
696 /* Local X axis (p0 -> p1) */
697 const float3 local_x = normalize(point_1 - point_0);
698 /* Local Y axis (cross to normal/x axis). */
699 const float3 local_y = normalize(cross(normal, local_x));
700
701 if (length_squared(local_x) == 0.0f || length_squared(local_y) == 0.0f) {
702 return float4x2::identity();
703 }
704
705 /* Get local space using first point as origin. */
706 const float4x2 mat = transpose(
707 float2x4(float4(local_x, -dot(point_0, local_x)), float4(local_y, -dot(point_0, local_y))));
708
709 return mat;
710}
711
712/*
713 * Returns the matrix that transforms from a 2D point in stroke-space to a 2D point in
714 * texture-space for a stroke `curve_i`
715 */
716static float3x2 get_stroke_to_texture_matrix(const float uv_rotation,
717 const float2 uv_translation,
718 const float2 uv_scale)
719{
720 using namespace blender::math;
721
722 const float2 uv_scale_inv = safe_rcp(uv_scale);
723 const float s = sin(uv_rotation);
724 const float c = cos(uv_rotation);
725 const float2x2 rot = float2x2(float2(c, s), float2(-s, c));
726
727 float3x2 texture_matrix = float3x2::identity();
728 /*
729 * The order in which the three transforms are applied has been carefully chosen to be easy to
730 * invert.
731 *
732 * The translation is applied last so that the origin goes to `uv_translation`
733 * The rotation is applied after the scale so that the `u` direction's angle is `uv_rotation`
734 * Scale is the only transform that changes the length of the basis vectors and if it is applied
735 * first it's independent of the other transforms.
736 *
737 * These properties are not true with a different order.
738 */
739
740 /* Apply scale. */
741 texture_matrix = from_scale<float2x2>(uv_scale_inv) * texture_matrix;
742
743 /* Apply rotation. */
744 texture_matrix = rot * texture_matrix;
745
746 /* Apply translation. */
747 texture_matrix[2] += uv_translation;
748
749 return texture_matrix;
750}
751
753{
754 float4x3 strokemat4x3 = float4x3(strokemat);
755
756 /*
757 * We need the diagonal of ones to start from the bottom right instead top left to properly
758 * apply the two matrices.
759 *
760 * i.e.
761 * # # # # # # # #
762 * We need # # # # Instead of # # # #
763 * 0 0 0 1 0 0 1 0
764 *
765 */
766 strokemat4x3[2][2] = 0.0f;
767 strokemat4x3[3][2] = 1.0f;
768
769 return strokemat4x3;
770}
771
773{
774 this->runtime->curve_texture_matrices.ensure([&](Vector<float4x2> &r_data) {
775 const CurvesGeometry &curves = this->strokes();
776 const AttributeAccessor attributes = curves.attributes();
777
778 const VArray<float> uv_rotations = *attributes.lookup_or_default<float>(
779 "uv_rotation", AttrDomain::Curve, 0.0f);
780 const VArray<float2> uv_translations = *attributes.lookup_or_default<float2>(
781 "uv_translation", AttrDomain::Curve, float2(0.0f, 0.0f));
782 const VArray<float2> uv_scales = *attributes.lookup_or_default<float2>(
783 "uv_scale", AttrDomain::Curve, float2(1.0f, 1.0f));
784
785 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
786 const Span<float3> positions = curves.positions();
788
789 r_data.reinitialize(curves.curves_num());
790 threading::parallel_for(curves.curves_range(), 512, [&](const IndexRange range) {
791 for (const int curve_i : range) {
792 const IndexRange points = points_by_curve[curve_i];
793 const float3 normal = normals[curve_i];
794 const float4x2 strokemat = get_local_to_stroke_matrix(positions.slice(points), normal);
795 const float3x2 texture_matrix = get_stroke_to_texture_matrix(
796 uv_rotations[curve_i], uv_translations[curve_i], uv_scales[curve_i]);
797
798 const float4x2 texspace = texture_matrix * expand_4x2_mat(strokemat);
799
800 r_data[curve_i] = texspace;
801 }
802 });
803 });
804 return this->runtime->curve_texture_matrices.data().as_span();
805}
806
808{
809 CurvesGeometry &curves = this->strokes_for_write();
811 SpanAttributeWriter<float> uv_rotations = attributes.lookup_or_add_for_write_span<float>(
812 "uv_rotation", AttrDomain::Curve);
814 "uv_translation", AttrDomain::Curve);
816 "uv_scale",
819
820 if (!uv_rotations || !uv_translations || !uv_scales) {
821 /* FIXME: It might be better to ensure the attributes exist and are on the right domain. */
822 return;
823 }
824
825 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
826 const Span<float3> positions = curves.positions();
827 const Span<float3> normals = this->curve_plane_normals();
828
829 selection.foreach_index(GrainSize(256), [&](const int64_t curve_i, const int64_t pos) {
830 const IndexRange points = points_by_curve[curve_i];
831 const float3 normal = normals[curve_i];
832 const float4x2 strokemat = get_local_to_stroke_matrix(positions.slice(points), normal);
833 const float4x2 texspace = matrices[pos];
834
835 /* We do the computation using doubles to avoid numerical precision errors. */
836 const double4x3 strokemat4x3 = double4x3(expand_4x2_mat(strokemat));
837
838 /*
839 * We want to solve for `texture_matrix` in the equation: `texspace = texture_matrix *
840 * strokemat4x3` Because these matrices are not square we can not use a standard inverse.
841 *
842 * Our problem has the form of: `X = A * Y`
843 * We can solve for `A` using: `A = X * B`
844 *
845 * Where `B` is the Right-sided inverse or Moore-Penrose pseudo inverse.
846 * Calculated as:
847 *
848 * |--------------------------|
849 * | B = T(Y) * (Y * T(Y))^-1 |
850 * |--------------------------|
851 *
852 * And `T()` is transpose and `()^-1` is the inverse.
853 */
854
855 const double3x4 transpose_strokemat = math::transpose(strokemat4x3);
856 const double3x4 right_inverse = transpose_strokemat *
857 math::invert(strokemat4x3 * transpose_strokemat);
858
859 const float3x2 texture_matrix = float3x2(double4x2(texspace) * right_inverse);
860
861 /* Solve for translation, the translation is simply the origin. */
862 const float2 uv_translation = texture_matrix[2];
863
864 /* Solve rotation, the angle of the `u` basis is the rotation. */
865 const float uv_rotation = math::atan2(texture_matrix[0][1], texture_matrix[0][0]);
866
867 /* Calculate the determinant to check if the `v` scale is negative. */
868 const float det = math::determinant(float2x2(texture_matrix));
869
870 /* Solve scale, scaling is the only transformation that changes the length, so scale factor
871 * is simply the length. And flip the sign of `v` if the determinant is negative. */
872 const float2 uv_scale = math::safe_rcp(float2(
873 math::length(texture_matrix[0]), math::sign(det) * math::length(texture_matrix[1])));
874
875 uv_rotations.span[curve_i] = uv_rotation;
876 uv_translations.span[curve_i] = uv_translation;
877 uv_scales.span[curve_i] = uv_scale;
878 });
879 uv_rotations.finish();
880 uv_translations.finish();
881 uv_scales.finish();
882
883 this->tag_texture_matrices_changed();
884}
885
887{
888 return this->geometry.wrap();
889}
890
892{
893 return this->geometry.wrap();
894}
895
897{
898 return *this->strokes().attributes().lookup_or_default<float>(
900}
901
903{
905 this->strokes_for_write(), AttrDomain::Point, ATTR_RADIUS, 0.01f);
906}
907
909{
910 return *this->strokes().attributes().lookup_or_default<float>(
912}
913
919
921{
922 return *this->strokes().attributes().lookup_or_default<ColorGeometry4f>(
923 ATTR_VERTEX_COLOR, AttrDomain::Point, ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
924}
925
933
935{
936 return *this->strokes().attributes().lookup_or_default<ColorGeometry4f>(
937 ATTR_FILL_COLOR, AttrDomain::Curve, ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
938}
939
947
949{
950 this->runtime->curve_texture_matrices.tag_dirty();
951}
952
954{
955 this->strokes_for_write().tag_positions_changed();
956 this->runtime->curve_plane_normals_cache.tag_dirty();
957 this->runtime->triangles_cache.tag_dirty();
958 this->tag_texture_matrices_changed();
959}
960
961void Drawing::tag_positions_changed(const IndexMask &changed_curves)
962{
963 if (changed_curves.is_empty()) {
964 return;
965 }
966 /* If more than half of the curves have changed, update the entire cache instead.
967 * The assumption here is that it's better to lazily compute the caches if more than half of the
968 * curves need to be updated.
969 * TODO: This could probably be a bit more rigorous once this function gets used in more places.
970 */
971 if (changed_curves.size() > this->strokes().curves_num() / 2) {
972 this->tag_positions_changed();
973 return;
974 }
975 if (!this->runtime->triangles_cache.is_cached() ||
976 !this->runtime->curve_plane_normals_cache.is_cached())
977 {
978 this->tag_positions_changed();
979 return;
980 }
981 /* Positions needs to be tagged first, because the triangle cache updates just after need the
982 * positions to be up-to-date. */
983 this->strokes_for_write().tag_positions_changed();
984 this->runtime->curve_plane_normals_cache.update([&](Vector<float3> &normals) {
985 const CurvesGeometry &curves = this->strokes();
987 curves.positions(), curves.points_by_curve(), changed_curves, normals);
988 });
989 this->runtime->triangles_cache.update([&](Vector<int3> &triangles) {
990 const CurvesGeometry &curves = this->strokes();
992 this->curve_plane_normals(),
994 this->triangle_offsets(),
995 curves.curves_range(),
996 triangles);
997 });
998 this->tag_texture_matrices_changed();
999}
1000
1002{
1003 this->runtime->triangle_offsets_cache.tag_dirty();
1004 this->tag_positions_changed();
1005 this->strokes_for_write().tag_topology_changed();
1006}
1007
1008void Drawing::tag_topology_changed(const IndexMask &changed_curves)
1009{
1010 if (changed_curves.is_empty()) {
1011 return;
1012 }
1013 /* If more than half of the curves have changed, update the entire cache instead.
1014 * The assumption here is that it's better to lazily compute the caches if more than half of the
1015 * curves need to be updated.
1016 * TODO: This could probably be a bit more rigorous once this function gets used in more places.
1017 */
1018 if (changed_curves.size() > this->strokes().curves_num() / 2) {
1019 this->tag_topology_changed();
1020 return;
1021 }
1022 if (!this->runtime->triangles_cache.is_cached() ||
1023 !this->runtime->curve_plane_normals_cache.is_cached())
1024 {
1025 this->tag_topology_changed();
1026 return;
1027 }
1028 /* Positions needs to be tagged first, because the triangle cache updates just after need the
1029 * positions to be up-to-date. */
1030 this->strokes_for_write().tag_positions_changed();
1031 this->runtime->curve_plane_normals_cache.update([&](Vector<float3> &normals) {
1032 const CurvesGeometry &curves = this->strokes();
1034 curves.positions(), curves.points_by_curve(), changed_curves, normals);
1035 });
1036 /* Copy the current triangle offsets. These are used to copy over the triangle data for curves
1037 * that don't need to be updated. */
1038 const Array<int> src_triangle_offset_data(this->triangle_offsets().data());
1039 const OffsetIndices<int> src_triangle_offsets = src_triangle_offset_data.as_span();
1040 /* Tag the `triangle_offsets_cache` so that the `triangles_cache` update can use the up-to-date
1041 * triangle offsets. */
1042 this->runtime->triangle_offsets_cache.tag_dirty();
1043
1044 this->runtime->triangles_cache.update([&](Vector<int3> &triangles) {
1045 const CurvesGeometry &curves = this->strokes();
1046 const OffsetIndices<int> dst_triangle_offsets = this->triangle_offsets();
1047
1048 IndexMaskMemory memory;
1049 const IndexMask curves_to_copy = changed_curves.complement(curves.curves_range(), memory);
1050
1051 const Vector<int3> src_triangles(triangles);
1052 triangles.reinitialize(dst_triangle_offsets.total_size());
1053 array_utils::copy_group_to_group(src_triangle_offsets,
1054 dst_triangle_offsets,
1055 curves_to_copy,
1056 src_triangles.as_span(),
1057 triangles.as_mutable_span());
1058
1060 this->curve_plane_normals(),
1062 dst_triangle_offsets,
1063 changed_curves,
1064 triangles);
1065 });
1066 this->tag_texture_matrices_changed();
1067}
1068
1069DrawingReference::DrawingReference()
1070{
1071 this->base.type = GP_DRAWING_REFERENCE;
1072 this->base.flag = 0;
1073
1074 this->id_reference = nullptr;
1075}
1076
1077DrawingReference::DrawingReference(const DrawingReference &other)
1078{
1079 this->base.type = GP_DRAWING_REFERENCE;
1080 this->base.flag = other.base.flag;
1081
1082 this->id_reference = other.id_reference;
1083}
1084
1085DrawingReference::~DrawingReference() = default;
1086
1089{
1090 BLI_assert(src_drawings.size() == dst_drawings.size());
1091 for (const int i : src_drawings.index_range()) {
1092 const GreasePencilDrawingBase *src_drawing_base = src_drawings[i];
1093 switch (src_drawing_base->type) {
1094 case GP_DRAWING: {
1095 const GreasePencilDrawing *src_drawing = reinterpret_cast<const GreasePencilDrawing *>(
1096 src_drawing_base);
1097 dst_drawings[i] = reinterpret_cast<GreasePencilDrawingBase *>(
1098 MEM_new<bke::greasepencil::Drawing>(__func__, src_drawing->wrap()));
1099 break;
1100 }
1101 case GP_DRAWING_REFERENCE: {
1102 const GreasePencilDrawingReference *src_drawing_reference =
1103 reinterpret_cast<const GreasePencilDrawingReference *>(src_drawing_base);
1104 dst_drawings[i] = reinterpret_cast<GreasePencilDrawingBase *>(
1105 MEM_new<bke::greasepencil::DrawingReference>(__func__, src_drawing_reference->wrap()));
1106 break;
1107 }
1108 }
1109 }
1110}
1111
1112TreeNode::TreeNode()
1113{
1114 this->next = this->prev = nullptr;
1115 this->parent = nullptr;
1116
1117 this->GreasePencilLayerTreeNode::name = nullptr;
1118 this->flag = 0;
1119 this->color[0] = this->color[1] = this->color[2] = 0;
1120}
1121
1122TreeNode::TreeNode(const GreasePencilLayerTreeNodeType type) : TreeNode()
1123{
1124 this->type = type;
1125}
1126
1128{
1129 this->type = type;
1130 this->GreasePencilLayerTreeNode::name = BLI_strdupn(name.data(), name.size());
1131}
1132
1134{
1135 this->GreasePencilLayerTreeNode::name = BLI_strdup_null(other.GreasePencilLayerTreeNode::name);
1136 this->flag = other.flag;
1137 copy_v3_v3(this->color, other.color);
1138}
1139
1144
1145void TreeNode::set_name(const StringRef name)
1146{
1148 this->GreasePencilLayerTreeNode::name = BLI_strdupn(name.data(), name.size());
1149}
1150
1152{
1153 return *reinterpret_cast<const LayerGroup *>(this);
1154}
1155
1157{
1158 return *reinterpret_cast<const Layer *>(this);
1159}
1160
1162{
1163 return *reinterpret_cast<LayerGroup *>(this);
1164}
1165
1167{
1168 return *reinterpret_cast<Layer *>(this);
1169}
1170
1172{
1173 return (this->parent) ? &this->parent->wrap() : nullptr;
1174}
1176{
1177 return (this->parent) ? &this->parent->wrap() : nullptr;
1178}
1180{
1181 return this->parent_group() ? &this->parent->wrap().as_node() : nullptr;
1182}
1184{
1185 return this->parent_group() ? &this->parent->wrap().as_node() : nullptr;
1186}
1187
1189{
1190 const LayerGroup *parent = this->parent_group();
1191 if (parent == nullptr) {
1192 /* The root group has a depth of 0. */
1193 return 0;
1194 }
1195 return 1 + parent->as_node().depth();
1196}
1197
1199{
1200 this->layer_name = nullptr;
1201 this->flag = 0;
1202}
1203
1205{
1206 this->layer_name = BLI_strdupn(name.data(), name.size());
1207}
1208
1210{
1211 this->layer_name = BLI_strdup_null(other.layer_name);
1212 this->flag = other.flag;
1213}
1214
1216{
1217 if (this->layer_name) {
1218 MEM_freeN(this->layer_name);
1219 }
1220}
1221
1223{
1224 frames_.clear();
1225 sorted_keys_cache_.tag_dirty();
1226 masks_.clear_and_shrink();
1227 trans_data_ = {};
1228}
1229
1231{
1232 new (&this->base) TreeNode(GP_LAYER_TREE_LEAF);
1233
1234 this->frames_storage.num = 0;
1235 this->frames_storage.keys = nullptr;
1236 this->frames_storage.values = nullptr;
1237 this->frames_storage.flag = 0;
1238
1240 this->opacity = 1.0f;
1241
1242 this->parent = nullptr;
1243 this->parsubstr = nullptr;
1244 unit_m4(this->parentinv);
1245
1246 zero_v3(this->translation);
1247 zero_v3(this->rotation);
1248 copy_v3_fl(this->scale, 1.0f);
1249
1250 this->viewlayername = nullptr;
1251
1252 BLI_listbase_clear(&this->masks);
1253 this->active_mask_index = 0;
1254
1255 this->runtime = MEM_new<LayerRuntime>(__func__);
1256}
1257
1259{
1260 new (&this->base) TreeNode(GP_LAYER_TREE_LEAF, name);
1261}
1262
1263Layer::Layer(const Layer &other) : Layer()
1264{
1265 new (&this->base) TreeNode(other.base.wrap());
1266
1267 LISTBASE_FOREACH (GreasePencilLayerMask *, other_mask, &other.masks) {
1268 LayerMask *new_mask = MEM_new<LayerMask>(__func__, *reinterpret_cast<LayerMask *>(other_mask));
1269 BLI_addtail(&this->masks, reinterpret_cast<GreasePencilLayerMask *>(new_mask));
1270 }
1272
1273 this->blend_mode = other.blend_mode;
1274 this->opacity = other.opacity;
1275
1276 this->parent = other.parent;
1277 this->set_parent_bone_name(other.parsubstr);
1278 copy_m4_m4(this->parentinv, other.parentinv);
1279
1280 copy_v3_v3(this->translation, other.translation);
1281 copy_v3_v3(this->rotation, other.rotation);
1282 copy_v3_v3(this->scale, other.scale);
1283
1285
1286 /* NOTE: We do not duplicate the frame storage since it is only needed for writing to file. */
1287 this->runtime->frames_ = other.runtime->frames_;
1288 this->runtime->sorted_keys_cache_ = other.runtime->sorted_keys_cache_;
1289 /* Tag the frames map, so the frame storage is recreated once the DNA is saved. */
1290 this->tag_frames_map_changed();
1291
1292 /* TODO: what about masks cache? */
1293}
1294
1296{
1297 this->base.wrap().~TreeNode();
1298
1301
1303 MEM_delete(reinterpret_cast<LayerMask *>(mask));
1304 }
1305 BLI_listbase_clear(&this->masks);
1306
1307 MEM_SAFE_FREE(this->parsubstr);
1309
1310 MEM_delete(this->runtime);
1311 this->runtime = nullptr;
1312}
1313
1315{
1316 return this->runtime->frames_;
1317}
1318
1320{
1321 return this->runtime->frames_;
1322}
1323
1324Layer::SortedKeysIterator Layer::remove_leading_end_frames_in_range(
1326{
1328 while (next_it != end && this->frames().lookup(*next_it).is_end()) {
1329 this->frames_for_write().remove_contained(*next_it);
1331 next_it = std::next(next_it);
1332 }
1333 return next_it;
1334}
1335
1336GreasePencilFrame *Layer::add_frame_internal(const FramesMapKeyT frame_number)
1337{
1338 if (!this->frames().contains(frame_number)) {
1340 this->frames_for_write().add_new(frame_number, frame);
1342 return this->frames_for_write().lookup_ptr(frame_number);
1343 }
1344 /* Overwrite end-frames. */
1345 if (this->frames().lookup(frame_number).is_end()) {
1347 this->frames_for_write().add_overwrite(frame_number, frame);
1348 this->tag_frames_map_changed();
1349 return this->frames_for_write().lookup_ptr(frame_number);
1350 }
1351 return nullptr;
1352}
1353
1354GreasePencilFrame *Layer::add_frame(const FramesMapKeyT key, /* const */ int duration)
1355{
1356 BLI_assert(duration == 0);
1357 duration = 0;
1358 GreasePencilFrame *frame = this->add_frame_internal(key);
1359 if (frame == nullptr) {
1360 return nullptr;
1361 }
1363 const FramesMapKeyT end_key = key + duration;
1364 /* Finds the next greater key that is stored in the map. */
1365 SortedKeysIterator next_key_it = std::upper_bound(sorted_keys.begin(), sorted_keys.end(), key);
1366 /* If the next frame we found is at the end of the frame we're inserting, then we are done. */
1367 if (next_key_it != sorted_keys.end() && *next_key_it == end_key) {
1368 return frame;
1369 }
1370 next_key_it = this->remove_leading_end_frames_in_range(next_key_it, sorted_keys.end());
1371 /* If the duration is set to 0, the frame is marked as an implicit hold. */
1372 if (duration == 0) {
1374 return frame;
1375 }
1376 /* If the next frame comes after the end of the frame we're inserting (or if there are no more
1377 * frames), add an end-frame. */
1378 if (next_key_it == sorted_keys.end() || *next_key_it > end_key) {
1379 this->frames_for_write().add_new(end_key, GreasePencilFrame::end());
1381 }
1382 return frame;
1383}
1384
1386{
1387 /* If the frame number is not in the frames map, do nothing. */
1388 if (!this->frames().contains(key)) {
1389 return false;
1390 }
1391 if (this->frames().size() == 1) {
1394 return true;
1395 }
1397 /* Find the index of the frame to remove in the `sorted_keys` array. */
1398 SortedKeysIterator remove_key_it = std::lower_bound(sorted_keys.begin(), sorted_keys.end(), key);
1399 /* If there is a next frame: */
1400 if (std::next(remove_key_it) != sorted_keys.end()) {
1401 SortedKeysIterator next_key_it = std::next(remove_key_it);
1402 this->remove_leading_end_frames_in_range(next_key_it, sorted_keys.end());
1403 }
1404 /* If there is a previous frame: */
1405 if (remove_key_it != sorted_keys.begin()) {
1406 SortedKeysIterator prev_key_it = std::prev(remove_key_it);
1407 const GreasePencilFrame &prev_frame = this->frames().lookup(*prev_key_it);
1408 /* If the previous frame is not an implicit hold (e.g. it has a fixed duration) and it's not an
1409 * end frame, we cannot just delete the frame. We need to replace it with an end frame. */
1410 /* FIXME: Disabled for now, do not create end frames ever. */
1411 if (false && !prev_frame.is_implicit_hold() && !prev_frame.is_end()) {
1412 this->frames_for_write().lookup(key) = GreasePencilFrame::end();
1413 this->tag_frames_map_changed();
1414 /* Since the original frame was replaced with an end frame, we consider the frame to be
1415 * successfully removed here. */
1416 return true;
1417 }
1418 }
1419 /* Finally, remove the actual frame. */
1422 return true;
1423}
1424
1426{
1427 this->runtime->sorted_keys_cache_.ensure([&](Vector<FramesMapKeyT> &r_data) {
1428 r_data.reinitialize(this->frames().size());
1429 int i = 0;
1430 for (const FramesMapKeyT key : this->frames().keys()) {
1431 r_data[i++] = key;
1432 }
1433 std::sort(r_data.begin(), r_data.end());
1434 });
1435 return this->runtime->sorted_keys_cache_.data();
1436}
1437
1439{
1441 /* No keyframes, return nullptr. */
1442 if (sorted_keys.is_empty()) {
1443 return nullptr;
1444 }
1445 /* Before the first frame, return nullptr. */
1446 if (frame_number < sorted_keys.first()) {
1447 return nullptr;
1448 }
1449 /* After or at the last frame, return iterator to last. */
1450 if (frame_number >= sorted_keys.last()) {
1451 return std::prev(sorted_keys.end());
1452 }
1453 /* Search for the frame. std::upper_bound will get the frame just after. */
1454 SortedKeysIterator it = std::upper_bound(sorted_keys.begin(), sorted_keys.end(), frame_number);
1455 if (it == sorted_keys.end()) {
1456 return nullptr;
1457 }
1458 return std::prev(it);
1459}
1460
1461std::optional<FramesMapKeyT> Layer::frame_key_at(const int frame_number) const
1462{
1463 SortedKeysIterator it = this->sorted_keys_iterator_at(frame_number);
1464 if (it == nullptr) {
1465 return {};
1466 }
1467 return *it;
1468}
1469
1470std::optional<int> Layer::start_frame_at(int frame_number) const
1471{
1472 const std::optional<FramesMapKeyT> frame_key = this->frame_key_at(frame_number);
1473 /* Return the frame number only if the frame key exists and if it's not an end frame. */
1474 if (frame_key && !this->frames().lookup_ptr(*frame_key)->is_end()) {
1475 return frame_key;
1476 }
1477 return {};
1478}
1479
1480int Layer::sorted_keys_index_at(const int frame_number) const
1481{
1482 SortedKeysIterator it = this->sorted_keys_iterator_at(frame_number);
1483 if (it == nullptr) {
1484 return -1;
1485 }
1486 return std::distance(this->sorted_keys().begin(), it);
1487}
1488
1489const GreasePencilFrame *Layer::frame_at(const int frame_number) const
1490{
1491 const GreasePencilFrame *frame_ptr = [&]() -> const GreasePencilFrame * {
1492 if (const GreasePencilFrame *frame = this->frames().lookup_ptr(frame_number)) {
1493 return frame;
1494 }
1495 /* Look for a keyframe that starts before `frame_number` and ends after `frame_number`. */
1496 const std::optional<FramesMapKeyT> frame_key = this->frame_key_at(frame_number);
1497 if (!frame_key) {
1498 return nullptr;
1499 }
1500 return this->frames().lookup_ptr(*frame_key);
1501 }();
1502 if (frame_ptr == nullptr || frame_ptr->is_end()) {
1503 /* Not a valid frame. */
1504 return nullptr;
1505 }
1506 return frame_ptr;
1507}
1508
1509GreasePencilFrame *Layer::frame_at(const int frame_number)
1510{
1511 GreasePencilFrame *frame_ptr = [&]() -> GreasePencilFrame * {
1512 if (GreasePencilFrame *frame = this->frames_for_write().lookup_ptr(frame_number)) {
1513 return frame;
1514 }
1515 /* Look for a keyframe that starts before `frame_number`. */
1516 const std::optional<FramesMapKeyT> frame_key = this->frame_key_at(frame_number);
1517 if (!frame_key) {
1518 return nullptr;
1519 }
1520 return this->frames_for_write().lookup_ptr(*frame_key);
1521 }();
1522 if (frame_ptr == nullptr || frame_ptr->is_end()) {
1523 /* Not a valid frame. */
1524 return nullptr;
1525 }
1526 return frame_ptr;
1527}
1528
1529int Layer::drawing_index_at(const int frame_number) const
1530{
1531 const GreasePencilFrame *frame = frame_at(frame_number);
1532 return (frame != nullptr) ? frame->drawing_index : -1;
1533}
1534
1535bool Layer::has_drawing_at(const int frame_number) const
1536{
1537 return frame_at(frame_number) != nullptr;
1538}
1539
1540int Layer::get_frame_duration_at(const int frame_number) const
1541{
1542 SortedKeysIterator it = this->sorted_keys_iterator_at(frame_number);
1543 if (it == nullptr) {
1544 return -1;
1545 }
1546 const FramesMapKeyT key = *it;
1547 const GreasePencilFrame *frame = this->frames().lookup_ptr(key);
1548 /* For frames that are implicitly held, we return a duration of 0. */
1549 if (frame->is_implicit_hold()) {
1550 return 0;
1551 }
1552 /* Frame is an end frame, so there is no keyframe at `frame_number`. */
1553 if (frame->is_end()) {
1554 return -1;
1555 }
1556 /* Last frame without a following end frame (end frames are not created anymore), it is
1557 * effectively held indefinitely. */
1558 if (std::next(it) == this->sorted_keys().end()) {
1559 return 0;
1560 } /* Compute the distance in frames between this key and the next key. */
1561 const int next_frame_number = *(std::next(it));
1562 return math::distance(key, next_frame_number);
1563}
1564
1569
1571{
1572 this->tag_frames_map_changed();
1573 this->runtime->sorted_keys_cache_.tag_dirty();
1574}
1575
1577{
1578 /* Re-create the frames storage only if it was tagged dirty. */
1580 return;
1581 }
1582
1585
1586 const size_t frames_num = size_t(frames().size());
1587 frames_storage.num = int(frames_num);
1588 frames_storage.keys = MEM_calloc_arrayN<int>(frames_num, __func__);
1590 const Span<int> sorted_keys_data = sorted_keys();
1591 for (const int64_t i : sorted_keys_data.index_range()) {
1592 frames_storage.keys[i] = sorted_keys_data[i];
1593 frames_storage.values[i] = frames().lookup(sorted_keys_data[i]);
1594 }
1595
1596 /* Reset the flag. */
1597 frames_storage.flag &= ~GP_LAYER_FRAMES_STORAGE_DIRTY;
1598}
1599
1601{
1602 /* Re-create frames data in runtime map. */
1603 /* NOTE: Avoid re-allocating runtime data to reduce 'false positive' change detection from
1604 * MEMFILE undo. */
1605 if (runtime) {
1606 runtime->clear();
1607 }
1608 else {
1609 runtime = MEM_new<blender::bke::greasepencil::LayerRuntime>(__func__);
1610 }
1612 for (int i = 0; i < frames_storage.num; i++) {
1614 }
1615}
1616
1618{
1619 if (this->parent == nullptr) {
1620 return object.object_to_world() * this->local_transform();
1621 }
1622 const Object &parent = *this->parent;
1623 return this->parent_to_world(parent) * this->parent_inverse() * this->local_transform();
1624}
1625
1627{
1628 if (this->parent == nullptr) {
1629 return this->local_transform();
1630 }
1631 const Object &parent = *this->parent;
1632 return object.world_to_object() * this->parent_to_world(parent) * this->parent_inverse() *
1633 this->local_transform();
1634}
1635
1637{
1638 return (this->parsubstr != nullptr) ? StringRefNull(this->parsubstr) : StringRefNull();
1639}
1640
1642{
1643 if (this->parsubstr != nullptr) {
1644 MEM_freeN(this->parsubstr);
1645 this->parsubstr = nullptr;
1646 }
1647 if (!new_name.is_empty()) {
1648 this->parsubstr = BLI_strdupn(new_name.data(), new_name.size());
1649 }
1650}
1651
1652float4x4 Layer::parent_to_world(const Object &parent) const
1653{
1654 const float4x4 &parent_object_to_world = parent.object_to_world();
1655 if (parent.type == OB_ARMATURE && !this->parent_bone_name().is_empty()) {
1657 this->parent_bone_name().c_str()))
1658 {
1659 return parent_object_to_world * float4x4_view(channel->pose_mat);
1660 }
1661 }
1662 return parent_object_to_world;
1663}
1664
1666{
1667 return float4x4_view(this->parentinv);
1668}
1669
1675
1677{
1679 *reinterpret_cast<float3 *>(this->translation),
1680 *reinterpret_cast<math::EulerXYZ *>(this->rotation),
1681 *reinterpret_cast<float3 *>(this->scale));
1682}
1683
1685{
1686 return (this->viewlayername != nullptr) ? StringRefNull(this->viewlayername) : StringRefNull();
1687}
1688
1690{
1691 if (this->viewlayername != nullptr) {
1692 MEM_freeN(this->viewlayername);
1693 this->viewlayername = nullptr;
1694 }
1695 if (!new_name.is_empty()) {
1696 this->viewlayername = BLI_strdupn(new_name.data(), new_name.size());
1697 }
1698}
1699
1701{
1702 new (&this->base) TreeNode(GP_LAYER_TREE_GROUP);
1703
1706
1707 this->runtime = MEM_new<LayerGroupRuntime>(__func__);
1708}
1709
1714
1716{
1717 new (&this->base) TreeNode(other.base.wrap());
1718
1720 switch (child->type) {
1721 case GP_LAYER_TREE_LEAF: {
1722 GreasePencilLayer *layer = reinterpret_cast<GreasePencilLayer *>(child);
1723 Layer *dup_layer = MEM_new<Layer>(__func__, layer->wrap());
1724 this->add_node(dup_layer->as_node());
1725 break;
1726 }
1727 case GP_LAYER_TREE_GROUP: {
1728 GreasePencilLayerTreeGroup *group = reinterpret_cast<GreasePencilLayerTreeGroup *>(child);
1729 LayerGroup *dup_group = MEM_new<LayerGroup>(__func__, group->wrap());
1730 this->add_node(dup_group->as_node());
1731 break;
1732 }
1733 }
1734 }
1735
1736 this->color_tag = other.color_tag;
1737}
1738
1740{
1741 this->base.wrap().~TreeNode();
1742
1744 switch (child->type) {
1745 case GP_LAYER_TREE_LEAF: {
1746 GreasePencilLayer *layer = reinterpret_cast<GreasePencilLayer *>(child);
1747 MEM_delete(&layer->wrap());
1748 break;
1749 }
1750 case GP_LAYER_TREE_GROUP: {
1751 GreasePencilLayerTreeGroup *group = reinterpret_cast<GreasePencilLayerTreeGroup *>(child);
1752 MEM_delete(&group->wrap());
1753 break;
1754 }
1755 }
1756 }
1757
1758 MEM_delete(this->runtime);
1759 this->runtime = nullptr;
1760}
1761
1763{
1764 if (this == &other) {
1765 return *this;
1766 }
1767
1768 this->~LayerGroup();
1769 new (this) LayerGroup(other);
1770
1771 return *this;
1772}
1773
1775{
1776 return BLI_listbase_is_empty(&this->children);
1777}
1778
1780{
1781 BLI_addtail(&this->children, &node);
1782 node.parent = reinterpret_cast<GreasePencilLayerTreeGroup *>(this);
1783 this->tag_nodes_cache_dirty();
1784 return node;
1785}
1787{
1788 BLI_assert(BLI_findindex(&this->children, &link) != -1);
1789 BLI_insertlinkbefore(&this->children, &link, &node);
1790 node.parent = reinterpret_cast<GreasePencilLayerTreeGroup *>(this);
1791 this->tag_nodes_cache_dirty();
1792}
1794{
1795 BLI_assert(BLI_findindex(&this->children, &link) != -1);
1796 BLI_insertlinkafter(&this->children, &link, &node);
1797 node.parent = reinterpret_cast<GreasePencilLayerTreeGroup *>(this);
1798 this->tag_nodes_cache_dirty();
1799}
1800
1802{
1803 BLI_listbase_link_move(&this->children, &node, step);
1804 this->tag_nodes_cache_dirty();
1805}
1807{
1808 BLI_listbase_link_move(&this->children, &node, -step);
1809 this->tag_nodes_cache_dirty();
1810}
1812{
1813 BLI_remlink(&this->children, &node);
1814 BLI_insertlinkafter(&this->children, this->children.last, &node);
1815 this->tag_nodes_cache_dirty();
1816}
1818{
1819 BLI_remlink(&this->children, &node);
1820 BLI_insertlinkbefore(&this->children, this->children.first, &node);
1821 this->tag_nodes_cache_dirty();
1822}
1823
1825{
1826 return BLI_listbase_count(&this->children);
1827}
1828
1830{
1831 this->ensure_nodes_cache();
1832 return this->runtime->nodes_cache_.size();
1833}
1834
1835bool LayerGroup::unlink_node(TreeNode &link, const bool keep_children)
1836{
1837 if (link.is_group() && !link.as_group().is_empty() && keep_children) {
1838 if (BLI_findindex(&this->children, &link) == -1) {
1839 return false;
1840 }
1841
1842 /* Take ownership of the children of `link` by replacing the node with the listbase of its
1843 * children. */
1844 ListBase link_children = link.as_group().children;
1846 link_children.first);
1847 GreasePencilLayerTreeNode *last = static_cast<GreasePencilLayerTreeNode *>(link_children.last);
1848
1849 /* Rewrite the parent pointers. */
1850 LISTBASE_FOREACH (GreasePencilLayerTreeNode *, child, &link_children) {
1851 child->parent = this;
1852 }
1853
1854 /* Update previous and/or next link(s). */
1855 if (link.next != nullptr) {
1856 link.next->prev = last;
1857 last->next = link.next;
1858 }
1859 if (link.prev != nullptr) {
1860 link.prev->next = first;
1861 first->prev = link.prev;
1862 }
1863
1864 /* Update first and/or last link(s). */
1865 if (this->children.last == &link) {
1866 this->children.last = last;
1867 }
1868 if (this->children.first == &link) {
1869 this->children.first = first;
1870 }
1871
1872 /* Listbase has been inserted in `this->children` we can clear the pointers in `link`. */
1874 link.parent = nullptr;
1875
1876 this->tag_nodes_cache_dirty();
1877 return true;
1878 }
1879 if (BLI_remlink_safe(&this->children, &link)) {
1880 link.parent = nullptr;
1881 this->tag_nodes_cache_dirty();
1882 return true;
1883 }
1884 return false;
1885}
1886
1888{
1889 this->ensure_nodes_cache();
1890 return this->runtime->nodes_cache_.as_span();
1891}
1892
1894{
1895 this->ensure_nodes_cache();
1896 return this->runtime->nodes_cache_.as_span();
1897}
1898
1900{
1901 this->ensure_nodes_cache();
1902 return this->runtime->layer_cache_.as_span();
1903}
1904
1906{
1907 this->ensure_nodes_cache();
1908 return this->runtime->layer_cache_.as_span();
1909}
1910
1912{
1913 this->ensure_nodes_cache();
1914 return this->runtime->layer_group_cache_.as_span();
1915}
1916
1918{
1919 this->ensure_nodes_cache();
1920 return this->runtime->layer_group_cache_.as_span();
1921}
1922
1924{
1925 for (const TreeNode *node : this->nodes()) {
1926 if (node->name() == name) {
1927 return node;
1928 }
1929 }
1930 return nullptr;
1931}
1932
1934{
1935 for (TreeNode *node : this->nodes_for_write()) {
1936 if (node->name() == name) {
1937 return node;
1938 }
1939 }
1940 return nullptr;
1941}
1942
1944{
1945 return (this->base.flag & GP_LAYER_TREE_NODE_EXPANDED) != 0;
1946}
1947
1948void LayerGroup::set_expanded(const bool expanded)
1949{
1951}
1952
1953void LayerGroup::print_nodes(const StringRef header) const
1954{
1955 std::cout << header << std::endl;
1958 TreeNode *child = reinterpret_cast<TreeNode *>(child_);
1959 next_node.push(std::make_pair(1, child));
1960 }
1961 while (!next_node.is_empty()) {
1962 auto [indent, node] = next_node.pop();
1963 for (int i = 0; i < indent; i++) {
1964 std::cout << " ";
1965 }
1966 if (node->is_layer()) {
1967 std::cout << node->name();
1968 }
1969 else if (node->is_group()) {
1970 std::cout << node->name() << ": ";
1971 LISTBASE_FOREACH_BACKWARD (GreasePencilLayerTreeNode *, child_, &node->as_group().children) {
1972 TreeNode *child = reinterpret_cast<TreeNode *>(child_);
1973 next_node.push(std::make_pair(indent + 1, child));
1974 }
1975 }
1976 std::cout << std::endl;
1977 }
1978 std::cout << std::endl;
1979}
1980
1981void LayerGroup::ensure_nodes_cache() const
1982{
1983 this->runtime->nodes_cache_mutex_.ensure([&]() {
1984 this->runtime->nodes_cache_.clear_and_shrink();
1985 this->runtime->layer_cache_.clear_and_shrink();
1986 this->runtime->layer_group_cache_.clear_and_shrink();
1987
1989 TreeNode *node = reinterpret_cast<TreeNode *>(child_);
1990 this->runtime->nodes_cache_.append(node);
1991 switch (node->type) {
1992 case GP_LAYER_TREE_LEAF: {
1993 this->runtime->layer_cache_.append(&node->as_layer());
1994 break;
1995 }
1996 case GP_LAYER_TREE_GROUP: {
1997 this->runtime->layer_group_cache_.append(&node->as_group());
1998 for (TreeNode *child : node->as_group().nodes_for_write()) {
1999 this->runtime->nodes_cache_.append(child);
2000 if (child->is_layer()) {
2001 this->runtime->layer_cache_.append(&child->as_layer());
2002 }
2003 else if (child->is_group()) {
2004 this->runtime->layer_group_cache_.append(&child->as_group());
2005 }
2006 }
2007 break;
2008 }
2009 }
2010 }
2011 });
2012}
2013
2014void LayerGroup::tag_nodes_cache_dirty() const
2015{
2016 this->runtime->nodes_cache_mutex_.tag_dirty();
2017 if (this->base.parent) {
2018 this->base.parent->wrap().tag_nodes_cache_dirty();
2019 }
2020}
2021
2023{
2024 LISTBASE_FOREACH (TreeNode *, child, &children) {
2025 switch (child->type) {
2026 case GP_LAYER_TREE_LEAF: {
2027 child->as_layer().prepare_for_dna_write();
2028 break;
2029 }
2030 case GP_LAYER_TREE_GROUP: {
2031 child->as_group().prepare_for_dna_write();
2032 break;
2033 }
2034 }
2035 }
2036}
2037
2039{
2040 LISTBASE_FOREACH (TreeNode *, child, &children) {
2041 switch (child->type) {
2042 case GP_LAYER_TREE_LEAF: {
2043 child->as_layer().update_from_dna_read();
2044 break;
2045 }
2046 case GP_LAYER_TREE_GROUP: {
2047 child->as_group().update_from_dna_read();
2048 break;
2049 }
2050 }
2051 }
2052}
2053
2055{
2056 for (bke::greasepencil::Layer *layer : grease_pencil.layers_for_write()) {
2057 if (layer->name().is_empty()) {
2058 grease_pencil.rename_node(bmain, layer->as_node(), DATA_("Layer"));
2059 }
2060 }
2061}
2062
2063} // namespace blender::bke::greasepencil
2064
2065namespace blender::bke {
2066
2069
2070std::optional<Span<float3>> GreasePencilDrawingEditHints::positions() const
2071{
2072 if (!this->positions_data.has_value()) {
2073 return std::nullopt;
2074 }
2075 const int points_num = this->drawing_orig->geometry.wrap().points_num();
2076 return Span(static_cast<const float3 *>(this->positions_data.data), points_num);
2077}
2078
2079std::optional<MutableSpan<float3>> GreasePencilDrawingEditHints::positions_for_write()
2080{
2081 if (!this->positions_data.has_value()) {
2082 return std::nullopt;
2083 }
2084
2085 const int points_num = this->drawing_orig->geometry.wrap().points_num();
2087 if (data.sharing_info->is_mutable()) {
2088 /* If the referenced component is already mutable, return it directly. */
2089 data.sharing_info->tag_ensured_mutable();
2090 }
2091 else {
2092 auto *new_sharing_info = new ImplicitSharedValue<Array<float3>>(*this->positions());
2093 data.sharing_info = ImplicitSharingPtr<>(new_sharing_info);
2094 data.data = new_sharing_info->data.data();
2095 }
2096
2097 return MutableSpan(const_cast<float3 *>(static_cast<const float3 *>(data.data)), points_num);
2098}
2099
2100} // namespace blender::bke
2101
2102/* ------------------------------------------------------------------- */
2108{
2109 return name == ATTR_POSITION;
2110}
2111
2113{
2114 GreasePencil *grease_pencil = BKE_id_new<GreasePencil>(bmain, name);
2115
2116 return grease_pencil;
2117}
2118
2120{
2121 GreasePencil *grease_pencil = BKE_id_new_nomain<GreasePencil>(nullptr);
2122 return grease_pencil;
2123}
2124
2126{
2127 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(
2128 BKE_id_copy_ex(nullptr, &grease_pencil_src->id, nullptr, LIB_ID_COPY_LOCALIZE));
2129 grease_pencil->runtime->eval_frame = grease_pencil_src->runtime->eval_frame;
2130 return grease_pencil;
2131}
2132
2143
2146{
2147 using namespace blender::bke::greasepencil;
2148 dst.as_node().flag = src.as_node().flag;
2149 copy_v3_v3(dst.as_node().color, src.as_node().color);
2150
2151 dst.blend_mode = src.blend_mode;
2152 dst.opacity = src.opacity;
2153
2154 LISTBASE_FOREACH (GreasePencilLayerMask *, src_mask, &src.masks) {
2155 LayerMask *new_mask = MEM_new<LayerMask>(__func__, *reinterpret_cast<LayerMask *>(src_mask));
2156 BLI_addtail(&dst.masks, reinterpret_cast<GreasePencilLayerMask *>(new_mask));
2157 }
2159
2160 dst.parent = src.parent;
2162 copy_m4_m4(dst.parentinv, src.parentinv);
2163
2165 copy_v3_v3(dst.rotation, src.rotation);
2166 copy_v3_v3(dst.scale, src.scale);
2167
2169}
2170
2179
2181 GreasePencil *grease_pencil_dst)
2182{
2183 using namespace blender;
2186
2187 /* Drawings. */
2188 free_drawing_array(*grease_pencil_dst);
2189 grease_pencil_dst->resize_drawings(grease_pencil_src->drawing_array_num);
2190 for (const int i : IndexRange(grease_pencil_dst->drawing_array_num)) {
2191 switch (grease_pencil_src->drawing_array[i]->type) {
2192 case GP_DRAWING: {
2193 const Drawing &src_drawing =
2194 reinterpret_cast<GreasePencilDrawing *>(grease_pencil_src->drawing_array[i])->wrap();
2195 grease_pencil_dst->drawing_array[i] = &MEM_new<Drawing>(__func__, src_drawing)->base;
2196 break;
2197 }
2199 const DrawingReference &src_drawing_ref = reinterpret_cast<GreasePencilDrawingReference *>(
2200 grease_pencil_src->drawing_array[i])
2201 ->wrap();
2202 grease_pencil_dst->drawing_array[i] =
2203 &MEM_new<DrawingReference>(__func__, src_drawing_ref)->base;
2204 break;
2205 }
2206 }
2207
2208 /* Layers. */
2209 CustomData_free(&grease_pencil_dst->layers_data);
2210 if (grease_pencil_dst->root_group_ptr) {
2211 MEM_delete(&grease_pencil_dst->root_group());
2212 }
2213
2214 grease_pencil_dst->root_group_ptr = MEM_new<bke::greasepencil::LayerGroup>(
2215 __func__, grease_pencil_src->root_group_ptr->wrap());
2216 BLI_assert(grease_pencil_src->layers().size() == grease_pencil_dst->layers().size());
2217
2218 /* Reset the active node. */
2219 grease_pencil_dst->active_node = nullptr;
2220
2221 CustomData_init_from(&grease_pencil_src->layers_data,
2222 &grease_pencil_dst->layers_data,
2223 eCustomDataMask(CD_MASK_ALL),
2224 grease_pencil_src->layers().size());
2225
2226 DEG_id_tag_update(&grease_pencil_dst->id, ID_RECALC_GEOMETRY);
2227
2228 BKE_id_free(nullptr, grease_pencil_src);
2229}
2230
2231void BKE_grease_pencil_vgroup_name_update(Object *ob, const char *old_name, const char *new_name)
2232{
2233 using namespace blender::bke::greasepencil;
2234 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(ob->data);
2235 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
2236 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2237 CurvesGeometry &curves = drawing.strokes_for_write();
2238 LISTBASE_FOREACH (bDeformGroup *, vgroup, &curves.vertex_group_names) {
2239 if (STREQ(vgroup->name, old_name)) {
2240 STRNCPY(vgroup->name, new_name);
2241 }
2242 }
2243 }
2244}
2245
2247 Scene *scene,
2248 Object *object,
2249 blender::bke::GeometrySet &geometry_set)
2250{
2251 /* Modifier evaluation modes. */
2252 const bool use_render = DEG_get_mode(depsgraph) == DAG_EVAL_RENDER;
2253 ModifierMode required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
2254 if (BKE_object_is_in_editmode(object)) {
2255 required_mode |= eModifierMode_Editmode;
2256 }
2257 ModifierApplyFlag apply_flag = use_render ? MOD_APPLY_RENDER : MOD_APPLY_USECACHE;
2258 const ModifierEvalContext mectx = {depsgraph, object, apply_flag};
2259
2261
2262 /* Get effective list of modifiers to execute. Some effects like shape keys
2263 * are added as virtual modifiers before the user created modifiers. */
2264 VirtualModifierData virtualModifierData;
2265 ModifierData *md = BKE_modifiers_get_virtual_modifierlist(object, &virtualModifierData);
2266
2267 /* Evaluate time modifiers.
2268 * The time offset modifier can change what drawings are shown on the current frame. But it
2269 * doesn't affect the drawings data. Modifiers that modify the drawings data are only evaluated
2270 * for the current frame, so we run the time offset modifiers before all the other ones. */
2271 ModifierData *tmd = md;
2272 for (; tmd; tmd = tmd->next) {
2274
2275 if (!BKE_modifier_is_enabled(scene, tmd, required_mode) ||
2277 {
2278 continue;
2279 }
2280
2281 blender::bke::ScopedModifierTimer modifier_timer{*md};
2282
2283 if (mti->modify_geometry_set != nullptr) {
2284 mti->modify_geometry_set(tmd, &mectx, &geometry_set);
2285 }
2286 }
2287
2288 /* Evaluate drawing modifiers. */
2289 for (; md; md = md->next) {
2291
2292 if (!BKE_modifier_is_enabled(scene, md, required_mode) ||
2294 {
2295 continue;
2296 }
2297
2298 blender::bke::ScopedModifierTimer modifier_timer{*md};
2299
2300 if (mti->modify_geometry_set != nullptr) {
2301 mti->modify_geometry_set(md, &mectx, &geometry_set);
2302 }
2303 }
2304}
2305
2307{
2308 using namespace blender;
2309 using namespace bke::greasepencil;
2310
2311 const bke::AttributeAccessor layer_attributes = grease_pencil.attributes();
2312
2313 struct LayerDrawingInfo {
2314 Drawing *drawing;
2315 const int layer_index;
2316 };
2317
2318 Set<Drawing *> all_drawings;
2319 Vector<LayerDrawingInfo> drawing_infos;
2320 for (const int layer_i : grease_pencil.layers().index_range()) {
2321 const Layer &layer = grease_pencil.layer(layer_i);
2322 /* Set of owned drawings, ignore drawing references to other data blocks. */
2323 if (Drawing *drawing = grease_pencil.get_eval_drawing(layer)) {
2324 if (all_drawings.add(drawing)) {
2325 drawing_infos.append({drawing, layer_i});
2326 }
2327 }
2328 }
2329
2330 if (layer_attributes.contains("radius_offset")) {
2331 const VArray<float> radius_offsets = *layer_attributes.lookup_or_default<float>(
2332 "radius_offset", bke::AttrDomain::Layer, 0.0f);
2333 threading::parallel_for_each(drawing_infos, [&](LayerDrawingInfo &info) {
2334 if (radius_offsets[info.layer_index] == 0.0f) {
2335 return;
2336 }
2337 MutableSpan<float> radii = info.drawing->radii_for_write();
2338 threading::parallel_for(radii.index_range(), 4096, [&](const IndexRange range) {
2339 for (const int i : range) {
2340 radii[i] += radius_offsets[info.layer_index];
2341 }
2342 });
2343 });
2344 }
2345
2346 if (layer_attributes.contains("tint_color")) {
2347 auto mix_tint = [](const float4 base, const float4 tint) -> float4 {
2348 return base * (1.0 - tint.w) + tint * tint.w;
2349 };
2350 const VArray<ColorGeometry4f> tint_colors =
2351 *layer_attributes.lookup_or_default<ColorGeometry4f>(
2352 "tint_color", bke::AttrDomain::Layer, ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
2353 threading::parallel_for_each(drawing_infos, [&](LayerDrawingInfo &info) {
2354 if (tint_colors[info.layer_index].a == 0.0f) {
2355 return;
2356 }
2357 MutableSpan<ColorGeometry4f> vertex_colors = info.drawing->vertex_colors_for_write();
2358 threading::parallel_for(vertex_colors.index_range(), 4096, [&](const IndexRange range) {
2359 for (const int i : range) {
2360 vertex_colors[i] = ColorGeometry4f(
2361 mix_tint(float4(vertex_colors[i]), float4(tint_colors[info.layer_index])));
2362 }
2363 });
2364 MutableSpan<ColorGeometry4f> fill_colors = info.drawing->fill_colors_for_write();
2365 threading::parallel_for(fill_colors.index_range(), 4096, [&](const IndexRange range) {
2366 for (const int i : range) {
2367 fill_colors[i] = ColorGeometry4f(
2368 mix_tint(float4(fill_colors[i]), float4(tint_colors[info.layer_index])));
2369 }
2370 });
2371 });
2372 }
2373}
2374
2376{
2377 using namespace blender;
2378 using namespace blender::bke::greasepencil;
2379
2380 /* Copy the layer cache into an array here, because removing a layer will invalidate the layer
2381 * cache. This will only copy the pointers to the layers, not the layers themselves. */
2382 Array<Layer *> layers = grease_pencil.layers_for_write();
2383
2384 for (const int layer_i : layers.index_range()) {
2385 Layer *layer = layers[layer_i];
2386 /* Store the original index of the layer. */
2387 layer->runtime->orig_layer_index_ = layer_i;
2388 /* When the visibility is animated, the layer should be retained even when it is invisible.
2389 * Changing the visibility through the animation system does NOT create another evaluated copy,
2390 * and thus the layer has to be kept for this future use. */
2391 if (layer->is_visible() || layer->runtime->is_visibility_animated_) {
2392 continue;
2393 }
2394
2395 /* Remove layer from evaluated data. */
2396 grease_pencil.remove_layer(*layer);
2397 }
2398}
2399
2401{
2402 using namespace blender;
2403 /* Store the frame that this grease pencil is evaluated on. */
2404 grease_pencil->runtime->eval_frame = int(DEG_get_ctime(depsgraph));
2405 /* This will remove layers that aren't visible. */
2406 grease_pencil_evaluate_layers(*grease_pencil);
2407}
2408
2410{
2411 using namespace blender;
2412 using namespace blender::bke;
2413 /* Free any evaluated data and restore original data. */
2415
2416 GreasePencil *grease_pencil = static_cast<GreasePencil *>(object->data);
2417 GeometrySet geometry_set = GeometrySet::from_grease_pencil(grease_pencil,
2418 GeometryOwnershipType::ReadOnly);
2419 /* The layer adjustments for tinting and radii offsets are applied before modifier evaluation.
2420 * This ensures that the evaluated geometry contains the modifications. In the future, it would
2421 * be better to move these into modifiers. For now, these are hardcoded. */
2422 const bke::AttributeAccessor layer_attributes = grease_pencil->attributes();
2423 if (layer_attributes.contains("tint_color") || layer_attributes.contains("radius_offset")) {
2425 }
2426 /* Only add the edit hint component in modes where users can potentially interact with deformed
2427 * drawings. */
2428 if (ELEM(object->mode,
2433 {
2434 GeometryComponentEditData &edit_component =
2436 edit_component.grease_pencil_edit_hints_ = std::make_unique<GreasePencilEditHints>(
2437 *static_cast<const GreasePencil *>(DEG_get_original(object)->data));
2438 }
2439 grease_pencil_evaluate_modifiers(depsgraph, scene, object, geometry_set);
2440
2441 if (geometry_set.has_grease_pencil()) {
2442 /* Output geometry set may be different from the input,
2443 * set the frame again to ensure a correct value. */
2444 geometry_set.get_grease_pencil()->runtime->eval_frame = int(DEG_get_ctime(depsgraph));
2445 }
2446 else {
2447 GreasePencil *empty_grease_pencil = BKE_grease_pencil_new_nomain();
2448 empty_grease_pencil->runtime->eval_frame = int(DEG_get_ctime(depsgraph));
2449 geometry_set.replace_grease_pencil(empty_grease_pencil);
2450 }
2451
2452 /* For now the evaluated data is not const. We could use #get_grease_pencil_for_write, but that
2453 * would result in a copy when it's shared. So for now, we use a const_cast here. */
2454 GreasePencil *grease_pencil_eval = const_cast<GreasePencil *>(geometry_set.get_grease_pencil());
2455
2456 /* Assign evaluated object. */
2457 BKE_object_eval_assign_data(object, &grease_pencil_eval->id, false);
2458 object->runtime->geometry_set_eval = new GeometrySet(std::move(geometry_set));
2459}
2460
2462 GreasePencil *grease_pencil_dst)
2463{
2464 using namespace blender;
2465 grease_pencil_dst->drawing_array_num = grease_pencil_src->drawing_array_num;
2466 if (grease_pencil_dst->drawing_array_num > 0) {
2468 grease_pencil_src->drawing_array_num, __func__);
2469 bke::greasepencil::copy_drawing_array(grease_pencil_src->drawings(),
2470 grease_pencil_dst->drawings());
2471 }
2472}
2473
2476/* ------------------------------------------------------------------- */
2482{
2483 using namespace blender;
2484
2485 for (const GreasePencilDrawingBase *base : grease_pencil.drawings()) {
2486 if (base->type != GP_DRAWING) {
2487 continue;
2488 }
2489 const bke::greasepencil::Drawing &drawing =
2490 reinterpret_cast<const GreasePencilDrawing *>(base)->wrap();
2491 const bke::CurvesGeometry &curves = drawing.strokes();
2492 if (curves.has_curve_with_type(type)) {
2493 return true;
2494 }
2495 }
2496
2497 return false;
2498}
2499
2501{
2502 using namespace blender;
2503
2504 int total_points = 0;
2505
2506 for (const int layer_i : grease_pencil.layers().index_range()) {
2507 const bke::greasepencil::Layer &layer = grease_pencil.layer(layer_i);
2508 const Map<bke::greasepencil::FramesMapKeyT, GreasePencilFrame> frames = layer.frames();
2509 frames.foreach_item(
2510 [&](const bke::greasepencil::FramesMapKeyT /*key*/, const GreasePencilFrame frame) {
2511 const GreasePencilDrawingBase *base = grease_pencil.drawing(frame.drawing_index);
2512 if (base->type != GP_DRAWING) {
2513 return;
2514 }
2515 const bke::greasepencil::Drawing &drawing =
2516 reinterpret_cast<const GreasePencilDrawing *>(base)->wrap();
2517 const bke::CurvesGeometry &curves = drawing.strokes();
2518 total_points += curves.points_num();
2519 });
2520 }
2521
2522 return total_points;
2523}
2524
2528{
2529 using namespace blender;
2530 int64_t index = 0;
2531 for (const int layer_i : grease_pencil.layers().index_range()) {
2532 const bke::greasepencil::Layer &layer = grease_pencil.layer(layer_i);
2533 const float4x4 layer_to_object = layer.local_transform();
2534 const Map<bke::greasepencil::FramesMapKeyT, GreasePencilFrame> frames = layer.frames();
2535 frames.foreach_item(
2536 [&](const bke::greasepencil::FramesMapKeyT /*key*/, const GreasePencilFrame frame) {
2537 const GreasePencilDrawingBase *base = grease_pencil.drawing(frame.drawing_index);
2538 if (base->type != GP_DRAWING) {
2539 return;
2540 }
2541 const bke::greasepencil::Drawing &drawing =
2542 reinterpret_cast<const GreasePencilDrawing *>(base)->wrap();
2543 const bke::CurvesGeometry &curves = drawing.strokes();
2544 const Span<float3> positions = curves.positions();
2545 const VArray<float> radii = drawing.radii();
2546
2548 for (const int i : curves.points_range()) {
2549 all_positions[index] = math::transform_point(layer_to_object, positions[i]);
2550 all_radii[index] = radii[i];
2551 index++;
2552 }
2553 }
2554 else {
2555 const Span<float3> handle_positions_left = curves.handle_positions_left();
2556 const Span<float3> handle_positions_right = curves.handle_positions_right();
2557 for (const int i : curves.points_range()) {
2558 const int index_pos = index * 3;
2559 all_positions[index_pos] = math::transform_point(layer_to_object,
2560 handle_positions_left[i]);
2561 all_positions[index_pos + 1] = math::transform_point(layer_to_object, positions[i]);
2562 all_positions[index_pos + 2] = math::transform_point(layer_to_object,
2563 handle_positions_right[i]);
2564 all_radii[index] = radii[i];
2565 index++;
2566 }
2567 }
2568 });
2569 }
2570}
2571
2573 blender::Span<blender::float3> all_positions,
2574 blender::Span<float> all_radii)
2575{
2576 using namespace blender;
2577 int64_t index = 0;
2578 for (const int layer_i : grease_pencil.layers().index_range()) {
2579 bke::greasepencil::Layer &layer = grease_pencil.layer(layer_i);
2580 const float4x4 layer_to_object = layer.local_transform();
2581 const float4x4 object_to_layer = math::invert(layer_to_object);
2582 const Map<bke::greasepencil::FramesMapKeyT, GreasePencilFrame> frames = layer.frames();
2583 frames.foreach_item([&](bke::greasepencil::FramesMapKeyT /*key*/, GreasePencilFrame frame) {
2584 GreasePencilDrawingBase *base = grease_pencil.drawing(frame.drawing_index);
2585 if (base->type != GP_DRAWING) {
2586 return;
2587 }
2588 bke::greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2589 bke::CurvesGeometry &curves = drawing.strokes_for_write();
2590
2591 MutableSpan<float3> positions = curves.positions_for_write();
2592 MutableSpan<float> radii = drawing.radii_for_write();
2593
2595 for (const int i : curves.points_range()) {
2596 positions[i] = math::transform_point(object_to_layer, all_positions[index]);
2597 radii[i] = all_radii[index];
2598 index++;
2599 }
2600 }
2601 else {
2602 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
2603 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
2604 for (const int i : curves.points_range()) {
2605 const int index_pos = index * 3;
2606 handle_positions_left[i] = math::transform_point(object_to_layer,
2607 all_positions[index_pos]);
2608 positions[i] = math::transform_point(object_to_layer, all_positions[index_pos + 1]);
2609 handle_positions_right[i] = math::transform_point(object_to_layer,
2610 all_positions[index_pos + 2]);
2611 radii[i] = all_radii[index];
2612 index++;
2613 }
2614 }
2615
2616 curves.tag_radii_changed();
2617 drawing.tag_positions_changed();
2618 });
2619 }
2620}
2621
2623 blender::Span<blender::float3> all_positions,
2624 blender::Span<float> all_radii,
2625 const blender::float4x4 &mat)
2626{
2627 using namespace blender;
2628 const float scalef = mat4_to_scale(mat.ptr());
2629 int64_t index = 0;
2630 for (const int layer_i : grease_pencil.layers().index_range()) {
2631 bke::greasepencil::Layer &layer = grease_pencil.layer(layer_i);
2632 const float4x4 layer_to_object = layer.local_transform();
2633 const float4x4 object_to_layer = math::invert(layer_to_object);
2634 const Map<bke::greasepencil::FramesMapKeyT, GreasePencilFrame> frames = layer.frames();
2635 frames.foreach_item([&](bke::greasepencil::FramesMapKeyT /*key*/, GreasePencilFrame frame) {
2636 GreasePencilDrawingBase *base = grease_pencil.drawing(frame.drawing_index);
2637 if (base->type != GP_DRAWING) {
2638 return;
2639 }
2640 bke::greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2641 bke::CurvesGeometry &curves = drawing.strokes_for_write();
2642
2643 MutableSpan<float3> positions = curves.positions_for_write();
2644 MutableSpan<float> radii = drawing.radii_for_write();
2645
2647 for (const int i : curves.points_range()) {
2648 positions[i] = math::transform_point(object_to_layer * mat, all_positions[index]);
2649 radii[i] = all_radii[index] * scalef;
2650 index++;
2651 }
2652 }
2653 else {
2654 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
2655 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
2656 for (const int i : curves.points_range()) {
2657 const int index_pos = index * 3;
2658 handle_positions_left[i] = math::transform_point(object_to_layer * mat,
2659 all_positions[index_pos]);
2660 positions[i] = math::transform_point(object_to_layer * mat,
2661 all_positions[index_pos + 1]);
2662 handle_positions_right[i] = math::transform_point(object_to_layer * mat,
2663 all_positions[index_pos + 2]);
2664 radii[i] = all_radii[index] * scalef;
2665 index++;
2666 }
2667 }
2668
2669 curves.tag_radii_changed();
2670 drawing.tag_positions_changed();
2671 });
2672 }
2673}
2674
2677/* ------------------------------------------------------------------- */
2682{
2683 short *totcol = BKE_object_material_len_p(ob);
2684 Material *read_ma = nullptr;
2685 for (short i = 0; i < *totcol; i++) {
2686 read_ma = BKE_object_material_get(ob, i + 1);
2687 if (STREQ(name, read_ma->id.name + 2)) {
2688 return i;
2689 }
2690 }
2691
2692 return -1;
2693}
2694
2696 Object *ob,
2697 const char *name,
2698 int *r_index)
2699{
2701 id_us_min(&ma->id); /* no users yet */
2702
2705
2706 if (r_index) {
2707 *r_index = ob->actcol - 1;
2708 }
2709 return ma;
2710}
2711
2713{
2714 if (brush && brush->gpencil_settings &&
2716 {
2717 return brush->gpencil_settings->material;
2718 }
2719
2720 return BKE_object_material_get(ob, ob->actcol);
2721}
2722
2724 Object *ob,
2725 const char *name,
2726 int *r_index)
2727{
2729 if (index != -1) {
2730 *r_index = index;
2731 return BKE_object_material_get(ob, index + 1);
2732 }
2733 return BKE_grease_pencil_object_material_new(bmain, ob, name, r_index);
2734}
2735
2737 Object *ob,
2738 Brush *brush)
2739{
2740 Material *ma = (brush->gpencil_settings) ? brush->gpencil_settings->material : nullptr;
2741
2742 if (ma) {
2743 /* Ensure we assign a local datablock if this is an editable asset. */
2744 ma = reinterpret_cast<Material *>(blender::bke::asset_edit_id_ensure_local(*bmain, ma->id));
2745 }
2746
2747 /* check if the material is already on object material slots and add it if missing */
2748 if (ma && BKE_object_material_index_get(ob, ma) < 0) {
2749 /* The object's active material is what's used for the unpinned material. Do not touch it
2750 * while using a pinned material. */
2751 const bool change_active_material = false;
2752
2753 BKE_object_material_slot_add(bmain, ob, change_active_material);
2755 }
2756
2757 return ma;
2758}
2759
2761 Object *ob,
2762 Brush *brush)
2763{
2764 /* Use pinned material. */
2765 if (brush && brush->gpencil_settings &&
2767 {
2769 return ma;
2770 }
2771
2772 /* It is easier to just unpin a null material, instead of setting a new one. */
2773 brush->gpencil_settings->flag &= ~GP_BRUSH_MATERIAL_PINNED;
2774 }
2775
2776 /* Use the active material. */
2777 if (Material *ma = BKE_object_material_get(ob, ob->actcol)) {
2778 return ma;
2779 }
2780
2781 /* Fall back to default material. */
2782 /* XXX FIXME This is critical abuse of the 'default material' feature, these IDs should never be
2783 * used/returned as 'regular' data. */
2785}
2786
2788 Object *ob,
2789 Brush *brush)
2790{
2791 Material *material_alt = (brush->gpencil_settings) ? brush->gpencil_settings->material_alt :
2792 nullptr;
2793 if (material_alt) {
2794 material_alt = reinterpret_cast<Material *>(
2795 blender::bke::asset_edit_id_find_local(*bmain, material_alt->id));
2796 if (material_alt && BKE_object_material_slot_find_index(ob, material_alt) != -1) {
2797 return material_alt;
2798 }
2799 }
2800
2802}
2803
2804void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol)
2805{
2806 using namespace blender::bke;
2807
2808 for (GreasePencilDrawingBase *base : grease_pencil->drawings()) {
2809 if (base->type != GP_DRAWING) {
2810 continue;
2811 }
2812 greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2814 SpanAttributeWriter<int> material_indices = attributes.lookup_for_write_span<int>(
2815 "material_index");
2816 if (!material_indices) {
2817 continue;
2818 }
2819 BLI_assert(material_indices.domain == AttrDomain::Curve);
2820 for (const int i : material_indices.span.index_range()) {
2821 BLI_assert(blender::IndexRange(totcol).contains(remap[material_indices.span[i]]));
2822 UNUSED_VARS_NDEBUG(totcol);
2823 material_indices.span[i] = remap[material_indices.span[i]];
2824 }
2825 material_indices.finish();
2826 }
2827}
2828
2829void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, const int index)
2830{
2831 using namespace blender;
2832 using namespace blender::bke;
2833
2834 for (GreasePencilDrawingBase *base : grease_pencil->drawings()) {
2835 if (base->type != GP_DRAWING) {
2836 continue;
2837 }
2838 greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2840 SpanAttributeWriter<int> material_indices = attributes.lookup_for_write_span<int>(
2841 "material_index");
2842 if (!material_indices) {
2843 continue;
2844 }
2845 BLI_assert(material_indices.domain == AttrDomain::Curve);
2846 for (const int i : material_indices.span.index_range()) {
2847 if (material_indices.span[i] > 0 && material_indices.span[i] >= index) {
2848 material_indices.span[i]--;
2849 }
2850 }
2851 material_indices.finish();
2852 }
2853}
2854
2856{
2857 using namespace blender;
2858 using namespace blender::bke;
2859
2860 for (GreasePencilDrawingBase *base : grease_pencil->drawings()) {
2861 if (base->type != GP_DRAWING) {
2862 continue;
2863 }
2864 greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2865 AttributeAccessor attributes = drawing.strokes().attributes();
2866 const VArraySpan<int> material_indices = *attributes.lookup_or_default<int>(
2867 "material_index", AttrDomain::Curve, 0);
2868
2869 if (material_indices.contains(index)) {
2870 return true;
2871 }
2872 }
2873 return false;
2874}
2875
2878/* ------------------------------------------------------------------- */
2883 const GreasePencil *grease_pencil)
2884{
2885 for (const GreasePencilDrawingBase *base : grease_pencil->drawings()) {
2886 if (base->type == GP_DRAWING_REFERENCE) {
2887 const auto *reference = reinterpret_cast<const GreasePencilDrawingReference *>(base);
2888 if (id_reference == reference->id_reference) {
2889 return true;
2890 }
2891
2892 if (grease_pencil_references_cyclic_check_internal(id_reference, reference->id_reference)) {
2893 return true;
2894 }
2895 }
2896 }
2897 return false;
2898}
2899
2901 const GreasePencil *grease_pencil)
2902{
2903 return grease_pencil_references_cyclic_check_internal(id_reference, grease_pencil);
2904}
2905
2908/* ------------------------------------------------------------------- */
2913 int mode) = nullptr;
2914void (*BKE_grease_pencil_batch_cache_free_cb)(GreasePencil *grease_pencil) = nullptr;
2915
2917{
2918 if (grease_pencil->runtime && grease_pencil->runtime->batch_cache) {
2919 BKE_grease_pencil_batch_cache_dirty_tag_cb(grease_pencil, mode);
2920 }
2921}
2922
2924{
2925 if (grease_pencil->runtime && grease_pencil->runtime->batch_cache) {
2927 }
2928}
2929
2932/* ------------------------------------------------------------------- */
2936template<typename T> static void grow_array(T **array, int *num, const int add_num)
2937{
2938 BLI_assert(add_num > 0);
2939 const int new_array_num = *num + add_num;
2940 T *new_array = MEM_calloc_arrayN<T>(new_array_num, __func__);
2941
2943 if (*array != nullptr) {
2944 MEM_freeN(*array);
2945 }
2946
2947 *array = new_array;
2948 *num = new_array_num;
2949}
2950template<typename T> static void shrink_array(T **array, int *num, const int shrink_num)
2951{
2952 BLI_assert(shrink_num > 0);
2953 const int new_array_num = *num - shrink_num;
2954 if (new_array_num == 0) {
2955 MEM_freeN(*array);
2956 *array = nullptr;
2957 *num = 0;
2958 return;
2959 }
2960
2961 T *new_array = MEM_calloc_arrayN<T>(new_array_num, __func__);
2962
2963 blender::uninitialized_move_n(*array, new_array_num, new_array);
2964 MEM_freeN(*array);
2965
2966 *array = new_array;
2967 *num = new_array_num;
2968}
2969
2970blender::Span<const GreasePencilDrawingBase *> GreasePencil::drawings() const
2971{
2972 return blender::Span<GreasePencilDrawingBase *>{this->drawing_array, this->drawing_array_num};
2973}
2974
2976{
2977 return blender::MutableSpan<GreasePencilDrawingBase *>{this->drawing_array,
2978 this->drawing_array_num};
2979}
2980
2981static void delete_drawing(GreasePencilDrawingBase *drawing_base)
2982{
2983 /* Unknown types from corrupt files, only the base struct was read. */
2984 if (!ELEM(drawing_base->type, GP_DRAWING, GP_DRAWING_REFERENCE)) {
2985 MEM_delete(drawing_base);
2986 return;
2987 }
2988
2989 switch (GreasePencilDrawingType(drawing_base->type)) {
2990 case GP_DRAWING: {
2991 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
2992 MEM_delete(&drawing->wrap());
2993 break;
2994 }
2995 case GP_DRAWING_REFERENCE: {
2996 GreasePencilDrawingReference *drawing_reference =
2997 reinterpret_cast<GreasePencilDrawingReference *>(drawing_base);
2998 MEM_delete(&drawing_reference->wrap());
2999 break;
3000 }
3001 }
3002}
3003
3004void GreasePencil::resize_drawings(const int new_num)
3005{
3006 using namespace blender;
3007 BLI_assert(new_num >= 0);
3008
3009 const int prev_num = int(this->drawings().size());
3010 if (new_num == prev_num) {
3011 return;
3012 }
3013 if (new_num > prev_num) {
3014 const int add_num = new_num - prev_num;
3015 grow_array<GreasePencilDrawingBase *>(&this->drawing_array, &this->drawing_array_num, add_num);
3016 }
3017 else { /* if (new_num < prev_num) */
3018 const int shrink_num = prev_num - new_num;
3019 MutableSpan<GreasePencilDrawingBase *> old_drawings = this->drawings().drop_front(new_num);
3020 for (const int64_t i : old_drawings.index_range()) {
3021 if (GreasePencilDrawingBase *drawing_base = old_drawings[i]) {
3022 delete_drawing(drawing_base);
3023 }
3024 }
3026 &this->drawing_array, &this->drawing_array_num, shrink_num);
3027 }
3028}
3029
3030void GreasePencil::add_empty_drawings(const int add_num)
3031{
3032 using namespace blender;
3033 BLI_assert(add_num > 0);
3034 const int prev_num = this->drawings().size();
3035 grow_array<GreasePencilDrawingBase *>(&this->drawing_array, &this->drawing_array_num, add_num);
3036 MutableSpan<GreasePencilDrawingBase *> new_drawings = this->drawings().drop_front(prev_num);
3037 for (const int i : new_drawings.index_range()) {
3038 new_drawings[i] = reinterpret_cast<GreasePencilDrawingBase *>(
3039 MEM_new<blender::bke::greasepencil::Drawing>(__func__));
3040 }
3041}
3042
3043void GreasePencil::add_duplicate_drawings(const int duplicate_num,
3045{
3046 using namespace blender;
3047 BLI_assert(duplicate_num > 0);
3048 const int prev_num = this->drawings().size();
3050 &this->drawing_array, &this->drawing_array_num, duplicate_num);
3051 MutableSpan<GreasePencilDrawingBase *> new_drawings = this->drawings().drop_front(prev_num);
3052 for (const int i : new_drawings.index_range()) {
3053 new_drawings[i] = reinterpret_cast<GreasePencilDrawingBase *>(
3054 MEM_new<bke::greasepencil::Drawing>(__func__, drawing));
3055 }
3056}
3057
3058blender::bke::greasepencil::Drawing *GreasePencil::insert_frame(
3060 const int frame_number,
3061 const int duration,
3062 const eBezTriple_KeyframeType keytype)
3063{
3064 using namespace blender;
3065 GreasePencilFrame *frame = layer.add_frame(frame_number, duration);
3066 if (frame == nullptr) {
3067 return nullptr;
3068 }
3069 this->add_empty_drawings(1);
3070 frame->drawing_index = this->drawings().index_range().last();
3071 frame->type = int8_t(keytype);
3072
3073 GreasePencilDrawingBase *drawing_base = this->drawings().last();
3074 BLI_assert(drawing_base->type == GP_DRAWING);
3075 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
3076 return &drawing->wrap();
3077}
3078
3079void GreasePencil::insert_frames(Span<blender::bke::greasepencil::Layer *> layers,
3080 const int frame_number,
3081 const int duration,
3082 const eBezTriple_KeyframeType keytype)
3083{
3084 using namespace blender;
3085 if (layers.is_empty()) {
3086 return;
3087 }
3089 frames.reserve(layers.size());
3090 for (bke::greasepencil::Layer *layer : layers) {
3091 BLI_assert(layer != nullptr);
3092 GreasePencilFrame *frame = layer->add_frame(frame_number, duration);
3093 if (frame != nullptr) {
3094 frames.append(frame);
3095 }
3096 }
3097
3098 if (frames.is_empty()) {
3099 return;
3100 }
3101
3102 this->add_empty_drawings(frames.size());
3103 const IndexRange new_drawings = this->drawings().index_range().take_back(frames.size());
3104 for (const int frame_i : frames.index_range()) {
3105 GreasePencilFrame *frame = frames[frame_i];
3106 frame->drawing_index = new_drawings[frame_i];
3107 frame->type = int8_t(keytype);
3108 }
3109}
3110
3111bool GreasePencil::insert_duplicate_frame(blender::bke::greasepencil::Layer &layer,
3112 const int src_frame_number,
3113 const int dst_frame_number,
3114 const bool do_instance)
3115{
3116 using namespace blender::bke::greasepencil;
3117
3118 if (!layer.frames().contains(src_frame_number)) {
3119 return false;
3120 }
3121
3122 if (layer.is_locked()) {
3123 return false;
3124 }
3125 const GreasePencilFrame src_frame = layer.frames().lookup(src_frame_number);
3126
3127 /* Create the new frame structure, with the same duration.
3128 * If we want to make an instance of the source frame, the drawing index gets copied from the
3129 * source frame. Otherwise, we set the drawing index to the size of the drawings array, since we
3130 * are going to add a new drawing copied from the source drawing. */
3131 const int duration = 0 /* layer.get_frame_duration_at(src_frame_number) */;
3132 GreasePencilFrame *dst_frame = layer.add_frame(dst_frame_number, duration);
3133 if (dst_frame == nullptr) {
3134 return false;
3135 }
3136 dst_frame->drawing_index = do_instance ? src_frame.drawing_index : int(this->drawings().size());
3137 dst_frame->type = src_frame.type;
3138
3139 const GreasePencilDrawingBase *src_drawing_base = this->drawing(src_frame.drawing_index);
3140 switch (src_drawing_base->type) {
3141 case GP_DRAWING: {
3142 const Drawing &src_drawing =
3143 reinterpret_cast<const GreasePencilDrawing *>(src_drawing_base)->wrap();
3144 if (do_instance) {
3145 /* Adds the duplicate frame as a new instance of the same drawing. We thus increase the
3146 * user count of the corresponding drawing. */
3147 src_drawing.add_user();
3148 }
3149 else {
3150 /* Create a copy of the drawing, and add it at the end of the drawings array.
3151 * Note that the frame already points to this new drawing, as the drawing index was set to
3152 * `int(this->drawings().size())`. */
3153 this->add_duplicate_drawings(1, src_drawing);
3154 }
3155 break;
3156 }
3158 /* TODO: Duplicate drawing references is not yet implemented.
3159 * For now, just remove the frame that we inserted. */
3160 layer.remove_frame(dst_frame_number);
3161 return false;
3162 }
3163 return true;
3164}
3165
3166bool GreasePencil::remove_frames(blender::bke::greasepencil::Layer &layer,
3167 blender::Span<int> frame_numbers)
3168{
3169 using namespace blender::bke::greasepencil;
3170 bool removed_any_drawing_user = false;
3171 for (const int frame_number : frame_numbers) {
3172 if (!layer.frames().contains(frame_number)) {
3173 continue;
3174 }
3175 const GreasePencilFrame frame_to_remove = layer.frames().lookup(frame_number);
3176 const int64_t drawing_index_to_remove = frame_to_remove.drawing_index;
3177 if (!layer.remove_frame(frame_number)) {
3178 /* If removing the frame was not successful, continue. */
3179 continue;
3180 }
3181 if (frame_to_remove.is_end()) {
3182 /* End frames don't reference a drawing, continue. */
3183 continue;
3184 }
3185 GreasePencilDrawingBase *drawing_base = this->drawing(drawing_index_to_remove);
3186 if (drawing_base->type != GP_DRAWING) {
3187 /* If the drawing is referenced from another object, we don't track it's users because we
3188 * cannot delete drawings from another object. */
3189 continue;
3190 }
3191 Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base)->wrap();
3192 drawing.remove_user();
3193 removed_any_drawing_user = true;
3194 }
3195 if (removed_any_drawing_user) {
3196 this->remove_drawings_with_no_users();
3197 return true;
3198 }
3199 return false;
3200}
3201
3202void GreasePencil::copy_frames_from_layer(blender::bke::greasepencil::Layer &dst_layer,
3203 const GreasePencil &src_grease_pencil,
3204 const blender::bke::greasepencil::Layer &src_layer,
3205 const std::optional<int> frame_select)
3206{
3207 using namespace blender;
3208
3209 const Span<const GreasePencilDrawingBase *> src_drawings = src_grease_pencil.drawings();
3210 Array<int> drawing_index_map(src_grease_pencil.drawing_array_num, -1);
3211
3212 for (auto [frame_number, src_frame] : src_layer.frames().items()) {
3213 if (frame_select && *frame_select != frame_number) {
3214 continue;
3215 }
3216
3217 const int src_drawing_index = src_frame.drawing_index;
3218 int dst_drawing_index = drawing_index_map[src_drawing_index];
3219 if (dst_drawing_index < 0) {
3220 switch (src_drawings[src_drawing_index]->type) {
3221 case GP_DRAWING: {
3222 const bke::greasepencil::Drawing &src_drawing =
3223 reinterpret_cast<const GreasePencilDrawing *>(src_drawings[src_drawing_index])
3224 ->wrap();
3225 this->add_duplicate_drawings(1, src_drawing);
3226 break;
3227 }
3229 /* Dummy drawing to keep frame reference valid. */
3230 this->add_empty_drawings(1);
3231 break;
3232 }
3233 dst_drawing_index = this->drawings().size() - 1;
3234 drawing_index_map[src_drawing_index] = dst_drawing_index;
3235 }
3236 BLI_assert(this->drawings().index_range().contains(dst_drawing_index));
3237
3238 GreasePencilFrame *dst_frame = dst_layer.add_frame(frame_number);
3239 dst_frame->flag = src_frame.flag;
3240 dst_frame->drawing_index = dst_drawing_index;
3241 }
3242}
3243
3244void GreasePencil::add_layers_with_empty_drawings_for_eval(const int num)
3245{
3246 using namespace blender;
3247 using namespace blender::bke::greasepencil;
3248 const int old_drawings_num = this->drawing_array_num;
3249 const int old_layers_num = this->layers().size();
3250 this->add_empty_drawings(num);
3251 this->add_layers_for_eval(num);
3253 for (const int i : range) {
3254 const int new_drawing_i = old_drawings_num + i;
3255 const int new_layer_i = old_layers_num + i;
3256 Layer &layer = this->layer(new_layer_i);
3257 GreasePencilFrame *frame = layer.add_frame(this->runtime->eval_frame);
3259 frame->drawing_index = new_drawing_i;
3260 }
3261 });
3262}
3263
3264void GreasePencil::remove_drawings_with_no_users()
3265{
3266 using namespace blender;
3267 using namespace blender::bke::greasepencil;
3268
3269 /* Compress the drawings array by finding unused drawings.
3270 * In every step two indices are found:
3271 * - The next unused drawing from the start
3272 * - The last used drawing from the end
3273 * These two drawings are then swapped. Rinse and repeat until both iterators meet somewhere in
3274 * the middle. At this point the drawings array is fully compressed.
3275 * Then the drawing indices in frame data are remapped. */
3276
3277 const MutableSpan<GreasePencilDrawingBase *> drawings = this->drawings();
3278 if (drawings.is_empty()) {
3279 return;
3280 }
3281
3282 auto is_drawing_used = [&](const int drawing_index) {
3283 GreasePencilDrawingBase *drawing_base = drawings[drawing_index];
3284 /* NOTE: GreasePencilDrawingReference does not have a user count currently, but should
3285 * eventually be counted like GreasePencilDrawing. */
3286 if (drawing_base->type != GP_DRAWING) {
3287 return false;
3288 }
3289 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
3290 return drawing->wrap().has_users();
3291 };
3292
3293 /* Index map to remap drawing indices in frame data.
3294 * Index -1 indicates that the drawing has not been moved. */
3295 constexpr const int unchanged_index = -1;
3296 Array<int> drawing_index_map(drawings.size(), unchanged_index);
3297
3298 int first_unused_drawing = -1;
3299 int last_used_drawing = drawings.size() - 1;
3300 /* Advance head and tail iterators to the next unused/used drawing respectively.
3301 * Returns true if an index pair was found that needs to be swapped. */
3302 auto find_next_swap_index = [&]() -> bool {
3303 do {
3304 ++first_unused_drawing;
3305 } while (first_unused_drawing <= last_used_drawing && is_drawing_used(first_unused_drawing));
3306 while (last_used_drawing >= 0 && !is_drawing_used(last_used_drawing)) {
3307 --last_used_drawing;
3308 }
3309
3310 return first_unused_drawing < last_used_drawing;
3311 };
3312
3313 while (find_next_swap_index()) {
3314 /* Found two valid iterators, now swap drawings. */
3315 std::swap(drawings[first_unused_drawing], drawings[last_used_drawing]);
3316 drawing_index_map[last_used_drawing] = first_unused_drawing;
3317 }
3318
3319 /* `last_used_drawing` is expected to be exactly the item before the first unused drawing, once
3320 * the loop above is fully done and all unused drawings are supposed to be at the end of the
3321 * array. */
3322 BLI_assert(last_used_drawing == first_unused_drawing - 1);
3323#ifndef NDEBUG
3324 for (const int i : drawings.index_range()) {
3325 if (i < first_unused_drawing) {
3326 BLI_assert(is_drawing_used(i));
3327 }
3328 else {
3329 BLI_assert(!is_drawing_used(i));
3330 }
3331 }
3332#endif
3333
3334 /* Tail range of unused drawings that can be removed. */
3335 const IndexRange drawings_to_remove = (first_unused_drawing > 0) ?
3336 drawings.index_range().drop_front(
3337 first_unused_drawing) :
3338 drawings.index_range();
3339 if (drawings_to_remove.is_empty()) {
3340 return;
3341 }
3342
3343 /* Free the unused drawings. */
3344 for (const int i : drawings_to_remove) {
3345 GreasePencilDrawingBase *unused_drawing_base = drawings[i];
3346 switch (unused_drawing_base->type) {
3347 case GP_DRAWING: {
3348 auto *unused_drawing = reinterpret_cast<GreasePencilDrawing *>(unused_drawing_base);
3349 MEM_delete(&unused_drawing->wrap());
3350 break;
3351 }
3352 case GP_DRAWING_REFERENCE: {
3353 auto *unused_drawing_ref = reinterpret_cast<GreasePencilDrawingReference *>(
3354 unused_drawing_base);
3355 MEM_delete(&unused_drawing_ref->wrap());
3356 break;
3357 }
3358 }
3359 }
3361 &this->drawing_array, &this->drawing_array_num, drawings_to_remove.size());
3362
3363 /* Remap drawing indices in frame data. */
3364 for (Layer *layer : this->layers_for_write()) {
3365 for (auto [key, value] : layer->frames_for_write().items()) {
3366 const int new_drawing_index = drawing_index_map[value.drawing_index];
3367 if (new_drawing_index != unchanged_index) {
3368 value.drawing_index = new_drawing_index;
3369 layer->tag_frames_map_changed();
3370 }
3371 }
3372 }
3373}
3374
3375void GreasePencil::update_drawing_users_for_layer(const blender::bke::greasepencil::Layer &layer)
3376{
3377 using namespace blender;
3378 for (auto [key, value] : layer.frames().items()) {
3379 BLI_assert(this->drawings().index_range().contains(value.drawing_index));
3380 GreasePencilDrawingBase *drawing_base = this->drawing(value.drawing_index);
3381 if (drawing_base->type != GP_DRAWING) {
3382 continue;
3383 }
3385 reinterpret_cast<GreasePencilDrawing *>(drawing_base)->wrap();
3386 if (!drawing.has_users()) {
3387 drawing.add_user();
3388 }
3389 }
3390}
3391
3392void GreasePencil::move_frames(blender::bke::greasepencil::Layer &layer,
3393 const blender::Map<int, int> &frame_number_destinations)
3394{
3395 this->move_duplicate_frames(
3396 layer, frame_number_destinations, blender::Map<int, GreasePencilFrame>());
3397}
3398
3399void GreasePencil::move_duplicate_frames(
3401 const blender::Map<int, int> &frame_number_destinations,
3402 const blender::Map<int, GreasePencilFrame> &duplicate_frames)
3403{
3404 using namespace blender;
3405 Map<int, GreasePencilFrame> layer_frames_copy = layer.frames();
3406
3407 /* Copy frames durations. */
3408 Map<int, int> src_layer_frames_durations;
3409 for (const auto [frame_number, frame] : layer.frames().items()) {
3410 src_layer_frames_durations.add(frame_number, layer.get_frame_duration_at(frame_number));
3411 }
3412
3413 /* Remove original frames for duplicates before inserting any frames.
3414 * This has to be done early to avoid removing frames that may be inserted
3415 * in place of the source frames. */
3416 for (const auto src_frame_number : frame_number_destinations.keys()) {
3417 if (!duplicate_frames.contains(src_frame_number)) {
3418 /* User count not decremented here, the same frame is inserted again later. */
3419 layer.remove_frame(src_frame_number);
3420 }
3421 }
3422
3423 auto get_source_frame = [&](const int frame_number) -> const GreasePencilFrame * {
3424 if (const GreasePencilFrame *ptr = duplicate_frames.lookup_ptr(frame_number)) {
3425 return ptr;
3426 }
3427 return layer_frames_copy.lookup_ptr(frame_number);
3428 };
3429
3430 for (const auto [src_frame_number, dst_frame_number] : frame_number_destinations.items()) {
3431 const GreasePencilFrame *src_frame = get_source_frame(src_frame_number);
3432 if (!src_frame) {
3433 continue;
3434 }
3435 const int duration = 0 /* src_layer_frames_durations.lookup_default(src_frame_number, 0) */;
3436
3437 /* Add and overwrite the frame at the destination number. */
3438 if (layer.frames().contains(dst_frame_number)) {
3439 GreasePencilFrame frame_to_overwrite = layer.frames().lookup(dst_frame_number);
3440 GreasePencilDrawingBase *drawing_base = this->drawing(frame_to_overwrite.drawing_index);
3441 if (drawing_base->type == GP_DRAWING) {
3442 reinterpret_cast<GreasePencilDrawing *>(drawing_base)->wrap().remove_user();
3443 }
3444 layer.remove_frame(dst_frame_number);
3445 }
3446 GreasePencilFrame *frame = layer.add_frame(dst_frame_number, duration);
3447 *frame = *src_frame;
3448 }
3449
3450 /* Remove drawings if they no longer have users. */
3451 this->remove_drawings_with_no_users();
3452}
3453
3454const blender::bke::greasepencil::Drawing *GreasePencil::get_drawing_at(
3455 const blender::bke::greasepencil::Layer &layer, const int frame_number) const
3456{
3457 if (this->drawings().is_empty()) {
3458 return nullptr;
3459 }
3460 const int drawing_index = layer.drawing_index_at(frame_number);
3461 if (drawing_index == -1) {
3462 /* No drawing found. */
3463 return nullptr;
3464 }
3465 const GreasePencilDrawingBase *drawing_base = this->drawing(drawing_index);
3466 if (drawing_base->type != GP_DRAWING) {
3467 /* TODO: Get reference drawing. */
3468 return nullptr;
3469 }
3470 const GreasePencilDrawing *drawing = reinterpret_cast<const GreasePencilDrawing *>(drawing_base);
3471 return &drawing->wrap();
3472}
3473
3474blender::bke::greasepencil::Drawing *GreasePencil::get_drawing_at(
3475 const blender::bke::greasepencil::Layer &layer, const int frame_number)
3476{
3477 if (this->drawings().is_empty()) {
3478 return nullptr;
3479 }
3480 const int drawing_index = layer.drawing_index_at(frame_number);
3481 if (drawing_index == -1) {
3482 /* No drawing found. */
3483 return nullptr;
3484 }
3485 GreasePencilDrawingBase *drawing_base = this->drawing(drawing_index);
3486 if (drawing_base->type != GP_DRAWING) {
3487 /* TODO: Get reference drawing. */
3488 return nullptr;
3489 }
3490 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
3491 return &drawing->wrap();
3492}
3493
3494blender::bke::greasepencil::Drawing *GreasePencil::get_editable_drawing_at(
3495 const blender::bke::greasepencil::Layer &layer, const int frame_number)
3496{
3497 if (!layer.is_editable()) {
3498 return nullptr;
3499 }
3500 if (this->drawings().is_empty()) {
3501 return nullptr;
3502 }
3503 const int drawing_index = layer.drawing_index_at(frame_number);
3504 if (drawing_index == -1) {
3505 /* No drawing found. */
3506 return nullptr;
3507 }
3508 GreasePencilDrawingBase *drawing_base = this->drawing(drawing_index);
3509 if (drawing_base->type != GP_DRAWING) {
3510 /* Drawing references are not editable. */
3511 return nullptr;
3512 }
3513 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
3514 return &drawing->wrap();
3515}
3516
3517const blender::bke::greasepencil::Drawing *GreasePencil::get_eval_drawing(
3518 const blender::bke::greasepencil::Layer &layer) const
3519{
3520 return this->get_drawing_at(layer, this->runtime->eval_frame);
3521}
3522
3523blender::bke::greasepencil::Drawing *GreasePencil::get_eval_drawing(
3525{
3526 return this->get_drawing_at(layer, this->runtime->eval_frame);
3527}
3528
3532{
3533 BLI_assert(src.size() == dst.size());
3534
3536 for (const int i : range) {
3537 dst[i] = blender::math::transform_point(transform, src[i]);
3538 }
3539 });
3540}
3541
3542std::optional<blender::Bounds<blender::float3>> GreasePencil::bounds_min_max(
3543 const int frame, const bool use_radius) const
3544{
3545 using namespace blender;
3546 std::optional<Bounds<float3>> bounds;
3547 const Span<const bke::greasepencil::Layer *> layers = this->layers();
3548 for (const int layer_i : layers.index_range()) {
3549 const bke::greasepencil::Layer &layer = *layers[layer_i];
3550 const float4x4 layer_to_object = layer.local_transform();
3551 if (!layer.is_visible()) {
3552 continue;
3553 }
3554 const bke::greasepencil::Drawing *drawing = this->get_drawing_at(layer, frame);
3555 if (!drawing) {
3556 continue;
3557 }
3558 const bke::CurvesGeometry &curves = drawing->strokes();
3559 if (curves.is_empty()) {
3560 continue;
3561 }
3562 if (layer_to_object == float4x4::identity()) {
3563 bounds = bounds::merge(bounds, curves.bounds_min_max(use_radius));
3564 continue;
3565 }
3566 const VArray<float> radius = curves.radius();
3567 Array<float3> positions_world(curves.evaluated_points_num());
3568 transform_positions(curves.evaluated_positions(), layer_to_object, positions_world);
3569 if (!use_radius) {
3570 const Bounds<float3> drawing_bounds = *bounds::min_max(positions_world.as_span());
3571 bounds = bounds::merge(bounds, drawing_bounds);
3572 continue;
3573 }
3574 if (const std::optional radius_single = radius.get_if_single()) {
3575 Bounds<float3> drawing_bounds = *curves.bounds_min_max(false);
3576 drawing_bounds.pad(*radius_single);
3577 bounds = bounds::merge(bounds, drawing_bounds);
3578 continue;
3579 }
3580 const Span radius_span = radius.get_internal_span();
3581 if (curves.is_single_type(CURVE_TYPE_POLY)) {
3582 const Bounds<float3> drawing_bounds = *bounds::min_max_with_radii(positions_world.as_span(),
3583 radius_span);
3584 bounds = bounds::merge(bounds, drawing_bounds);
3585 continue;
3586 }
3588 Array<float> radii_eval(curves.evaluated_points_num());
3589 curves.interpolate_to_evaluated(radius_span, radii_eval.as_mutable_span());
3590 const Bounds<float3> drawing_bounds = *bounds::min_max_with_radii(positions_world.as_span(),
3591 radii_eval.as_span());
3592 bounds = bounds::merge(bounds, drawing_bounds);
3593 }
3594 return bounds;
3595}
3596
3597std::optional<blender::Bounds<blender::float3>> GreasePencil::bounds_min_max_eval(
3598 const bool use_radius) const
3599{
3600 return this->bounds_min_max(this->runtime->eval_frame, use_radius);
3601}
3602
3603void GreasePencil::count_memory(blender::MemoryCounter &memory) const
3604{
3605 using namespace blender::bke;
3606 for (const GreasePencilDrawingBase *base : this->drawings()) {
3607 if (base->type != GP_DRAWING) {
3608 continue;
3609 }
3610 const greasepencil::Drawing &drawing =
3611 reinterpret_cast<const GreasePencilDrawing *>(base)->wrap();
3612 drawing.strokes().count_memory(memory);
3613 }
3614}
3615
3616std::optional<int> GreasePencil::material_index_max() const
3617{
3618 using namespace blender;
3619 using namespace blender::bke;
3620 std::optional<int> max_index;
3621 for (const GreasePencilDrawingBase *drawing_base : this->drawings()) {
3622 if (drawing_base->type != GP_DRAWING) {
3623 continue;
3624 }
3625 const GreasePencilDrawing *drawing = reinterpret_cast<const GreasePencilDrawing *>(
3626 drawing_base);
3627 const std::optional<int> max_index_in_drawing = drawing->wrap().strokes().material_index_max();
3628 if (max_index) {
3629 if (max_index_in_drawing) {
3630 max_index = std::max(*max_index, *max_index_in_drawing);
3631 }
3632 }
3633 else {
3634 max_index = max_index_in_drawing;
3635 }
3636 }
3637 return max_index;
3638}
3639
3641{
3642 BLI_assert(this->runtime != nullptr);
3643 return this->root_group().layers();
3644}
3645
3646blender::Span<blender::bke::greasepencil::Layer *> GreasePencil::layers_for_write()
3647{
3648 BLI_assert(this->runtime != nullptr);
3649 return this->root_group().layers_for_write();
3650}
3651
3653{
3654 BLI_assert(this->runtime != nullptr);
3655 return this->root_group().groups();
3656}
3657
3658blender::Span<blender::bke::greasepencil::LayerGroup *> GreasePencil::layer_groups_for_write()
3659{
3660 BLI_assert(this->runtime != nullptr);
3661 return this->root_group().groups_for_write();
3662}
3663
3665{
3666 BLI_assert(this->runtime != nullptr);
3667 return this->root_group().nodes();
3668}
3669
3670blender::Span<blender::bke::greasepencil::TreeNode *> GreasePencil::nodes_for_write()
3671{
3672 BLI_assert(this->runtime != nullptr);
3673 return this->root_group().nodes_for_write();
3674}
3675
3676std::optional<int> GreasePencil::get_layer_index(
3677 const blender::bke::greasepencil::Layer &layer) const
3678{
3679 const int index = int(this->layers().first_index_try(&layer));
3680 if (index == -1) {
3681 return {};
3682 }
3683 return index;
3684}
3685
3686const blender::bke::greasepencil::Layer *GreasePencil::get_active_layer() const
3687{
3688 if (this->active_node == nullptr) {
3689 return nullptr;
3690 }
3691 const blender::bke::greasepencil::TreeNode &active_node = *this->get_active_node();
3692 if (!active_node.is_layer()) {
3693 return nullptr;
3694 }
3695 return &active_node.as_layer();
3696}
3697
3698blender::bke::greasepencil::Layer *GreasePencil::get_active_layer()
3699{
3700 if (this->active_node == nullptr) {
3701 return nullptr;
3702 }
3703 blender::bke::greasepencil::TreeNode &active_node = *this->get_active_node();
3704 if (!active_node.is_layer()) {
3705 return nullptr;
3706 }
3707 return &active_node.as_layer();
3708}
3709
3710void GreasePencil::set_active_layer(blender::bke::greasepencil::Layer *layer)
3711{
3712 this->active_node = reinterpret_cast<GreasePencilLayerTreeNode *>(&layer->as_node());
3713
3714 if (this->flag & GREASE_PENCIL_AUTOLOCK_LAYERS) {
3715 this->autolock_inactive_layers();
3716 }
3717}
3718
3719bool GreasePencil::is_layer_active(const blender::bke::greasepencil::Layer *layer) const
3720{
3721 if (layer == nullptr) {
3722 return false;
3723 }
3724 return this->get_active_layer() == layer;
3725}
3726
3727void GreasePencil::autolock_inactive_layers()
3728{
3729 using namespace blender::bke::greasepencil;
3730
3731 for (Layer *layer : this->layers_for_write()) {
3732 if (this->is_layer_active(layer)) {
3733 layer->set_locked(false);
3734 continue;
3735 }
3736 layer->set_locked(true);
3737 }
3738}
3739
3740const blender::bke::greasepencil::LayerGroup *GreasePencil::get_active_group() const
3741{
3742 if (this->active_node == nullptr) {
3743 return nullptr;
3744 }
3745 const blender::bke::greasepencil::TreeNode &active_node = *this->get_active_node();
3746 if (!active_node.is_group()) {
3747 return nullptr;
3748 }
3749 return &active_node.as_group();
3750}
3751
3752blender::bke::greasepencil::LayerGroup *GreasePencil::get_active_group()
3753{
3754 if (this->active_node == nullptr) {
3755 return nullptr;
3756 }
3757 blender::bke::greasepencil::TreeNode &active_node = *this->get_active_node();
3758 if (!active_node.is_group()) {
3759 return nullptr;
3760 }
3761 return &active_node.as_group();
3762}
3763
3764const blender::bke::greasepencil::TreeNode *GreasePencil::get_active_node() const
3765{
3766 if (this->active_node == nullptr) {
3767 return nullptr;
3768 }
3769 return &this->active_node->wrap();
3770}
3771
3772blender::bke::greasepencil::TreeNode *GreasePencil::get_active_node()
3773{
3774 if (this->active_node == nullptr) {
3775 return nullptr;
3776 }
3777 return &this->active_node->wrap();
3778}
3779
3780void GreasePencil::set_active_node(blender::bke::greasepencil::TreeNode *node)
3781{
3782 this->active_node = reinterpret_cast<GreasePencilLayerTreeNode *>(node);
3783}
3784
3786{
3787 using namespace blender;
3789 for (const blender::bke::greasepencil::TreeNode *node : grease_pencil.nodes()) {
3790 names.add(node->name());
3791 }
3792 return names;
3793}
3794
3795static std::string unique_node_name(const GreasePencil &grease_pencil,
3797{
3798 using namespace blender;
3800 const VectorSet<StringRef> names = get_node_names(grease_pencil);
3801 return BLI_uniquename_cb(
3802 [&](const StringRef check_name) { return names.contains(check_name); }, '.', name);
3803}
3804
3805std::string GreasePencil::unique_layer_name(blender::StringRef name)
3806{
3807 if (name.is_empty()) {
3808 /* Default name is "Layer". */
3809 name = DATA_("Layer");
3810 }
3811 return unique_node_name(*this, name);
3812}
3813
3814static std::string unique_layer_group_name(const GreasePencil &grease_pencil,
3816{
3817 if (name.is_empty()) {
3818 /* Default name is "Group". */
3819 name = DATA_("Group");
3820 }
3821 return unique_node_name(grease_pencil, name);
3822}
3823
3824blender::bke::greasepencil::Layer &GreasePencil::add_layer(const blender::StringRef name,
3825 const bool check_name_is_unique)
3826{
3827 using namespace blender;
3828 std::string unique_name = check_name_is_unique ? unique_layer_name(name) : std::string(name);
3829 const int numLayers = layers().size();
3830 CustomData_realloc(&layers_data, numLayers, numLayers + 1, CD_SET_DEFAULT);
3831 bke::greasepencil::Layer *new_layer = MEM_new<bke::greasepencil::Layer>(__func__, unique_name);
3832 /* Enable Lights by default. */
3834 /* Hide masks by default. */
3836 bke::greasepencil::Layer &layer = root_group().add_node(new_layer->as_node()).as_layer();
3837
3838 /* Initialize the attributes with default values. */
3839 bke::MutableAttributeAccessor attributes = this->attributes_for_write();
3843 IndexRange::from_single(numLayers));
3844
3845 return layer;
3846}
3847
3848blender::bke::greasepencil::Layer &GreasePencil::add_layer(
3851 const bool check_name_is_unique)
3852{
3853 using namespace blender;
3854 blender::bke::greasepencil::Layer &new_layer = this->add_layer(name, check_name_is_unique);
3855 move_node_into(new_layer.as_node(), parent_group);
3856 return new_layer;
3857}
3858
3859void GreasePencil::add_layers_for_eval(const int num_new_layers)
3860{
3861 using namespace blender;
3862 const int num_layers = this->layers().size();
3863 CustomData_realloc(&layers_data, num_layers, num_layers + num_new_layers);
3864 for ([[maybe_unused]] const int i : IndexRange(num_new_layers)) {
3865 bke::greasepencil::Layer *new_layer = MEM_new<bke::greasepencil::Layer>(__func__);
3866 /* Hide masks by default. */
3868 this->root_group().add_node(new_layer->as_node());
3869 }
3870}
3871
3872blender::bke::greasepencil::Layer &GreasePencil::duplicate_layer(
3873 const blender::bke::greasepencil::Layer &duplicate_layer)
3874{
3875 using namespace blender;
3876 std::string unique_name = unique_layer_name(duplicate_layer.name());
3877 std::optional<int> duplicate_layer_idx = get_layer_index(duplicate_layer);
3878 BLI_assert(duplicate_layer_idx.has_value());
3879 const int numLayers = layers().size();
3880 CustomData_realloc(&layers_data, numLayers, numLayers + 1);
3881 for (const int layer_index : IndexRange(layers_data.totlayer)) {
3883 &layers_data, &layers_data, layer_index, layer_index, *duplicate_layer_idx, numLayers, 1);
3884 }
3885 bke::greasepencil::Layer *new_layer = MEM_new<bke::greasepencil::Layer>(__func__,
3886 duplicate_layer);
3887 root_group().add_node(new_layer->as_node());
3888 this->update_drawing_users_for_layer(*new_layer);
3889 new_layer->set_name(unique_name);
3890 return *new_layer;
3891}
3892
3893blender::bke::greasepencil::Layer &GreasePencil::duplicate_layer(
3895 const blender::bke::greasepencil::Layer &duplicate_layer)
3896{
3897 using namespace blender;
3898 bke::greasepencil::Layer &new_layer = this->duplicate_layer(duplicate_layer);
3899 move_node_into(new_layer.as_node(), parent_group);
3900 return new_layer;
3901}
3902
3903blender::bke::greasepencil::LayerGroup &GreasePencil::add_layer_group(
3904 const blender::StringRef name, const bool check_name_is_unique)
3905{
3906 using namespace blender;
3907 std::string unique_name = check_name_is_unique ? unique_layer_group_name(*this, name) :
3908 std::string(name);
3909 bke::greasepencil::LayerGroup *new_group = MEM_new<bke::greasepencil::LayerGroup>(__func__,
3910 unique_name);
3911 return root_group().add_node(new_group->as_node()).as_group();
3912}
3913
3914blender::bke::greasepencil::LayerGroup &GreasePencil::add_layer_group(
3917 const bool check_name_is_unique)
3918{
3919 using namespace blender;
3920 bke::greasepencil::LayerGroup &new_group = this->add_layer_group(name, check_name_is_unique);
3921 move_node_into(new_group.as_node(), parent_group);
3922 return new_group;
3923}
3924
3925static void reorder_customdata(CustomData &data, const Span<int> new_by_old_map)
3926{
3927 CustomData new_data;
3928 CustomData_init_layout_from(&data, &new_data, CD_MASK_ALL, CD_CONSTRUCT, new_by_old_map.size());
3929
3930 for (const int old_i : new_by_old_map.index_range()) {
3931 const int new_i = new_by_old_map[old_i];
3932 CustomData_copy_data(&data, &new_data, old_i, new_i, 1);
3933 }
3935 data = new_data;
3936}
3937
3938static void reorder_layer_data(GreasePencil &grease_pencil,
3939 const blender::FunctionRef<void()> do_layer_order_changes)
3940{
3941 using namespace blender;
3942 Span<const bke::greasepencil::Layer *> layers = grease_pencil.layers();
3943
3944 /* Stash the initial layer order that we can refer back to later */
3945 Map<const bke::greasepencil::Layer *, int> old_layer_index_by_layer;
3946 old_layer_index_by_layer.reserve(layers.size());
3947 for (const int i : layers.index_range()) {
3948 old_layer_index_by_layer.add_new(layers[i], i);
3949 }
3950
3951 /* Execute the callback that changes the order of the layers. */
3952 do_layer_order_changes();
3953 layers = grease_pencil.layers();
3954 BLI_assert(layers.size() == old_layer_index_by_layer.size());
3955
3956 /* Compose the mapping from old layer indices to new layer indices */
3957 Array<int> new_by_old_map(layers.size());
3958 for (const int layer_i_new : layers.index_range()) {
3959 const bke::greasepencil::Layer *layer = layers[layer_i_new];
3960 BLI_assert(old_layer_index_by_layer.contains(layer));
3961 const int layer_i_old = old_layer_index_by_layer.pop(layer);
3962 new_by_old_map[layer_i_old] = layer_i_new;
3963 }
3964 BLI_assert(old_layer_index_by_layer.is_empty());
3965
3966 /* Use the mapping to re-order the custom data */
3967 reorder_customdata(grease_pencil.layers_data, new_by_old_map);
3968}
3969
3970void GreasePencil::move_node_up(blender::bke::greasepencil::TreeNode &node, const int step)
3971{
3972 using namespace blender;
3973 if (!node.parent_group()) {
3974 return;
3975 }
3976 reorder_layer_data(*this, [&]() { node.parent_group()->move_node_up(node, step); });
3977}
3978void GreasePencil::move_node_down(blender::bke::greasepencil::TreeNode &node, const int step)
3979{
3980 using namespace blender;
3981 if (!node.parent_group()) {
3982 return;
3983 }
3984 reorder_layer_data(*this, [&]() { node.parent_group()->move_node_down(node, step); });
3985}
3986void GreasePencil::move_node_top(blender::bke::greasepencil::TreeNode &node)
3987{
3988 using namespace blender;
3989 if (!node.parent_group()) {
3990 return;
3991 }
3992 reorder_layer_data(*this, [&]() { node.parent_group()->move_node_top(node); });
3993}
3994void GreasePencil::move_node_bottom(blender::bke::greasepencil::TreeNode &node)
3995{
3996 using namespace blender;
3997 if (!node.parent_group()) {
3998 return;
3999 }
4000 reorder_layer_data(*this, [&]() { node.parent_group()->move_node_bottom(node); });
4001}
4002
4003void GreasePencil::move_node_after(blender::bke::greasepencil::TreeNode &node,
4005{
4006 using namespace blender;
4007 if (!target_node.parent_group() || !node.parent_group()) {
4008 return;
4009 }
4010 reorder_layer_data(*this, [&]() {
4011 node.parent_group()->unlink_node(node);
4012 target_node.parent_group()->add_node_after(node, target_node);
4013 });
4014}
4015
4016void GreasePencil::move_node_before(blender::bke::greasepencil::TreeNode &node,
4018{
4019 using namespace blender;
4020 if (!target_node.parent_group() || !node.parent_group()) {
4021 return;
4022 }
4023 reorder_layer_data(*this, [&]() {
4024 node.parent_group()->unlink_node(node);
4025 target_node.parent_group()->add_node_before(node, target_node);
4026 });
4027}
4028
4029void GreasePencil::move_node_into(blender::bke::greasepencil::TreeNode &node,
4031{
4032 using namespace blender;
4033 if (!node.parent_group()) {
4034 return;
4035 }
4036 reorder_layer_data(*this, [&]() {
4037 node.parent_group()->unlink_node(node);
4038 parent_group.add_node(node);
4039 });
4040}
4041
4042const blender::bke::greasepencil::TreeNode *GreasePencil::find_node_by_name(
4043 const blender::StringRef name) const
4044{
4045 return this->root_group().find_node_by_name(name);
4046}
4047
4048blender::bke::greasepencil::TreeNode *GreasePencil::find_node_by_name(
4050{
4051 return this->root_group().find_node_by_name(name);
4052}
4053
4054blender::IndexMask GreasePencil::layer_selection_by_name(const blender::StringRef name,
4055 blender::IndexMaskMemory &memory) const
4056{
4057 using namespace blender::bke::greasepencil;
4058 const TreeNode *node = this->find_node_by_name(name);
4059 if (!node) {
4060 return {};
4061 }
4062
4063 if (node->is_layer()) {
4064 const int index = *this->get_layer_index(node->as_layer());
4066 }
4067 if (node->is_group()) {
4068 blender::Vector<int64_t> layer_indices;
4069 for (const int64_t layer_index : this->layers().index_range()) {
4070 const Layer &layer = *this->layers()[layer_index];
4071 if (layer.is_child_of(node->as_group())) {
4072 layer_indices.append(layer_index);
4073 }
4074 }
4075 return blender::IndexMask::from_indices(layer_indices.as_span(), memory);
4076 }
4077 return {};
4078}
4079
4081{
4082 switch (md->type) {
4084 auto *amd = reinterpret_cast<GreasePencilArmatureModifierData *>(md);
4085 return &amd->influence;
4086 }
4088 auto *mmd = reinterpret_cast<GreasePencilArrayModifierData *>(md);
4089 return &mmd->influence;
4090 }
4092 auto *bmd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
4093 return &bmd->influence;
4094 }
4096 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
4097 return &cmd->influence;
4098 }
4100 auto *dmd = reinterpret_cast<GreasePencilDashModifierData *>(md);
4101 return &dmd->influence;
4102 }
4104 auto *emd = reinterpret_cast<GreasePencilEnvelopeModifierData *>(md);
4105 return &emd->influence;
4106 }
4108 auto *hmd = reinterpret_cast<GreasePencilHookModifierData *>(md);
4109 return &hmd->influence;
4110 }
4112 auto *lmd = reinterpret_cast<GreasePencilLatticeModifierData *>(md);
4113 return &lmd->influence;
4114 }
4116 auto *lmd = reinterpret_cast<GreasePencilLengthModifierData *>(md);
4117 return &lmd->influence;
4118 }
4120 auto *mmd = reinterpret_cast<GreasePencilMirrorModifierData *>(md);
4121 return &mmd->influence;
4122 }
4124 auto *mmd = reinterpret_cast<GreasePencilMultiModifierData *>(md);
4125 return &mmd->influence;
4126 }
4128 auto *nmd = reinterpret_cast<GreasePencilNoiseModifierData *>(md);
4129 return &nmd->influence;
4130 }
4132 auto *omd = reinterpret_cast<GreasePencilOffsetModifierData *>(md);
4133 return &omd->influence;
4134 }
4136 auto *omd = reinterpret_cast<GreasePencilOpacityModifierData *>(md);
4137 return &omd->influence;
4138 }
4140 auto *omd = reinterpret_cast<GreasePencilOutlineModifierData *>(md);
4141 return &omd->influence;
4142 }
4144 auto *smd = reinterpret_cast<GreasePencilShrinkwrapModifierData *>(md);
4145 return &smd->influence;
4146 }
4148 auto *smd = reinterpret_cast<GreasePencilSimplifyModifierData *>(md);
4149 return &smd->influence;
4150 }
4152 auto *smd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
4153 return &smd->influence;
4154 }
4156 auto *smd = reinterpret_cast<GreasePencilSubdivModifierData *>(md);
4157 return &smd->influence;
4158 }
4160 auto *tmd = reinterpret_cast<GreasePencilTextureModifierData *>(md);
4161 return &tmd->influence;
4162 }
4164 auto *tmd = reinterpret_cast<GreasePencilThickModifierData *>(md);
4165 return &tmd->influence;
4166 }
4168 auto *tmd = reinterpret_cast<GreasePencilTimeModifierData *>(md);
4169 return &tmd->influence;
4170 }
4172 auto *tmd = reinterpret_cast<GreasePencilTintModifierData *>(md);
4173 return &tmd->influence;
4174 }
4176 auto *wmd = reinterpret_cast<GreasePencilWeightAngleModifierData *>(md);
4177 return &wmd->influence;
4178 }
4180 auto *wmd = reinterpret_cast<GreasePencilWeightProximityModifierData *>(md);
4181 return &wmd->influence;
4182 }
4185 default:
4186 return nullptr;
4187 }
4188 return nullptr;
4189}
4190
4191void GreasePencil::rename_node(Main &bmain,
4193 const blender::StringRef new_name)
4194{
4195 using namespace blender;
4196 if (node.name() == new_name) {
4197 return;
4198 }
4199
4200 /* Rename the node. */
4201 std::string old_name = node.name();
4202 if (node.is_layer()) {
4203 node.set_name(unique_layer_name(new_name));
4204 }
4205 else if (node.is_group()) {
4206 node.set_name(unique_layer_group_name(*this, new_name));
4207 }
4208
4209 /* Update layer name dependencies. */
4210 if (node.is_layer()) {
4211 BKE_animdata_fix_paths_rename_all(&this->id, "layers", old_name.c_str(), node.name().c_str());
4212 /* Update names in layer masks. */
4213 for (bke::greasepencil::Layer *layer : this->layers_for_write()) {
4215 if (STREQ(mask->layer_name, old_name.c_str())) {
4216 mask->layer_name = BLI_strdup(node.name().c_str());
4217 }
4218 }
4219 }
4220 }
4221
4222 /* Update name dependencies outside of the ID. */
4223 LISTBASE_FOREACH (Object *, object, &bmain.objects) {
4224 if (object->data != this) {
4225 continue;
4226 }
4227
4228 /* Update the layer name of the influence data of the modifiers. */
4229 LISTBASE_FOREACH (ModifierData *, md, &object->modifiers) {
4230 char *dst_layer_name = nullptr;
4231 size_t dst_layer_name_len = 0;
4232 /* LineArt doesn't use the `GreasePencilModifierInfluenceData` struct. */
4233 if (md->type == eModifierType_GreasePencilLineart) {
4234 auto *lmd = reinterpret_cast<GreasePencilLineartModifierData *>(md);
4235 dst_layer_name = lmd->target_layer;
4236 dst_layer_name_len = sizeof(lmd->target_layer);
4237 }
4239 md))
4240 {
4241 dst_layer_name = influence_data->layer_name;
4242 dst_layer_name_len = sizeof(influence_data->layer_name);
4243 }
4244 if (dst_layer_name && STREQ(dst_layer_name, old_name.c_str())) {
4245 BLI_strncpy(dst_layer_name, node.name().c_str(), dst_layer_name_len);
4246 }
4247 }
4248 }
4249}
4250
4251static void shrink_customdata(CustomData &data, const int index_to_remove, const int size)
4252{
4253 using namespace blender;
4254 CustomData new_data;
4256 CustomData_realloc(&new_data, size, size - 1);
4257
4258 const IndexRange range_before(index_to_remove);
4259 const IndexRange range_after(index_to_remove + 1, size - index_to_remove - 1);
4260
4261 if (!range_before.is_empty()) {
4263 &data, &new_data, range_before.start(), range_before.start(), range_before.size());
4264 }
4265 if (!range_after.is_empty()) {
4267 &data, &new_data, range_after.start(), range_after.start() - 1, range_after.size());
4268 }
4269
4271 data = new_data;
4272}
4273
4276{
4277 using namespace blender::bke::greasepencil;
4278 /* 1. Try setting the node below (within the same group) to be active. */
4279 if (node.prev != nullptr) {
4280 grease_pencil.set_active_node(reinterpret_cast<TreeNode *>(node.prev));
4281 }
4282 /* 2. If there is no node below, try setting the node above (within the same group) to be the
4283 * active one. */
4284 else if (node.next != nullptr) {
4285 grease_pencil.set_active_node(reinterpret_cast<TreeNode *>(node.next));
4286 }
4287 /* 3. If this is the only node within its parent group and the parent group is not the root
4288 * group, try setting the parent to be active. */
4289 else if (node.parent != grease_pencil.root_group_ptr) {
4290 grease_pencil.set_active_node(&node.parent->wrap().as_node());
4291 }
4292 /* 4. Otherwise, clear the active node. */
4293 else {
4294 grease_pencil.set_active_node(nullptr);
4295 }
4296}
4297
4298void GreasePencil::remove_layer(blender::bke::greasepencil::Layer &layer)
4299{
4300 using namespace blender::bke::greasepencil;
4301 /* If the layer is active, update the active layer. */
4302 if (&layer.as_node() == this->get_active_node()) {
4304 }
4305
4306 /* Remove all the layer attributes and shrink the `CustomData`. */
4307 const int layer_index = *this->get_layer_index(layer);
4308 shrink_customdata(this->layers_data, layer_index, this->layers().size());
4309
4310 /* Unlink the layer from the parent group. */
4311 layer.parent_group().unlink_node(layer.as_node());
4312
4313 /* Remove drawings. */
4314 for (const GreasePencilFrame frame : layer.frames().values()) {
4315 GreasePencilDrawingBase *drawing_base = this->drawing(frame.drawing_index);
4316 if (drawing_base->type != GP_DRAWING) {
4317 /* TODO: Remove drawing reference. */
4318 continue;
4319 }
4320 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
4321 drawing->wrap().remove_user();
4322 }
4323 this->remove_drawings_with_no_users();
4324
4325 /* Delete the layer. */
4326 MEM_delete(&layer);
4327}
4328
4329void GreasePencil::remove_group(blender::bke::greasepencil::LayerGroup &group,
4330 const bool keep_children)
4331{
4332 using namespace blender::bke::greasepencil;
4333 /* If the group is active, update the active layer. */
4334 if (&group.as_node() == this->get_active_node()) {
4335 /* If we keep the children and there is at least one child, make it the active node. */
4336 if (keep_children && !group.is_empty()) {
4337 this->set_active_node(reinterpret_cast<TreeNode *>(group.children.last));
4338 }
4339 else {
4341 }
4342 }
4343
4344 if (!keep_children) {
4345 /* Recursively remove groups and layers. */
4347 switch (child->type) {
4348 case GP_LAYER_TREE_LEAF: {
4349 this->remove_layer(reinterpret_cast<GreasePencilLayer *>(child)->wrap());
4350 break;
4351 }
4352 case GP_LAYER_TREE_GROUP: {
4353 this->remove_group(reinterpret_cast<GreasePencilLayerTreeGroup *>(child)->wrap(), false);
4354 break;
4355 }
4356 default:
4358 }
4359 }
4361 }
4362
4363 /* Unlink then delete active group node. */
4364 group.as_node().parent_group()->unlink_node(group.as_node(), true);
4365 MEM_delete(&group);
4366}
4367
4368void GreasePencil::print_layer_tree()
4369{
4370 using namespace blender::bke::greasepencil;
4371 this->root_group().print_nodes("Layer Tree:");
4372}
4373
4374blender::bke::AttributeAccessor GreasePencil::attributes() const
4375{
4378}
4379
4380blender::bke::MutableAttributeAccessor GreasePencil::attributes_for_write()
4381{
4384}
4385
4388/* ------------------------------------------------------------------- */
4393{
4394 switch (type) {
4395 case GP_DRAWING:
4396 return sizeof(GreasePencilDrawing);
4398 return sizeof(GreasePencilDrawingReference);
4399 }
4400 return sizeof(GreasePencilDrawingBase);
4401}
4402
4403static void read_drawing_array(GreasePencil &grease_pencil, BlendDataReader *reader)
4404{
4406 grease_pencil.drawing_array_num,
4407 reinterpret_cast<void **>(&grease_pencil.drawing_array));
4408 for (int i = 0; i < grease_pencil.drawing_array_num; i++) {
4410 reader, &grease_pencil.drawing_array[i], [](const GreasePencilDrawingBase &drawing) {
4411 return drawing_struct_size(GreasePencilDrawingType(drawing.type));
4412 });
4413 GreasePencilDrawingBase *drawing_base = grease_pencil.drawing_array[i];
4414
4415 /* Replace drawings that failed to read from corrupt files by an empty drawing, since further
4416 * reading code expects valid drawings. */
4417 if (drawing_base == nullptr) {
4418 grease_pencil.drawing_array[i] = reinterpret_cast<GreasePencilDrawingBase *>(
4419 MEM_new<blender::bke::greasepencil::Drawing>(__func__));
4420 continue;
4421 }
4422
4423 switch (GreasePencilDrawingType(drawing_base->type)) {
4424 case GP_DRAWING: {
4425 GreasePencilDrawing *drawing = reinterpret_cast<GreasePencilDrawing *>(drawing_base);
4426 drawing->wrap().strokes_for_write().blend_read(*reader);
4427 /* Initialize runtime data. */
4428 drawing->runtime = MEM_new<blender::bke::greasepencil::DrawingRuntime>(__func__);
4429 break;
4430 }
4431 case GP_DRAWING_REFERENCE: {
4432 break;
4433 }
4434 }
4435 }
4436}
4437
4438static void write_drawing_array(GreasePencil &grease_pencil,
4440 BlendWriter *writer)
4441{
4442 using namespace blender;
4443 BLO_write_pointer_array(writer, grease_pencil.drawing_array_num, grease_pencil.drawing_array);
4444 for (int i = 0; i < grease_pencil.drawing_array_num; i++) {
4445 GreasePencilDrawingBase *drawing_base = grease_pencil.drawing_array[i];
4446 switch (GreasePencilDrawingType(drawing_base->type)) {
4447 case GP_DRAWING: {
4448 GreasePencilDrawing &drawing_copy = scope.construct<GreasePencilDrawing>();
4449 drawing_copy = *reinterpret_cast<GreasePencilDrawing *>(drawing_base);
4450 bke::CurvesGeometry &curves = drawing_copy.geometry.wrap();
4451
4452 bke::CurvesGeometry::BlendWriteData write_data(scope);
4453 curves.blend_write_prepare(write_data);
4454 drawing_copy.runtime = nullptr;
4455
4456 BLO_write_struct_at_address(writer, GreasePencilDrawing, drawing_base, &drawing_copy);
4457 curves.blend_write(*writer, grease_pencil.id, write_data);
4458 break;
4459 }
4460 case GP_DRAWING_REFERENCE: {
4461 GreasePencilDrawingReference *drawing_reference =
4462 reinterpret_cast<GreasePencilDrawingReference *>(drawing_base);
4463 BLO_write_struct(writer, GreasePencilDrawingReference, drawing_reference);
4464 break;
4465 }
4466 }
4467 }
4468}
4469
4470static void free_drawing_array(GreasePencil &grease_pencil)
4471{
4472 grease_pencil.resize_drawings(0);
4473}
4474
4477/* ------------------------------------------------------------------- */
4481static void read_layer(BlendDataReader *reader,
4484{
4485 BLO_read_string(reader, &node->base.name);
4486 node->base.parent = parent;
4487 BLO_read_string(reader, &node->parsubstr);
4488 BLO_read_string(reader, &node->viewlayername);
4489
4490 /* Read frames storage. */
4491 BLO_read_int32_array(reader, node->frames_storage.num, &node->frames_storage.keys);
4493 reader, GreasePencilFrame, node->frames_storage.num, &node->frames_storage.values);
4494
4495 /* Read layer masks. */
4498 BLO_read_string(reader, &mask->layer_name);
4499 }
4500
4501 /* NOTE: Ideally this should be cleared on write, to reduce false 'changes' detection in memfile
4502 * undo system. This is not easily doable currently though, since modifying to actual data during
4503 * write is not an option (a shallow copy of the #Layer data would be needed then). */
4504 node->runtime = nullptr;
4505 node->wrap().update_from_dna_read();
4506}
4507
4509{
4510 switch (type) {
4511 case GP_LAYER_TREE_LEAF:
4512 return sizeof(GreasePencilLayer);
4514 return sizeof(GreasePencilLayerTreeGroup);
4515 }
4516 return sizeof(GreasePencilLayerTreeNode);
4517}
4518
4522{
4523 BLO_read_string(reader, &node->base.name);
4524 node->base.parent = parent;
4525 /* Read list of children. */
4527 reader, &node->children, [](const GreasePencilLayerTreeNode &child) {
4528 return layer_tree_node_struct_size(GreasePencilLayerTreeNodeType(child.type));
4529 });
4530 LISTBASE_FOREACH (GreasePencilLayerTreeNode *, child, &node->children) {
4531 switch (child->type) {
4532 case GP_LAYER_TREE_LEAF: {
4533 GreasePencilLayer *layer = reinterpret_cast<GreasePencilLayer *>(child);
4534 read_layer(reader, layer, node);
4535 break;
4536 }
4537 case GP_LAYER_TREE_GROUP: {
4538 GreasePencilLayerTreeGroup *group = reinterpret_cast<GreasePencilLayerTreeGroup *>(child);
4539 read_layer_tree_group(reader, group, node);
4540 break;
4541 }
4542 }
4543 }
4544
4545 node->wrap().runtime = MEM_new<blender::bke::greasepencil::LayerGroupRuntime>(__func__);
4546}
4547
4548static void read_layer_tree(GreasePencil &grease_pencil, BlendDataReader *reader)
4549{
4550 /* Read root group. */
4552 /* This shouldn't normally happen, but for files that were created before the root group became a
4553 * pointer, this address will not exist. In this case, we clear the pointer to the active layer
4554 * and create an empty root group to avoid crashes. */
4555 if (grease_pencil.root_group_ptr == nullptr) {
4556 grease_pencil.root_group_ptr = MEM_new<blender::bke::greasepencil::LayerGroup>(__func__);
4557 grease_pencil.set_active_node(nullptr);
4558 return;
4559 }
4560 read_layer_tree_group(reader, grease_pencil.root_group_ptr, nullptr);
4561 /* Read active layer after the tree, so it is null if its node failed to read. */
4562 BLO_read_struct(reader, GreasePencilLayerTreeNode, &grease_pencil.active_node);
4563
4564 grease_pencil.root_group_ptr->wrap().update_from_dna_read();
4565}
4566
4568{
4570 BLO_write_string(writer, node->base.name);
4571 BLO_write_string(writer, node->parsubstr);
4572 BLO_write_string(writer, node->viewlayername);
4573
4574 BLO_write_int32_array(writer, node->frames_storage.num, node->frames_storage.keys);
4576 writer, GreasePencilFrame, node->frames_storage.num, node->frames_storage.values);
4577
4580 BLO_write_string(writer, mask->layer_name);
4581 }
4582}
4583
4585{
4587 BLO_write_string(writer, node->base.name);
4588 LISTBASE_FOREACH (GreasePencilLayerTreeNode *, child, &node->children) {
4589 switch (child->type) {
4590 case GP_LAYER_TREE_LEAF: {
4591 GreasePencilLayer *layer = reinterpret_cast<GreasePencilLayer *>(child);
4592 write_layer(writer, layer);
4593 break;
4594 }
4595 case GP_LAYER_TREE_GROUP: {
4596 GreasePencilLayerTreeGroup *group = reinterpret_cast<GreasePencilLayerTreeGroup *>(child);
4597 write_layer_tree_group(writer, group);
4598 break;
4599 }
4600 }
4601 }
4602}
4603
4604static void write_layer_tree(GreasePencil &grease_pencil, BlendWriter *writer)
4605{
4606 grease_pencil.root_group_ptr->wrap().prepare_for_dna_write();
4607 write_layer_tree_group(writer, grease_pencil.root_group_ptr);
4608}
Blender kernel action and pose functionality.
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void BKE_animdata_free(ID *id, bool do_id_user)
Definition anim_data.cc:187
void BKE_animdata_fix_paths_rename_all(struct ID *ref_id, const char *prefix, const char *oldName, const char *newName)
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
void CustomData_blend_write_prepare(CustomData &data, blender::bke::AttrDomain domain, int domain_size, blender::Vector< CustomDataLayer, 16 > &layers_to_write, blender::bke::AttributeStorage::BlendWriteData &write_data)
void CustomData_realloc(CustomData *data, int old_size, int new_size, eCDAllocType alloctype=CD_CONSTRUCT)
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, blender::StringRef name, int totelem)
@ CD_SET_DEFAULT
@ CD_CONSTRUCT
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, blender::StringRef name)
void CustomData_reset(CustomData *data)
void CustomData_blend_write(BlendWriter *writer, CustomData *data, blender::Span< CustomDataLayer > layers_to_write, int count, eCustomDataMask cddata_mask, ID *id)
void CustomData_copy_data_layer(const CustomData *source, CustomData *dest, int src_layer_index, int dst_layer_index, int src_index, int dst_index, int count)
void CustomData_free(CustomData *data)
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
void CustomData_blend_read(BlendDataReader *reader, CustomData *data, int count)
void CustomData_init_layout_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
support for deformation groups and hooks.
void BKE_defbase_blend_write(BlendWriter *writer, const ListBase *defbase)
Definition deform.cc:1632
void BKE_defgroup_copy_list(ListBase *outbase, const ListBase *inbase)
Definition deform.cc:71
Low-level operations for grease pencil that cannot be defined in the C++ header yet.
void(* BKE_grease_pencil_batch_cache_dirty_tag_cb)(GreasePencil *grease_pencil, int mode)
void BKE_grease_pencil_batch_cache_dirty_tag(GreasePencil *grease_pencil, int mode)
void BKE_grease_pencil_batch_cache_free(GreasePencil *grease_pencil)
void(* BKE_grease_pencil_batch_cache_free_cb)(GreasePencil *grease_pencil)
Low-level operations for grease pencil.
void BKE_grease_pencil_point_coords_apply(GreasePencil &grease_pencil, blender::Span< blender::float3 > all_positions, blender::Span< float > all_radii)
void BKE_grease_pencil_copy_parameters(const GreasePencil &src, GreasePencil &dst)
void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol)
void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, int index)
void BKE_grease_pencil_vgroup_name_update(Object *ob, const char *old_name, const char *new_name)
Material * BKE_grease_pencil_object_material_alt_ensure_from_brush(Main *bmain, Object *ob, Brush *brush)
void BKE_grease_pencil_point_coords_apply_with_mat4(GreasePencil &grease_pencil, blender::Span< blender::float3 > all_positions, blender::Span< float > all_radii, const blender::float4x4 &mat)
void BKE_grease_pencil_point_coords_get(const GreasePencil &grease_pencil, blender::MutableSpan< blender::float3 > all_positions, blender::MutableSpan< float > all_radii)
void BKE_object_eval_grease_pencil(Depsgraph *depsgraph, Scene *scene, Object *object)
bool BKE_grease_pencil_references_cyclic_check(const GreasePencil *id_reference, const GreasePencil *grease_pencil)
Material * BKE_grease_pencil_object_material_new(Main *bmain, Object *ob, const char *name, int *r_index)
GreasePencil * BKE_grease_pencil_add(Main *bmain, const char *name)
Material * BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain, Object *ob, Brush *brush)
void BKE_grease_pencil_nomain_to_grease_pencil(GreasePencil *grease_pencil_src, GreasePencil *grease_pencil_dst)
void BKE_grease_pencil_eval_geometry(Depsgraph *depsgraph, GreasePencil *grease_pencil)
void BKE_grease_pencil_duplicate_drawing_array(const GreasePencil *grease_pencil_src, GreasePencil *grease_pencil_dst)
bool BKE_grease_pencil_has_curve_with_type(const GreasePencil &grease_pencil, CurveType type)
Material * BKE_grease_pencil_object_material_from_brush_get(Object *ob, Brush *brush)
int BKE_grease_pencil_stroke_point_count(const GreasePencil &grease_pencil)
GreasePencil * BKE_grease_pencil_new_nomain()
void BKE_grease_pencil_copy_layer_parameters(const blender::bke::greasepencil::Layer &src, blender::bke::greasepencil::Layer &dst)
void BKE_grease_pencil_copy_layer_group_parameters(const blender::bke::greasepencil::LayerGroup &src, blender::bke::greasepencil::LayerGroup &dst)
bool BKE_grease_pencil_drawing_attribute_required(const GreasePencilDrawing *, blender::StringRef name)
GreasePencil * BKE_grease_pencil_copy_for_eval(const GreasePencil *grease_pencil_src)
bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index)
int BKE_grease_pencil_object_material_index_get_by_name(Object *ob, const char *name)
Material * BKE_grease_pencil_object_material_ensure_by_name(Main *bmain, Object *ob, const char *name, int *r_index)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:44
void BKE_id_free(Main *bmain, void *idv)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:767
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1495
@ LIB_ID_COPY_LOCALIZE
const char * BKE_id_name(const ID &id)
void id_us_min(ID *id)
Definition lib_id.cc:361
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1500
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2611
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
@ IDWALK_CB_USER
General operations, lookup, etc. for materials.
short * BKE_object_material_len_p(Object *ob)
short BKE_object_material_slot_find_index(Object *ob, Material *ma)
Material * BKE_gpencil_material_add(Main *bmain, const char *name)
bool BKE_object_material_slot_add(Main *bmain, Object *ob, bool set_active=true)
Material * BKE_material_default_gpencil()
Material * BKE_object_material_get(Object *ob, short act)
@ BKE_MAT_ASSIGN_USERPREF
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
int BKE_object_material_index_get(Object *ob, const Material *ma)
void BKE_modifiers_clear_errors(Object *ob)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
ModifierApplyFlag
@ MOD_APPLY_USECACHE
@ MOD_APPLY_RENDER
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const Object *ob)
void BKE_object_eval_assign_data(Object *object, ID *data, bool is_owned)
void BKE_object_free_derived_caches(Object *ob)
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define ATTR_FALLTHROUGH
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
#define LISTBASE_FOREACH_BACKWARD(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:332
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:436
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:154
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:371
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
float mat4_to_scale(const float mat[4][4])
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void unit_m4(float m[4][4])
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void zero_v3(float r[3])
#define BLI_MEMARENA_STD_BUFSIZE
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
ATTR_WARN_UNUSED_RESULT const size_t num
void BLI_polyfill_calc_arena(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3], struct MemArena *arena)
int key
std::string data
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.cc:30
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
Definition string.cc:46
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
size_t void BLI_uniquename_cb(blender::FunctionRef< bool(blender::StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
unsigned int uint
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define STREQ(a, b)
void BLO_write_int32_array(BlendWriter *writer, int64_t num, const int32_t *data_ptr)
void BLO_read_int32_array(BlendDataReader *reader, int64_t array_size, int32_t **ptr_p)
Definition readfile.cc:5738
void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
Definition readfile.cc:5937
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5647
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5783
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
void BLO_read_struct_with_type_size(BlendDataReader *reader, T **ptr_p, blender::FunctionRef< size_t(const T &data)> type_size_fn)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define BLO_write_struct_list(writer, struct_name, list_ptr)
#define BLO_read_struct(reader, struct_name, ptr_p)
void BLO_write_pointer_array(BlendWriter *writer, int64_t num, const void *data_ptr)
void BLO_read_pointer_array(BlendDataReader *reader, int64_t array_size, void **ptr_p)
Definition readfile.cc:5834
#define BLO_write_struct_at_address(writer, struct_name, address, data_ptr)
#define BLT_I18NCONTEXT_ID_GPENCIL
#define DATA_(msgid)
float[3] Vector
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
void DEG_id_tag_update(ID *id, unsigned int flags)
@ DAG_EVAL_RENDER
float DEG_get_ctime(const Depsgraph *graph)
bool DEG_is_evaluated(const T *id)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
T * DEG_get_original(T *id)
ID and Library types, which are fundamental for SDNA.
#define FILTER_ID_OB
Definition DNA_ID.h:1122
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
#define FILTER_ID_MA
Definition DNA_ID.h:1116
@ INDEX_ID_GP
Definition DNA_ID.h:1245
#define FILTER_ID_GP
Definition DNA_ID.h:1144
Enumerations for DNA_ID.h.
@ GP_BRUSH_MATERIAL_PINNED
eBezTriple_KeyframeType
CurveType
@ CURVE_TYPE_BEZIER
@ CURVE_TYPE_POLY
#define CD_MASK_ALL
#define DNA_struct_default_get(struct_name)
struct GreasePencilDrawingBase GreasePencilDrawingBase
struct GreasePencilLayerTreeGroup GreasePencilLayerTreeGroup
struct GreasePencilDrawingReference GreasePencilDrawingReference
@ GP_LAYER_BLEND_NONE
@ GP_LAYER_FRAMES_STORAGE_DIRTY
@ LAYERGROUP_COLOR_NONE
struct GreasePencil GreasePencil
GreasePencilLayerTreeNodeType
@ GP_LAYER_TREE_GROUP
@ GP_FRAME_IMPLICIT_HOLD
@ GP_LAYER_TREE_NODE_EXPANDED
@ GP_LAYER_TREE_NODE_USE_LIGHTS
@ GP_LAYER_TREE_NODE_HIDE_MASKS
struct GreasePencilDrawing GreasePencilDrawing
GreasePencilDrawingType
@ GP_DRAWING_REFERENCE
struct GreasePencilLayerTreeNode GreasePencilLayerTreeNode
@ GREASE_PENCIL_AUTOLOCK_LAYERS
struct GreasePencilLayer GreasePencilLayer
@ eModifierMode_Render
@ eModifierMode_Editmode
@ eModifierMode_Realtime
@ eModifierType_GreasePencilSmooth
@ eModifierType_GreasePencilWeightProximity
@ eModifierType_GreasePencilMirror
@ eModifierType_GreasePencilOffset
@ eModifierType_GreasePencilThickness
@ eModifierType_GreasePencilEnvelope
@ eModifierType_GreasePencilArmature
@ eModifierType_GreasePencilSubdiv
@ eModifierType_GreasePencilTint
@ eModifierType_GreasePencilNoise
@ eModifierType_GreasePencilMultiply
@ eModifierType_GreasePencilBuild
@ eModifierType_GreasePencilTime
@ eModifierType_GreasePencilTexture
@ eModifierType_GreasePencilSimplify
@ eModifierType_GreasePencilColor
@ eModifierType_GreasePencilDash
@ eModifierType_GreasePencilLineart
@ eModifierType_GreasePencilOutline
@ eModifierType_GreasePencilWeightAngle
@ eModifierType_GreasePencilOpacity
@ eModifierType_GreasePencilLength
@ eModifierType_GreasePencilShrinkwrap
@ eModifierType_GreasePencilLattice
@ eModifierType_GreasePencilHook
@ eModifierType_GreasePencilArray
@ OB_MODE_VERTEX_GREASE_PENCIL
@ OB_MODE_EDIT
@ OB_MODE_SCULPT_GREASE_PENCIL
@ OB_MODE_WEIGHT_GREASE_PENCIL
@ OB_ARMATURE
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
static const char * ATTR_POSITION
iter begin(iter)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
Span< T > as_span() const
Definition BLI_array.hh:232
IndexRange index_range() const
Definition BLI_array.hh:349
static const CPPType & get()
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr int64_t start() const
constexpr IndexRange take_back(int64_t n) const
static constexpr IndexRange from_single(const int64_t index)
constexpr IndexRange drop_front(int64_t n) const
void clear()
Definition BLI_map.hh:1038
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void remove_contained(const Key &key)
Definition BLI_map.hh:387
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
KeyIterator keys() const &
Definition BLI_map.hh:875
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
constexpr bool is_empty() const
Definition BLI_span.hh:509
constexpr T * data() const
Definition BLI_span.hh:539
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:607
constexpr T & first() const
Definition BLI_span.hh:679
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
T & construct(Args &&...args)
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr const T * data() const
Definition BLI_span.hh:215
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
constexpr bool contains(const T &value) const
Definition BLI_span.hh:277
bool is_empty() const
Definition BLI_stack.hh:308
void push(const T &value)
Definition BLI_stack.hh:213
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
std::optional< T > get_if_single() const
Span< T > get_internal_span() const
bool add(const Key &key)
bool contains(const Key &key) const
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
void reserve(const int64_t min_capacity)
void reinitialize(const int64_t new_size)
Span< T > as_span() const
bool contains(StringRef attribute_id) const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
MutableSpan< float3 > positions_for_write()
OffsetIndices< int > points_by_curve() const
void ensure_can_interpolate_to_evaluated() const
IndexRange curves_range() const
MutableSpan< float3 > handle_positions_left_for_write()
MutableAttributeAccessor attributes_for_write()
MutableSpan< float3 > handle_positions_right_for_write()
VArray< float > radius() const
void blend_write(BlendWriter &writer, ID &id, const BlendWriteData &write_data)
void blend_write_prepare(BlendWriteData &write_data)
Span< float3 > handle_positions_left() const
void interpolate_to_evaluated(int curve_index, GSpan src, GMutableSpan dst) const
IndexRange points_range() const
Span< float3 > positions() const
OffsetIndices< int > evaluated_points_by_curve() const
bool has_curve_with_type(CurveType type) const
Span< float3 > handle_positions_right() const
AttributeAccessor attributes() const
void count_memory(MemoryCounter &memory) const
bool is_single_type(CurveType type) const
std::optional< Bounds< float3 > > bounds_min_max(bool use_radius=true) const
Span< float3 > evaluated_positions() const
std::unique_ptr< GreasePencilEditHints > grease_pencil_edit_hints_
std::optional< MutableSpan< float3 > > positions_for_write()
std::optional< Span< float3 > > positions() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef attribute_id)
VArray< ColorGeometry4f > vertex_colors() const
Drawing & operator=(const Drawing &other)
Span< float3 > curve_plane_normals() const
MutableSpan< float > opacities_for_write()
Span< float4x2 > texture_matrices() const
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
VArray< ColorGeometry4f > fill_colors() const
VArray< float > opacities() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void set_texture_matrices(Span< float4x2 > matrices, const IndexMask &selection)
TreeNode & add_node(TreeNode &node)
void move_node_down(TreeNode &node, int step=1)
bool unlink_node(TreeNode &link, bool keep_children=false)
const TreeNode * find_node_by_name(StringRef name) const
Span< const Layer * > layers() const
void add_node_before(TreeNode &node, TreeNode &link)
void add_node_after(TreeNode &node, TreeNode &link)
void move_node_up(TreeNode &node, int step=1)
void print_nodes(StringRef header) const
LayerGroup & operator=(const LayerGroup &other)
Span< const LayerGroup * > groups() const
Span< const TreeNode * > nodes() const
SharedCache< Vector< FramesMapKeyT > > sorted_keys_cache_
Map< FramesMapKeyT, GreasePencilFrame > frames_
SortedKeysIterator sorted_keys_iterator_at(int frame_number) const
StringRefNull parent_bone_name() const
int sorted_keys_index_at(int frame_number) const
float4x4 to_world_space(const Object &object) const
StringRefNull view_layer_name() const
void set_local_transform(const float4x4 &transform)
void set_view_layer_name(StringRef new_name)
bool remove_frame(FramesMapKeyT key)
const Map< FramesMapKeyT, GreasePencilFrame > & frames() const
GreasePencilFrame * add_frame(FramesMapKeyT key, int duration=0)
const GreasePencilFrame * frame_at(const int frame_number) const
bool has_drawing_at(const int frame_number) const
int drawing_index_at(const int frame_number) const
int get_frame_duration_at(const int frame_number) const
std::optional< int > start_frame_at(int frame_number) const
float4x4 to_object_space(const Object &object) const
Span< FramesMapKeyT > sorted_keys() const
const LayerGroup & parent_group() const
Map< FramesMapKeyT, GreasePencilFrame > & frames_for_write()
void set_parent_bone_name(StringRef new_name)
const TreeNode * parent_node() const
const LayerGroup & as_group() const
const LayerGroup * parent_group() const
static IndexMask from_indices(Span< T > indices, IndexMaskMemory &memory)
IndexMask complement(const IndexMask &universe, IndexMaskMemory &memory) const
void foreach_index(Fn &&fn) const
void foreach_segment(Fn &&fn) const
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
OperationNode * node
Scene scene
const Depsgraph * depsgraph
#define rot(x, k)
static float normals[][3]
PointerRNA ptr
SearchInfo info
StringRefNull name
uint pos
#define sin
unsigned int uint32_t
#define this
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
MatBase< R, C > transpose(MatBase< C, R >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
static void shrink_array(T **array, int *num, const int shrink_num)
static void transform_positions(const Span< blender::float3 > src, const blender::float4x4 &transform, blender::MutableSpan< blender::float3 > dst)
static void read_layer_tree_group(BlendDataReader *reader, GreasePencilLayerTreeGroup *node, GreasePencilLayerTreeGroup *parent)
static void read_drawing_array(GreasePencil &grease_pencil, BlendDataReader *reader)
static void grease_pencil_evaluate_layers(GreasePencil &grease_pencil)
static GreasePencilModifierInfluenceData * influence_data_from_modifier(ModifierData *md)
static void grow_array(T **array, int *num, const int add_num)
static bool grease_pencil_references_cyclic_check_internal(const GreasePencil *id_reference, const GreasePencil *grease_pencil)
static void grease_pencil_free_data(ID *id)
static std::string unique_node_name(const GreasePencil &grease_pencil, const blender::StringRef name)
static void reorder_layer_data(GreasePencil &grease_pencil, const blender::FunctionRef< void()> do_layer_order_changes)
static void free_drawing_array(GreasePencil &grease_pencil)
static void write_layer_tree_group(BlendWriter *writer, GreasePencilLayerTreeGroup *node)
static bool grease_pencil_validate_drawing_data(GreasePencil &grease_pencil)
static void update_active_node_from_node_to_remove(GreasePencil &grease_pencil, const blender::bke::greasepencil::TreeNode &node)
static void grease_pencil_replace_end_frames_after_read(GreasePencil &grease_pencil)
static void grease_pencil_do_layer_adjustments(GreasePencil &grease_pencil)
static const char * ATTR_POSITION
static void shrink_customdata(CustomData &data, const int index_to_remove, const int size)
static void grease_pencil_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
static size_t layer_tree_node_struct_size(const GreasePencilLayerTreeNodeType type)
void BKE_grease_pencil_batch_cache_free(GreasePencil *grease_pencil)
static void reorder_customdata(CustomData &data, const Span< int > new_by_old_map)
static void read_layer_tree(GreasePencil &grease_pencil, BlendDataReader *reader)
static void write_layer(BlendWriter *writer, GreasePencilLayer *node)
IDTypeInfo IDType_ID_GP
static Material * grease_pencil_object_material_ensure_from_brush_pinned(Main *bmain, Object *ob, Brush *brush)
static std::string unique_layer_group_name(const GreasePencil &grease_pencil, blender::StringRef name)
static void write_layer_tree(GreasePencil &grease_pencil, BlendWriter *writer)
static void grease_pencil_set_runtime_visibilities(ID &id_dst, GreasePencil &grease_pencil)
static void delete_drawing(GreasePencilDrawingBase *drawing_base)
static void grease_pencil_blend_read_data(BlendDataReader *reader, ID *id)
static void write_drawing_array(GreasePencil &grease_pencil, blender::ResourceScope &scope, BlendWriter *writer)
static blender::VectorSet< blender::StringRef > get_node_names(const GreasePencil &grease_pencil)
static CLG_LogRef LOG
void BKE_grease_pencil_duplicate_drawing_array(const GreasePencil *grease_pencil_src, GreasePencil *grease_pencil_dst)
static void grease_pencil_foreach_id(ID *id, LibraryForeachIDData *data)
static void grease_pencil_blend_write(BlendWriter *writer, ID *id, const void *id_address)
static size_t drawing_struct_size(const GreasePencilDrawingType type)
static void grease_pencil_init_data(ID *id)
static void read_layer(BlendDataReader *reader, GreasePencilLayer *node, GreasePencilLayerTreeGroup *parent)
static void grease_pencil_evaluate_modifiers(Depsgraph *depsgraph, Scene *scene, Object *object, blender::bke::GeometrySet &geometry_set)
IndexRange range
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static ulong * next
#define T
void copy_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
GAttributeReader lookup(const void *owner, const StringRef name)
void ensure_non_empty_layer_names(Main &bmain, GreasePencil &grease_pencil)
static void update_curve_plane_normal_cache(const Span< float3 > positions, const OffsetIndices< int > points_by_curve, const IndexMask &curve_mask, MutableSpan< float3 > normals)
static int domain_num(const CurvesGeometry &curves, const AttrDomain domain)
static CustomData & domain_custom_data(CurvesGeometry &curves, const AttrDomain domain)
constexpr StringRef ATTR_RADIUS
static void update_triangle_cache(const Span< float3 > positions, const Span< float3 > normals, const OffsetIndices< int > points_by_curve, const OffsetIndices< int > triangle_offsets, const IndexMask &curve_mask, MutableSpan< int3 > triangles)
static float4x2 get_local_to_stroke_matrix(const Span< float3 > positions, const float3 normal)
constexpr StringRef ATTR_VERTEX_COLOR
static MutableSpan< T > get_mutable_attribute(CurvesGeometry &curves, const AttrDomain domain, const StringRef name, const T default_value=T())
static float3x2 get_stroke_to_texture_matrix(const float uv_rotation, const float2 uv_translation, const float2 uv_scale)
const AttributeAccessorFunctions & get_attribute_accessor_functions()
constexpr StringRef ATTR_OPACITY
constexpr StringRef ATTR_FILL_COLOR
static float4x3 expand_4x2_mat(float4x2 strokemat)
void copy_drawing_array(Span< const GreasePencilDrawingBase * > src_drawings, MutableSpan< GreasePencilDrawingBase * > dst_drawings)
constexpr StringRef ATTR_RADIUS
static MutableSpan< T > get_mutable_attribute(CurvesGeometry &curves, const AttrDomain domain, const StringRef name, const T default_value=T())
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void fill_attribute_range_default(MutableAttributeAccessor dst_attributes, AttrDomain domain, const AttributeFilter &attribute_filter, IndexRange range)
eCustomDataType cpp_type_to_custom_data_type(const CPPType &type)
ID * asset_edit_id_find_local(Main &global_main, ID &id)
void grease_pencil_convert_storage_to_customdata(GreasePencil &grease_pencil)
ID * asset_edit_id_ensure_local(Main &global_main, ID &id)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:26
std::optional< Bounds< T > > min_max_with_radii(const Span< T > values, const Span< RadiusT > radii)
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:55
T length_squared(const VecBase< T, Size > &a)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T safe_rcp(const T &a)
T sign(const T &a)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
bool is_zero(const T &a)
CartesianBasis invert(const CartesianBasis &basis)
T atan2(const T &y, const T &x)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void to_loc_rot_scale_safe(const MatBase< T, 4, 4 > &mat, VecBase< T, 3 > &r_location, RotationT &r_rotation, VecBase< T, 3 > &r_scale)
T determinant(const MatBase< T, Size, Size > &mat)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:56
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< double, 4, 2 > double4x2
MatBase< float, 2, 2 > float2x2
MatBase< float, 2, 4 > float2x4
VecBase< float, 4 > float4
MatView< float, 4, 4, 4, 4, 0, 0, alignof(float)> float4x4_view
VecBase< float, 2 > float2
MatBase< float, 3, 2 > float3x2
MatBase< float, 4, 3 > float4x3
void uninitialized_relocate_n(T *src, int64_t n, T *dst)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:342
VecBase< float, 3 > float3
MatBase< double, 4, 3 > double4x3
void uninitialized_move_n(T *src, int64_t n, T *dst)
float wrap(float value, float max, float min)
Definition node_math.h:71
static void unique_name(bNode *node)
PropertyRNA * RNA_struct_type_find_property(StructRNA *srna, const char *identifier)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1173
__int64 int64_t
Definition stdint.h:89
signed char int8_t
Definition stdint.h:75
struct Material * material
struct Material * material_alt
struct BrushGpencilSettings * gpencil_settings
CustomData point_data
CurvesGeometryRuntimeHandle * runtime
ListBase vertex_group_names
CustomData curve_data
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilDrawingBase base
GreasePencilDrawingRuntimeHandle * runtime
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilLayerTreeNode base
GreasePencilLayerGroupRuntimeHandle * runtime
struct GreasePencilLayerTreeNode * next
struct GreasePencilLayerTreeNode * prev
struct GreasePencilLayerTreeGroup * parent
GreasePencilLayerRuntimeHandle * runtime
GreasePencilLayerTreeNode base
GreasePencilLayerFramesMapStorage frames_storage
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilModifierInfluenceData influence
GreasePencilLayerTreeNode * active_node
struct Material ** material_array
GreasePencilLayerTreeGroup * root_group_ptr
GreasePencilRuntimeHandle * runtime
GreasePencilDrawingBase ** drawing_array
GreasePencilOnionSkinningSettings onion_skinning_settings
struct AnimData * adt
struct AttributeStorage attribute_storage
Definition DNA_ID.h:404
char name[66]
Definition DNA_ID.h:415
void * last
void * first
ListBase objects
Definition BKE_main.hh:247
struct ModifierData * next
void(* modify_geometry_set)(ModifierData *md, const ModifierEvalContext *ctx, blender::bke::GeometrySet *geometry_set)
struct bPose * pose
void pad(const PaddingT &padding)
const c_style_mat & ptr() const
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
const GreasePencil * get_grease_pencil() const
GreasePencil * get_grease_pencil_for_write()
void replace_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
float frame
#define N_(msgid)