85 if (!stage || !mesh_prim) {
89 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
93 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
94 mesh_prim.GetPath().GetAsString().c_str());
98 pxr::UsdSkelSkeleton skel;
99 if (!skel_api.GetSkeleton(&skel)) {
100 pxr::SdfPath skel_path = mesh_prim.GetParent().GetPath().AppendChild(
usdtokens::Skel);
101 skel = pxr::UsdSkelSkeleton::Define(stage, skel_path);
105 "Couldn't find or create skeleton bound to mesh prim %s",
106 mesh_prim.GetPath().GetAsString().c_str());
110 skel_api.CreateSkeletonRel().AddTarget(skel.GetPath());
113 pxr::VtMatrix4dArray bind_transforms(1, pxr::GfMatrix4d(1.0));
114 pxr::VtMatrix4dArray rest_transforms(1, pxr::GfMatrix4d(1.0));
115 skel.CreateBindTransformsAttr().Set(bind_transforms);
116 skel.GetRestTransformsAttr().Set(rest_transforms);
121 skel.CreateJointsAttr().Set(joints);
124 pxr::UsdAttribute temp_weights_attr = pxr::UsdGeomPrimvarsAPI(mesh_prim).GetPrimvar(
127 if (!temp_weights_attr) {
133 pxr::UsdSkelAnimation anim = pxr::UsdSkelAnimation::Define(stage, anim_path);
136 CLOG_WARN(&
LOG,
"Couldn't define animation at path %s", anim_path.GetAsString().c_str());
140 pxr::VtTokenArray blendshape_names;
141 skel_api.GetBlendShapesAttr().Get(&blendshape_names);
142 anim.CreateBlendShapesAttr().Set(blendshape_names);
144 std::vector<double> times;
145 temp_weights_attr.GetTimeSamples(×);
147 pxr::UsdAttribute anim_weights_attr = anim.CreateBlendShapeWeightsAttr();
149 pxr::VtFloatArray weights;
150 for (
const double time : times) {
151 if (temp_weights_attr.Get(&weights, time)) {
152 anim_weights_attr.Set(weights, time);
158 skel_api = pxr::UsdSkelBindingAPI::Apply(skel.GetPrim());
162 "Couldn't apply UsdSkelBindingAPI to skeleton prim %s",
163 skel.GetPath().GetAsString().c_str());
167 if (!skel_api.CreateAnimationSourceRel().AddTarget(pxr::SdfPath(
usdtokens::Anim))) {
169 "Couldn't set animation source on skeleton %s",
170 skel.GetPath().GetAsString().c_str());
197 const pxr::UsdPrim &mesh_prim,
202 if (!(key && mesh_prim)) {
206 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
210 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
211 mesh_prim.GetPath().GetAsString().c_str());
215 pxr::VtTokenArray blendshape_names;
216 std::vector<pxr::SdfPath> blendshape_paths;
225 int basis_totelem = basis_key->
totelem;
228 if (&kb == basis_key) {
234 blendshape_names.push_back(name);
236 pxr::SdfPath path = mesh_prim.GetPath().AppendChild(name);
237 blendshape_paths.push_back(path);
239 pxr::UsdSkelBlendShape blendshape = pxr::UsdSkelBlendShape::Define(stage, path);
241 pxr::UsdAttribute offsets_attr = blendshape.CreateOffsetsAttr();
246 pxr::UsdAttribute point_indices_attr = blendshape.CreatePointIndicesAttr();
248 pxr::VtVec3fArray offsets(kb.
totelem);
249 pxr::VtIntArray indices(kb.
totelem);
250 std::iota(indices.begin(), indices.end(), 0);
252 const float (*fp)[3] =
static_cast<float (*)[3]
>(kb.
data);
254 const float (*basis_fp)[3] =
static_cast<float (*)[3]
>(basis_key->
data);
256 for (
int i = 0; i < kb.
totelem; ++i) {
262 offsets_attr.Set(offsets);
263 point_indices_attr.Set(indices);
267 pxr::UsdAttribute blendshape_attr = skel_api.CreateBlendShapesAttr();
268 blendshape_attr.Set(blendshape_names);
269 skel_api.CreateBlendShapeTargetsRel().SetTargets(blendshape_paths);
274 if (!skel_api.GetJointIndicesAttr().HasAuthoredValue()) {
275 pxr::VtArray<int> joint_indices(basis_totelem, 0);
276 skel_api.CreateJointIndicesPrimvar(
false, 1).GetAttr().Set(joint_indices);
279 if (!skel_api.GetJointWeightsAttr().HasAuthoredValue()) {
280 pxr::VtArray<float> joint_weights(basis_totelem, 1.0f);
281 skel_api.CreateJointWeightsPrimvar(
false, 1).GetAttr().Set(joint_weights);
303 const pxr::SdfPath &skel_path,
304 const pxr::SdfPathSet &mesh_paths)
306 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Get(stage, skel_path);
309 CLOG_WARN(&
LOG,
"Couldn't get skeleton from path %s", skel_path.GetAsString().c_str());
314 pxr::UsdPrim anim_prim;
315 pxr::UsdSkelAnimation anim;
316 if (skel_api.GetAnimationSource(&anim_prim)) {
317 anim = pxr::UsdSkelAnimation(anim_prim);
321 anim = pxr::UsdSkelAnimation::Define(stage, anim_path);
325 CLOG_WARN(&
LOG,
"Couldn't get animation under skeleton %s", skel_path.GetAsString().c_str());
340 for (
const pxr::SdfPath &mesh_path : mesh_paths) {
342 pxr::UsdPrim mesh_prim = stage->GetPrimAtPath(mesh_path);
343 pxr::UsdSkelBindingAPI mesh_skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
344 if (!mesh_skel_api) {
346 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
347 mesh_path.GetAsString().c_str());
352 pxr::UsdAttribute blend_shapes_attr = mesh_skel_api.GetBlendShapesAttr();
354 if (!blend_shapes_attr) {
358 pxr::VtTokenArray names;
359 if (!mesh_skel_api.GetBlendShapesAttr().Get(&names)) {
364 pxr::VtTokenArray unique_names;
366 for (
const pxr::TfToken &name : names.AsConst()) {
367 std::string unique = add_unique_name(merged_names, name.GetString());
368 unique_names.push_back(pxr::TfToken(unique));
372 mesh_skel_api.GetBlendShapesAttr().Set(unique_names);
375 const pxr::UsdAttribute temp_weights_attr = pxr::UsdGeomPrimvarsAPI(mesh_prim).GetPrimvar(
378 if (!temp_weights_attr) {
384 merge_info.
append(BlendShapeMergeInfo());
385 merge_info.
last().src_blend_shapes = unique_names;
386 merge_info.
last().src_weights_attr = temp_weights_attr;
395 pxr::VtTokenArray skel_blend_shape_names;
396 for (
const std::string &name : merged_names) {
397 skel_blend_shape_names.push_back(pxr::TfToken(name));
401 for (BlendShapeMergeInfo &info : merge_info) {
402 info.init_anim_map(skel_blend_shape_names);
406 anim.CreateBlendShapesAttr().
Set(skel_blend_shape_names);
408 pxr::UsdAttribute dst_weights_attr = anim.CreateBlendShapeWeightsAttr();
411 std::vector<double> times;
412 merge_info.
first().src_weights_attr.GetTimeSamples(×);
417 times.push_back(pxr::UsdTimeCode::Default().GetValue());
420 pxr::VtFloatArray dst_weights;
422 for (
const double time : times) {
423 for (
const BlendShapeMergeInfo &info : merge_info) {
424 pxr::VtFloatArray src_weights;
425 if (info.src_weights_attr.Get(&src_weights, time)) {
426 if (!info.anim_map.Remap(src_weights.AsConst(), &dst_weights)) {
427 CLOG_WARN(&
LOG,
"Failed remapping blend shape weights");
432 dst_weights_attr.Set(dst_weights, time);