Skip to content
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

adapt to refactoring in core USD of registry handling of prim specs versus prim definitions #738

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ See Pixar's official github page for instructions on how to build USD: https://g

| | ![](images/pxr.png) |
|:------------: |:---------------: |
| CommitID/Tags | release: [v19.11](https://github.com/PixarAnimationStudios/USD/releases/tag/v19.11) or [v20.02](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.02) or [v20.05](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.05) or [v20.08](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.08) <br> dev: [8a13d20](https://github.com/PixarAnimationStudios/USD/commit/8a13d20062d14d486b7e130d252c4652aab0263e) |
| CommitID/Tags | release: [v19.11](https://github.com/PixarAnimationStudios/USD/releases/tag/v19.11) or [v20.02](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.02) or [v20.05](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.05) or [v20.08](https://github.com/PixarAnimationStudios/USD/releases/tag/v20.08) <br> dev: [5d1771d](https://github.com/PixarAnimationStudios/USD/commit/5d1771d23b935740bcd5bfb985a92a6652e070c0) |

For additional information on building Pixar USD, see the ***Additional Build Instruction*** section below.

Expand Down
113 changes: 88 additions & 25 deletions lib/mayaUsd/fileio/utils/adaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,12 @@ UsdMayaAdaptor::GetSchemaByName(const TfToken& schemaName) const
return SchemaAdaptor();
}

// Get the schema definition. If it's registered, there should be a def.
SdfPrimSpecHandle primSpec;

#if USD_VERSION_NUM > 2002
// Is this an API schema?
const UsdSchemaRegistry &schemaReg = UsdSchemaRegistry::GetInstance();
if (const UsdPrimDefinition *primDef =
schemaReg.FindAppliedAPIPrimDefinition(schemaName)) {
primSpec = primDef->GetSchemaPrimSpec();
return SchemaAdaptor(_handle.object(), schemaName, primDef);
}
// Is this a typed schema?
else if (const UsdPrimDefinition *primDef =
Expand All @@ -208,7 +205,7 @@ UsdMayaAdaptor::GetSchemaByName(const TfToken& schemaName) const
const TfToken objectTypeName = GetUsdTypeName();
if (schemaName == objectTypeName) {
// There's an exact MFn::Type match? Easy-peasy.
primSpec = primDef->GetSchemaPrimSpec();
return SchemaAdaptor(_handle.object(), schemaName, primDef);
}
else {
// If no match, do not allow usage of the typed-schema adaptor
Expand All @@ -218,6 +215,9 @@ UsdMayaAdaptor::GetSchemaByName(const TfToken& schemaName) const
}
}
#else
// Get the schema definition. If it's registered, there should be a def.
SdfPrimSpecHandle primSpec;

// Get the schema's TfType; its name should be registered as an alias.
const TfType schemaType =
TfType::Find<UsdSchemaBase>().FindDerivedByName(schemaName);
Expand Down Expand Up @@ -251,11 +251,11 @@ UsdMayaAdaptor::GetSchemaByName(const TfToken& schemaName) const
return SchemaAdaptor();
}
}
#endif

if (primSpec) {
return SchemaAdaptor(_handle.object(), primSpec);
}
#endif

// We shouldn't be able to reach this (everything is either typed or API).
TF_CODING_ERROR("'%s' isn't a known API or typed schema",
Expand Down Expand Up @@ -343,7 +343,19 @@ UsdMayaAdaptor::ApplySchemaByName(
return SchemaAdaptor();
}

SdfPrimSpecHandle primSpec = primDef->GetSchemaPrimSpec();
// Add to schema list (if not yet present).
TfTokenVector currentSchemas = GetAppliedSchemas();
if (std::find(currentSchemas.begin(), currentSchemas.end(), schemaName) ==
currentSchemas.end()) {
currentSchemas.push_back(schemaName);
SetMetadata(
UsdTokens->apiSchemas,
_GetListOpForTokenVector(currentSchemas),
modifier);
}

return SchemaAdaptor(_handle.object(), schemaName, primDef);

#else
// Get the schema's TfType; its name should be registered as an alias.
const TfType schemaType =
Expand All @@ -366,8 +378,6 @@ UsdMayaAdaptor::ApplySchemaByName(
// Get the schema definition. If it's registered, there should be a def.
SdfPrimSpecHandle primSpec =
UsdSchemaRegistry::GetInstance().GetPrimDefinition(schemaName);
#endif

if (!primSpec) {
TF_CODING_ERROR("Can't find schema definition for name '%s'",
schemaName.GetText());
Expand All @@ -386,6 +396,7 @@ UsdMayaAdaptor::ApplySchemaByName(
}

return SchemaAdaptor(_handle.object(), primSpec);
#endif
}

void
Expand Down Expand Up @@ -713,11 +724,23 @@ UsdMayaAdaptor::SchemaAdaptor::SchemaAdaptor()
{
}

#if USD_VERSION_NUM > 2002
UsdMayaAdaptor::SchemaAdaptor::SchemaAdaptor(
const MObjectHandle& handle,
const TfToken &schemaName,
const UsdPrimDefinition *schemaDef)
: _handle(handle), _schemaDef(schemaDef), _schemaName(schemaName)
{
}
#else
UsdMayaAdaptor::SchemaAdaptor::SchemaAdaptor(
const MObjectHandle& handle, SdfPrimSpecHandle schemaDef)
: _handle(handle), _schemaDef(schemaDef)
: _handle(handle)
, _schemaDef(schemaDef)
, _schemaName(schemaDef->GetNameToken())
{
}
#endif

UsdMayaAdaptor::SchemaAdaptor::operator bool() const
{
Expand All @@ -732,7 +755,7 @@ UsdMayaAdaptor::SchemaAdaptor::operator bool() const

std::string
UsdMayaAdaptor::SchemaAdaptor::_GetMayaAttrNameOrAlias(
const SdfAttributeSpecHandle& attrSpec) const
const TfToken& name) const
{
if (!*this) {
TF_CODING_ERROR("Schema adaptor is not valid");
Expand All @@ -745,7 +768,6 @@ UsdMayaAdaptor::SchemaAdaptor::_GetMayaAttrNameOrAlias(
MFnDependencyNode depNode(thisObject);

// If the generated name exists, it is the most preferred name,
const TfToken name = attrSpec->GetNameToken();
const std::string genName = _GetMayaAttrNameForAttrName(name);
if (depNode.hasAttribute(genName.c_str())) {
return genName;
Expand Down Expand Up @@ -784,24 +806,40 @@ UsdMayaAdaptor::SchemaAdaptor::GetName() const
return TfToken();
}

return _schemaDef->GetNameToken();
return _schemaName;
}

#if USD_VERSION_NUM > 2002
static
SdfAttributeSpecHandle
_GetAttributeSpec(const UsdPrimDefinition *primDef, const TfToken &attrName)
{
return primDef->GetSchemaAttributeSpec(attrName);
}
#else
static
SdfAttributeSpecHandle
_GetAttributeSpec(const SdfPrimSpecHandle &primSpec, const TfToken &attrName)
{
return primSpec->GetAttributes()[attrName];
}
#endif

UsdMayaAdaptor::AttributeAdaptor
UsdMayaAdaptor::SchemaAdaptor::GetAttribute(const TfToken& attrName) const
{
if (!*this) {
return AttributeAdaptor();
}

SdfAttributeSpecHandle attrDef = _schemaDef->GetAttributes()[attrName];
SdfAttributeSpecHandle attrDef = _GetAttributeSpec(_schemaDef, attrName);
if (!attrDef) {
TF_CODING_ERROR("Attribute '%s' doesn't exist on schema '%s'",
attrName.GetText(), _schemaDef->GetName().c_str());
attrName.GetText(), _schemaName.GetText());
return AttributeAdaptor();
}

std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrDef);
std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrName);
MFnDependencyNode node(_handle.object());
MPlug plug = node.findPlug(mayaAttrName.c_str());
if (plug.isNull()) {
Expand All @@ -827,14 +865,14 @@ UsdMayaAdaptor::SchemaAdaptor::CreateAttribute(
return AttributeAdaptor();
}

SdfAttributeSpecHandle attrDef = _schemaDef->GetAttributes()[attrName];
SdfAttributeSpecHandle attrDef = _GetAttributeSpec(_schemaDef, attrName);
if (!attrDef) {
TF_CODING_ERROR("Attribute '%s' doesn't exist on schema '%s'",
attrName.GetText(), _schemaDef->GetName().c_str());
attrName.GetText(), _schemaName.GetText());
return AttributeAdaptor();
}

std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrDef);
std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrName);
std::string mayaNiceAttrName = attrDef->GetName();
MFnDependencyNode node(_handle.object());

Expand Down Expand Up @@ -874,14 +912,14 @@ UsdMayaAdaptor::SchemaAdaptor::RemoveAttribute(
return;
}

SdfAttributeSpecHandle attrDef = _schemaDef->GetAttributes()[attrName];
SdfAttributeSpecHandle attrDef = _GetAttributeSpec(_schemaDef, attrName);
if (!attrDef) {
TF_CODING_ERROR("Attribute '%s' doesn't exist on schema '%s'",
attrName.GetText(), _schemaDef->GetName().c_str());
attrName.GetText(), _schemaName.GetText());
return;
}

std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrDef);
std::string mayaAttrName = _GetMayaAttrNameOrAlias(attrName);
MFnDependencyNode node(_handle.object());
if (node.hasAttribute(mayaAttrName.c_str())) {
MObject attr = node.attribute(mayaAttrName.c_str());
Expand All @@ -899,12 +937,23 @@ UsdMayaAdaptor::SchemaAdaptor::GetAuthoredAttributeNames() const

MFnDependencyNode node(_handle.object());
TfTokenVector result;
#if USD_VERSION_NUM > 2002
for (const TfToken& propName : _schemaDef->GetPropertyNames()) {
if (_schemaDef->GetSpecType(propName) == SdfSpecTypeAttribute) {
std::string mayaAttrName = _GetMayaAttrNameOrAlias(propName);
if (node.hasAttribute(mayaAttrName.c_str())) {
result.push_back(propName);
}
}
}
#else
for (const SdfAttributeSpecHandle& attr : _schemaDef->GetAttributes()) {
std::string mayaAttrName = _GetMayaAttrNameOrAlias(attr);
std::string mayaAttrName = _GetMayaAttrNameOrAlias(attr->GetNameToken());
if (node.hasAttribute(mayaAttrName.c_str())) {
result.push_back(attr->GetNameToken());
}
}
#endif

return result;
}
Expand All @@ -917,20 +966,34 @@ UsdMayaAdaptor::SchemaAdaptor::GetAttributeNames() const
}

TfTokenVector attrNames;
#if USD_VERSION_NUM > 2002
for (const TfToken& propName : _schemaDef->GetPropertyNames()) {
if (_schemaDef->GetSpecType(propName) == SdfSpecTypeAttribute) {
attrNames.push_back(propName);
}
}
#else
for (const SdfAttributeSpecHandle attr : _schemaDef->GetAttributes()) {
attrNames.push_back(attr->GetNameToken());
}
#endif

return attrNames;
}

#if USD_VERSION_NUM > 2002
const UsdPrimDefinition *
UsdMayaAdaptor::SchemaAdaptor::GetSchemaDefinition() const
{
return _schemaDef;
}
#else
const SdfPrimSpecHandle
UsdMayaAdaptor::SchemaAdaptor::GetSchemaDefinition() const
{
return _schemaDef;
}


#endif

UsdMayaAdaptor::AttributeAdaptor::AttributeAdaptor()
: _plug(), _node(), _attr(), _attrDef(nullptr)
Expand Down
24 changes: 21 additions & 3 deletions lib/mayaUsd/fileio/utils/adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,26 @@ class UsdMayaAdaptor {
/// import.
class SchemaAdaptor {
MObjectHandle _handle;
#if USD_VERSION_NUM > 2002
const UsdPrimDefinition *_schemaDef;
#else
SdfPrimSpecHandle _schemaDef;
#endif
TfToken _schemaName;

public:
MAYAUSD_CORE_PUBLIC
SchemaAdaptor();

#if USD_VERSION_NUM > 2002
MAYAUSD_CORE_PUBLIC
SchemaAdaptor(const MObjectHandle& object,
const TfToken &schemaName,
const UsdPrimDefinition *schemaPrimDef);
#else
MAYAUSD_CORE_PUBLIC
SchemaAdaptor(const MObjectHandle& object, SdfPrimSpecHandle schemaDef);
#endif

MAYAUSD_CORE_PUBLIC
explicit operator bool() const;
Expand Down Expand Up @@ -302,17 +314,23 @@ class UsdMayaAdaptor {
MAYAUSD_CORE_PUBLIC
TfTokenVector GetAttributeNames() const;

#if USD_VERSION_NUM > 2002
/// Gets the prim definition for this schema from the schema registry.
/// Returns a null pointer if this schema adaptor is invalid.
MAYAUSD_CORE_PUBLIC
const UsdPrimDefinition *GetSchemaDefinition() const;
#else
/// Gets the prim spec for this schema from the schema registry.
/// Returns a null handle if this schema adaptor is invalid.
MAYAUSD_CORE_PUBLIC
const SdfPrimSpecHandle GetSchemaDefinition() const;
#endif

private:
/// Gets the name of the adapted Maya attribute for the given attribute
/// definition. The name may come from the registered aliases if one
/// name. The name may come from the registered aliases if one
/// exists and is already present on the node.
std::string _GetMayaAttrNameOrAlias(
const SdfAttributeSpecHandle& attrSpec) const;
std::string _GetMayaAttrNameOrAlias(const TfToken& attrName) const;
};

MAYAUSD_CORE_PUBLIC
Expand Down