48 CLOG_WARN(&
LOG,
"Animation curve too large to import, exceeds max int size");
58 BLI_assert(key_index >= 0 && key_index < curve->totvert);
61 bez.
vec[1][1] = value;
82 const ufbx_anim_layer &flayer)
86 for (
const ufbx_anim_prop &fprop : flayer.anim_props) {
87 if (fprop.anim_value->curves[0] ==
nullptr && fprop.anim_value->curves[1] ==
nullptr &&
88 fprop.anim_value->curves[2] ==
nullptr)
92 bool supported_prop =
false;
94 const bool is_position =
STREQ(fprop.prop_name.data,
"Lcl Translation");
95 const bool is_rotation =
STREQ(fprop.prop_name.data,
"Lcl Rotation");
96 const bool is_scale =
STREQ(fprop.prop_name.data,
"Lcl Scaling");
97 const bool is_blend_shape =
STREQ(fprop.prop_name.data,
"DeformPercent");
98 const bool is_focal_length =
STREQ(fprop.prop_name.data,
"FocalLength");
99 const bool is_focus_dist =
STREQ(fprop.prop_name.data,
"FocusDistance");
100 const bool is_diffuse =
STREQ(fprop.prop_name.data,
"DiffuseColor");
101 if (is_position || is_rotation || is_scale || is_blend_shape || is_focal_length ||
102 is_focus_dist || is_diffuse)
104 supported_prop =
true;
107 if (!supported_prop) {
111 const bool is_anim_camera = is_focal_length || is_focus_dist;
112 const bool is_anim_mat = is_diffuse;
114 ID *target_id =
nullptr;
117 if (is_blend_shape) {
120 if (target_key !=
nullptr) {
121 target_id = &target_key->
id;
124 else if (is_anim_camera) {
126 if (fprop.element->instances.count > 0) {
130 target_id = (
ID *)
obj->data;
135 else if (is_anim_mat) {
139 if (mat !=
nullptr) {
140 target_id = (
ID *)mat;
145 const ufbx_node *fnode = ufbx_as_node(fprop.element);
150 if (
obj ==
nullptr) {
153 if (
obj ==
nullptr) {
161 target_id = &
obj->id;
165 if (target_id ==
nullptr) {
171 anims.
order = order++;
184 if (is_blend_shape) {
187 if (is_focal_length) {
219 std::string rna_prefix;
221 const ufbx_node *fnode = ufbx_as_node(anim.
fbx_elem);
225 rna_prefix = std::string(
"pose.bones[\"") +
BLI_str_escape(group_name_str) +
"\"].";
233 int rot_channels = 3;
238 rna_rotation = curve_name_alloc.
copy_string(rna_prefix +
"rotation_quaternion");
242 rna_rotation = curve_name_alloc.
copy_string(rna_prefix +
"rotation_axis_angle");
246 rna_rotation = curve_name_alloc.
copy_string(rna_prefix +
"rotation_euler");
254 for (
int i = 0; i < 3; i++) {
255 r_curve_desc.
append({rna_position, i, {}, {}, group_name});
257 for (
int i = 0; i < rot_channels; i++) {
258 r_curve_desc.
append({rna_rotation, i, {}, {}, group_name});
260 for (
int i = 0; i < 3; i++) {
261 r_curve_desc.
append({rna_scale, i, {}, {}, group_name});
266 const ufbx_anim *fbx_anim,
269 const float anim_offset,
272 const ufbx_node *fnode = ufbx_as_node(anim.
fbx_elem);
273 ufbx_matrix bone_xform = ufbx_identity_matrix;
281 const bool bone_at_scene_root = fnode->node_depth <= 1;
282 ufbx_matrix world_to_arm = ufbx_identity_matrix;
283 if (!bone_at_scene_root) {
285 if (arm_obj !=
nullptr) {
287 arm_obj, ufbx_identity_matrix);
292 bone_xform = ufbx_matrix_invert(&bone_xform);
295 int rot_channels = 3;
316 const ufbx_anim_curve *input_curves[9] = {};
328 input_curves[6] = anim.
prop_scale->anim_value->curves[0];
329 input_curves[7] = anim.
prop_scale->anim_value->curves[1];
330 input_curves[8] = anim.
prop_scale->anim_value->curves[2];
335 for (
int i = 0; i < 9; i++) {
336 if (input_curves[i] !=
nullptr) {
337 for (
const ufbx_keyframe &key : input_curves[i]->keyframes) {
338 if (key.interpolation == UFBX_INTERPOLATION_CUBIC) {
340 const_cast<ufbx_keyframe &
>(key).interpolation = UFBX_INTERPOLATION_LINEAR;
342 unique_key_times.
add(key.time);
347 std::ranges::sort(sorted_key_times);
350 int64_t rot_index = pos_index + 3;
351 int64_t scale_index = rot_index + rot_channels;
352 int64_t tot_curves = scale_index + 3;
355 CLOG_WARN(&
LOG,
"Animation curve too large to import, exceeds max int size");
358 for (
int64_t i = 0; i < tot_curves; i++) {
367 for (
int64_t i = 0; i < key_count; i++) {
368 double t = sorted_key_times[i];
369 float tf =
float(t * fps + anim_offset);
370 ufbx_transform xform = ufbx_evaluate_transform(fbx_anim, fnode, t);
374 xform = ufbx_matrix_to_transform(&matrix);
381 math::Quaternion quat(xform.rotation.w, xform.rotation.x, xform.rotation.y, xform.rotation.z);
385 if (i != 0 &&
math::dot(quat, quat_prev) < 0.0f) {
419 const float anim_offset)
426 const ufbx_anim_curve *input_curve = anim.
prop_focal_length->anim_value->curves[0];
429 const ufbx_keyframe &fkey = input_curve->keyframes[i];
430 float tf =
float(fkey.time * fps + anim_offset);
431 float val =
float(fkey.value);
438 const ufbx_anim_curve *input_curve = anim.
prop_focus_dist->anim_value->curves[0];
440 channelbag, {
"dof.focus_distance", 0}, input_curve->keyframes.count);
442 const ufbx_keyframe &fkey = input_curve->keyframes[i];
443 float tf =
float(fkey.time * fps + anim_offset);
456 const float anim_offset)
462 const char *rna_path_1 =
"diffuse_color";
463 const char *rna_path_2 =
"nodes[\"Principled BSDF\"].inputs[0].default_value";
467 ID *target_ntree =
reinterpret_cast<ID *
>(target_mat->nodetree);
475 for (
int ch = 0; ch < 3; ch++) {
476 const ufbx_anim_curve *input_curve = anim.
prop_mat_diffuse->anim_value->curves[ch];
477 FCurve *curve_1 =
create_fcurve(channelbag, {rna_path_1, ch}, input_curve->keyframes.count);
478 FCurve *curve_2 =
create_fcurve(chbag_node, {rna_path_2, ch}, input_curve->keyframes.count);
480 const ufbx_keyframe &fkey = input_curve->keyframes[i];
481 float tf =
float(fkey.time * fps + anim_offset);
482 float val =
float(fkey.value);
495 const float anim_offset)
497 const ufbx_blend_channel *fchan = ufbx_as_blend_channel(anim.
prop_blend_shape->element);
499 std::string
rna_path = std::string(
"key_blocks[\"") +
501 const ufbx_anim_curve *input_curve = anim.
prop_blend_shape->anim_value->curves[0];
504 const ufbx_keyframe &fkey = input_curve->keyframes[i];
505 double t = fkey.time;
506 float tf =
float(t * fps + anim_offset);
507 float val =
float(fkey.value / 100.0);
514 const ufbx_scene &
fbx,
517 const float anim_offset)
521 for (
const ufbx_anim_stack *fstack :
fbx.anim_stacks) {
522 for (
const ufbx_anim_layer *flayer : fstack->layers) {
529 std::string action_name = fstack->name.data;
530 if (!
STREQ(fstack->name.data, flayer->name.data) && fstack->layers.count != 1) {
532 action_name += flayer->name.data;
545 animated_ids.add(anim.target_id);
552 for (
ID *
id : animated_ids) {
555 const std::string slot_name =
id->name;
561 BLI_assert_msg(adt !=
nullptr,
"fbx: could not create animation data for an ID");
562 if (adt->
action ==
nullptr) {
570 "fbx: failed to assign slot to ID");
586 anim_transform_curve_index[
index] = curve_desc.
size();
590 anim_transform_curve_index[
index] = -1;
606 transform_curves.
data() +
607 anim_transform_curve_index[
index]);
620 for (
FCurve *curve : transform_curves) {
Functions and classes to work with Actions.
Functions to work with AnimData.
Blender kernel action and pose functionality.
#define CLOG_WARN(clg_ref,...)
Object is a sort of wrapper for general info.
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
StringRefNull copy_string(StringRef str)
SubIterator begin() const
Value & lookup_or_add_default(const Key &key)
ValueIterator values() const &
const Value & lookup(const Key &key) const
Value lookup_default(const Key &key, const Value &default_value) const
bool contains(const Key &key) const
void append(const T &value)
IndexRange index_range() const
Span< T > as_span() const
void layer_keystrip_ensure()
const Layer * layer(int64_t index) const
Slot & slot_add_for_id_type(ID_Type idtype)
void slot_identifier_define(Slot &slot, StringRefNull new_identifier)
Vector< FCurve * > fcurve_create_many(Main *bmain, Span< FCurveDescriptor > fcurve_descriptors)
FCurve & fcurve_ensure(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
const Strip * strip(int64_t index) const
static constexpr slot_handle_t unassigned
Channelbag & channelbag_for_slot_ensure(const Slot &slot)
const T & data(const Action &owning_action) const
Slot * assign_action_ensure_slot_for_keying(Action &action, ID &animated_id)
Action & action_add(Main &bmain, StringRefNull name)
Channelbag & action_channelbag_ensure(bAction &dna_action, ID &animated_id)
bool assign_action(bAction *action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(Slot *slot_to_assign, ID &animated_id)
ActionSlotAssignmentResult
static void create_transform_curve_desc(const FbxElementMapping &mapping, const ElementAnimations &anim, LinearAllocator<> &curve_name_alloc, Vector< animrig::FCurveDescriptor > &r_curve_desc)
static void create_camera_curves(const ufbx_metadata &metadata, const ElementAnimations &anim, animrig::Channelbag &channelbag, const double fps, const float anim_offset)
const char * get_fbx_name(const ufbx_string &name, const char *def)
static void create_transform_curve_data(const FbxElementMapping &mapping, const ufbx_anim *fbx_anim, const ElementAnimations &anim, const double fps, const float anim_offset, FCurve **curves)
static void create_material_curves(const ElementAnimations &anim, bAction *action, animrig::Channelbag &channelbag, const double fps, const float anim_offset)
static void set_curve_sample(FCurve *curve, int64_t key_index, float time, float value)
static void finalize_curve(FCurve *cu)
static Vector< ElementAnimations > gather_animated_properties(const FbxElementMapping &mapping, const ufbx_anim_layer &flayer)
void import_animations(Main &bmain, const ufbx_scene &fbx, const FbxElementMapping &mapping, const double fps, const float anim_offset)
static FCurve * create_fcurve(animrig::Channelbag &channelbag, const animrig::FCurveDescriptor &descriptor, int64_t key_count)
ufbx_matrix calc_bone_pose_matrix(const ufbx_transform &local_xform, const ufbx_node &node, const ufbx_matrix &local_bind_inv_matrix)
static void create_blend_shape_curves(const ElementAnimations &anim, animrig::Channelbag &channelbag, const double fps, const float anim_offset)
bool size_fits_in_int(const int64_t size)
QuaternionBase< float > Quaternion
AxisAngleBase< float, AngleRadianBase< float > > AxisAngle
EulerXYZBase< float > EulerXYZ
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
AxisAngleBase< T, AngleT > to_axis_angle(const EulerXYZBase< T > &euler)
Euler3Base< T > to_euler(const AxisAngleBase< T, AngleT > &axis_angle, EulerOrder order)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void id_fake_user_set(ID *id)
AnimData * BKE_animdata_ensure_id(ID *id)
void BKE_fcurve_handles_recalc(FCurve &fcu)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
ID_Type GS(const char *name)
void BKE_fcurve_bezt_resize(FCurve &fcu, int new_totvert)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eBezTriple_Interpolation ipo
const ufbx_anim_prop * prop_focal_length
const ufbx_anim_prop * prop_blend_shape
eRotationModes object_rotmode
const ufbx_anim_prop * prop_mat_diffuse
const ufbx_anim_prop * prop_rotation
const ufbx_anim_prop * prop_position
const ufbx_anim_prop * prop_focus_dist
const ufbx_element * fbx_elem
const ufbx_anim_prop * prop_scale
Map< const ufbx_node *, Object * > bone_to_armature
Map< const ufbx_material *, Material * > mat_to_material
Map< const ufbx_node *, std::string > node_to_name
Set< const ufbx_node * > node_is_blender_bone
Map< const Object *, ufbx_matrix > armature_world_to_arm_pose_matrix
ufbx_matrix calc_local_bind_matrix(const ufbx_node *bone_node, const ufbx_matrix &world_to_arm) const
Map< const ufbx_element *, Object * > el_to_object
Map< const ufbx_element *, Key * > el_to_shape_key
const AngleT & angle() const
const vec3_type & axis() const
static QuaternionBase identity()