37 const pxr::SdfPath &proto_path,
38 const pxr::SdfPath &ref_path)
40 pxr::UsdPrim proto_prim = stage->GetPrimAtPath(proto_path);
43 CLOG_ERROR(&
LOG,
"Couldn't find prototype prim %s", proto_path.GetAsString().c_str());
48 pxr::SdfPathVector child_paths;
49 pxr::UsdPrimSiblingRange children = proto_prim.GetFilteredChildren(
50 pxr::Usd_PrimFlagsPredicate());
51 for (
const auto &child_prim : children) {
52 child_paths.push_back(child_prim.GetPath());
56 for (
const pxr::SdfPath &child_path : child_paths) {
57 stage->RemovePrim(child_path);
60 proto_prim.GetReferences().AddInternalReference(pxr::SdfPath(ref_path));
61 proto_prim.SetInstanceable(
true);
75 pxr::UsdPrimRange range(stage->GetPseudoRoot());
76 for (pxr::UsdPrim prim : range) {
77 if (prim.IsInstanceable()) {
79 pxr::UsdPrimCompositionQuery query = pxr::UsdPrimCompositionQuery::GetDirectReferences(prim);
81 for (
const auto &arc : query.GetCompositionArcs()) {
82 pxr::SdfPath target_prim_path = arc.GetTargetPrimPath();
83 protos.
add(target_prim_path);
84 references.
append(target_prim_path);
86 references_map.
add(prim.GetPath(), references);
99 protos_root_str +=
"/prototypes";
100 pxr::SdfPath protos_root_path =
get_unique_path(stage, protos_root_str);
103 if (!stage->CreateClassPrim(protos_root_path)) {
104 CLOG_ERROR(&
LOG,
"Couldn't create class prim %s.", protos_root_path.GetAsString().c_str());
112 for (
const pxr::SdfPath &proto_path : protos) {
113 pxr::SdfPath copy_path = protos_root_path;
115 copy_path = copy_path.AppendChild(proto_path.GetNameToken());
119 static pxr::TfToken xform_type_tok(
"Xform");
120 pxr::UsdPrim dest_prim = stage->DefinePrim(copy_path, xform_type_tok);
123 "Couldn't create destination prim %s for copying prototype %s",
124 copy_path.GetAsString().c_str(),
125 proto_path.GetAsString().c_str());
130 proto_to_copy_map.insert(std::make_pair(proto_path, dest_prim.GetPath()));
134 for (
const auto item : references_map.
items()) {
135 pxr::SdfPath inst_path = item.key;
136 pxr::UsdPrim inst_prim = stage->GetPrimAtPath(item.key);
138 CLOG_ERROR(&
LOG,
"Couldn't get prim for instance %s.", inst_path.GetAsString().c_str());
145 for (
const pxr::SdfPath &target_path : ref_targets) {
146 PathMap::const_iterator iter = proto_to_copy_map.find(target_path);
147 if (iter != proto_to_copy_map.end()) {
148 new_ref_targets.
append(iter->second);
154 pxr::UsdReferences refs = inst_prim.GetReferences();
155 refs.ClearReferences();
156 for (
const pxr::SdfPath &target : new_ref_targets) {
157 refs.AddInternalReference(target);
168 for (PathMap::reverse_iterator riter = proto_to_copy_map.rbegin();
169 riter != proto_to_copy_map.rend();
172 const pxr::SdfPath &src_path = riter->first;
173 const pxr::SdfPath &dst_path = riter->second;
174 if (!pxr::SdfCopySpec(
175 stage->GetRootLayer(), riter->first, stage->GetRootLayer(), riter->second))
178 "Couldn't copy prim %s to %s",
179 src_path.GetAsString().c_str(),
180 dst_path.GetAsString().c_str());