-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3190 from Autodesk/bailp/EMSUSD-243/ref-without-d…
…efault-prim EMSUSD-243 load reference and payload without default prim
- Loading branch information
Showing
9 changed files
with
279 additions
and
76 deletions.
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.