-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EMSUSD-243 load reference and payload without default prim #3190
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// | ||
// Copyright 2023 Autodesk | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
#include "UsdUndoAddRefOrPayloadCommand.h" | ||
|
||
#include <pxr/base/tf/stringUtils.h> | ||
#include <pxr/usd/sdf/layer.h> | ||
#include <pxr/usd/sdf/path.h> | ||
#include <pxr/usd/usd/payloads.h> | ||
#include <pxr/usd/usd/references.h> | ||
|
||
namespace USDUFE_NS_DEF { | ||
|
||
using namespace PXR_NS; | ||
|
||
static std::string validatePrimSpec(const UsdPrim& prim, const SdfPrimSpecHandle& primSpec) | ||
{ | ||
if (!primSpec) | ||
return "is not valid"; | ||
|
||
// A common error is to reference a prim that is not the same type as the prim | ||
// that contains the reference. Since only the type of the prim that contains | ||
// the reference is used, the referenced prim might not show up. | ||
// | ||
// This happens a lot when trying to reference geometry (mesh) instead of the | ||
// prim containing the geometry. Of vis-versa, referencing a prim inside a mesh. | ||
const std::string& primType = prim.GetTypeName(); | ||
const std::string& targetType = primSpec->GetTypeName(); | ||
if (primType != targetType) | ||
return TfStringPrintf( | ||
"does not have the same type as the targeted prim: [%s] vs [%s]", | ||
primType.c_str(), | ||
targetType.c_str()); | ||
|
||
return ""; | ||
} | ||
|
||
static PXR_NS::SdfPath | ||
getPrimPath(const UsdPrim& prim, const std::string& filePath, const std::string& primPath) | ||
{ | ||
// When an explicit prim path was given, use that. | ||
if (!primPath.empty()) | ||
return SdfPath(primPath); | ||
|
||
// If no prim path were specified and we are referencing a MaterialX file | ||
// then use the MaterialX prim as the target for the reference. | ||
// | ||
// TODO: should we force this even when the referenced file has a default prim? | ||
if (TfStringEndsWith(filePath, ".mtlx")) | ||
return SdfPath("/MaterialX"); | ||
|
||
// Retrieve the layer for analysis. | ||
// | ||
// Note: we don't print any warning if the layer cannot be found as we assume | ||
// the load itself will also fail and print an error. | ||
SdfLayerRefPtr layerRef = SdfLayer::FindOrOpen(filePath); | ||
if (!layerRef) | ||
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()); | ||
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()); | ||
|
||
std::string errorMessage; | ||
for (const SdfPrimSpecHandle primSpec : layerRef->GetRootPrims()) { | ||
if (!primSpec) | ||
continue; | ||
|
||
errorMessage = validatePrimSpec(prim, primSpec); | ||
if (errorMessage.empty()) | ||
return primSpec->GetPath(); | ||
} | ||
|
||
if (errorMessage.empty()) { | ||
TF_WARN("Could not find any valid root prim."); | ||
} else { | ||
TF_WARN("Could not find a valid root prim, the root prim %s.", errorMessage.c_str()); | ||
} | ||
|
||
return SdfPath(); | ||
} | ||
|
||
/* static */ UsdListPosition UsdUndoAddRefOrPayloadCommand::getListPosition(bool prepend) | ||
{ | ||
return prepend ? UsdListPositionBackOfPrependList : UsdListPositionBackOfAppendList; | ||
} | ||
|
||
UsdUndoAddRefOrPayloadCommand::UsdUndoAddRefOrPayloadCommand( | ||
const UsdPrim& prim, | ||
const std::string& filePath, | ||
const std::string& primPath, | ||
UsdListPosition listPos, | ||
bool isPayload) | ||
: _prim(prim) | ||
, _filePath(filePath) | ||
, _primPath(primPath) | ||
, _listPos(listPos) | ||
, _isPayload(isPayload) | ||
{ | ||
} | ||
|
||
void UsdUndoAddRefOrPayloadCommand::executeImplementation() | ||
{ | ||
if (!_prim.IsValid()) | ||
return; | ||
|
||
SdfPath primPath = getPrimPath(_prim, _filePath, _primPath); | ||
if (_isPayload) { | ||
SdfPayload payload(_filePath, primPath); | ||
UsdPayloads primPayloads = _prim.GetPayloads(); | ||
primPayloads.AddPayload(payload, _listPos); | ||
} else { | ||
SdfReference ref(_filePath, primPath); | ||
UsdReferences primRefs = _prim.GetReferences(); | ||
primRefs.AddReference(ref, _listPos); | ||
} | ||
} | ||
|
||
} // namespace USDUFE_NS_DEF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright 2023 Autodesk | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
#ifndef USD_UFE_ADD_REF_OR_PAYLOAD_COMMAND | ||
#define USD_UFE_ADD_REF_OR_PAYLOAD_COMMAND | ||
|
||
#include <usdUfe/base/api.h> | ||
#include <usdUfe/ufe/UsdUndoableCommand.h> | ||
|
||
#include <pxr/usd/usd/common.h> | ||
#include <pxr/usd/usd/prim.h> | ||
|
||
#include <ufe/undoableCommand.h> | ||
|
||
#include <string> | ||
|
||
namespace USDUFE_NS_DEF { | ||
|
||
//! \brief Command to add a reference or payload to a prim. | ||
class USDUFE_PUBLIC UsdUndoAddRefOrPayloadCommand : public UsdUndoableCommand<Ufe::UndoableCommand> | ||
{ | ||
public: | ||
UsdUndoAddRefOrPayloadCommand( | ||
const PXR_NS::UsdPrim& prim, | ||
const std::string& filePath, | ||
const std::string& primPath, | ||
PXR_NS::UsdListPosition listPos, | ||
bool isPayload); | ||
|
||
protected: | ||
static PXR_NS::UsdListPosition getListPosition(bool prepend); | ||
|
||
void executeImplementation() override; | ||
|
||
private: | ||
PXR_NS::UsdPrim _prim; | ||
std::string _filePath; | ||
std::string _primPath; | ||
PXR_NS::UsdListPosition _listPos; | ||
bool _isPayload; | ||
}; | ||
|
||
} // namespace USDUFE_NS_DEF | ||
|
||
#endif /* USD_UFE_ADD_REF_OR_PAYLOAD_COMMAND */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code that this refactor was already doing this. It was added as part of the LookDevX effort. We can revisit (that is remove) this once we have a UI to specify the prim we want to load.