-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Pxr] Add UsdStage::GetObjectAtPath. #390
[Pxr] Add UsdStage::GetObjectAtPath. #390
Conversation
e74f0c4
to
5efc44c
Compare
Filed as internal issue #156422. |
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.
Passing along some review notes, thanks!
pxr/usd/lib/usd/stage.h
Outdated
/// Suggested use: | ||
/// | ||
/// \code | ||
/// if (UsdObject myObj = stage.GetObjectAtPath(path)) { |
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.
We can just use UsdObject::As in this example, since As is documented to return an invalid object if it's called on an invalid object, like:
if (UsdObject obj = stage->GetObjectAtPath(path)) {
if (UsdPrim prim = obj.As<UsdPrim>()) {
// Do things with prim
}
else if (UsdProperty prop = obj.As<UsdProperty>()) {
// Do things with property. We can also cast to
// UsdRelationship or UsdAttribute using this same pattern.
}
}
else {
// No object at specified path
}
pxr/usd/lib/usd/stage.h
Outdated
/// proxy prim if a prim exists at the corresponding path in that instance's | ||
/// master. | ||
/// | ||
/// Suggested use: |
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.
Instead of 'Suggested use:', can we say 'Example:' instead?
pxr/usd/lib/usd/stage.cpp
Outdated
} | ||
|
||
bool isPrimPath = path.IsPrimPath(); | ||
bool isPropPath = path.IsPropertyPath(); |
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.
Can we make these bools const? Also, can we avoid calling IsPropertyPath() if isPrimPath is already true:
const bool isPrimPath = path.IsPrimPath();
const bool isPropPath = !isPrimPath && path.IsPropertyPath();
pxr/usd/lib/usd/stage.cpp
Outdated
} | ||
|
||
// A valid prim must be found to return either a prim or prop | ||
auto prim = GetPrimAtPath(path.GetPrimPath()); |
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.
Can we avoid calling GetPrimPath() on paths we already know are prim paths?
pxr/usd/lib/usd/stage.cpp
Outdated
auto prim = GetPrimAtPath(path.GetPrimPath()); | ||
if (prim) { | ||
if (isPrimPath) { | ||
return prim.As<UsdObject>(); |
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.
Shouldn't need to explicitly cast to UsdObject using As() here and below, the implicit derived-to-base conversion should be sufficient.
@@ -700,6 +700,33 @@ class UsdStage : public TfRefBase, public TfWeakBase { | |||
USD_API | |||
UsdPrim GetPrimAtPath(const SdfPath &path) const; | |||
|
|||
/// Return the UsdObject at \p path, or an invalid UsdObject if none exists. | |||
/// | |||
/// If \p path indicates a prim beneath an instance, returns an instance |
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.
Can we add to this doc to mention properties and instance proxies as well? Something like,
If \p path indicates a property beneath a child of an instance, returns a property
whose parent prim is an instance proxy prim.
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.
Thanks for the notes, I'll get an updated branch pushed up this weekend.
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.
Sorry for the delay @sunyab, updated
5efc44c
to
c8dce55
Compare
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.
Sorry for the delay, just have a few small follow-up notes. Thanks!
pxr/usd/lib/usd/stage.cpp
Outdated
|
||
// A valid prim must be found to return either a prim or prop | ||
if (isPrimPath) { | ||
if (auto prim = GetPrimAtPath(path)) { |
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.
I think we can just return GetPrimAtPath(path) here and avoid unnecessarily checking the validity of the prim here.
pxr/usd/lib/usd/stage.cpp
Outdated
} | ||
} else if (isPropPath) { | ||
if (auto prim = GetPrimAtPath(path.GetPrimPath())) { | ||
if (auto prop = prim.GetProperty(path.GetNameToken())) { |
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.
Similar here; we need to check the validity of the prim, but we should be able to just return prim.GetProperty(...) and not need to check the validity of the property object before returning it.
c8dce55
to
b179b9c
Compare
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.
testUsdPrims is currently failing on the asserts I commented on below. I tested the fixes I noted in the comments and verified they work, so if you can make these changes I should be able to (finally) merge this in. Thanks!
# check for prims/props that dont exist | ||
p = s.GetObjectAtPath(u'/nonexistent') | ||
assert not p | ||
assert type(p) is Usd.Object |
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.
These asserts are failing now. I think this assert needs to be:
assert type(p) is Usd.Prim
|
||
a = s.GetObjectAtPath(u'/foo.nonexistentattr') | ||
assert not a | ||
assert type(a) is Usd.Object |
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.
assert type(p) is Usd.Property
|
||
r = s.GetObjectAtPath(u'/foo.nonexistentrelationship') | ||
assert not r | ||
assert type(r) is Usd.Object |
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.
assert type(r) is Usd.Property
Sorry about that! I forgot CI isn't running the tests, I'll get this pushed in a few |
b179b9c
to
f4b05ce
Compare
This allows a convenient way to get items in a stage from paths, whether they are property paths or prim paths.
f4b05ce
to
069c4b1
Compare
Ok @sunyab should be good to go. I also smashed it down into one commit. |
[Pxr] Add UsdStage::GetObjectAtPath.
Description of Change(s)
This allows a convenient way to get items in a stage from paths,
whether they are property paths or prim paths.
Fixes Issue(s)
This was requested on the interest forum a while back, don't have the link handy.