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

Support namespaces in propertyNamespacePrefix for usdGenSchema #3090

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
31 changes: 10 additions & 21 deletions pxr/usd/usd/codegenTemplates/schemaClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
#include "pxr/usd/sdf/types.h"
#include "pxr/usd/sdf/assetPath.h"

{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
#include "pxr/base/tf/staticTokens.h"

{% endif %}
{% if useExportAPI %}
{{ namespaceOpen }}

Expand All @@ -52,13 +48,6 @@ TF_REGISTRY_FUNCTION(TfType)
{% endif %}
}

{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
TF_DEFINE_PRIVATE_TOKENS(
_schemaTokens,
({{ cls.propertyNamespacePrefix }})
);

{% endif %}
/* virtual */
{{ cls.cppClassName }}::~{{ cls.cppClassName }}()
{
Expand All @@ -73,10 +62,10 @@ TF_DEFINE_PRIVATE_TOKENS(
TF_CODING_ERROR("Invalid stage");
return {{ cls.cppClassName }}();
}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
TfToken name;
if (!Is{{ cls.usdPrimTypeName }}Path(path, &name)) {
TF_CODING_ERROR("Invalid {{ cls.propertyNamespacePrefix }} path <%s>.", path.GetText());
TF_CODING_ERROR("Invalid {{ cls.propertyNamespace.prefix }} path <%s>.", path.GetText());
return {{ cls.cppClassName }}();
}
return {{ cls.cppClassName }}(stage->GetPrimAtPath(path.GetPrimPath()), name);
Expand Down Expand Up @@ -123,7 +112,7 @@ std::vector<{{ cls.cppClassName }}>
stage->DefinePrim(path, usdPrimTypeName));
}
{% endif %}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}

/* static */
bool
Expand Down Expand Up @@ -167,9 +156,9 @@ bool
}

if (tokens.size() >= 2
&& tokens[0] == _schemaTokens->{{ cls.propertyNamespacePrefix }}) {
&& tokens[0] == {{ tokensPrefix }}Tokens->{{ cls.propertyNamespace.token }}) {
*name = TfToken(propertyName.substr(
_schemaTokens->{{ cls.propertyNamespacePrefix }}.GetString().size() + 1));
{{ tokensPrefix }}Tokens->{{ cls.propertyNamespace.token }}.GetString().size() + 1));
return true;
}

Expand Down Expand Up @@ -244,7 +233,7 @@ const TfType &
{
return _GetStaticTfType();
}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}

/// Returns the property name prefixed with the correct namespace prefix, which
/// is composed of the the API's propertyNamespacePrefix metadata and the
Expand All @@ -265,7 +254,7 @@ _GetNamespacedPropertyName(const TfToken instanceName, const TfToken propName)
UsdAttribute
{{ cls.cppClassName }}::Get{{ Proper(attr.apiName) }}Attr() const
{
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
return GetPrim().GetAttribute(
_GetNamespacedPropertyName(
GetName(),
Expand All @@ -279,7 +268,7 @@ UsdAttribute
UsdAttribute
{{ cls.cppClassName }}::Create{{ Proper(attr.apiName) }}Attr(VtValue const &defaultValue, bool writeSparsely) const
{
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
return UsdSchemaBase::_CreateAttr(
_GetNamespacedPropertyName(
GetName(),
Expand All @@ -304,7 +293,7 @@ UsdAttribute
UsdRelationship
{{ cls.cppClassName }}::Get{{ Proper(rel.apiName) }}Rel() const
{
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
return GetPrim().GetRelationship(
_GetNamespacedPropertyName(
GetName(),
Expand All @@ -318,7 +307,7 @@ UsdRelationship
UsdRelationship
{{ cls.cppClassName }}::Create{{ Proper(rel.apiName) }}Rel() const
{
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
return GetPrim().CreateRelationship(
_GetNamespacedPropertyName(
GetName(),
Expand Down
8 changes: 4 additions & 4 deletions pxr/usd/usd/codegenTemplates/schemaClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class {{ cls.cppClassName }} : public {{ cls.parentCppClassName }}
/// {{ cls.cppClassName }}::Get(
/// prim.GetStage(),
/// prim.GetPath().AppendProperty(
/// "{{ cls.propertyNamespacePrefix }}:name"));
/// "{{ cls.propertyNamespace.prefix }}:name"));
///
/// for a \em valid \p prim, but will not immediately throw an error for
/// an invalid \p prim
Expand Down Expand Up @@ -161,9 +161,9 @@ class {{ cls.cppClassName }} : public {{ cls.parentCppClassName }}
/// Return a {{ cls.cppClassName }} holding the prim adhering to this
/// schema at \p path on \p stage. If no prim exists at \p path on
/// \p stage, or if the prim at that path does not adhere to this schema,
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
/// return an invalid schema object. \p path must be of the format
/// <path>.{{ cls.propertyNamespacePrefix }}:name .
/// <path>.{{ cls.propertyNamespace.prefix }}:name .
///
/// This is shorthand for the following:
///
Expand Down Expand Up @@ -234,7 +234,7 @@ class {{ cls.cppClassName }} : public {{ cls.parentCppClassName }}
static {{ cls.cppClassName }}
Define(const UsdStagePtr &stage, const SdfPath &path);
{% endif %}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
/// Checks if the given name \p baseName is the base name of a property
/// of {{ cls.usdPrimTypeName }}.
{% if useExportAPI -%}
Expand Down
4 changes: 2 additions & 2 deletions pxr/usd/usd/codegenTemplates/wrapSchemaClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ _Create{{ Proper(attr.apiName) }}Attr({{ cls.cppClassName }} &self,
}
{% endif %}
{% endfor %}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}

static bool _WrapIs{{ cls.usdPrimTypeName }}Path(const SdfPath &path) {
TfToken collectionName;
Expand Down Expand Up @@ -254,7 +254,7 @@ void wrap{{ cls.cppClassName }}()
&This::Create{{ Proper(rel.apiName) }}Rel)
{% endif %}
{% endfor %}
{% if cls.isMultipleApply and cls.propertyNamespacePrefix %}
{% if cls.isMultipleApply and cls.propertyNamespace %}
.def("Is{{ cls.usdPrimTypeName }}Path", _WrapIs{{ cls.usdPrimTypeName }}Path)
.staticmethod("Is{{ cls.usdPrimTypeName }}Path")
{% endif %}
Expand Down
11 changes: 2 additions & 9 deletions pxr/usd/usd/collectionAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "pxr/usd/sdf/types.h"
#include "pxr/usd/sdf/assetPath.h"

#include "pxr/base/tf/staticTokens.h"

PXR_NAMESPACE_OPEN_SCOPE

// Register the schema with the TfType system.
Expand All @@ -40,11 +38,6 @@ TF_REGISTRY_FUNCTION(TfType)

}

TF_DEFINE_PRIVATE_TOKENS(
_schemaTokens,
(collection)
);

/* virtual */
UsdCollectionAPI::~UsdCollectionAPI()
{
Expand Down Expand Up @@ -131,9 +124,9 @@ UsdCollectionAPI::IsCollectionAPIPath(
}

if (tokens.size() >= 2
&& tokens[0] == _schemaTokens->collection) {
&& tokens[0] == UsdTokens->collection) {
*name = TfToken(propertyName.substr(
_schemaTokens->collection.GetString().size() + 1));
UsdTokens->collection.GetString().size() + 1));
return true;
}

Expand Down
42 changes: 28 additions & 14 deletions pxr/usd/usd/usdGenSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"""

from __future__ import print_function
import dataclasses
import sys, os, re, inspect
import keyword
from argparse import ArgumentParser
Expand Down Expand Up @@ -293,14 +294,14 @@ def _CamelCase(aString):
Token = namedtuple('Token', ['id', 'value', 'desc'])

def _GetNameAndGeneratedSchemaPropNameForPropInfo(sdfPropName, classInfo):
if classInfo.propertyNamespacePrefix:
if classInfo.propertyNamespace:
# A property namespace prefix will only exist for multiple apply API
# schemas and is used to create the instanceable namespace prefix
# prepended to all its properties. We prepend this instanceable
# prefix to the raw name here.
generatedSchemaPropName = \
Usd.SchemaRegistry.MakeMultipleApplyNameTemplate(
classInfo.propertyNamespacePrefix, sdfPropName)
classInfo.propertyNamespace.prefix, sdfPropName)
# Since the property info's name is used to create the identifier
# used for tokens and such, we make it from the instanced property
# name with the instance name placeholder replaced with
Expand Down Expand Up @@ -480,6 +481,17 @@ def _InheritsOwnFamily(p):
p.GetPath().name)
return family in allInheritedFamilies

@dataclasses.dataclass
class MultiApplyPropertyNamespace:
prefix : str
token : str

@classmethod
def Create(cls, prefix, useLiteralIdentifier):
return cls(prefix, _MakeValidToken(prefix, useLiteralIdentifier)) \
if prefix else None


class ClassInfo(object):
def __init__(self, usdPrim, sdfPrim, useLiteralIdentifier=False):
# First validate proper class naming...
Expand Down Expand Up @@ -601,8 +613,10 @@ def __init__(self, usdPrim, sdfPrim, useLiteralIdentifier=False):
not self.isAPISchemaBase and not self.isTypedBase
self.apiSchemaType = self.customData.get(API_SCHEMA_TYPE,
SINGLE_APPLY if self.isApi else None)
self.propertyNamespacePrefix = \
self.customData.get(PROPERTY_NAMESPACE_PREFIX)
self.propertyNamespace = MultiApplyPropertyNamespace.Create(
self.customData.get(PROPERTY_NAMESPACE_PREFIX),
useLiteralIdentifier
)
self.apiAutoApply = self.customData.get(API_AUTO_APPLY)
self.apiCanOnlyApply = self.customData.get(API_CAN_ONLY_APPLY)
self.apiAllowedInstanceNames = self.customData.get(
Expand All @@ -616,7 +630,7 @@ def __init__(self, usdPrim, sdfPrim, useLiteralIdentifier=False):
sdfPrim.path)

if self.apiSchemaType != MULTIPLE_APPLY:
if self.propertyNamespacePrefix:
if self.propertyNamespace:
raise _GetSchemaDefException(
"%s should only be used as a customData field on "
"multiple-apply API schemas." % PROPERTY_NAMESPACE_PREFIX,
Expand Down Expand Up @@ -795,7 +809,7 @@ def _MakeMultipleApplySchemaNameTemplate(apiSchemaName):
# use the USD prim because we need to know all override properties for the
# flattened schema class, so this will include any overrides provided purely
# through inheritance.
def _GetAPISchemaOverridePropertyNames(usdPrim, propertyNamespacePrefix):
def _GetAPISchemaOverridePropertyNames(usdPrim, propertyNamespace):
apiSchemaOverridePropertyNames = []

for usdProp in usdPrim.GetProperties():
Expand Down Expand Up @@ -845,9 +859,9 @@ def _GetAPISchemaOverridePropertyNames(usdPrim, propertyNamespacePrefix):
# schemas. If so, the property names need to be converted into their
# template names to match the properties that will be in the
# generatedSchema.
if propertyNamespacePrefix:
if propertyNamespace:
propName = Usd.SchemaRegistry.MakeMultipleApplyNameTemplate(
propertyNamespacePrefix, propName)
propertyNamespace.prefix, propName)

# Add the property name to the list.
apiSchemaOverridePropertyNames.append(propName)
Expand Down Expand Up @@ -880,13 +894,13 @@ def ParseUsd(usdFilePath):
# make sure that if we have a multiple-apply schema with a property
# namespace prefix that the prim actually has some properties
if classInfo.apiSchemaType == MULTIPLE_APPLY:
if classInfo.propertyNamespacePrefix and \
if classInfo.propertyNamespace and \
len(sdfPrim.properties) == 0:
raise _GetSchemaDefException(
"Multiple-apply schemas that have the "
nvmkuruc marked this conversation as resolved.
Show resolved Hide resolved
"propertyNamespacePrefix metadata fields must have at "
"least one property", sdfPrim.path)
if not classInfo.propertyNamespacePrefix and \
if not classInfo.propertyNamespace and \
not len(sdfPrim.properties) == 0:
raise _GetSchemaDefException(
"Multiple-apply schemas that do not"
nvmkuruc marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -967,7 +981,7 @@ def ParseUsd(usdFilePath):
# schemas.
classInfo.apiSchemaOverridePropertyNames = \
_GetAPISchemaOverridePropertyNames(
usdPrim, classInfo.propertyNamespacePrefix)
usdPrim, classInfo.propertyNamespace)

for classInfo in classes:
# If this is an applied API schema that does not inherit from
Expand Down Expand Up @@ -1265,9 +1279,9 @@ def GatherTokens(classes, libName, libTokens,

# Add property namespace prefix token for multiple-apply API
# schema to token set
if cls.propertyNamespacePrefix:
_AddToken(tokenDict, cls.tokens, cls.propertyNamespacePrefix,
cls.propertyNamespacePrefix,
if cls.propertyNamespace:
_AddToken(tokenDict, cls.tokens, cls.propertyNamespace.token,
cls.propertyNamespace.prefix,
"Property namespace prefix for the %s schema." \
% cls.cppClassName, True)

Expand Down
11 changes: 2 additions & 9 deletions pxr/usd/usdPhysics/driveAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "pxr/usd/sdf/types.h"
#include "pxr/usd/sdf/assetPath.h"

#include "pxr/base/tf/staticTokens.h"

PXR_NAMESPACE_OPEN_SCOPE

// Register the schema with the TfType system.
Expand All @@ -40,11 +38,6 @@ TF_REGISTRY_FUNCTION(TfType)

}

TF_DEFINE_PRIVATE_TOKENS(
_schemaTokens,
(drive)
);

/* virtual */
UsdPhysicsDriveAPI::~UsdPhysicsDriveAPI()
{
Expand Down Expand Up @@ -131,9 +124,9 @@ UsdPhysicsDriveAPI::IsPhysicsDriveAPIPath(
}

if (tokens.size() >= 2
&& tokens[0] == _schemaTokens->drive) {
&& tokens[0] == UsdPhysicsTokens->drive) {
*name = TfToken(propertyName.substr(
_schemaTokens->drive.GetString().size() + 1));
UsdPhysicsTokens->drive.GetString().size() + 1));
return true;
}

Expand Down
11 changes: 2 additions & 9 deletions pxr/usd/usdPhysics/limitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "pxr/usd/sdf/types.h"
#include "pxr/usd/sdf/assetPath.h"

#include "pxr/base/tf/staticTokens.h"

PXR_NAMESPACE_OPEN_SCOPE

// Register the schema with the TfType system.
Expand All @@ -40,11 +38,6 @@ TF_REGISTRY_FUNCTION(TfType)

}

TF_DEFINE_PRIVATE_TOKENS(
_schemaTokens,
(limit)
);

/* virtual */
UsdPhysicsLimitAPI::~UsdPhysicsLimitAPI()
{
Expand Down Expand Up @@ -123,9 +116,9 @@ UsdPhysicsLimitAPI::IsPhysicsLimitAPIPath(
}

if (tokens.size() >= 2
&& tokens[0] == _schemaTokens->limit) {
&& tokens[0] == UsdPhysicsTokens->limit) {
*name = TfToken(propertyName.substr(
_schemaTokens->limit.GetString().size() + 1));
UsdPhysicsTokens->limit.GetString().size() + 1));
return true;
}

Expand Down
Loading
Loading