10#include <pxr/usd/usdGeom/primvarsAPI.h>
11#include <pxr/usd/usdSkel/animation.h>
12#include <pxr/usd/usdSkel/bindingAPI.h>
13#include <pxr/usd/usdSkel/blendShape.h>
14#include <pxr/usd/usdSkel/cache.h>
15#include <pxr/usd/usdSkel/skeletonQuery.h>
16#include <pxr/usd/usdSkel/utils.h>
63inline float max_mag_component(
const pxr::GfVec3d &vec)
65 return pxr::GfMax(pxr::GfAbs(vec[0]), pxr::GfAbs(vec[1]), pxr::GfAbs(vec[2]));
68void resize_fcurve(
FCurve &fcu,
uint bezt_count)
71 if (bezt_count == fcu.totvert) {
89void import_skeleton_curves(
Main *bmain,
91 const pxr::UsdSkelSkeletonQuery &skel_query,
95 using namespace blender::io::usd;
97 if (!(bmain && arm_obj && skel_query)) {
101 if (joint_to_bone_map.is_empty()) {
105 const pxr::UsdSkelAnimQuery &anim_query = skel_query.GetAnimQuery();
112 std::vector<double> samples;
113 anim_query.GetJointTransformTimeSamples(&samples);
115 if (samples.empty()) {
119 const size_t num_samples = samples.size();
123 BKE_id_rename(*bmain, act->
id, anim_query.GetPrim().GetName().GetText());
128 const pxr::VtTokenArray joint_order = skel_query.GetJointOrder();
131 constexpr int curves_per_joint = 10;
134 curve_desc.
reserve(joint_order.size() * curves_per_joint);
137 for (
const pxr::TfToken &joint : joint_order) {
138 const std::string *name = joint_to_bone_map.lookup_ptr(joint);
139 if (name ==
nullptr) {
147 std::string
rna_path =
"pose.bones[\"" + *name +
"\"].location";
149 curve_desc.
append({path_desc, 0, {}, {}, *name});
150 curve_desc.
append({path_desc, 1, {}, {}, *name});
151 curve_desc.
append({path_desc, 2, {}, {}, *name});
154 rna_path =
"pose.bones[\"" + *name +
"\"].rotation_quaternion";
156 curve_desc.
append({path_desc, 0, {}, {}, *name});
157 curve_desc.
append({path_desc, 1, {}, {}, *name});
158 curve_desc.
append({path_desc, 2, {}, {}, *name});
159 curve_desc.
append({path_desc, 3, {}, {}, *name});
162 rna_path =
"pose.bones[\"" + *name +
"\"].scale";
164 curve_desc.
append({path_desc, 0, {}, {}, *name});
165 curve_desc.
append({path_desc, 1, {}, {}, *name});
166 curve_desc.
append({path_desc, 2, {}, {}, *name});
171 for (
FCurve *fcu : fcurves) {
172 if (fcu !=
nullptr) {
189 pxr::VtMatrix4dArray usd_bind_xforms;
190 if (!skel_query.GetJointWorldBindTransforms(&usd_bind_xforms)) {
193 "%s: Couldn't get world bind transforms for skeleton %s",
195 skel_query.GetSkeleton().GetPrim().GetPath().GetAsString().c_str());
199 if (usd_bind_xforms.size() != joint_order.size()) {
203 "%s: Number of bind transforms does not match the number of joints for skeleton %s",
205 skel_query.GetSkeleton().GetPrim().GetPath().GetAsString().c_str());
209 const pxr::UsdSkelTopology &skel_topology = skel_query.GetTopology();
211 const pxr::VtMatrix4dArray &bind_xforms = usd_bind_xforms.AsConst();
212 pxr::VtMatrix4dArray joint_local_bind_xforms(bind_xforms.size());
213 for (
int i = 0; i < bind_xforms.size(); ++i) {
214 const int parent_id = skel_topology.GetParent(i);
216 if (parent_id >= 0) {
219 joint_local_bind_xforms[i] = bind_xforms[i] * bind_xforms[parent_id].GetInverse();
223 joint_local_bind_xforms[i] = bind_xforms[i];
230 for (
const double frame : samples) {
231 pxr::VtMatrix4dArray joint_local_xforms;
232 if (!skel_query.ComputeJointLocalTransforms(&joint_local_xforms, frame)) {
233 CLOG_WARN(&
LOG,
"Couldn't compute joint local transforms on frame %f", frame);
237 if (joint_local_xforms.size() != joint_order.size()) {
240 "Number of joint local transform entries %zu does not match the number of joints %zu",
241 joint_local_xforms.size(),
246 for (
int i = 0; i < joint_local_xforms.size(); ++i) {
247 const pxr::GfMatrix4d bone_xform = joint_local_xforms.AsConst()[i] *
248 joint_local_bind_xforms[i].GetInverse();
254 if (!pxr::UsdSkelDecomposeTransform(bone_xform, &t, &qrot, &s)) {
255 CLOG_WARN(&
LOG,
"Error decomposing matrix on frame %f", frame);
259 if (bezt_index > 0) {
264 if (pxr::GfDot(prev_rot[i], qrot) < 0.0f) {
270 const float re = qrot.GetReal();
271 const pxr::GfVec3f &im = qrot.GetImaginary();
273 for (
int j = 0; j < 3; ++j) {
274 const int k = curves_per_joint * i + j;
275 if (k >= fcurves.size()) {
276 CLOG_ERROR(&
LOG,
"Out of bounds translation curve index %d", k);
279 if (
FCurve *fcu = fcurves[k]) {
284 for (
int j = 0; j < 4; ++j) {
285 const int k = curves_per_joint * i + j + 3;
286 if (k >= fcurves.size()) {
287 CLOG_ERROR(&
LOG,
"Out of bounds rotation curve index %d", k);
290 if (
FCurve *fcu = fcurves[k]) {
300 for (
int j = 0; j < 3; ++j) {
301 const int k = curves_per_joint * i + j + 7;
302 if (k >= fcurves.size()) {
306 if (
FCurve *fcu = fcurves[k]) {
316 for (
FCurve *fcu : fcurves) {
317 if (fcu !=
nullptr) {
318 resize_fcurve(*fcu, bezt_index);
325void add_skinned_mesh_bindings(
const pxr::UsdSkelSkeleton &skel,
326 const pxr::UsdPrim &mesh_prim,
327 pxr::UsdGeomXformCache &xf_cache)
329 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
333 "Couldn't apply UsdSkelBindingAPI to skinned mesh prim %s",
334 mesh_prim.GetPath().GetAsString().c_str());
339 pxr::SdfPath skel_path = skel.GetPath();
340 skel_api.CreateSkeletonRel().SetTargets(pxr::SdfPathVector({skel_path}));
343 if (pxr::UsdAttribute geom_bind_attr = skel_api.CreateGeomBindTransformAttr()) {
345 pxr::GfMatrix4d mesh_xf = xf_cache.GetLocalToWorldTransform(mesh_prim);
346 pxr::GfMatrix4d skel_xf = xf_cache.GetLocalToWorldTransform(skel.GetPrim());
347 pxr::GfMatrix4d bind_xf = mesh_xf * skel_xf.GetInverse();
348 geom_bind_attr.Set(bind_xf);
352 "Couldn't create geom bind transform attribute for skinned mesh %s",
353 mesh_prim.GetPath().GetAsString().c_str());
363 const pxr::UsdPrim &prim,
365 const bool import_anim)
367 if (!(mesh_obj && mesh_obj->
data && mesh_obj->
type ==
OB_MESH && prim)) {
371 if (prim.IsInstanceProxy()) {
377 pxr::UsdSkelBindingAPI skel_api(prim);
382 if (!skel_api.GetBlendShapeTargetsRel().HasAuthoredTargets()) {
387 pxr::SdfPathVector targets;
388 if (!skel_api.GetBlendShapeTargetsRel().GetTargets(&targets)) {
391 "%s: Couldn't get blendshape targets for prim %s",
393 prim.GetPath().GetAsString().c_str());
397 if (targets.empty()) {
401 if (!skel_api.GetBlendShapesAttr().HasAuthoredValue()) {
406 pxr::VtTokenArray usd_blendshapes;
407 if (!skel_api.GetBlendShapesAttr().Get(&usd_blendshapes)) {
411 if (usd_blendshapes.empty()) {
416 if (targets.size() != usd_blendshapes.size()) {
420 "%s: Number of blendshapes does not match number of blendshape targets for prim %s",
422 prim.GetPath().GetAsString().c_str());
426 pxr::UsdStageRefPtr stage = prim.GetStage();
431 "%s: Couldn't get stage for prim %s",
433 prim.GetPath().GetAsString().c_str());
454 for (
int i = 0; i < targets.size(); ++i) {
456 const pxr::SdfPath &path = targets[i];
457 pxr::UsdSkelBlendShape blendshape(stage->GetPrimAtPath(path));
464 if (!blendshape.GetOffsetsAttr().HasAuthoredValue()) {
469 pxr::VtVec3fArray usd_offsets;
470 if (!blendshape.GetOffsetsAttr().Get(&usd_offsets)) {
473 "%s: Couldn't get offsets for blend shape %s",
475 path.GetAsString().c_str());
479 if (usd_offsets.empty()) {
482 "%s: No offsets for blend shape %s",
484 path.GetAsString().c_str());
488 shapekey_names.
add(blendshapes[i]);
500 pxr::VtArray<int> point_indices;
501 if (blendshape.GetPointIndicesAttr().HasAuthoredValue()) {
502 blendshape.GetPointIndicesAttr().Get(&point_indices);
505 float *fp =
static_cast<float *
>(kb->
data);
508 if (point_indices.empty()) {
511 for (
int a = 0; a < kb->
totelem; ++a, fp += 3) {
512 if (a >= offsets.
size()) {
516 "%s: Number of offsets greater than number of mesh vertices for blend shape %s",
518 path.GetAsString().c_str());
528 for (
const int point : point_indices.AsConst()) {
529 if (point < 0 || point >= kb->
totelem) {
531 "Out of bounds point index %d for blendshape %s",
533 path.GetAsString().c_str());
537 if (a >= offsets.
size()) {
541 "%s: Number of offsets greater than number of mesh vertices for blend shape %s",
543 path.GetAsString().c_str());
559 pxr::UsdSkelSkeleton skel_prim = skel_api.GetInheritedSkeleton();
565 skel_api = pxr::UsdSkelBindingAPI(skel_prim.GetPrim());
567 pxr::UsdPrim anim_prim = skel_api.GetInheritedAnimationSource();
572 skel_api.GetAnimationSource(&anim_prim);
579 pxr::UsdSkelAnimation skel_anim(anim_prim);
586 if (!skel_anim.GetBlendShapesAttr().HasAuthoredValue()) {
590 pxr::UsdAttribute weights_attr = skel_anim.GetBlendShapeWeightsAttr();
592 if (!(weights_attr && weights_attr.HasAuthoredValue())) {
597 std::vector<double> times;
598 if (!weights_attr.GetTimeSamples(×)) {
607 if (!skel_anim.GetBlendShapesAttr().Get(&usd_blendshapes)) {
611 if (usd_blendshapes.empty()) {
621 curves.reserve(usd_blendshapes.size());
622 processed_shapes.
reserve(usd_blendshapes.size());
624 for (
auto blendshape_name : usd_blendshapes.AsConst()) {
625 if (!shapekey_names.
contains(blendshape_name)) {
632 if (!processed_shapes.
add(blendshape_name)) {
634 "Duplicate blendshape '%s' encountered for %s",
635 blendshape_name.GetText(),
636 skel_anim.GetPath().GetAsString().c_str());
650 for (
double frame : times) {
651 pxr::VtFloatArray usd_weights;
652 if (!weights_attr.Get(&usd_weights, frame)) {
653 CLOG_WARN(&
LOG,
"Couldn't get blendshape weights for time %f", frame);
657 if (usd_weights.size() !=
curves.size()) {
660 "Number of weight samples does not match number of shapekey curve entries for frame %f",
666 for (
int wi = 0; wi < weights.
size(); ++wi) {
667 if (
curves[wi] !=
nullptr) {
676 auto recalc_handles = [bezt_index](
FCurve *fcu) {
678 resize_fcurve(*fcu, bezt_index);
682 std::for_each(
curves.begin(),
curves.end(), recalc_handles);
688 const pxr::VtArray<pxr::GfMatrix4d> &bind_xforms,
689 const pxr::VtTokenArray &joint_order,
691 const pxr::UsdSkelTopology &skel_topology,
692 const pxr::UsdSkelSkeletonQuery &skel_query)
694 if (!skel_query.HasRestPose()) {
698 pxr::VtArray<pxr::GfMatrix4d> rest_xforms;
699 if (skel_query.ComputeJointLocalTransforms(&rest_xforms, pxr::UsdTimeCode::Default(),
true)) {
703 for (
const pxr::TfToken &joint : joint_order) {
704 const std::string *name = joint_to_bone_map.
lookup_ptr(joint);
705 if (name ==
nullptr) {
712 pxr::GfMatrix4d xf = rest_xforms.AsConst()[i];
713 pxr::GfMatrix4d bind_xf = bind_xforms[i];
715 const int parent_id = skel_topology.GetParent(i);
716 if (parent_id >= 0) {
717 bind_xf = bind_xf * bind_xforms[parent_id].GetInverse();
720 xf = xf * bind_xf.GetInverse();
722 pxr::GfMatrix4f mat(xf);
732 const pxr::UsdSkelSkeleton &skel,
734 const bool import_anim)
740 pxr::UsdSkelCache skel_cache;
741 pxr::UsdSkelSkeletonQuery skel_query = skel_cache.GetSkelQuery(skel);
743 if (!skel_query.IsValid()) {
746 "%s: Couldn't query skeleton %s",
748 skel.GetPath().GetAsString().c_str());
752 const pxr::UsdSkelTopology &skel_topology = skel_query.GetTopology();
753 const pxr::VtTokenArray joint_order = skel_query.GetJointOrder();
755 if (joint_order.size() != skel_topology.size()) {
758 "%s: Topology and joint order size mismatch for skeleton %s",
760 skel.GetPath().GetAsString().c_str());
766 unique_joint_paths.
reserve(joint_order.size());
767 const bool all_valid_paths = std::all_of(
768 joint_order.cbegin(), joint_order.cend(), [&unique_joint_paths](
const pxr::TfToken &val) {
769 const bool is_valid = pxr::SdfPath::IsValidPathString(val);
770 return is_valid && unique_joint_paths.add(val);
772 if (!all_valid_paths) {
775 "%s: USD joint order array contains invalid or duplicated paths for skeleton %s",
777 skel.GetPath().GetAsString().c_str());
795 for (
const pxr::TfToken &joint : joint_order) {
796 pxr::SdfPath bone_path(joint);
797 const std::string &bone_name = bone_path.GetName();
802 "%s: Couldn't add bone for joint %s",
804 joint.GetString().c_str());
805 edit_bones.
append(
nullptr);
808 joint_to_bone_map.
add(joint, bone->
name);
813 const size_t num_joints = skel_topology.GetNumJoints();
814 if (edit_bones.
size() != num_joints) {
817 "%s: Mismatch in bone and joint counts for skeleton %s",
819 skel.GetPath().GetAsString().c_str());
824 pxr::VtMatrix4dArray bind_xforms;
825 if (!skel_query.GetJointWorldBindTransforms(&bind_xforms)) {
828 "%s: Couldn't get world bind transforms for skeleton %s",
830 skel.GetPath().GetAsString().c_str());
834 if (bind_xforms.size() != num_joints) {
837 "%s: Mismatch in bind xforms and joint counts for skeleton %s",
839 skel.GetPath().GetAsString().c_str());
853 bool negative_determinant =
false;
856 for (
size_t i = 0; i < num_joints; ++i) {
863 pxr::GfMatrix4f mat(bind_xforms.AsConst()[i]);
868 pxr::GfVec3f head(0.0f, 0.0f, 0.0f);
869 pxr::GfVec3f tail(0.0f, 1.0f, 0.0f);
876 if (mat.GetDeterminant() < 0.0) {
877 negative_determinant =
true;
881 bool valid_skeleton =
true;
882 if (negative_determinant) {
883 valid_skeleton =
false;
887 "USD Skeleton Import: bone matrices with negative determinants detected in prim %s. "
888 "Such matrices may indicate negative scales, possibly due to mirroring operations, "
889 "and cannot currently be converted to Blender's bone representation. "
890 "The skeletal animation won't be imported",
891 skel.GetPath().GetAsString().c_str());
904 for (
size_t i = 0; i < num_joints; ++i) {
905 const int parent_idx = skel_topology.GetParent(i);
906 if (parent_idx < 0) {
909 if (parent_idx >= edit_bones.
size()) {
911 "Out of bounds parent index for bone %s on skeleton %s",
912 pxr::SdfPath(joint_order[i]).GetAsString().c_str(),
913 skel.GetPath().GetAsString().c_str());
917 child_bones[parent_idx].
append(i);
918 if (edit_bones[i] && edit_bones[parent_idx]) {
919 edit_bones[i]->parent = edit_bones[parent_idx];
924 const pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(skel.GetPrim());
926 if (pv_lengths.HasValue()) {
927 pxr::VtArray<float> blender_bone_lengths;
928 pv_lengths.ComputeFlattened(&blender_bone_lengths);
930 Span<float> bone_lengths =
Span(blender_bone_lengths.cdata(), blender_bone_lengths.size());
931 for (
size_t i = 0; i < num_joints; ++i) {
933 pxr::GfVec3f head(bone->
head);
934 pxr::GfVec3f tail(bone->
tail);
936 tail = head + (tail - head).GetNormalized() * bone_lengths[i];
941 float avg_len_scale = 0;
942 for (
size_t i = 0; i < num_joints; ++i) {
948 if (child_bones[i].is_empty()) {
957 pxr::GfVec3f avg_child_head(0);
958 for (
int j : child_bones[i]) {
963 pxr::GfVec3f child_head(child->
head);
964 avg_child_head += child_head;
967 avg_child_head /= child_bones[i].
size();
969 pxr::GfVec3f parent_head(parent->
head);
970 pxr::GfVec3f parent_tail(parent->
tail);
972 const float new_len = (avg_child_head - parent_head).GetLength();
975 if (new_len > .00001 * max_mag_component(parent_head)) {
976 parent_tail = parent_head + (parent_tail - parent_head).GetNormalized() * new_len;
978 avg_len_scale += new_len;
983 avg_len_scale /= num_joints;
985 for (
size_t i = 0; i < num_joints; ++i) {
986 if (!child_bones[i].is_empty()) {
994 pxr::GfVec3f head(bone->
head);
997 if (avg_len_scale > .00001 * max_mag_component(head)) {
998 pxr::GfVec3f tail(bone->
tail);
999 tail = head + (tail - head).GetNormalized() * avg_len_scale;
1010 bmain, arm_obj, arm, bind_xforms, joint_order, joint_to_bone_map, skel_topology, skel_query);
1012 if (import_anim && valid_skeleton) {
1013 import_skeleton_curves(bmain, arm_obj, skel_query, joint_to_bone_map, reports);
1019 if (!(mesh_obj && mesh_obj->
type ==
OB_MESH && prim)) {
1023 if (prim.IsInstanceProxy()) {
1029 pxr::UsdSkelBindingAPI skel_api(prim);
1031 pxr::UsdSkelSkeleton skel = skel_api.GetInheritedSkeleton();
1039 pxr::VtArray<pxr::TfToken> joints;
1041 if (skel_api.GetJointsAttr().HasAuthoredValue()) {
1042 skel_api.GetJointsAttr().Get(&joints);
1044 else if (skel.GetJointsAttr().HasAuthoredValue()) {
1045 skel.GetJointsAttr().Get(&joints);
1048 if (joints.empty()) {
1053 pxr::UsdGeomPrimvar joint_indices_primvar = skel_api.GetJointIndicesPrimvar();
1054 if (!(joint_indices_primvar && joint_indices_primvar.HasAuthoredValue())) {
1059 pxr::UsdGeomPrimvar joint_weights_primvar = skel_api.GetJointWeightsPrimvar();
1060 if (!(joint_weights_primvar && joint_weights_primvar.HasAuthoredValue())) {
1066 int joint_indices_elem_size = joint_indices_primvar.GetElementSize();
1067 int joint_weights_elem_size = joint_weights_primvar.GetElementSize();
1070 if (joint_indices_elem_size != joint_weights_elem_size) {
1073 "%s: Joint weights and joint indices element size mismatch for prim %s",
1075 prim.GetPath().GetAsString().c_str());
1080 pxr::VtIntArray joint_indices;
1081 joint_indices_primvar.ComputeFlattened(&joint_indices);
1083 pxr::VtFloatArray joint_weights;
1084 joint_weights_primvar.ComputeFlattened(&joint_weights);
1086 if (joint_indices.empty() || joint_weights.empty()) {
1090 if (joint_indices.size() != joint_weights.size()) {
1093 "%s: Joint weights and joint indices size mismatch for prim %s",
1095 prim.GetPath().GetAsString().c_str());
1101 const pxr::TfToken
interp = joint_weights_primvar.GetInterpolation();
1104 if (!
ELEM(
interp, pxr::UsdGeomTokens->
vertex, pxr::UsdGeomTokens->constant)) {
1107 "%s: Unexpected joint weights interpolation type %s for prim %s",
1109 interp.GetString().c_str(),
1110 prim.GetPath().GetAsString().c_str());
1116 joint_weights.size() !=
int64_t(
mesh->verts_num) * joint_weights_elem_size)
1120 "%s: Joint weights of unexpected size for vertex interpolation for prim %s",
1122 prim.GetPath().GetAsString().c_str());
1126 if (
interp == pxr::UsdGeomTokens->constant && joint_weights.size() != joint_weights_elem_size) {
1129 "%s: Joint weights of unexpected size for constant interpolation for prim %s",
1131 prim.GetPath().GetAsString().c_str());
1137 for (
int index : joint_indices.AsConst()) {
1138 if (std::find(used_indices.
begin(), used_indices.
end(),
index) == used_indices.
end()) {
1140 if (index < 0 || index >= joints.size()) {
1155 "%s: Error creating deform group data for mesh %s",
1171 for (
int idx : used_indices) {
1172 std::string joint_name = pxr::SdfPath(joints.AsConst()[idx]).GetName();
1175 joint_def_grps[idx] = def_grp;
1185 offset = i * joint_weights_elem_size;
1187 for (
int j = 0; j < joint_weights_elem_size; ++j) {
1189 const float w = joint_weights.AsConst()[k];
1194 const int joint_idx = joint_indices.AsConst()[k];
1195 if (
bDeformGroup *def_grp = joint_def_grps[joint_idx]) {
1206 const Depsgraph *depsgraph)
1211 pxr::UsdGeomXformCache xf_cache(1.0);
1213 stage, armature_export_map, skinned_mesh_export_map, xf_cache, depsgraph);
1220 pxr::UsdGeomXformCache &xf_cache,
1221 const Depsgraph *depsgraph)
1224 for (
const auto &item : skinned_mesh_export_map.
items()) {
1225 const Object *mesh_obj = item.key;
1226 const pxr::SdfPath &mesh_path = item.value;
1229 pxr::UsdPrim mesh_prim = stage->GetPrimAtPath(mesh_path);
1232 "Invalid export map prim path %s for mesh object %s",
1233 mesh_path.GetAsString().c_str(),
1241 CLOG_WARN(&
LOG,
"Invalid armature modifier for skinned mesh %s", mesh_obj->
id.
name + 2);
1245 const pxr::SdfPath *path = armature_export_map.
lookup_ptr(arm_obj);
1247 CLOG_WARN(&
LOG,
"No export map entry for armature object %s", mesh_obj->
id.
name + 2);
1251 pxr::UsdPrim skel_prim = stage->GetPrimAtPath(*path);
1252 pxr::UsdSkelSkeleton skel(skel_prim);
1254 CLOG_WARN(&
LOG,
"Invalid USD skeleton for armature object %s", arm_obj->
id.
name + 2);
1258 add_skinned_mesh_bindings(skel, mesh_prim, xf_cache);
1272 for (
const auto &item : shape_key_mesh_export_map.
items()) {
1273 const Object *mesh_obj = item.key;
1274 const pxr::SdfPath &mesh_path = item.value;
1277 pxr::UsdPrim mesh_prim = stage->GetPrimAtPath(mesh_path);
1280 "Invalid export map prim path %s for mesh object %s",
1281 mesh_path.GetAsString().c_str(),
1287 mesh_prims.
append(mesh_prim);
1289 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
1293 "Couldn't apply UsdSkelBindingAPI to prim %s",
1294 mesh_prim.GetPath().GetAsString().c_str());
1298 pxr::UsdSkelSkeleton skel;
1299 if (skel_api.GetSkeleton(&skel)) {
1301 pxr::SdfPathSet *mesh_paths = skel_to_mesh.
lookup_ptr(skel.GetPath());
1303 skel_to_mesh.
add_new(skel.GetPath(), pxr::SdfPathSet());
1304 mesh_paths = skel_to_mesh.
lookup_ptr(skel.GetPath());
1307 mesh_paths->insert(mesh_prim.GetPath());
1320 for (
const auto &item : skel_to_mesh.
items()) {
1325 for (
const pxr::UsdPrim &prim : mesh_prims) {
1331 const pxr::UsdSkelBindingAPI &skel_api,
1347 for (
int i = 0; i < bone_names.
size(); ++i) {
1348 if (bone_names[i] == def.
name) {
1354 joint_index.
append(bone_idx);
1363 int max_totweight = 1;
1366 max_totweight = std::max(vert.totweight, max_totweight);
1371 const int element_size = max_totweight;
1372 int num_points =
mesh->verts_num;
1374 pxr::VtArray<int> joint_indices(num_points * element_size, 0);
1375 pxr::VtArray<float> joint_weights(num_points * element_size, 0.0f);
1383 for (
int j = 0; j < element_size; ++j, ++offset) {
1385 if (offset >= joint_indices.size()) {
1390 if (j >= vert.totweight) {
1394 int def_nr = int(vert.dw[j].def_nr);
1396 if (def_nr >= joint_index.
size()) {
1401 if (joint_index[def_nr] == -1) {
1405 joint_indices[offset] = joint_index[def_nr];
1406 joint_weights[offset] = vert.dw[j].weight;
1410 pxr::UsdSkelNormalizeWeights(joint_weights, element_size);
1412 skel_api.CreateJointIndicesPrimvar(
false, element_size).GetAttr().Set(joint_indices);
1413 skel_api.CreateJointWeightsPrimvar(
false, element_size).GetAttr().Set(joint_weights);
Functions and classes to work with Actions.
Functions to work with AnimData.
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
StringRefNull copy_string(StringRef str)
const Value * lookup_ptr(const Key &key) const
bool add(const Key &key, const Value &value)
void add_new(const Key &key, const Value &value)
ItemIterator items() const &
void reserve(const int64_t n)
bool contains(const Key &key) const
constexpr int64_t size() const
constexpr IndexRange index_range() const
void append(const T &value)
void reserve(const int64_t min_capacity)
Span< T > as_span() const
void append_n_times(const T &value, const int64_t n)
Vector< FCurve * > fcurve_create_many(Main *bmain, Span< FCurveDescriptor > fcurve_descriptors)
ccl_device_inline float interp(const float a, const float b, const float t)
bAction * id_action_ensure(Main *bmain, ID *id)
Channelbag & action_channelbag_ensure(bAction &dna_action, ID &animated_id)
void vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
void set_fcurve_sample(FCurve *fcu, int64_t sample_index, const float frame, const float value)
Map< const Object *, pxr::SdfPath > ObjExportMap
void shape_key_export_chaser(pxr::UsdStageRefPtr stage, const ObjExportMap &shape_key_mesh_export_map)
void import_mesh_skel_bindings(Object *mesh_obj, const pxr::UsdPrim &prim, ReportList *reports)
void set_fcurve_sample(FCurve *fcu, int64_t sample_index, const float frame, const float value)
void skel_export_chaser(pxr::UsdStageRefPtr stage, const ObjExportMap &armature_export_map, const ObjExportMap &skinned_mesh_export_map, const ObjExportMap &shape_key_mesh_export_map, const Depsgraph *depsgraph)
void export_deform_verts(const Mesh *mesh, const pxr::UsdSkelBindingAPI &skel_api, const Span< StringRef > bone_names)
void remap_blend_shape_anim(pxr::UsdStageRefPtr stage, const pxr::SdfPath &skel_path, const pxr::SdfPathSet &mesh_paths)
static void set_rest_pose(Main *bmain, Object *arm_obj, bArmature *arm, const pxr::VtArray< pxr::GfMatrix4d > &bind_xforms, const pxr::VtTokenArray &joint_order, const Map< pxr::TfToken, std::string > &joint_to_bone_map, const pxr::UsdSkelTopology &skel_topology, const pxr::UsdSkelSkeletonQuery &skel_query)
const pxr::TfToken BlenderBoneLengths("blender:bone_lengths", pxr::TfToken::Immortal)
void import_blendshapes(Main *bmain, Object *mesh_obj, const pxr::UsdPrim &prim, ReportList *reports, const bool import_anim)
void skinned_mesh_export_chaser(pxr::UsdStageRefPtr stage, const ObjExportMap &armature_export_map, const ObjExportMap &skinned_mesh_export_map, pxr::UsdGeomXformCache &xf_cache, const Depsgraph *depsgraph)
void ensure_blend_shape_skeleton(pxr::UsdStageRefPtr stage, pxr::UsdPrim &mesh_prim)
pxr::TfToken TempBlendShapeWeightsPrimvarName
void import_skeleton(Main *bmain, Object *arm_obj, const pxr::UsdSkelSkeleton &skel, ReportList *reports, const bool import_anim)
const Object * get_armature_modifier_obj(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)
struct MDeformVert * BKE_object_defgroup_data_create(struct ID *id)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void ED_armature_to_edit(bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
void ED_armature_ebone_from_mat4(EditBone *ebone, const float mat[4][4])
void BKE_fcurve_handles_recalc(FCurve &fcu)
Key * BKE_key_add(Main *bmain, ID *id)
BMesh const char void * data
ModifierData * BKE_modifier_new(int type)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
struct bDeformGroup * BKE_object_defgroup_add_name(struct Object *ob, const char *name)
void ED_armature_edit_free(bArmature *arm)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
MINLINE void add_v3_v3(float r[3], const float a[3])
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
EditBone * ED_armature_ebone_add(bArmature *arm, const char *name)
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
IDNewNameResult BKE_id_rename(Main &bmain, ID &id, StringRefNull name, const IDNewNameMode mode=IDNewNameMode::RenameExistingNever)
void BKE_fcurve_bezt_resize(FCurve &fcu, int new_totvert)
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
ShapekeyContainerType type
ListBaseT< ModifierData > modifiers