Blender V4.3
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#include "BLI_math_rotation.h"
15
16#include "DNA_action_types.h"
17
18/* Collection of animation curves */
20 private:
21 Object *reference = NULL;
22 bContext *mContext;
23
24 public:
27
28 BCAnimation(bContext *C, Object *ob) : mContext(C)
29 {
30 Main *bmain = CTX_data_main(mContext);
31 reference = (Object *)BKE_id_copy(bmain, &ob->id);
32 id_us_min(&reference->id);
33 }
34
36 {
37 BCAnimationCurveMap::iterator it;
38 for (it = curve_map.begin(); it != curve_map.end(); ++it) {
39 delete it->second;
40 }
41
42 if (reference && reference->id.us == 0) {
43 Main *bmain = CTX_data_main(mContext);
44 BKE_id_delete(bmain, &reference->id);
45 }
46 curve_map.clear();
47 }
48
50 {
51 return reference;
52 }
53};
54
55typedef std::map<Object *, BCAnimation *> BCAnimationObjectMap;
56
58
59 /* Each frame on the timeline that needs to be sampled will have
60 * one BCSampleFrame where we collect sample information about all objects
61 * that need to be sampled for that frame. */
62
63 private:
64 BCSampleMap sampleMap;
65
66 public:
68 {
69 BCSampleMap::iterator it;
70 for (it = sampleMap.begin(); it != sampleMap.end(); ++it) {
71 BCSample *sample = it->second;
72 delete sample;
73 }
74 sampleMap.clear();
75 }
76
77 BCSample &add(Object *ob);
78
79 /* Following methods return NULL if object is not in the sampleMap. */
80
82 const BCSample *get_sample(Object *ob) const;
83 const BCMatrix *get_sample_matrix(Object *ob) const;
85 const BCMatrix *get_sample_matrix(Object *ob, Bone *bone) const;
86
88 bool has_sample_for(Object *ob) const;
90 bool has_sample_for(Object *ob, Bone *bone) const;
91};
92
93typedef std::map<int, BCSampleFrame> BCSampleFrameMap;
94
96
97 /*
98 * The BCSampleFrameContainer stores a map of BCSampleFrame objects
99 * with the timeline frame as key.
100 *
101 * Some details on the purpose:
102 * An Animation is made of multiple FCurves where each FCurve can
103 * have multiple keyframes. When we want to export the animation we
104 * also can decide whether we want to export the keyframes or a set
105 * of sample frames at equidistant locations (sample period).
106 * In any case we must resample first need to resample it fully
107 * to resolve things like:
108 *
109 * - animations by constraints
110 * - animations by drivers
111 *
112 * For this purpose we need to step through the entire animation and
113 * then sample each frame that contains at least one keyFrame or
114 * sampleFrame. Then for each frame we have to store the transform
115 * information for all exported objects in a BCSampleframe
116 *
117 * The entire set of BCSampleframes is finally collected into
118 * a BCSampleframneContainer
119 */
120
121 private:
122 BCSampleFrameMap sample_frames;
123
124 public:
126
127 BCSample &add(Object *ob, int frame_index);
129 BCSampleFrame *get_frame(int frame_index);
130
132 int get_frames(std::vector<int> &frames) const;
133 int get_frames(Object *ob, BCFrames &frames) const;
134 int get_frames(Object *ob, Bone *bone, BCFrames &frames) const;
135
136 int get_samples(Object *ob, BCFrameSampleMap &samples) const;
137 int get_matrices(Object *ob, BCMatrixSampleMap &samples) const;
138 int get_matrices(Object *ob, Bone *bone, BCMatrixSampleMap &samples) const;
139};
140
142 private:
143 BCExportSettings &export_settings;
144 BCSampleFrameContainer sample_data;
145 BCAnimationObjectMap objects;
146
147 void generate_transform(Object *ob, const BCCurveKey &key, BCAnimationCurveMap &curves);
148 void generate_transforms(Object *ob,
149 const std::string prep,
150 const BC_animation_type type,
151 BCAnimationCurveMap &curves);
152 void generate_transforms(Object *ob, Bone *bone, BCAnimationCurveMap &curves);
153
154 void initialize_curves(BCAnimationCurveMap &curves, Object *ob);
160 void initialize_keyframes(BCFrameSet &frameset, Object *ob);
161 BCSample &sample_object(Object *ob, int frame_index, bool for_opensim);
162 void update_animation_curves(BCAnimation &animation,
164 Object *ob,
165 int frame_index);
166 void check_property_is_animated(
167 BCAnimation &animation, float *ref, float *val, std::string data_path, int length);
168
169 public:
170 BCAnimationSampler(BCExportSettings &export_settings, BCObjectSet &object_set);
172
173 void add_object(Object *ob);
174
175 void sample_scene(BCExportSettings &export_settings, bool keyframe_at_end);
176
178 void get_object_frames(BCFrames &frames, Object *ob);
179 bool get_object_samples(BCMatrixSampleMap &samples, Object *ob);
180 void get_bone_frames(BCFrames &frames, Object *ob, Bone *bone);
181 bool get_bone_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone);
182
183 static void get_animated_from_export_set(std::set<Object *> &animated_objects,
184 LinkNode &export_set);
185 static void find_depending_animated(std::set<Object *> &animated_objects,
186 std::set<Object *> &candidates);
187 static bool is_animated_by_constraint(Object *ob,
188 ListBase *conlist,
189 std::set<Object *> &animated_objects);
190};
BC_animation_type
std::map< BCCurveKey, BCAnimationCurve * > BCAnimationCurveMap
std::vector< float > BCFrames
std::set< float > BCFrameSet
std::map< Object *, BCAnimation * > BCAnimationObjectMap
std::map< int, BCSampleFrame > BCSampleFrameMap
std::map< int, const BCSample * > BCFrameSampleMap
std::map< Object *, BCSample * > BCSampleMap
std::map< int, const BCMatrix * > BCMatrixSampleMap
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:765
void id_us_min(ID *id)
Definition lib_id.cc:359
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
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
#define NULL