Blender V5.3
grease_pencil_bake_animation.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 <fmt/format.h>
10
11#include "ANIM_animdata.hh"
12
13#include "BKE_anim_data.hh"
14#include "BKE_context.hh"
15#include "BKE_curves.hh"
16#include "BKE_duplilist.hh"
17#include "BKE_material.hh"
18#include "BKE_scene.hh"
19
20#include "BLI_listbase.hh"
21#include "BLI_math_matrix.hh"
22#include "BLI_set.hh"
23
24#include "BLT_translation.hh"
25
27
28#include "DNA_anim_types.h"
29
30#include "ED_grease_pencil.hh"
31#include "ED_object.hh"
32
33#include "RNA_access.hh"
34#include "RNA_define.hh"
35
36#include "WM_types.hh"
37
38namespace blender {
39
40namespace ed::greasepencil {
41static void ensure_valid_frame_end(Main * /*main*/, Scene * /*scene*/, PointerRNA *ptr)
42{
43 const int frame_start = RNA_int_get(ptr, "frame_start");
44 const int frame_end = RNA_int_get(ptr, "frame_end");
45
46 if (frame_end <= frame_start) {
47 RNA_int_set(ptr, "frame_end", frame_start + 1);
48 }
49}
50
52 wmOperator *op,
53 const wmEvent * /*event*/)
54{
55 const Scene *scene = CTX_data_scene(C);
56
57 PropertyRNA *prop_frame_start = RNA_struct_find_property(op->ptr, "frame_start");
58 if (!RNA_property_is_set(op->ptr, prop_frame_start)) {
59 const int frame_start = RNA_property_int_get(op->ptr, prop_frame_start);
60 if (frame_start < scene->r.sfra) {
61 RNA_property_int_set(op->ptr, prop_frame_start, scene->r.sfra);
62 }
63 }
64
65 PropertyRNA *prop_frame_end = RNA_struct_find_property(op->ptr, "frame_end");
66 if (!RNA_property_is_set(op->ptr, prop_frame_end)) {
67 const int frame_end = RNA_property_int_get(op->ptr, prop_frame_end);
68 if (frame_end > scene->r.efra) {
69 RNA_property_int_set(op->ptr, prop_frame_end, scene->r.efra);
70 }
71 }
72
74 C, op, 250, IFACE_("Bake Object Transform to Grease Pencil"), IFACE_("Bake"));
75}
76
77static Vector<Object *> get_bake_targets(bContext &C, Depsgraph &depsgraph)
78{
79 Vector<Object *> bake_targets;
80 Object *active_object = CTX_data_active_object(&C);
81
82 DupliList duplilist;
83
84 if (active_object->type == OB_GREASE_PENCIL) {
85 bake_targets.append(active_object);
86 }
87 else if (active_object->type == OB_EMPTY) {
88 object_duplilist(&depsgraph, active_object, nullptr, duplilist);
89 for (DupliObject &duplicate_object : duplilist) {
90 if (duplicate_object.ob->type != OB_GREASE_PENCIL) {
91 continue;
92 }
93
94 bake_targets.append(duplicate_object.ob);
95 }
96 duplilist.clear();
97 }
98
99 CTX_DATA_BEGIN (&C, Object *, object, selected_objects) {
100 if (object == active_object) {
101 continue;
102 }
103
104 if (object->type == OB_GREASE_PENCIL) {
105 bake_targets.append(object);
106 }
107 else if (object->type == OB_EMPTY) {
108 object_duplilist(&depsgraph, active_object, nullptr, duplilist);
109 for (DupliObject &duplicate_object : duplilist) {
110 if (duplicate_object.ob->type != OB_GREASE_PENCIL) {
111 continue;
112 }
113
114 bake_targets.append(duplicate_object.ob);
115 }
116 duplilist.clear();
117 }
118 }
120 return bake_targets;
121}
122
124{
125 Set<int> keyframes;
126 for (Object *bake_target : bake_targets) {
127 AnimData *adt = BKE_animdata_from_id(&bake_target->id);
128 for (FCurve *fcurve : animrig::fcurves_for_assigned_action(adt)) {
129 for (const int i : IndexRange(fcurve->totvert)) {
130 BezTriple bezt = fcurve->bezt[i];
131 if (bezt.f2 & SELECT) {
132 keyframes.add(int(bezt.vec[1][0]));
133 }
134 }
135 }
136 }
137
138 return keyframes;
139}
140
142{
143 using namespace bke::greasepencil;
144
145 Main &bmain = *CTX_data_main(C);
146 Depsgraph &depsgraph = *CTX_data_ensure_evaluated_depsgraph(C);
147 Scene &scene = *CTX_data_scene(C);
148
149 const int step = RNA_int_get(op->ptr, "step");
150
151 const int frame_start = (scene.r.sfra > RNA_int_get(op->ptr, "frame_start")) ?
152 scene.r.sfra :
153 RNA_int_get(op->ptr, "frame_start");
154
155 const int frame_end = (scene.r.efra < RNA_int_get(op->ptr, "frame_end")) ?
156 scene.r.efra :
157 RNA_int_get(op->ptr, "frame_end");
158
159 const bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
160 const int frame_offset = RNA_int_get(op->ptr, "frame_target") - frame_start;
161 const ReprojectMode reproject_mode = ReprojectMode(RNA_enum_get(op->ptr, "project_type"));
162
163 View3D *v3d = CTX_wm_view3d(C);
164 ARegion *region = CTX_wm_region(C);
165
166 Vector<Object *> bake_targets = get_bake_targets(*C, depsgraph);
167
168 uint8_t local_view_bits = (v3d && v3d->localvd) ? v3d->local_view_uid : 0;
169 Object *target_object = object::add_type(
170 C, OB_GREASE_PENCIL, nullptr, scene.cursor.location, float3(0), false, local_view_bits);
171
172 const float4x4 target_imat = math::invert(target_object->object_to_world());
173
174 WM_cursor_wait(true);
175
176 GreasePencil &target = *id_cast<GreasePencil *>(target_object->data);
177 Object *target_object_eval = DEG_get_evaluated(&depsgraph, target_object);
178
179 std::optional<Set<int>> keyframes;
180 if (only_selected) {
181 keyframes = get_selected_object_keyframes(bake_targets);
182 }
183
184 const int prior_frame = int(DEG_get_ctime(&depsgraph));
185
186 for (int frame = frame_start; frame <= frame_end; frame++) {
187 if ((frame - frame_start) % step != 0 && frame != frame_end) {
188 continue;
189 }
190
191 if (keyframes && keyframes->contains(frame)) {
192 continue;
193 }
194
195 scene.r.cfra = frame;
197
198 for (Object *source_object : bake_targets) {
199 Object *source_object_eval = DEG_get_evaluated(&depsgraph, source_object);
200 GreasePencil &source_eval_grease_pencil = *id_cast<GreasePencil *>(source_object_eval->data);
201 for (const Layer *source_layer : source_eval_grease_pencil.layers()) {
202 const float4x4 to_target = target_imat * source_layer->to_world_space(*source_object_eval);
203 std::string layer_name = fmt::format(
204 "{}_{}", source_object->id.name + 2, source_layer->name());
205 TreeNode *node = target.find_node_by_name(layer_name);
206 if (node == nullptr) {
207 Layer &new_layer = target.add_layer(layer_name);
208 target.set_active_layer(&new_layer);
209 }
210
211 Layer &target_layer = target.find_node_by_name(layer_name)->as_layer();
212 std::optional<DrawingPlacement> drawing_placement;
213 if (reproject_mode != ReprojectMode::Keep) {
214 drawing_placement = DrawingPlacement(
215 scene, *region, *v3d, *target_object_eval, &target_layer, reproject_mode);
216 }
217
218 const GreasePencilFrame *source_frame = source_layer->frame_at(scene.r.cfra);
219 if (source_frame == nullptr) {
220 continue;
221 }
222
223 const int target_frame_num = scene.r.cfra + frame_offset;
224 Drawing &source_drawing = *source_eval_grease_pencil.get_drawing_at(*source_layer,
225 scene.r.cfra);
226 Drawing &target_drawing = *target.insert_frame(target_layer, target_frame_num);
227 target_drawing.strokes_for_write() = source_drawing.strokes();
228
229 bke::AttributeAccessor source_attributes = source_drawing.strokes().attributes();
230 const VArray<int> source_material_indices = *source_attributes.lookup_or_default<int>(
231 "material_index", bke::AttrDomain::Curve, 0);
232 bke::CurvesGeometry &target_strokes = target_drawing.strokes_for_write();
233 bke::SpanAttributeWriter<int> target_material_indices =
234 target_strokes.attributes_for_write().lookup_or_add_for_write_span<int>(
235 "material_index", bke::AttrDomain::Curve);
236
237 for (const int i : target_drawing.strokes().curves_range()) {
238 Material *source_material = BKE_object_material_get(source_object,
239 source_material_indices[i] + 1);
240 BLI_assert(source_material != nullptr);
241
242 bool found = false;
243 for (const int target_index : IndexRange(target_object->totcol)) {
244 Material *target_material = BKE_object_material_get(target_object, target_index + 1);
245 if (source_material == target_material) {
246 found = true;
247 break;
248 }
249 }
250
251 if (!found) {
252 BKE_object_material_slot_add(&bmain, target_object);
254 target_object,
255 source_material,
256 target_object->totcol,
258 }
259
260 target_material_indices.span[i] = BKE_object_material_index_get(target_object,
261 source_material);
262 }
263
264 target_material_indices.finish();
265 MutableSpan<float3> positions = target_strokes.positions_for_write();
266 math::transform_points(to_target, positions);
267 if (drawing_placement) {
268 threading::parallel_for(positions.index_range(), 4096, [&](IndexRange range) {
269 for (const int i : range) {
270 positions[i] = drawing_placement->reproject(positions[i]);
271 }
272 });
273 }
274 }
275 }
276 }
277 scene.r.cfra = prior_frame;
279
285
286 WM_cursor_wait(false);
287 return OPERATOR_FINISHED;
288}
289
291{
292 const Object *obact = CTX_data_active_object(C);
293
294 /* Check if grease pencil or empty for dupli groups. */
295 if ((obact == nullptr) || (obact->mode != OB_MODE_OBJECT) ||
297 {
298 return false;
299 }
300
301 /* Only if the current view is 3D View. */
302 const ScrArea *area = CTX_wm_area(C);
303 return (area && area->spacetype);
304}
305
307{
308 ot->name = "Bake Object Transform to Grease Pencil";
309 ot->idname = "GREASE_PENCIL_OT_bake_grease_pencil_animation";
310 ot->description = "Bake Grease Pencil object transform to Grease Pencil keyframes";
311
315
317
318 RNA_def_int(ot->srna, "frame_start", 1, 1, 100000, "Start Frame", "The start frame", 1, 100000);
319
320 PropertyRNA *prop = RNA_def_int(
321 ot->srna, "frame_end", 250, 1, 100000, "End Frame", "The end frame of animation", 1, 100000);
323
324 RNA_def_int(ot->srna, "step", 1, 1, 100, "Step", "Step between generated frames", 1, 100);
325
327 "only_selected",
328 false,
329 "Only Selected Keyframes",
330 "Convert only selected keyframes");
332 ot->srna, "frame_target", 1, 1, 100000, "Target Frame", "Destination frame", 1, 100000);
333
334 static const EnumPropertyItem rna_grease_pencil_reproject_type_items[] = {
335 {int(ReprojectMode::Keep), "KEEP", 0, "No Reproject", ""},
337 "FRONT",
338 0,
339 "Front",
340 "Reproject the strokes using the X-Z plane"},
341 {int(ReprojectMode::Side), "SIDE", 0, "Side", "Reproject the strokes using the Y-Z plane"},
342 {int(ReprojectMode::Top), "TOP", 0, "Top", "Reproject the strokes using the X-Y plane"},
344 "VIEW",
345 0,
346 "View",
347 "Reproject the strokes to end up on the same plane, as if drawn from the current "
348 "viewpoint "
349 "using 'Cursor' Stroke Placement"},
351 "CURSOR",
352 0,
353 "Cursor",
354 "Reproject the strokes using the orientation of 3D cursor"},
355 {0, nullptr, 0, nullptr, nullptr},
356 };
357
358 RNA_def_enum(ot->srna,
359 "project_type",
360 rna_grease_pencil_reproject_type_items,
362 "Projection Type",
363 "");
364}
365} // namespace ed::greasepencil
366
372
373} // namespace blender
Functions to work with AnimData.
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
#define SELECT
Definition BKE_mask.hh:19
General operations, lookup, etc. for materials.
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define ND_OB_ACTIVE
Definition WM_types.hh:459
#define NC_SCENE
Definition WM_types.hh:391
#define NA_ADDED
Definition WM_types.hh:636
#define NC_OBJECT
Definition WM_types.hh:392
#define ELEM(...)
constexpr IndexRange()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
AttributeAccessor attributes() const
IndexRange curves_range() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
Span< FCurve * > fcurves_for_assigned_action(AnimData *adt)
Definition animdata.cc:349
static Vector< Object * > get_bake_targets(bContext &C, Depsgraph &depsgraph)
static wmOperatorStatus bake_grease_pencil_animation_exec(bContext *C, wmOperator *op)
static wmOperatorStatus bake_grease_pencil_animation_invoke(bContext *C, wmOperator *op, const wmEvent *)
static Set< int > get_selected_object_keyframes(Span< Object * > bake_targets)
static bool bake_grease_pencil_animation_poll(bContext *C)
static void GREASE_PENCIL_OT_bake_grease_pencil_animation(wmOperatorType *ot)
static void ensure_valid_frame_end(Main *, Scene *, PointerRNA *ptr)
Object * add_type(bContext *C, int type, const char *name, const float loc[3], const float rot[3], bool enter_editmode, unsigned short local_view_bits) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
CartesianBasis invert(const CartesianBasis &basis)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
ScrArea * CTX_wm_area(const bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool BKE_object_material_slot_add(Main *bmain, Object *ob, bool set_active=true)
Material * BKE_object_material_get(Object *ob, short act)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void ED_operatortypes_grease_pencil_bake_animation()
int RNA_int_get(PointerRNA *ptr, const char *name)
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
MatBase< float, 4, 4 > float4x4
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void object_duplilist(Depsgraph *depsgraph, Object *ob, Set< const Object * > *include_objects, DupliList &r_duplilist)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
@ BKE_MAT_ASSIGN_USERPREF
unsigned char uint8_t
Definition BKE_text.h:79
VectorList< DupliObject > DupliList
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
int BKE_object_material_index_get(Object *ob, const Material *ma)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
void WM_cursor_wait(bool val)
Object * CTX_data_active_object(const bContext *C)
float DEG_get_ctime(const Depsgraph *graph)
ARegion * CTX_wm_region(const bContext *C)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
View3D * CTX_wm_view3d(const bContext *C)
eBezTriple_Flag f2
View3DCursor cursor
struct RenderData r
struct View3D * localvd
unsigned short local_view_uid
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141