Skip to content

Commit

Permalink
Merge pull request #3253 from Autodesk/bailp/EMSUSD-306/no-auto-defau…
Browse files Browse the repository at this point in the history
…lt-prim

EMSUSD-306 don't use a random default prim
  • Loading branch information
seando-adsk authored Aug 1, 2023
2 parents 0511b9e + ccd8f93 commit 903b807
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 72 deletions.
58 changes: 13 additions & 45 deletions lib/usdUfe/ufe/UsdUndoAddRefOrPayloadCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,11 @@ namespace USDUFE_NS_DEF {

PXR_NAMESPACE_USING_DIRECTIVE

static std::string validatePrimSpec(const UsdPrim& prim, const SdfPrimSpecHandle& primSpec)
{
if (!primSpec)
return "is not valid";

// A common error is to reference geometry (mesh) instead of the
// prim containing the geometry. Of vis-versa, referencing a prim inside a mesh.
// So we warn
const std::string& primType = prim.GetTypeName();
const std::string& targetType = primSpec->GetTypeName();
if (primType != "Mesh" && targetType == "Mesh")
return "is referencing a mesh into a non-mesh, the mesh may not be visible";

return "";
}

static PXR_NS::SdfPath
getPrimPath(const UsdPrim& prim, const std::string& filePath, const std::string& primPath)
static PXR_NS::SdfPath getPrimPath(
const UsdPrim& prim,
const std::string& filePath,
const std::string& primPath,
bool isPayload)
{
// When an explicit prim path was given, use that.
if (!primPath.empty())
Expand All @@ -64,35 +51,16 @@ getPrimPath(const UsdPrim& prim, const std::string& filePath, const std::string&
return SdfPath();

// If the referenced file has a default prim, leave the prim path empty.
if (layerRef->HasDefaultPrim()) {
TfToken primName = layerRef->GetDefaultPrim();
SdfPrimSpecHandle primSpec = layerRef->GetPrimAtPath(SdfPath(primName.GetText()));
const std::string errorMessage = validatePrimSpec(prim, primSpec);
if (!errorMessage.empty())
TF_WARN("The default prim in file [%s] %s.", filePath.c_str(), errorMessage.c_str());
if (layerRef->HasDefaultPrim())
return SdfPath();
}

// If the referenced file has no default prim, return the path to the first
// valid root prim we find.

TF_STATUS(
"The file [%s] does not contain a default prim, the first valid root prim "
"will be used.",
filePath.c_str());

for (const SdfPrimSpecHandle primSpec : layerRef->GetRootPrims()) {
if (!primSpec)
continue;

const std::string errorMessage = validatePrimSpec(prim, primSpec);
if (!errorMessage.empty())
TF_WARN("The root prim %s.", errorMessage.c_str());

return primSpec->GetPath();
}
// If the referenced file has no default prim, tell the user it won't be
// loaded properly.
TF_WARN(
"The file [%s] does not contain a default prim, the %s will be invalid.",
filePath.c_str(),
isPayload ? "payload" : "reference");

TF_WARN("Could not find any valid root prim.");
return SdfPath();
}

Expand Down Expand Up @@ -120,7 +88,7 @@ void UsdUndoAddRefOrPayloadCommand::executeImplementation()
if (!_prim.IsValid())
return;

SdfPath primPath = getPrimPath(_prim, _filePath, _primPath);
SdfPath primPath = getPrimPath(_prim, _filePath, _primPath, _isPayload);
if (_isPayload) {
SdfPayload payload(_filePath, primPath);
UsdPayloads primPayloads = _prim.GetPayloads();
Expand Down
27 changes: 0 additions & 27 deletions test/lib/ufe/testReferenceCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,6 @@ def testAddAndClearReferenceCommands(self):
self.assertFalse(prim.HasAuthoredReferences())
self.assertEqual(originalRootContents, filterUsdStr(self.stage.GetRootLayer().ExportToString()))

def testAddAReferenceWithoutDefaultPrim(self):
'''
Test add reference command when the file has no default prim.
'''
# Get the session layer
prim = mayaUsd.ufe.ufePathToPrim("|stage1|stageShape1,/A")

self.assertFalse(prim.HasAuthoredReferences())
originalRootContents = filterUsdStr(self.stage.GetRootLayer().ExportToString())

referencedFile = testUtils.getTestScene('twoSpheres', 'sphere_no_default.usda')
cmd = usdUfe.AddReferenceCommand(prim, referencedFile, True)

cmd.execute()
self.assertTrue(prim.HasAuthoredReferences())
self.assertIsNotNone(self.stage.GetPrimAtPath('/A/sphere'))
self.assertTrue(self.stage.GetPrimAtPath('/A/sphere'))

cmd.undo()
self.assertFalse(prim.HasAuthoredReferences())
self.assertEqual(originalRootContents, filterUsdStr(self.stage.GetRootLayer().ExportToString()))

cmd.redo()
self.assertTrue(prim.HasAuthoredReferences())
self.assertIsNotNone(self.stage.GetPrimAtPath('/A/sphere'))
self.assertTrue(self.stage.GetPrimAtPath('/A/sphere'))


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 903b807

Please sign in to comment.