Blender V5.3
usd_skel_root_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 NVIDIA Corporation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include <pxr/usd/usd/primRange.h>
8#include <pxr/usd/usdGeom/xform.h>
9#include <pxr/usd/usdSkel/bindingAPI.h>
10#include <pxr/usd/usdSkel/root.h>
11
12#include "BKE_report.hh"
13
14#include "WM_types.hh"
15
16#include "CLG_log.h"
17
18namespace blender {
19
20static CLG_LogRef LOG = {"io.usd"};
21
22/* Utility: return the common Xform ancestor of the given prims. Is no such ancestor can
23 * be found, return an in valid Xform. */
24static pxr::UsdGeomXform get_xform_ancestor(const pxr::UsdPrim &prim1, const pxr::UsdPrim &prim2)
25{
26 if (!prim1 || !prim2) {
27 return pxr::UsdGeomXform();
28 }
29
30 pxr::SdfPath prefix = prim1.GetPath().GetCommonPrefix(prim2.GetPath());
31
32 if (prefix.IsEmpty()) {
33 return pxr::UsdGeomXform();
34 }
35
36 pxr::UsdPrim ancestor = prim1.GetStage()->GetPrimAtPath(prefix);
37
38 if (!ancestor) {
39 return pxr::UsdGeomXform();
40 }
41
42 while (ancestor && !ancestor.IsA<pxr::UsdGeomXform>()) {
43 ancestor = ancestor.GetParent();
44 }
45
46 if (ancestor && ancestor.IsA<pxr::UsdGeomXform>()) {
47 return pxr::UsdGeomXform(ancestor);
48 }
49
50 return pxr::UsdGeomXform();
51}
52
53namespace io::usd {
54
55void create_skel_roots(pxr::UsdStageRefPtr stage, const USDExportParams &params)
56{
57 if (!stage || !(params.export_armatures || params.export_shapekeys)) {
58 return;
59 }
60
61 ReportList *reports = params.worker_status ? params.worker_status->reports : nullptr;
62
63 /* Whether we converted any prims to UsdSkel. */
64 bool converted_to_usdskel = false;
65
66 pxr::UsdPrimRange it = stage->Traverse();
67 for (pxr::UsdPrim prim : it) {
68
69 if (!prim) {
70 continue;
71 }
72
73 if (prim.IsA<pxr::UsdSkelSkeleton>() || !prim.HasAPI<pxr::UsdSkelBindingAPI>()) {
74 continue;
75 }
76
77 pxr::UsdSkelBindingAPI skel_bind_api(prim);
78
79 if (!skel_bind_api) {
81 "Couldn't apply UsdSkelBindingAPI to prim %s",
82 prim.GetPath().GetAsString().c_str());
83 continue;
84 }
85
86 /* If we got here, then this prim has the skel binding API. */
87
88 /* Get this prim's bound skeleton. */
89 pxr::UsdSkelSkeleton skel;
90 if (!skel_bind_api.GetSkeleton(&skel)) {
91 continue;
92 }
93
94 if (!skel.GetPrim().IsValid()) {
95 CLOG_WARN(&LOG, "Invalid skeleton for prim %s", prim.GetPath().GetAsString().c_str());
96 continue;
97 }
98
99 /* Try to find a common ancestor of the skinned prim and its bound skeleton. */
100 pxr::UsdSkelRoot prim_skel_root = pxr::UsdSkelRoot::Find(prim);
101 pxr::UsdSkelRoot skel_skel_root = pxr::UsdSkelRoot::Find(skel.GetPrim());
102
103 if (prim_skel_root && skel_skel_root && prim_skel_root.GetPath() == skel_skel_root.GetPath()) {
104 continue;
105 }
106
107 if (pxr::UsdGeomXform xf = get_xform_ancestor(prim, skel.GetPrim())) {
108 /* We found a common Xform ancestor, so we set its type to UsdSkelRoot. */
110 &LOG, "Converting Xform prim %s to a SkelRoot", prim.GetPath().GetAsString().c_str());
111
112 pxr::UsdSkelRoot::Define(stage, xf.GetPath());
113 converted_to_usdskel = true;
114 }
115 else {
116 BKE_reportf(reports,
118 "%s: Couldn't find a common Xform ancestor for skinned prim %s "
119 "and skeleton %s to convert to a USD SkelRoot. "
120 "This can be addressed by setting a root primitive in the export options",
121 __func__,
122 prim.GetPath().GetAsString().c_str(),
123 skel.GetPath().GetAsString().c_str());
124 }
125 }
126
127 if (!converted_to_usdskel) {
128 return;
129 }
130
131 /* Check for nested SkelRoots, i.e., SkelRoots beneath other SkelRoots, which we want to avoid.
132 */
133 it = stage->Traverse();
134 for (pxr::UsdPrim prim : it) {
135 if (prim.IsA<pxr::UsdSkelRoot>()) {
136 if (pxr::UsdSkelRoot root = pxr::UsdSkelRoot::Find(prim.GetParent())) {
137 /* This is a nested SkelRoot, so convert it to an Xform. */
138 pxr::UsdGeomXform::Define(stage, prim.GetPath());
139 }
140 }
141 }
142}
143
144} // namespace io::usd
145} // namespace blender
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
void create_skel_roots(pxr::UsdStageRefPtr stage, const USDExportParams &params)
BMesh Iterators.
@ RPT_WARNING
Definition BKE_report.hh:40
static pxr::UsdGeomXform get_xform_ancestor(const pxr::UsdPrim &prim1, const pxr::UsdPrim &prim2)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static CLG_LogRef LOG
Definition logging.cpp:12
wmJobWorkerStatus * worker_status
Definition usd.hh:184