39 const pxr::SdfPath &proto_path,
40 const pxr::SdfPath &ref_path)
42 pxr::UsdPrim proto_prim = stage->GetPrimAtPath(proto_path);
45 CLOG_ERROR(&
LOG,
"Couldn't find prototype prim %s", proto_path.GetAsString().c_str());
50 pxr::SdfPathVector child_paths;
51 pxr::UsdPrimSiblingRange children = proto_prim.GetFilteredChildren(
52 pxr::Usd_PrimFlagsPredicate());
53 for (
const auto &child_prim : children) {
54 child_paths.push_back(child_prim.GetPath());
58 for (
const pxr::SdfPath &child_path : child_paths) {
59 stage->RemovePrim(child_path);
62 proto_prim.GetReferences().AddInternalReference(pxr::SdfPath(ref_path));
63 proto_prim.SetInstanceable(
true);
77 pxr::UsdPrimRange range(stage->GetPseudoRoot());
78 for (pxr::UsdPrim prim : range) {
79 if (prim.IsInstanceable()) {
81 pxr::UsdPrimCompositionQuery query = pxr::UsdPrimCompositionQuery::GetDirectReferences(prim);
83 for (
const auto &arc : query.GetCompositionArcs()) {
84 pxr::SdfPath target_prim_path = arc.GetTargetPrimPath();
85 protos.
add(target_prim_path);
86 references.
append(target_prim_path);
88 references_map.
add(prim.GetPath(), references);
101 protos_root_str +=
"/prototypes";
102 pxr::SdfPath protos_root_path =
get_unique_path(stage, protos_root_str);
105 if (!stage->CreateClassPrim(protos_root_path)) {
106 CLOG_ERROR(&
LOG,
"Couldn't create class prim %s.", protos_root_path.GetAsString().c_str());
114 for (
const pxr::SdfPath &proto_path : protos) {
115 pxr::SdfPath copy_path = protos_root_path;
117 copy_path = copy_path.AppendChild(proto_path.GetNameToken());
121 static pxr::TfToken xform_type_tok(
"Xform");
122 pxr::UsdPrim dest_prim = stage->DefinePrim(copy_path, xform_type_tok);
125 "Couldn't create destination prim %s for copying prototype %s",
126 copy_path.GetAsString().c_str(),
127 proto_path.GetAsString().c_str());
132 proto_to_copy_map.insert(std::make_pair(proto_path, dest_prim.GetPath()));
136 for (
const auto item : references_map.
items()) {
137 pxr::SdfPath inst_path = item.key;
138 pxr::UsdPrim inst_prim = stage->GetPrimAtPath(item.key);
140 CLOG_ERROR(&
LOG,
"Couldn't get prim for instance %s.", inst_path.GetAsString().c_str());
147 for (
const pxr::SdfPath &target_path : ref_targets) {
148 PathMap::const_iterator iter = proto_to_copy_map.find(target_path);
149 if (iter != proto_to_copy_map.end()) {
150 new_ref_targets.
append(iter->second);
156 pxr::UsdReferences refs = inst_prim.GetReferences();
157 refs.ClearReferences();
158 for (
const pxr::SdfPath &target : new_ref_targets) {
159 refs.AddInternalReference(target);
170 for (PathMap::reverse_iterator riter = proto_to_copy_map.rbegin();
171 riter != proto_to_copy_map.rend();
174 const pxr::SdfPath &src_path = riter->first;
175 const pxr::SdfPath &dst_path = riter->second;
176 if (!pxr::SdfCopySpec(
177 stage->GetRootLayer(), riter->first, stage->GetRootLayer(), riter->second))
180 "Couldn't copy prim %s to %s",
181 src_path.GetAsString().c_str(),
182 dst_path.GetAsString().c_str());