Blender V4.5
BCAnimationSampler.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BCAnimationCurve.h"
8#include "BCSampleData.h"
9#include "collada_utils.h"
10
11#include "BKE_action.hh"
12#include "BKE_lib_id.hh"
13
14/* Collection of animation curves */
16 private:
17 Object *reference = nullptr;
18 bContext *mContext;
19
20 public:
23
24 BCAnimation(bContext *C, Object *ob) : mContext(C)
25 {
26 Main *bmain = CTX_data_main(mContext);
27 reference = (Object *)BKE_id_copy(bmain, &ob->id);
28 id_us_min(&reference->id);
29 }
30
32 {
33 BCAnimationCurveMap::iterator it;
34 for (it = curve_map.begin(); it != curve_map.end(); ++it) {
35 delete it->second;
36 }
37
38 if (reference && reference->id.us == 0) {
39 Main *bmain = CTX_data_main(mContext);
40 BKE_id_delete(bmain, &reference->id);
41 }
42 curve_map.clear();
43 }
44
46 {
47 return reference;
48 }
49};
50
51using BCAnimationObjectMap = std::map<Object *, BCAnimation *>;
52
54
55 /* Each frame on the timeline that needs to be sampled will have
56 * one BCSampleFrame where we collect sample information about all objects
57 * that need to be sampled for that frame. */
58
59 private:
60 BCSampleMap sampleMap;
61
62 public:
64 {
65 BCSampleMap::iterator it;
66 for (it = sampleMap.begin(); it != sampleMap.end(); ++it) {
67 BCSample *sample = it->second;
68 delete sample;
69 }
70 sampleMap.clear();
71 }
72
73 BCSample &add(Object *ob);
74
75 /* Following methods return NULL if object is not in the sampleMap. */
76
78 const BCSample *get_sample(Object *ob) const;
79 const BCMatrix *get_sample_matrix(Object *ob) const;
81 const BCMatrix *get_sample_matrix(Object *ob, Bone *bone) const;
82
84 bool has_sample_for(Object *ob) const;
86 bool has_sample_for(Object *ob, Bone *bone) const;
87};
88
89using BCSampleFrameMap = std::map<int, BCSampleFrame>;
90
92
93 /*
94 * The BCSampleFrameContainer stores a map of BCSampleFrame objects
95 * with the timeline frame as key.
96 *
97 * Some details on the purpose:
98 * An Animation is made of multiple FCurves where each FCurve can
99 * have multiple keyframes. When we want to export the animation we
100 * also can decide whether we want to export the keyframes or a set
101 * of sample frames at equidistant locations (sample period).
102 * In any case we must resample first need to resample it fully
103 * to resolve things like:
104 *
105 * - animations by constraints
106 * - animations by drivers
107 *
108 * For this purpose we need to step through the entire animation and
109 * then sample each frame that contains at least one keyFrame or
110 * sampleFrame. Then for each frame we have to store the transform
111 * information for all exported objects in a BCSampleframe
112 *
113 * The entire set of BCSampleframes is finally collected into
114 * a BCSampleframneContainer
115 */
116
117 private:
118 BCSampleFrameMap sample_frames;
119
120 public:
122
123 BCSample &add(Object *ob, int frame_index);
125 BCSampleFrame *get_frame(int frame_index);
126
128 int get_frames(std::vector<int> &frames) const;
129 int get_frames(Object *ob, BCFrames &frames) const;
130 int get_frames(Object *ob, Bone *bone, BCFrames &frames) const;
131
132 int get_samples(Object *ob, BCFrameSampleMap &samples) const;
133 int get_matrices(Object *ob, BCMatrixSampleMap &samples) const;
134 int get_matrices(Object *ob, Bone *bone, BCMatrixSampleMap &samples) const;
135};
136
138 private:
139 BCExportSettings &export_settings;
140 BCSampleFrameContainer sample_data;
141 BCAnimationObjectMap objects;
142
143 void generate_transform(Object *ob, const BCCurveKey &key, BCAnimationCurveMap &curves);
144 void generate_transforms(Object *ob,
145 const std::string prep,
146 const BC_animation_type type,
147 BCAnimationCurveMap &curves);
148 void generate_transforms(Object *ob, Bone *bone, BCAnimationCurveMap &curves);
149
150 void initialize_curves(BCAnimationCurveMap &curves, Object *ob);
156 void initialize_keyframes(BCFrameSet &frameset, Object *ob);
157 BCSample &sample_object(Object *ob, int frame_index, bool for_opensim);
158 void update_animation_curves(BCAnimation &animation,
160 Object *ob,
161 int frame_index);
162 void check_property_is_animated(
163 BCAnimation &animation, float *ref, float *val, std::string data_path, int length);
164
165 public:
166 BCAnimationSampler(BCExportSettings &export_settings, BCObjectSet &object_set);
168
169 void add_object(Object *ob);
170
171 void sample_scene(BCExportSettings &export_settings, bool keyframe_at_end);
172
174 void get_object_frames(BCFrames &frames, Object *ob);
175 bool get_object_samples(BCMatrixSampleMap &samples, Object *ob);
176 void get_bone_frames(BCFrames &frames, Object *ob, Bone *bone);
177 bool get_bone_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone);
178
179 static void get_animated_from_export_set(std::set<Object *> &animated_objects,
180 LinkNode &export_set);
181 static void find_depending_animated(std::set<Object *> &animated_objects,
182 std::set<Object *> &candidates);
183 static bool is_animated_by_constraint(Object *ob,
184 ListBase *conlist,
185 std::set<Object *> &animated_objects);
186};
std::set< float > BCFrameSet
std::map< BCCurveKey, BCAnimationCurve * > BCAnimationCurveMap
std::vector< float > BCFrames
BC_animation_type
std::map< Object *, BCAnimation * > BCAnimationObjectMap
std::map< int, BCSampleFrame > BCSampleFrameMap
std::map< int, const BCMatrix * > BCMatrixSampleMap
std::map< int, const BCSample * > BCFrameSampleMap
std::map< Object *, BCSample * > BCSampleMap
Blender kernel action and pose functionality.
Main * CTX_data_main(const bContext *C)
void BKE_id_delete(Main *bmain, void *idv) ATTR_NONNULL()
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition lib_id.cc:772
void id_us_min(ID *id)
Definition lib_id.cc:361
#define C
Definition RandGen.cpp:29
void get_object_frames(BCFrames &frames, Object *ob)
static void find_depending_animated(std::set< Object * > &animated_objects, std::set< Object * > &candidates)
bool get_bone_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone)
static void get_animated_from_export_set(std::set< Object * > &animated_objects, LinkNode &export_set)
void get_bone_frames(BCFrames &frames, Object *ob, Bone *bone)
BCAnimationSampler(BCExportSettings &export_settings, BCObjectSet &object_set)
void add_object(Object *ob)
void sample_scene(BCExportSettings &export_settings, bool keyframe_at_end)
static bool is_animated_by_constraint(Object *ob, ListBase *conlist, std::set< Object * > &animated_objects)
bool get_object_samples(BCMatrixSampleMap &samples, Object *ob)
BCAnimationCurveMap * get_curves(Object *ob)
Object * get_reference()
BCAnimation(bContext *C, Object *ob)
BCFrameSet frame_set
BCAnimationCurveMap curve_map
BCSample & add(Object *ob, int frame_index)
int get_frames(std::vector< int > &frames) const
BCSampleFrame * get_frame(int frame_index)
int get_samples(Object *ob, BCFrameSampleMap &samples) const
~BCSampleFrameContainer()=default
int get_matrices(Object *ob, BCMatrixSampleMap &samples) const
const BCMatrix * get_sample_matrix(Object *ob) const
bool has_sample_for(Object *ob) const
const BCSample * get_sample(Object *ob) const
BCSample & add(Object *ob)
std::set< Object * > BCObjectSet
float length(VecOp< float, D >) RET