Blender V5.3
MOD_grease_pencil_build.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array.hh"
10#include "BLI_sort.hh"
11
12#include "BLT_translation.hh"
13
14#include "BLO_read_write.hh"
15
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
18#include "DNA_screen_types.h"
19
21
22#include "BKE_curves.hh"
23#include "BKE_geometry_set.hh"
24#include "BKE_grease_pencil.hh"
25#include "BKE_lib_query.hh"
26#include "BKE_modifier.hh"
27
29#include "UI_resources.hh"
30
32#include "MOD_modifiertypes.hh"
33#include "MOD_ui_common.hh"
34
35#include "RNA_access.hh"
36#include "RNA_prototypes.hh"
37
38#include "GEO_reorder.hh"
39
40namespace blender {
41
42/* The time in seconds strokes will take when the `delta_time` attribute does not exist. */
43constexpr float GP_BUILD_TIME_DEFAULT_STROKES = 1.0f;
44
45static void init_data(ModifierData *md)
46{
47 auto *gpmd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
49 modifier::greasepencil::init_influence_data(&gpmd->influence, false);
50}
51
52static void copy_data(const ModifierData *md, ModifierData *target, int flags)
53{
54 const auto *omd = reinterpret_cast<const GreasePencilBuildModifierData *>(md);
55 auto *tomd = reinterpret_cast<GreasePencilBuildModifierData *>(target);
56
58
59 BKE_modifier_copydata_generic(md, target, flags);
60 modifier::greasepencil::copy_influence_data(&omd->influence, &tomd->influence, flags);
61}
62
63static void free_data(ModifierData *md)
64{
65 auto *omd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
67}
68
69static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
70{
71 auto *omd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
72 modifier::greasepencil::foreach_influence_ID_link(&omd->influence, ob, walk, user_data);
73 walk(user_data, ob, reinterpret_cast<ID **>(&omd->object), IDWALK_CB_NOP);
74}
75
77{
78 auto *mmd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
79 if (mmd->object != nullptr) {
80 DEG_add_object_relation(ctx->node, mmd->object, DEG_OB_COMP_TRANSFORM, "Build Modifier");
81 }
82 DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Build Modifier");
83}
84
85static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
86{
87 const auto *mmd = reinterpret_cast<const GreasePencilBuildModifierData *>(md);
88
89 writer->write_struct(mmd);
90 modifier::greasepencil::write_influence_data(writer, &mmd->influence);
91}
92
93static void blend_read(BlendDataReader *reader, ModifierData *md)
94{
95 auto *mmd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
96
97 modifier::greasepencil::read_influence_data(reader, &mmd->influence);
98}
99
102 const IndexMask &selection,
103 const GreasePencilBuildTimeAlignment time_alignment,
104 const GreasePencilBuildTransition transition,
105 const float factor,
106 const bool clamp_points,
107 int &r_curves_num,
108 int &r_points_num)
109{
110 const int stroke_count = curves.curves_num();
111 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
112 const VArray<bool> cyclic = curves.cyclic();
113
114 curves.ensure_evaluated_lengths();
115 float max_length = 0;
116 for (const int stroke : curves.curves_range()) {
117 const bool stroke_cyclic = cyclic[stroke];
118 const float len = curves.evaluated_length_total_for_curve(stroke, stroke_cyclic);
119 max_length = math::max(max_length, len);
120 }
121
122 float factor_to_keep = transition == MOD_GREASE_PENCIL_BUILD_TRANSITION_GROW ? factor :
123 1.0f - factor;
124 if (clamp_points) {
125 r_curves_num = r_points_num = 0;
126 factor_to_keep = std::clamp(factor_to_keep, 0.0f, 1.0f);
127 }
128
129 auto get_stroke_factor = [&](const float factor, const int index) {
130 const bool stroke_cyclic = cyclic[index];
131 const float total_length = curves.evaluated_length_total_for_curve(index, stroke_cyclic);
132 if (total_length == 0) {
133 return factor > 0.5f ? 1.0f : 0.0f;
134 }
135 const float max_factor = max_length / total_length;
136 if (time_alignment == MOD_GREASE_PENCIL_BUILD_TIMEALIGN_START) {
137 if (clamp_points) {
138 return std::clamp(factor * max_factor, 0.0f, 1.0f);
139 }
140 return factor * max_factor;
141 }
142 if (time_alignment == MOD_GREASE_PENCIL_BUILD_TIMEALIGN_END) {
143 const float min_factor = max_factor - 1.0f;
144 const float use_factor = factor * max_factor;
145 if (clamp_points) {
146 return std::clamp(use_factor - min_factor, 0.0f, 1.0f);
147 }
148 return use_factor - min_factor;
149 }
150 return 0.0f;
151 };
152
153 Array<bool> select(stroke_count);
154 selection.to_bools(select.as_mutable_span());
155 Array<int> result(stroke_count);
156 for (const int curve : curves.curves_range()) {
157 const float local_factor = select[curve] ? get_stroke_factor(factor_to_keep, curve) : 1.0f;
158 const int num_points = points_by_curve[curve].size() * local_factor;
159 result[curve] = num_points;
160 if (clamp_points) {
161 r_points_num += num_points;
162 if (num_points > 0) {
163 r_curves_num++;
164 }
165 }
166 }
167 return result;
168}
169
172 const IndexMask &selection,
173 const GreasePencilBuildTimeAlignment time_alignment,
174 const GreasePencilBuildTransition transition,
175 const float factor,
176 const float factor_start,
177 const float factor_opacity,
178 const float factor_radii,
179 StringRefNull target_vgname)
180{
181 int dst_curves_num, dst_points_num;
182 const bool has_fade = factor_start != factor;
183 const Array<int> point_counts_to_keep = point_counts_to_keep_concurrent(
184 curves, selection, time_alignment, transition, factor, true, dst_curves_num, dst_points_num);
185 if (dst_curves_num == 0) {
186 return {};
187 }
188 const Array<int> starts_per_curve = has_fade ? point_counts_to_keep_concurrent(curves,
189 selection,
190 time_alignment,
191 transition,
192 factor_start,
193 false,
194 dst_curves_num,
195 dst_points_num) :
196 Array<int>(0);
197 const Array<int> ends_per_curve = has_fade ? point_counts_to_keep_concurrent(curves,
198 selection,
199 time_alignment,
200 transition,
201 factor,
202 false,
203 dst_curves_num,
204 dst_points_num) :
205 Array<int>(0);
206
207 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
208 MutableSpan<float> opacities = drawing.opacities_for_write();
209 MutableSpan<float> radii = drawing.radii_for_write();
210 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
211 bke::SpanAttributeWriter<float> weights = attributes.lookup_for_write_span<float>(target_vgname);
212
213 const bool is_vanishing = transition == MOD_GREASE_PENCIL_BUILD_TRANSITION_VANISH;
214
215 bke::CurvesGeometry dst_curves(dst_points_num, dst_curves_num);
216 Array<int> dst_to_src_point(dst_points_num);
217 Array<int> dst_to_src_curve(dst_curves_num);
218 MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
219 dst_offsets[0] = 0;
220
221 int next_curve = 0;
222 int next_point = 0;
223 for (const int curve : curves.curves_range()) {
224 if (!point_counts_to_keep[curve]) {
225 continue;
226 }
227 const IndexRange points = points_by_curve[curve];
228 dst_offsets[next_curve] = point_counts_to_keep[curve];
229 const int curve_size = points.size();
230
231 auto get_fade_weight = [&](const int local_index) {
232 const float fade_range = std::abs(ends_per_curve[curve] - starts_per_curve[curve]);
233 if (is_vanishing) {
234 const float factor_from_start = local_index - curve_size + ends_per_curve[curve];
235 return 1.0f - std::clamp(factor_from_start / fade_range, 0.0f, 1.0f);
236 }
237 const float factor_from_start = local_index - starts_per_curve[curve];
238 return std::clamp(factor_from_start / fade_range, 0.0f, 1.0f);
239 };
240
241 const int extra_offset = is_vanishing ? points.size() - point_counts_to_keep[curve] : 0;
242 for (const int stroke_point : IndexRange(point_counts_to_keep[curve])) {
243 const int src_point_index = points.first() + extra_offset + stroke_point;
244 if (has_fade) {
245 const float fade_weight = get_fade_weight(extra_offset + stroke_point);
246 opacities[src_point_index] = opacities[src_point_index] *
247 (1.0f - fade_weight * factor_opacity);
248 radii[src_point_index] = radii[src_point_index] * (1.0f - fade_weight * factor_radii);
249 if (!weights.span.is_empty()) {
250 weights.span[src_point_index] = fade_weight;
251 }
252 }
253 dst_to_src_point[next_point] = src_point_index;
254 next_point++;
255 }
256 dst_to_src_curve[next_curve] = curve;
257 next_curve++;
258 }
259 weights.finish();
260
262
263 const bke::AttributeAccessor src_attributes = curves.attributes();
264 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
265
266 gather_attributes(src_attributes,
269 {},
270 dst_to_src_point,
271 dst_attributes);
272 gather_attributes(src_attributes,
275 {},
276 dst_to_src_curve,
277 dst_attributes);
278
279 dst_curves.update_curve_types();
280
281 return dst_curves;
282}
283
285 const IndexMask &selection,
286 const GreasePencilBuildTransition transition,
287 const float factor,
288 const bool clamp_points,
289 int &r_curves_num,
290 int &r_points_num)
291{
292 const int stroke_count = curves.curves_num();
293 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
294
295 float factor_to_keep = transition == MOD_GREASE_PENCIL_BUILD_TRANSITION_GROW ? factor :
296 (1.0f - factor);
297 if (clamp_points) {
298 factor_to_keep = std::clamp(factor_to_keep, 0.0f, 1.0f);
299 }
300
301 const bool is_vanishing = transition == MOD_GREASE_PENCIL_BUILD_TRANSITION_VANISH;
302
303 int effective_points_num = offset_indices::sum_group_sizes(points_by_curve, selection);
304
305 const int untouched_points_num = points_by_curve.total_size() - effective_points_num;
306 effective_points_num *= factor_to_keep;
307 effective_points_num += untouched_points_num;
308
309 r_points_num = effective_points_num;
310 r_curves_num = 0;
311
312 Array<bool> select(stroke_count);
313 selection.to_bools(select.as_mutable_span());
314
315 int counted_points_num = 0;
316 for (const int i : curves.curves_range()) {
317 const int stroke = is_vanishing ? stroke_count - i - 1 : i;
318 if (select[stroke] && counted_points_num >= effective_points_num) {
319 continue;
320 }
321 counted_points_num += points_by_curve[stroke].size();
322 r_curves_num++;
323 }
324}
325
328 const IndexMask &selection,
329 const GreasePencilBuildTransition transition,
330 const float factor,
331 const float factor_start,
332 const float factor_opacity,
333 const float factor_radii,
334 StringRefNull target_vgname)
335{
336 const bool has_fade = factor_start != factor;
337 int dst_curves_num, dst_points_num;
338 int start_points_num, end_points_num, dummy_curves_num;
340 curves, selection, transition, factor, true, dst_curves_num, dst_points_num);
341
342 if (dst_curves_num == 0) {
343 return {};
344 }
345
347 curves, selection, transition, factor_start, false, dummy_curves_num, start_points_num);
349 curves, selection, transition, factor, false, dummy_curves_num, end_points_num);
350
351 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
352 MutableSpan<float> opacities = drawing.opacities_for_write();
353 MutableSpan<float> radii = drawing.radii_for_write();
354 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
355 bke::SpanAttributeWriter<float> weights = attributes.lookup_for_write_span<float>(target_vgname);
356
357 const bool is_vanishing = transition == MOD_GREASE_PENCIL_BUILD_TRANSITION_VANISH;
358
359 bke::CurvesGeometry dst_curves(dst_points_num, dst_curves_num);
360 MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
361 Array<int> dst_to_src_point(dst_points_num);
362 Array<int> dst_to_src_curve(dst_curves_num);
363
364 dst_offsets[0] = 0;
365
366 int next_curve = 1, next_point = 0;
367 IndexMaskMemory memory;
368 selection.complement(curves.curves_range(), memory).foreach_index([&](const int stroke) {
369 for (const int point : points_by_curve[stroke]) {
370 dst_to_src_point[next_point] = point;
371 next_point++;
372 }
373 dst_to_src_curve[next_curve - 1] = stroke;
374 dst_offsets[next_curve] = next_point;
375 next_curve++;
376 });
377
378 const int untouched_curves_num = next_curve - 1;
379 const int untouched_points_num = next_point;
380
381 const int stroke_count = curves.curves_num();
382 bool done_scanning = false;
383 selection.foreach_index([&](const int i) {
384 const int stroke = is_vanishing ? stroke_count - i - 1 : i;
385 if (done_scanning || next_point >= dst_points_num) {
386 done_scanning = true;
387 return;
388 }
389
390 auto get_fade_weight = [&](const int next_point_count) {
391 return std::clamp(float(next_point_count - start_points_num) /
392 float(abs(end_points_num - start_points_num)),
393 0.0f,
394 1.0f);
395 };
396
397 const IndexRange points = points_by_curve[stroke];
398 for (const int point : points) {
399 const int local_index = point - points.first();
400 const int src_point_index = is_vanishing ? points.last() - local_index : point;
401 dst_to_src_point[next_point] = src_point_index;
402
403 if (has_fade) {
404 const float fade_weight = get_fade_weight(next_point);
405 opacities[src_point_index] = opacities[src_point_index] *
406 (1.0f - fade_weight * factor_opacity);
407 radii[src_point_index] = radii[src_point_index] * (1.0f - fade_weight * factor_radii);
408 if (!weights.span.is_empty()) {
409 weights.span[src_point_index] = fade_weight;
410 }
411 }
412
413 next_point++;
414 if (next_point >= dst_points_num) {
415 done_scanning = true;
416 break;
417 }
418 }
419 dst_offsets[next_curve] = next_point;
420 dst_to_src_curve[next_curve - 1] = stroke;
421 next_curve++;
422 });
423 weights.finish();
424
425 if (is_vanishing) {
426 /* The selected strokes were built last-to-first with reversed points (to pick which points
427 * to keep). Flip the built range back so the kept strokes keep their original order and
428 * point direction. */
429 MutableSpan<int> built_points = dst_to_src_point.as_mutable_span().drop_front(
430 untouched_points_num);
431 MutableSpan<int> built_curves = dst_to_src_curve.as_mutable_span().drop_front(
432 untouched_curves_num);
433 std::reverse(built_points.begin(), built_points.end());
434 std::reverse(built_curves.begin(), built_curves.end());
435 MutableSpan<int> built_offsets = dst_offsets.drop_front(untouched_curves_num);
436 Array<int> sizes(built_curves.size());
437 for (const int i : sizes.index_range()) {
438 sizes[i] = built_offsets[i + 1] - built_offsets[i];
439 }
440 std::reverse(sizes.begin(), sizes.end());
441 built_offsets.take_front(sizes.size()).copy_from(sizes);
442 offset_indices::accumulate_counts_to_offsets(built_offsets, untouched_points_num);
443 }
444
445 BLI_assert(next_curve == (dst_curves_num + 1));
446 BLI_assert(next_point == dst_points_num);
447
448 const bke::AttributeAccessor src_attributes = curves.attributes();
449 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
450
451 gather_attributes(src_attributes,
454 {},
455 dst_to_src_point,
456 dst_attributes);
457 gather_attributes(src_attributes,
460 {},
461 dst_to_src_curve,
462 dst_attributes);
463
464 dst_curves.update_curve_types();
465
466 return dst_curves;
467}
468
470 const Span<bool> select,
471 const Object &object,
472 MutableSpan<bool> r_selection)
473{
474 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
475 const Span<float3> positions = curves.positions();
476 const float3 center = object.object_to_world().location();
477
478 struct Pair {
479 float value;
480 int index;
481 bool selected;
482 };
483
484 Array<Pair> distances(curves.curves_num());
485 for (const int stroke : curves.curves_range()) {
486 const IndexRange points = points_by_curve[stroke];
487 const float3 p1 = positions[points.first()];
488 const float3 p2 = positions[points.last()];
489 distances[stroke].value = math::max(math::distance(p1, center), math::distance(p2, center));
490 distances[stroke].index = stroke;
491 distances[stroke].selected = select[stroke];
492 }
493
495 distances.begin(), distances.end(), [](Pair &a, Pair &b) { return a.value < b.value; });
496
497 Array<int> new_order(curves.curves_num());
498 for (const int i : curves.curves_range()) {
499 new_order[i] = distances[i].index;
500 r_selection[i] = distances[i].selected;
501 }
502
503 return geometry::reorder_curves_geometry(curves, new_order.as_span(), {});
504}
505
507 const float time_elapsed,
508 const float speed_fac,
509 const float max_gap,
510 const float frame_duration)
511{
512 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
513 const bke::AttributeAccessor attributes = curves.attributes();
514 const VArray<float> init_times = *attributes.lookup_or_default<float>(
515 "init_time", bke::AttrDomain::Curve, 0.0f);
516 const VArray<float> src_delta_times = *attributes.lookup_or_default<float>(
517 "delta_time", bke::AttrDomain::Point, 0.0f);
518
519 Array<float> delta_times(curves.points_num());
520
521 if (const std::optional<float> src_delta_time = src_delta_times.get_if_single()) {
522 delta_times.fill(*src_delta_time);
523 }
524 else {
525 array_utils::copy(src_delta_times, delta_times.as_mutable_span());
526 }
527
532 for (const int curve : curves.curves_range()) {
533 const IndexRange points = points_by_curve[curve];
534 if (delta_times[points.last()] == 0.0f && points.size() != 1) {
535 for (const int point_id : points.index_range()) {
536 const int point_i = points[point_id];
537 delta_times[point_i] = GP_BUILD_TIME_DEFAULT_STROKES * float(point_id) /
538 float(points.size() - 1);
539 }
540 }
541 }
542
543 Array<float> start_times(curves.curves_num());
544 start_times[0] = 0;
545 float accumulated_shift_delta_time = init_times[0];
546 for (const int curve : curves.curves_range().drop_front(1)) {
547 const float previous_start_time = start_times[curve - 1];
548 const float init_time = init_times[curve];
549 const float previous_delta_time = delta_times[points_by_curve[curve - 1].last()];
550 const float previous_end_time = previous_start_time + previous_delta_time;
551 float shifted_start_time = init_time - accumulated_shift_delta_time;
552
553 /* Make each stroke have no gap, if the `init_time` is at the default. */
554 if (init_time == 0.0f) {
555 shifted_start_time = previous_end_time;
556 }
557
558 const float gap_delta_time = math::min(math::abs(shifted_start_time - previous_end_time),
559 max_gap);
560
561 start_times[curve] = previous_end_time + gap_delta_time;
562 accumulated_shift_delta_time += math::max(shifted_start_time - start_times[curve], 0.0f);
563 }
564
565 /* Calculates the maximum time of this frame, which is the time between the beginning of the
566 * first stroke and the end of the last stroke. `start_times.last()` gives the starting time of
567 * the last stroke related to frame beginning, and `delta_time.last()` gives how long that stroke
568 * lasted. */
569 const float max_time = start_times.last() + delta_times.last();
570
571 /* If the time needed for building the frame is shorter than frame length, this gives the
572 * percentage of time it needs to be compared to original drawing time. `max_time/speed_fac`
573 * gives time after speed scaling, then divided by `frame_duration` gives the percentage. */
574 const float time_compress_factor = math::max(max_time / speed_fac / frame_duration, 1.0f);
575
576 /* Finally actual building limit is then scaled with speed factor and time compress factor. */
577 const float limit = time_elapsed * speed_fac * time_compress_factor;
578
579 for (const int curve : curves.curves_range()) {
580 const float start_time = start_times[curve];
581 for (const int point : points_by_curve[curve]) {
582 if (start_time + delta_times[point] >= limit) {
583 return math::clamp(float(point) / float(curves.points_num()), 0.0f, 1.0f);
584 }
585 }
586 }
587
588 return 1.0f;
589}
590
591static float get_build_factor(const GreasePencilBuildTimeMode time_mode,
592 const int current_frame,
593 const int start_frame,
594 const int frame_duration,
595 const int length,
596 const float percentage,
598 const float scene_fps,
599 const float speed_fac,
600 const float max_gap,
601 const float fade)
602{
603 const float use_time = math::round(float(current_frame) /
604 float(math::min(frame_duration, length)) * float(length));
605 const float build_factor_frames = math::clamp(
606 float(use_time - start_frame) / length, 0.0f, 1.0f) *
607 (1.0f + fade);
608 switch (time_mode) {
610 return build_factor_frames;
612 return percentage * (1.0f + fade);
615 float(current_frame) / scene_fps,
616 speed_fac,
617 max_gap,
618 float(frame_duration) / scene_fps) *
619 (1.0f + fade);
620 }
622 return 0.0f;
623}
624
626 const Object &ob,
628 const bke::greasepencil::Drawing *previous_drawing,
629 const int current_time,
630 const int frame_duration,
631 const float scene_fps)
632{
635
636 if (curves.is_empty()) {
637 return;
638 }
639
640 IndexMaskMemory memory;
642 &ob, curves, mmd.influence, memory);
643
644 /* Remove a count of #prev_strokes. */
645 if (mmd.mode == MOD_GREASE_PENCIL_BUILD_MODE_ADDITIVE && previous_drawing != nullptr) {
646 const bke::CurvesGeometry &prev_curves = previous_drawing->strokes();
647 const int prev_strokes = prev_curves.curves_num();
648 const int added_strokes = curves.curves_num() - prev_strokes;
649 if (added_strokes > 0) {
650 Array<bool> work_on_select(curves.curves_num());
651 selection.to_bools(work_on_select.as_mutable_span());
652 work_on_select.as_mutable_span().take_front(prev_strokes).fill(false);
653 selection = IndexMask::from_bools(work_on_select, memory);
654 }
655 }
656
657 if (mmd.object) {
658 const int curves_num = curves.curves_num();
659 Array<bool> select(curves_num), reordered_select(curves_num);
660 selection.to_bools(select);
662 curves, select.as_span(), *mmd.object, reordered_select.as_mutable_span());
663 selection = IndexMask::from_bools(reordered_select, memory);
664 }
665
666 const float fade_factor = ((mmd.flag & MOD_GREASE_PENCIL_BUILD_USE_FADING) != 0) ? mmd.fade_fac :
667 0.0f;
669 current_time,
670 mmd.start_delay,
671 frame_duration,
672 mmd.length,
673 mmd.percentage_fac,
674 curves,
675 scene_fps,
676 mmd.speed_fac,
677 mmd.speed_maxgap,
678 fade_factor);
679 float factor_start = factor - fade_factor;
681 std::swap(factor, factor_start);
682 }
683
684 const GreasePencilBuildTimeAlignment use_time_alignment =
689 mmd.time_alignment;
690 switch (mmd.mode) {
691 default:
693 curves = build_sequential(drawing,
694 curves,
695 selection,
696 mmd.transition,
697 factor,
698 factor_start,
701 mmd.target_vgname);
702 break;
704 curves = build_concurrent(drawing,
705 curves,
706 selection,
707 use_time_alignment,
708 mmd.transition,
709 factor,
710 factor_start,
713 mmd.target_vgname);
714 break;
716 curves = build_sequential(drawing,
717 curves,
718 selection,
719 mmd.transition,
720 factor,
721 factor_start,
724 mmd.target_vgname);
725 break;
726 }
727
728 drawing.tag_topology_changed();
729}
730
732 const ModifierEvalContext *ctx,
733 bke::GeometrySet *geometry_set)
734{
735 const auto *mmd = reinterpret_cast<GreasePencilBuildModifierData *>(md);
736
737 if (!geometry_set->has_grease_pencil()) {
738 return;
739 }
740
742 const int eval_frame = grease_pencil.runtime->eval_frame;
743
744 IndexMaskMemory mask_memory;
746 grease_pencil, mmd->influence, mask_memory);
749
751 if (eval_frame < mmd->start_frame || eval_frame > mmd->end_frame) {
752 return;
753 }
754 }
755
756 const Scene &scene = *DEG_get_evaluated_scene(ctx->depsgraph);
757 const float scene_fps = float(scene.r.frs_sec) / scene.r.frs_sec_base;
759
761 drawing_infos, [&](modifier::greasepencil::LayerDrawingInfo drawing_info) {
762 const bke::greasepencil::Layer &layer = *layers[drawing_info.layer_index];
763
764 /* This will always return a valid start frame because we're iterating over the valid
765 * drawings on `eval_frame`. Each drawing will have a start frame. */
766 const int start_frame = *layer.start_frame_at(eval_frame);
767 BLI_assert(start_frame <= eval_frame);
768
769 const bke::greasepencil::Drawing *prev_drawing = grease_pencil.get_drawing_at(
770 layer, start_frame - 1);
771
772 const int relative_start_frame = eval_frame - start_frame;
773
774 const int frame_index = layer.sorted_keys_index_at(eval_frame);
775 BLI_assert(frame_index != -1);
776
777 int frame_duration = INT_MAX;
778 if (frame_index != layer.sorted_keys().index_range().last()) {
779 const int next_frame = layer.sorted_keys()[frame_index + 1];
780 frame_duration = math::distance(start_frame, next_frame);
781 }
782
783 build_drawing(*mmd,
784 *ctx->object,
785 *drawing_info.drawing,
786 prev_drawing,
787 relative_start_frame,
788 frame_duration,
789 scene_fps);
790 });
791}
792
793static void panel_draw(const bContext *C, Panel *panel)
794{
795 ui::Layout &layout = *panel->layout;
796
797 PointerRNA ob_ptr;
799
802
803 layout.use_property_split_set(true);
804
805 /* First: Build mode and build settings. */
806 layout.prop(ptr, "mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
808 layout.prop(ptr, "transition", UI_ITEM_NONE, std::nullopt, ICON_NONE);
809 }
811 /* Concurrent mode doesn't support MOD_GREASE_PENCIL_BUILD_TIMEMODE_DRAWSPEED, so unset it. */
815 }
816 layout.prop(ptr, "transition", UI_ITEM_NONE, std::nullopt, ICON_NONE);
817 }
818 layout.separator();
819
820 /* Second: Time mode and time settings. */
821
822 layout.prop(ptr, "time_mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
824 layout.prop(ptr, "concurrent_time_alignment", UI_ITEM_NONE, std::nullopt, ICON_NONE);
825 }
826 switch (time_mode) {
828 layout.prop(ptr, "speed_factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
829 layout.prop(ptr, "speed_maxgap", UI_ITEM_NONE, std::nullopt, ICON_NONE);
830 break;
832 layout.prop(ptr, "length", UI_ITEM_NONE, IFACE_("Frames"), ICON_NONE);
834 layout.prop(ptr, "start_delay", UI_ITEM_NONE, std::nullopt, ICON_NONE);
835 }
836 break;
838 layout.prop(ptr, "percentage_factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
839 break;
840 default:
841 break;
842 }
843 layout.separator();
844 layout.prop(ptr, "object", UI_ITEM_NONE, std::nullopt, ICON_NONE);
845 ui::PanelLayout restrict_frame_range_layout = layout.panel_prop_with_bool_header(
846 C,
847 ptr,
848 "open_frame_range_panel",
849 ptr,
850 "use_restrict_frame_range",
851 IFACE_("Effective Range"));
852 if (ui::Layout *panel = restrict_frame_range_layout.body) {
853 const bool active = RNA_boolean_get(ptr, "use_restrict_frame_range");
854 ui::Layout &col = panel->column(false);
855 col.active_set(active);
856 col.prop(ptr, "frame_start", UI_ITEM_NONE, IFACE_("Start"), ICON_NONE);
857 col.prop(ptr, "frame_end", UI_ITEM_NONE, IFACE_("End"), ICON_NONE);
858 }
859 ui::PanelLayout fading_layout = layout.panel_prop_with_bool_header(
860 C, ptr, "open_fading_panel", ptr, "use_fading", IFACE_("Fading"));
861 if (ui::Layout *panel = fading_layout.body) {
862 const bool active = RNA_boolean_get(ptr, "use_fading");
863 ui::Layout &col = panel->column(false);
864 col.active_set(active);
865
866 col.prop(ptr, "fade_factor", UI_ITEM_NONE, IFACE_("Factor"), ICON_NONE);
867
868 ui::Layout &subcol = col.column(true);
869 subcol.prop(ptr, "fade_thickness_strength", UI_ITEM_NONE, IFACE_("Thickness"), ICON_NONE);
870 subcol.prop(ptr, "fade_opacity_strength", UI_ITEM_NONE, IFACE_("Opacity"), ICON_NONE);
871
872 col.prop_search(
873 ptr, "target_vertex_group", &ob_ptr, "vertex_groups", IFACE_("Weight Output"), ICON_NONE);
874 }
875
876 if (ui::Layout *influence_panel = layout.panel_prop(
877 C, ptr, "open_influence_panel", IFACE_("Influence")))
878 {
881 }
882
883 modifier_error_message_draw(layout, ptr);
884}
885
890
892 /*idname*/ "GreasePencilBuildModifier",
893 /*name*/ N_("Build"),
894 /*struct_name*/ "GreasePencilBuildModifierData",
895 /*struct_size*/ sizeof(GreasePencilBuildModifierData),
896 /*srna*/ &RNA_GreasePencilBuildModifier,
898 /*flags*/
901 /*icon*/ ICON_MOD_LENGTH,
902
903 /*copy_data*/ copy_data,
904
905 /*deform_verts*/ nullptr,
906 /*deform_matrices*/ nullptr,
907 /*deform_verts_EM*/ nullptr,
908 /*deform_matrices_EM*/ nullptr,
909 /*modify_mesh*/ nullptr,
910 /*modify_geometry_set*/ modify_geometry_set,
911
912 /*init_data*/ init_data,
913 /*required_data_mask*/ nullptr,
914 /*free_data*/ free_data,
915 /*is_disabled*/ nullptr,
916 /*update_depsgraph*/ update_depsgraph,
917 /*depends_on_time*/ nullptr,
918 /*depends_on_normals*/ nullptr,
919 /*foreach_ID_link*/ foreach_ID_link,
920 /*foreach_tex_link*/ nullptr,
921 /*free_runtime_data*/ nullptr,
922 /*panel_register*/ panel_register,
923 /*blend_write*/ blend_write,
924 /*blend_read*/ blend_read,
925};
926
927} // namespace blender
Low-level operations for curves.
Low-level operations for grease pencil.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * end() const
Definition BLI_array.hh:336
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
const T * begin() const
Definition BLI_array.hh:332
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange index_range() const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr T * end() const
Definition BLI_span.hh:554
constexpr T * begin() const
Definition BLI_span.hh:550
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
MutableSpan< int > offsets_for_write()
MutableAttributeAccessor attributes_for_write()
GSpanAttributeWriter lookup_for_write_span(StringRef name)
MutableSpan< float > opacities_for_write()
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
constexpr void copy_from(Span< T > values) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
void to_bools(MutableSpan< bool > r_bools) const
#define INT_MAX
#define layer
#define abs
float length(VecOp< float, D >) RET
float fade(float t)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
bke::CurvesGeometry reorder_curves_geometry(const bke::CurvesGeometry &src_curves, Span< int > old_by_new_map, const bke::AttributeFilter &attribute_filter)
Definition reorder.cc:368
T clamp(const T &a, const T &min, const T &max)
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
T abs(const T &a)
T round(const T &a)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void draw_material_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
Vector< LayerDrawingInfo > get_drawing_infos_by_layer(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_layer_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
void free_influence_data(GreasePencilModifierInfluenceData *influence_data)
void foreach_influence_ID_link(GreasePencilModifierInfluenceData *influence_data, Object *ob, IDWalkFunc walk, void *user_data)
void copy_influence_data(const GreasePencilModifierInfluenceData *influence_data_src, GreasePencilModifierInfluenceData *influence_data_dst, const int)
void ensure_no_bezier_curves(Drawing &drawing)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
int sum_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
BMesh Iterators.
@ MOD_GREASE_PENCIL_BUILD_TIMEMODE_PERCENTAGE
@ MOD_GREASE_PENCIL_BUILD_TIMEMODE_DRAWSPEED
@ MOD_GREASE_PENCIL_BUILD_TIMEMODE_FRAMES
constexpr float GP_BUILD_TIME_DEFAULT_STROKES
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
static bke::CurvesGeometry build_concurrent(bke::greasepencil::Drawing &drawing, bke::CurvesGeometry &curves, const IndexMask &selection, const GreasePencilBuildTimeAlignment time_alignment, const GreasePencilBuildTransition transition, const float factor, const float factor_start, const float factor_opacity, const float factor_radii, StringRefNull target_vgname)
@ eModifierType_GreasePencilBuild
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void update_depsgraph(tGraphSliderOp *gso)
@ MOD_GREASE_PENCIL_BUILD_MODE_SEQUENTIAL
@ MOD_GREASE_PENCIL_BUILD_MODE_CONCURRENT
@ MOD_GREASE_PENCIL_BUILD_MODE_ADDITIVE
@ MOD_GREASE_PENCIL_BUILD_TIMEALIGN_START
@ MOD_GREASE_PENCIL_BUILD_TIMEALIGN_END
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
static float get_factor_from_draw_speed(const bke::CurvesGeometry &curves, const float time_elapsed, const float speed_fac, const float max_gap, const float frame_duration)
ModifierTypeInfo modifierType_GreasePencilBuild
static float get_build_factor(const GreasePencilBuildTimeMode time_mode, const int current_frame, const int start_frame, const int frame_duration, const int length, const float percentage, const bke::CurvesGeometry &curves, const float scene_fps, const float speed_fac, const float max_gap, const float fade)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition BLI_sort.hh:37
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
@ MOD_GREASE_PENCIL_BUILD_TRANSITION_GROW
@ MOD_GREASE_PENCIL_BUILD_TRANSITION_VANISH
static bke::CurvesGeometry reorder_strokes(const bke::CurvesGeometry &curves, const Span< bool > select, const Object &object, MutableSpan< bool > r_selection)
@ MOD_GREASE_PENCIL_BUILD_USE_FADING
@ MOD_GREASE_PENCIL_BUILD_RESTRICT_TIME
static void free_data(ModifierData *md)
static void build_drawing(const GreasePencilBuildModifierData &mmd, const Object &ob, bke::greasepencil::Drawing &drawing, const bke::greasepencil::Drawing *previous_drawing, const int current_time, const int frame_duration, const float scene_fps)
static void panel_register(ARegionType *region_type)
static Array< int > point_counts_to_keep_concurrent(const bke::CurvesGeometry &curves, const IndexMask &selection, const GreasePencilBuildTimeAlignment time_alignment, const GreasePencilBuildTransition transition, const float factor, const bool clamp_points, int &r_curves_num, int &r_points_num)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
static void blend_read(BlendDataReader *, ModifierData *md)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
VecBase< float, 3 > float3
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
static void points_info_sequential(const bke::CurvesGeometry &curves, const IndexMask &selection, const GreasePencilBuildTransition transition, const float factor, const bool clamp_points, int &r_curves_num, int &r_points_num)
static bke::CurvesGeometry build_sequential(bke::greasepencil::Drawing &drawing, bke::CurvesGeometry &curves, const IndexMask &selection, const GreasePencilBuildTransition transition, const float factor, const float factor_start, const float factor_opacity, const float factor_radii, StringRefNull target_vgname)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
GreasePencilModifierInfluenceData influence
GreasePencilBuildTimeAlignment time_alignment
ui::Layout * layout
struct RenderData r
GreasePencil * get_grease_pencil_for_write()
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
PanelLayout panel_prop_with_bool_header(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name, PointerRNA *bool_prop_owner, StringRefNull bool_prop_name, std::optional< StringRef > label)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
#define N_(msgid)