Blender V5.3
usd_armature_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "usd_utils.hh"
7
8#include "ANIM_action.hh"
9#include "ANIM_fcurve.hh"
10
11#include "BKE_armature.hh"
12#include "BKE_fcurve.hh"
13#include "BKE_modifier.hh"
14
16#include "BLI_string_ref.hh"
17#include "BLI_vector.hh"
18
19#include "DEG_depsgraph.hh"
21#include "DNA_action_types.h"
22#include "DNA_armature_types.h"
23
25
26/* Utility: create new fcurve and add it as a channel to a group. */
28 const animrig::FCurveDescriptor &fcurve_descriptor,
29 const int sample_count)
30{
31 FCurve *fcurve = channelbag.fcurve_create_unique(nullptr, fcurve_descriptor);
32 BLI_assert_msg(fcurve, "The same F-Curve is being created twice, this is unexpected.");
33 if (fcurve) {
34 BKE_fcurve_bezt_resize(*fcurve, sample_count);
35 }
36 return fcurve;
37}
38
39/* Utility: fill in a single fcurve sample at the provided index. */
40void set_fcurve_sample(FCurve *fcu, int64_t sample_index, const float frame, const float value)
41{
42 BLI_assert(sample_index >= 0 && sample_index < fcu->totvert);
43 BezTriple &bez = fcu->bezt[sample_index];
44 bez.vec[1][0] = frame;
45 bez.vec[1][1] = value;
46 bez.ipo = BEZT_IPO_LIN;
47 bez.f1 = bez.f2 = bez.f3 = BEZT_FLAG_SELECT;
48 bez.h1 = bez.h2 = HD_AUTO;
49}
50
51/* Recursively invoke the 'visitor' function on the given bone and its children. */
52static void visit_bones(const Bone *bone, FunctionRef<void(const Bone *)> visitor)
53{
54 if (!(bone && visitor)) {
55 return;
56 }
57
58 visitor(bone);
59
60 for (const Bone &child : bone->childbase) {
61 visit_bones(&child, visitor);
62 }
63}
64
66 ModifierType type,
67 const Depsgraph *depsgraph)
68{
69 BLI_assert(depsgraph);
70
71 const Scene *scene = DEG_get_input_scene(depsgraph);
72 eEvaluationMode mode = DEG_get_mode(depsgraph);
73
74 for (ModifierData &md : obj.modifiers) {
75
76 if (!BKE_modifier_is_enabled(scene, &md, mode)) {
77 continue;
78 }
79
80 if (md.type == type) {
81 return &md;
82 }
83 }
84
85 return nullptr;
86}
87
88/* Return the armature modifier on the given object. Return null if no armature modifier
89 * can be found. */
91 const Depsgraph *depsgraph)
92{
93 const ArmatureModifierData *mod = reinterpret_cast<const ArmatureModifierData *>(
95 return mod;
96}
97
98void visit_bones(const Object *ob_arm, FunctionRef<void(const Bone *)> visitor)
99{
100 if (!(ob_arm && ob_arm->type == OB_ARMATURE && ob_arm->data)) {
101 return;
102 }
103
104 const bArmature *armature = id_cast<bArmature *>(ob_arm->data);
105 for (const Bone &bone : armature->bonebase) {
106 visit_bones(&bone, visitor);
107 }
108}
109
111 const bool use_deform,
112 Vector<StringRef> &r_names)
113{
115 if (use_deform) {
116 init_deform_bones_map(ob_arm, &deform_map);
117 }
118
119 auto visitor = [&](const Bone *bone) {
120 const StringRef bone_name(bone->name);
121 if (use_deform && !deform_map.contains(bone_name)) {
122 return;
123 }
124
125 r_names.append(bone_name);
126 };
127
128 visit_bones(ob_arm, visitor);
129}
130
131pxr::TfToken build_usd_joint_path(const Bone *bone, bool allow_unicode)
132{
133 std::string path(make_safe_name(bone->name, allow_unicode));
134
135 const Bone *parent = bone->parent;
136 while (parent) {
137 path = make_safe_name(parent->name, allow_unicode) + '/' + path;
138 parent = parent->parent;
139 }
140
141 return pxr::TfToken(path);
142}
143
144void create_pose_joints(pxr::UsdSkelAnimation &skel_anim,
145 const Object &obj,
146 const Map<StringRef, const Bone *> *deform_map,
147 bool allow_unicode)
148{
149 BLI_assert(obj.pose);
150
151 pxr::VtTokenArray joints;
152
153 const bPose *pose = obj.pose;
154 const bArmature &arm = *id_cast<bArmature *>(obj.data);
156
157 for (const bPoseChannel &pchan : pose->chanbase) {
158 const Bone *pchan_bone = pchan.bone_get(arm);
159 if (pchan_bone) {
160 if (deform_map && !deform_map->contains(pchan.name)) {
161 /* If deform_map is passed in, assume we're going deform-only.
162 * Bones not found in the map should be skipped. */
163 continue;
164 }
165
166 joints.push_back(build_usd_joint_path(pchan_bone, allow_unicode));
167 }
168 }
169
170 skel_anim.GetJointsAttr().Set(joints);
171}
172
173const Object *get_armature_modifier_obj(const Object &obj, const Depsgraph *depsgraph)
174{
176 return mod ? mod->object : nullptr;
177}
178
180 const StringRefNull name,
181 const Depsgraph *depsgraph)
182{
183 const ArmatureModifierData *arm_mod = get_armature_modifier(obj, depsgraph);
184
185 if (!arm_mod || !arm_mod->object || !arm_mod->object->data) {
186 return false;
187 }
188
189 bArmature *arm = id_cast<bArmature *>(arm_mod->object->data);
190
191 return BKE_armature_find_bone_name(arm, name.c_str());
192}
193
194bool can_export_skinned_mesh(const Object &obj, const Depsgraph *depsgraph)
195{
196 return get_enabled_modifier(obj, eModifierType_Armature, depsgraph) != nullptr;
197}
198
200{
201 if (!deform_map) {
202 return;
203 }
204
205 deform_map->clear();
206
207 auto deform_visitor = [&](const Bone *bone) {
208 if (!bone) {
209 return;
210 }
211
212 const bool deform = !(bone->flag & BONE_NO_DEFORM);
213 if (deform) {
214 deform_map->add(bone->name, bone);
215 }
216 };
217
218 visit_bones(obj, deform_visitor);
219
220 /* Get deform parents */
221 for (const auto &item : deform_map->items()) {
222 BLI_assert(item.value);
223 for (const Bone *parent = item.value->parent; parent; parent = parent->parent) {
224 deform_map->add(parent->name, parent);
225 }
226 }
227}
228
229} // namespace blender::io::usd
Functions and classes to work with Actions.
Functions to modify FCurves.
#define BLI_assert_msg(a, msg)
void clear()
Definition BLI_map.hh:1038
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr const char * c_str() const
void append(const T &value)
FCurve * fcurve_create_unique(Main *bmain, const FCurveDescriptor &fcurve_descriptor)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
void init_deform_bones_map(const Object *obj, Map< StringRef, const Bone * > *deform_map)
void set_fcurve_sample(FCurve *fcu, int64_t sample_index, const float frame, const float value)
void get_armature_bone_names(const Object *ob_arm, const bool use_deform, Vector< StringRef > &r_names)
static const ArmatureModifierData * get_armature_modifier(const Object &obj, const Depsgraph *depsgraph)
bool is_armature_modifier_bone_name(const Object &obj, const StringRefNull name, const Depsgraph *depsgraph)
void create_pose_joints(pxr::UsdSkelAnimation &skel_anim, const Object &obj, const Map< StringRef, const Bone * > *deform_map, bool allow_unicode)
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
pxr::TfToken build_usd_joint_path(const Bone *bone, bool allow_unicode)
const ModifierData * get_enabled_modifier(const Object &obj, ModifierType type, const Depsgraph *depsgraph)
const Object * get_armature_modifier_obj(const Object &obj, const Depsgraph *depsgraph)
static void visit_bones(const Bone *bone, FunctionRef< void(const Bone *)> visitor)
bool can_export_skinned_mesh(const Object &obj, const Depsgraph *depsgraph)
FCurve * create_fcurve(animrig::Channelbag &channelbag, const animrig::FCurveDescriptor &fcurve_descriptor, const int sample_count)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void BKE_pose_ensure_bone_indices(const Object &pose_object)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
Scene * DEG_get_input_scene(const Depsgraph *graph)
void BKE_fcurve_bezt_resize(FCurve &fcu, int new_totvert)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
eBezTriple_Flag f2
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Interpolation ipo
eBezTriple_Handle h1
struct Bone * parent
ListBaseT< Bone > childbase
ListBaseT< Bone > bonebase