Blender  V3.3
BCAnimationSampler.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #include "BCAnimationCurve.h"
6 #include "BCSampleData.h"
7 #include "collada_utils.h"
8 
9 #include "BKE_action.h"
10 #include "BKE_lib_id.h"
11 
12 #include "BLI_math_rotation.h"
13 
14 #include "DNA_action_types.h"
15 
16 /* Collection of animation curves */
17 class BCAnimation {
18  private:
19  Object *reference = NULL;
20  bContext *mContext;
21 
22  public:
25 
26  BCAnimation(bContext *C, Object *ob) : mContext(C)
27  {
28  Main *bmain = CTX_data_main(mContext);
29  reference = (Object *)BKE_id_copy(bmain, &ob->id);
30  id_us_min(&reference->id);
31  }
32 
34  {
35  BCAnimationCurveMap::iterator it;
36  for (it = curve_map.begin(); it != curve_map.end(); ++it) {
37  delete it->second;
38  }
39 
40  if (reference && reference->id.us == 0) {
41  Main *bmain = CTX_data_main(mContext);
42  BKE_id_delete(bmain, &reference->id);
43  }
44  curve_map.clear();
45  }
46 
48  {
49  return reference;
50  }
51 };
52 
53 typedef std::map<Object *, BCAnimation *> BCAnimationObjectMap;
54 
56 
57  /* Each frame on the timeline that needs to be sampled will have
58  * one BCSampleFrame where we collect sample information about all objects
59  * that need to be sampled for that frame. */
60 
61  private:
62  BCSampleMap sampleMap;
63 
64  public:
66  {
67  BCSampleMap::iterator it;
68  for (it = sampleMap.begin(); it != sampleMap.end(); ++it) {
69  BCSample *sample = it->second;
70  delete sample;
71  }
72  sampleMap.clear();
73  }
74 
75  BCSample &add(Object *ob);
76 
77  /* Following methods return NULL if object is not in the sampleMap. */
78 
80  const BCSample *get_sample(Object *ob) const;
81  const BCMatrix *get_sample_matrix(Object *ob) const;
83  const BCMatrix *get_sample_matrix(Object *ob, Bone *bone) const;
84 
86  bool has_sample_for(Object *ob) const;
88  bool has_sample_for(Object *ob, Bone *bone) const;
89 };
90 
91 typedef std::map<int, BCSampleFrame> BCSampleFrameMap;
92 
94 
95  /*
96  * The BCSampleFrameContainer stores a map of BCSampleFrame objects
97  * with the timeline frame as key.
98  *
99  * Some details on the purpose:
100  * An Animation is made of multiple FCurves where each FCurve can
101  * have multiple keyframes. When we want to export the animation we
102  * also can decide whether we want to export the keyframes or a set
103  * of sample frames at equidistant locations (sample period).
104  * In any case we must resample first need to resample it fully
105  * to resolve things like:
106  *
107  * - animations by constraints
108  * - animations by drivers
109  *
110  * For this purpose we need to step through the entire animation and
111  * then sample each frame that contains at least one keyFrame or
112  * sampleFrame. Then for each frame we have to store the transform
113  * information for all exported objects in a BCSampleframe
114  *
115  * The entire set of BCSampleframes is finally collected into
116  * a BCSampleframneContainer
117  */
118 
119  private:
120  BCSampleFrameMap sample_frames;
121 
122  public:
124  {
125  }
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,
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,
163  BCSample &sample,
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
Definition: BCSampleData.h:47
std::map< Object *, BCSample * > BCSampleMap
Definition: BCSampleData.h:46
std::map< int, const BCMatrix * > BCMatrixSampleMap
Definition: BCSampleData.h:48
Blender kernel action and pose functionality.
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct ID * BKE_id_copy(struct Main *bmain, const struct ID *id)
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to curves
#define C
Definition: RandGen.cpp:25
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)
BCAnimation(bContext *C, Object *ob)
Object * get_reference()
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
Definition: collada_utils.h:58
T length(const vec_base< T, Size > &a)
int us
Definition: DNA_ID.h:388
Definition: BKE_main.h:121