Skip to content

Commit

Permalink
Merge pull request #2027 from Autodesk/t_gamaj/MAYA-114335/expand_uni…
Browse files Browse the repository at this point in the history
…t_test

MAYA-114335 - Extended Python schema API wrappers
  • Loading branch information
Krystian Ligenza authored Jan 26, 2022
2 parents 06b4968 + 8d25524 commit 08bf007
Show file tree
Hide file tree
Showing 16 changed files with 995 additions and 199 deletions.
2 changes: 1 addition & 1 deletion lib/mayaUsd/fileio/schemaApiAdaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void UsdMayaSchemaApiAdaptor::RemoveAttribute(const TfToken& attrName, MDGModifi
}
}

MObject UsdMayaSchemaApiAdaptor::GetMayaObjectForSchema() const { return {}; };
MObject UsdMayaSchemaApiAdaptor::GetMayaObjectForSchema() const { return _handle.object(); };

TfToken UsdMayaSchemaApiAdaptor::GetMayaNameForUsdAttrName(const TfToken& usdAttrName) const
{
Expand Down
28 changes: 19 additions & 9 deletions lib/mayaUsd/fileio/utils/readUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,25 +722,35 @@ bool UsdMayaReadUtil::SetMayaAttr(
ok = true;
}
} else if (newValue.IsHolding<GfVec2d>()) {
GfVec2d v = newValue.Get<GfVec2d>();
MFnNumericData data;
if (Converter::hasNumericType(attrPlug, MFnNumericData::k2Double)) {
GfVec2d v = newValue.Get<GfVec2d>();
MFnNumericData data;
MObject dataObj = data.create(MFnNumericData::k2Double);
MObject dataObj = data.create(MFnNumericData::k2Double);
data.setData2Double(v[0], v[1]);
modifier.newPlugValue(attrPlug, dataObj);
ok = true;
} else if (Converter::hasNumericType(attrPlug, MFnNumericData::k2Float)) {
MObject dataObj = data.create(MFnNumericData::k2Float);
data.setData2Float(v[0], v[1]);
modifier.newPlugValue(attrPlug, dataObj);
ok = true;
}
} else if (newValue.IsHolding<GfVec3d>()) {
GfVec3d v = newValue.Get<GfVec3d>();
if (unlinearizeColors) {
v = _ConvertVec(attrPlug, v);
}
MFnNumericData data;
if (Converter::hasNumericType(attrPlug, MFnNumericData::k3Double)) {
GfVec3d v = newValue.Get<GfVec3d>();
if (unlinearizeColors) {
v = _ConvertVec(attrPlug, v);
}
MFnNumericData data;
MObject dataObj = data.create(MFnNumericData::k3Double);
MObject dataObj = data.create(MFnNumericData::k3Double);
data.setData3Double(v[0], v[1], v[2]);
modifier.newPlugValue(attrPlug, dataObj);
ok = true;
} else if (Converter::hasNumericType(attrPlug, MFnNumericData::k3Float)) {
MObject dataObj = data.create(MFnNumericData::k3Float);
data.setData3Float(v[0], v[1], v[2]);
modifier.newPlugValue(attrPlug, dataObj);
ok = true;
}
} else if (newValue.IsHolding<GfVec4d>()) {
if (Converter::hasNumericType(attrPlug, MFnNumericData::k4Double)) {
Expand Down
1 change: 1 addition & 0 deletions lib/mayaUsd/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(${PYTHON_TARGET_NAME} SHARED)
target_sources(${PYTHON_TARGET_NAME}
PRIVATE
module.cpp
wrapSparseValueWriter.cpp
wrapAdaptor.cpp
wrapBlockSceneModificationContext.cpp
wrapColorSpace.cpp
Expand Down
1 change: 1 addition & 0 deletions lib/mayaUsd/python/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PXR_NAMESPACE_USING_DIRECTIVE

TF_WRAP_MODULE
{
TF_WRAP(SparseValueWriter);
TF_WRAP(Adaptor);
TF_WRAP(BlockSceneModificationContext);
TF_WRAP(ColorSpace);
Expand Down
135 changes: 126 additions & 9 deletions lib/mayaUsd/python/wrapAdaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <mayaUsd/utils/undoHelperCommand.h>
#include <mayaUsd/utils/util.h>

#include <pxr/base/tf/pyPolymorphic.h>
#include <pxr/base/tf/pyResultConversions.h>
#include <pxr/pxr.h>
#include <pxr/usd/usd/attribute.h>
Expand All @@ -36,6 +37,102 @@ using namespace boost::python;

PXR_NAMESPACE_USING_DIRECTIVE;

class SchemaAdaptorWrapper
: public UsdMayaSchemaAdaptor
, public TfPyPolymorphic<UsdMayaSchemaAdaptor>
{
public:
typedef SchemaAdaptorWrapper This;
typedef UsdMayaSchemaAdaptor base_t;

SchemaAdaptorWrapper() { }

SchemaAdaptorWrapper(
const MObjectHandle& object,
const TfToken& schemaName,
const UsdPrimDefinition* schemaPrimDef)
: base_t(object, schemaName, schemaPrimDef)
{
}

static std::shared_ptr<This> New(uintptr_t createdWrapper)
{
return *((std::shared_ptr<This>*)createdWrapper);
}

virtual ~SchemaAdaptorWrapper() { }

UsdMayaAttributeAdaptor default_GetAttribute(const TfToken& attrName) const
{
return base_t::GetAttribute(attrName);
}
UsdMayaAttributeAdaptor GetAttribute(const TfToken& attrName) const override
{
return this->CallVirtual("GetAttribute", &This::default_GetAttribute)(attrName);
}

TfTokenVector default_GetAuthoredAttributeNames() const
{
return base_t::GetAuthoredAttributeNames();
}
TfTokenVector GetAuthoredAttributeNames() const override
{
return this->CallVirtual(
"GetAuthoredAttributeNames", &This::default_GetAuthoredAttributeNames)();
}

UsdMayaAttributeAdaptor
default_UndoableCreateAttribute(const TfToken& attrName, MDGModifier& modifier)
{
return base_t::CreateAttribute(attrName, modifier);
}
UsdMayaAttributeAdaptor CreateAttribute(const TfToken& attrName, MDGModifier& modifier) override
{
// Not using TfPolymorphic::CallVirtual because MDGModifier is non-copyable
TfPyLock pyLock;
auto pyOverride = this->GetOverride("UndoableCreateAttribute");
if (pyOverride) {
// Do *not* call through if there's an active python exception.
if (!PyErr_Occurred()) {
try {
return boost::python::call<UsdMayaAttributeAdaptor>(
pyOverride.ptr(), attrName, modifier);
} catch (boost::python::error_already_set const&) {
// Convert any exception to TF_ERRORs.
TfPyConvertPythonExceptionToTfErrors();
PyErr_Clear();
}
}
}
return default_UndoableCreateAttribute(attrName, modifier);
}

void default_UndoableRemoveAttribute(const TfToken& attrName, MDGModifier& modifier)
{
base_t::RemoveAttribute(attrName, modifier);
}
void RemoveAttribute(const TfToken& attrName, MDGModifier& modifier) override
{
// Not using TfPolymorphic::CallVirtual because MDGModifier is non-copyable
TfPyLock pyLock;
auto pyOverride = this->GetOverride("UndoableRemoveAttribute");
if (pyOverride) {
// Do *not* call through if there's an active python exception.
if (!PyErr_Occurred()) {
try {
return boost::python::call<void>(pyOverride.ptr(), attrName, modifier);
} catch (boost::python::error_already_set const&) {
// Convert any exception to TF_ERRORs.
TfPyConvertPythonExceptionToTfErrors();
PyErr_Clear();
}
}
}
return default_UndoableRemoveAttribute(attrName, modifier);
}
};
using SchemaAdaptorWrapperPtr = std::shared_ptr<SchemaAdaptorWrapper>;

static UsdMayaAdaptor* _Adaptor__init__(const std::string& dagPath)
{
MObject object;
Expand Down Expand Up @@ -231,21 +328,41 @@ void wrapAdaptor()
.def("RegisterTypedSchemaConversion", &::RegisterTypedSchemaConversion)
.staticmethod("RegisterTypedSchemaConversion");

class_<UsdMayaSchemaAdaptor, UsdMayaSchemaAdaptorPtr>("SchemaAdaptor")
class_<UsdMayaAttributeAdaptor>("AttributeAdaptor")
.def(!self)
.def("__repr__", _AttributeAdaptor__repr__)
.def("GetName", &UsdMayaAttributeAdaptor::GetName)
.def("Get", _AttributeAdaptor_Get)
.def("Set", _AttributeAdaptor_Set)
.def("GetAttributeDefinition", &UsdMayaAttributeAdaptor::GetAttributeDefinition);

class_<SchemaAdaptorWrapper, boost::noncopyable> c("SchemaAdaptor", boost::python::no_init);
boost::python::scope s(c);

c.def(!self)
.def("__init__", make_constructor(&SchemaAdaptorWrapper::New))
.def("__repr__", _SchemaAdaptor__repr__)
.def("GetName", &UsdMayaSchemaAdaptor::GetName)
.def("GetAttribute", &UsdMayaSchemaAdaptor::GetAttribute)
.def(
"GetAttribute",
&SchemaAdaptorWrapper::GetAttribute,
&SchemaAdaptorWrapper::default_GetAttribute)
.def("CreateAttribute", _SchemaAdaptor_CreateAttribute)
.def("RemoveAttribute", _SchemaAdaptor_RemoveAttribute)
.def("GetAuthoredAttributeNames", &UsdMayaSchemaAdaptor::GetAuthoredAttributeNames)
.def(
"GetAuthoredAttributeNames",
&SchemaAdaptorWrapper::GetAuthoredAttributeNames,
&SchemaAdaptorWrapper::default_GetAuthoredAttributeNames)
.def("GetAttributeNames", &UsdMayaSchemaAdaptor::GetAttributeNames);

class_<UsdMayaAttributeAdaptor>("AttributeAdaptor")
// For wrapping UsdMayaSchemaAdaptor created in c++
boost::python::class_<UsdMayaSchemaAdaptor, UsdMayaSchemaAdaptorPtr, boost::noncopyable>(
"SchemaAdaptor", boost::python::no_init)
.def(!self)
.def("__repr__", _AttributeAdaptor__repr__)
.def("GetName", &UsdMayaAttributeAdaptor::GetName)
.def("Get", _AttributeAdaptor_Get)
.def("Set", _AttributeAdaptor_Set)
.def("GetAttributeDefinition", &UsdMayaAttributeAdaptor::GetAttributeDefinition);
.def("GetName", &UsdMayaSchemaAdaptor::GetName)
.def("GetAttribute", &UsdMayaSchemaAdaptor::GetAttribute)
.def("CreateAttribute", _SchemaAdaptor_CreateAttribute)
.def("RemoveAttribute", _SchemaAdaptor_RemoveAttribute)
.def("GetAuthoredAttributeNames", &UsdMayaSchemaAdaptor::GetAuthoredAttributeNames)
.def("GetAttributeNames", &UsdMayaSchemaAdaptor::GetAttributeNames);
}
Loading

0 comments on commit 08bf007

Please sign in to comment.