Blender V5.3
usd_writer_armature.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
8#include "usd_utils.hh"
9
10#include "ANIM_action.hh"
11
13
14#include "BKE_action.hh"
15
16#include "DNA_armature_types.h"
17#include "DNA_object_types.h"
18
19#include <pxr/base/gf/matrix4d.h>
20#include <pxr/base/gf/matrix4f.h>
21#include <pxr/usd/usdGeom/primvarsAPI.h>
22#include <pxr/usd/usdSkel/animation.h>
23#include <pxr/usd/usdSkel/bindingAPI.h>
24#include <pxr/usd/usdSkel/skeleton.h>
25#include <pxr/usd/usdSkel/utils.h>
26
27#include "CLG_log.h"
28
29namespace blender {
30
31static CLG_LogRef LOG = {"io.usd"};
32
38static pxr::GfMatrix4d parent_relative_pose_mat(const bPoseChannel *pchan)
39{
40 /* Note that the float matrix will be returned as GfMatrix4d, because
41 * USD requires doubles. */
42 const pxr::GfMatrix4f pose_mat(pchan->pose_mat);
43
44 if (pchan->parent) {
45 const pxr::GfMatrix4f parent_pose_mat(pchan->parent->pose_mat);
46 const pxr::GfMatrix4f xf = pose_mat * parent_pose_mat.GetInverse();
47 return pxr::GfMatrix4d(xf);
48 }
49
50 /* No parent, so return the pose matrix directly. */
51 return pxr::GfMatrix4d(pose_mat);
52}
53
54/* Initialize the given skeleton and animation from
55 * the given armature object. */
56static void initialize(const Object *obj,
57 pxr::UsdSkelSkeleton &skel,
58 pxr::UsdSkelAnimation &skel_anim,
59 const Map<StringRef, const Bone *> *deform_bones,
60 bool allow_unicode)
61{
62 using namespace blender::io::usd;
63
64 pxr::VtTokenArray joints;
65 pxr::VtArray<float> bone_lengths;
66 pxr::VtArray<pxr::GfMatrix4d> bind_xforms;
67 pxr::VtArray<pxr::GfMatrix4d> rest_xforms;
68
69 /* Function to collect the bind and rest transforms from each bone. */
70 auto visitor = [&](const Bone *bone) {
71 if (!bone) {
72 return;
73 }
74
75 if (deform_bones && !deform_bones->contains(bone->name)) {
76 /* If deform_map is passed in, assume we're going deform-only.
77 * Bones not found in the map should be skipped. */
78 return;
79 }
80
81 /* Store Blender bone lengths to facilitate better round-tripping. */
82 bone_lengths.push_back(bone->length);
83
84 joints.push_back(build_usd_joint_path(bone, allow_unicode));
85 const pxr::GfMatrix4f arm_mat(bone->arm_mat);
86 bind_xforms.push_back(pxr::GfMatrix4d(arm_mat));
87
88 /* Set the rest transform to the parent-relative pose matrix, or the parent-relative
89 * armature matrix, if no pose channel exists. */
90 if (const bPoseChannel *pchan = BKE_pose_channel_find_name(obj->pose, bone->name)) {
91 rest_xforms.push_back(parent_relative_pose_mat(pchan));
92 }
93 else if (bone->parent) {
94 pxr::GfMatrix4f parent_arm_mat(bone->parent->arm_mat);
95 const pxr::GfMatrix4f rest_mat = arm_mat * parent_arm_mat.GetInverse();
96 rest_xforms.push_back(pxr::GfMatrix4d(rest_mat));
97 }
98 else {
99 rest_xforms.push_back(pxr::GfMatrix4d(arm_mat));
100 }
101 };
102
103 visit_bones(obj, visitor);
104 skel.GetJointsAttr().Set(joints);
105 skel.GetBindTransformsAttr().Set(bind_xforms);
106 skel.GetRestTransformsAttr().Set(rest_xforms);
107
108 const pxr::UsdPrim skel_prim = skel.GetPrim();
109
110 /* Store the custom bone lengths as just a regular Primvar attached to the Skeleton. */
111 const pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(skel_prim);
112 pxr::UsdGeomPrimvar pv_lengths = pv_api.CreatePrimvar(
113 BlenderBoneLengths, pxr::SdfValueTypeNames->FloatArray, pxr::UsdGeomTokens->uniform);
114 pv_lengths.Set(bone_lengths);
115
116 pxr::UsdSkelBindingAPI usd_skel_api = pxr::UsdSkelBindingAPI::Apply(skel_prim);
117
118 if (skel_anim) {
119 usd_skel_api.CreateAnimationSourceRel().SetTargets(
120 pxr::SdfPathVector({pxr::SdfPath(skel_anim.GetPath().GetName())}));
121 create_pose_joints(skel_anim, *obj, deform_bones, allow_unicode);
122 }
123}
124
125namespace io::usd {
126
127/* Add skeleton transform samples from the armature pose channels. */
128static void add_anim_sample(pxr::UsdSkelAnimation &skel_anim,
129 const Object *obj,
130 const pxr::UsdTimeCode time,
131 const Map<StringRef, const Bone *> *deform_map,
132 pxr::UsdUtilsSparseValueWriter &value_writer)
133{
134 if (!(skel_anim && obj && obj->pose)) {
135 return;
136 }
137
138 pxr::VtArray<pxr::GfMatrix4d> xforms;
139
140 const bPose *pose = obj->pose;
141
142 for (const bPoseChannel &pchan : pose->chanbase) {
143 if (deform_map && !deform_map->contains(pchan.name)) {
144 /* If deform_map is passed in, assume we're going deform-only.
145 * Bones not found in the map should be skipped. */
146 continue;
147 }
148
149 xforms.push_back(parent_relative_pose_mat(&pchan));
150 }
151
152 /* Perform the same steps as UsdSkelAnimation::SetTransforms but write data out sparsely. */
153 pxr::VtArray<pxr::GfVec3f> translations;
154 pxr::VtArray<pxr::GfQuatf> rotations;
155 pxr::VtArray<pxr::GfVec3h> scales;
156 if (pxr::UsdSkelDecomposeTransforms(xforms, &translations, &rotations, &scales)) {
157 set_attribute(skel_anim.GetTranslationsAttr(), translations, time, value_writer);
158 set_attribute(skel_anim.GetRotationsAttr(), rotations, time, value_writer);
159 set_attribute(skel_anim.GetScalesAttr(), scales, time, value_writer);
160 }
161 else {
162 CLOG_WARN(&LOG, "Could not decompose skeleton transforms for frame time %f", time.GetValue());
163 }
164}
165
167
169{
170 if (!(context.object && context.object->type == OB_ARMATURE && context.object->data)) {
172 return;
173 }
174
175 /* Create the skeleton. */
176 pxr::UsdStageRefPtr stage = usd_export_context_.stage;
177 pxr::UsdSkelSkeleton skel = pxr::UsdSkelSkeleton::Define(stage, usd_export_context_.usd_path);
178
179 if (!skel) {
180 CLOG_WARN(&LOG,
181 "Couldn't define UsdSkelSkeleton %s",
182 usd_export_context_.usd_path.GetString().c_str());
183 return;
184 }
185
186 pxr::UsdSkelAnimation skel_anim;
187
188 const bool allow_unicode = usd_export_context_.export_params.allow_unicode;
189
191 /* Use the action name as the animation name. */
192 const animrig::Action *action = animrig::get_action(context.object->id);
193 const pxr::TfToken anim_name(action ? make_safe_name(action->id.name + 2, allow_unicode) :
194 "Action");
195
196 /* Create the skeleton animation primitive as a child of the skeleton. */
197 pxr::SdfPath anim_path = usd_export_context_.usd_path.AppendChild(anim_name);
198 skel_anim = pxr::UsdSkelAnimation::Define(stage, anim_path);
199
200 if (!skel_anim) {
201 CLOG_WARN(&LOG, "Couldn't define UsdSkelAnimation %s", anim_path.GetString().c_str());
202 return;
203 }
204 }
205
207 &deform_map_ :
208 nullptr;
209
210 if (!this->frame_has_been_written_) {
211 init_deform_bones_map(context.object, deform_map);
212 initialize(context.object, skel, skel_anim, deform_map, allow_unicode);
213 }
214
217 skel_anim, context.object, get_export_time_code(), deform_map, usd_value_writer_);
218 }
219}
220
222{
223 const Object *obj = context.object;
224
225 if (!(obj && obj->type == OB_ARMATURE)) {
226 return false;
227 }
228
229 return obj->adt != nullptr;
230}
231
232} // namespace io::usd
233} // namespace blender
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
bool contains(const Key &key) const
Definition BLI_map.hh:353
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
USDAbstractWriter(const USDExporterContext &usd_export_context)
const USDExporterContext usd_export_context_
bool check_is_animated(const HierarchyContext &context) const override
USDArmatureWriter(const USDExporterContext &ctx)
void do_write(HierarchyContext &context) override
#define uniform(slot)
Action * get_action(ID &animated_id)
void visit_bones(const Object *ob_arm, FunctionRef< void(const Bone *)> visitor)
void init_deform_bones_map(const Object *obj, Map< StringRef, const Bone * > *deform_map)
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
const pxr::TfToken BlenderBoneLengths("blender:bone_lengths", pxr::TfToken::Immortal)
pxr::TfToken build_usd_joint_path(const Bone *bone, bool allow_unicode)
static void add_anim_sample(pxr::UsdSkelAnimation &skel_anim, const Object *obj, const pxr::UsdTimeCode time, const Map< StringRef, const Bone * > *deform_map, pxr::UsdUtilsSparseValueWriter &value_writer)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode time, pxr::UsdUtilsSparseValueWriter &value_writer)
BMesh Iterators.
static pxr::GfMatrix4d parent_relative_pose_mat(const bPoseChannel *pchan)
static void initialize(const Object *obj, pxr::UsdSkelSkeleton &skel, pxr::UsdSkelAnimation &skel_anim, const Map< StringRef, const Bone * > *deform_bones, bool allow_unicode)
static CLG_LogRef LOG
Definition logging.cpp:12
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
char name[258]
Definition DNA_ID.h:433
struct bPoseChannel * parent