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

MAYA-114335 - Extended Python schema API wrappers #2027

Merged
merged 4 commits into from
Jan 26, 2022

Conversation

JGamache-autodesk
Copy link
Collaborator

Goal was to reach unit test parity with C++. The Python wrappers can now implement the full Bullet simulation unit test originally implemented as a C++ USD plugin.

Goal was to reach unit test parity with C++. The Python wrappers can
 now implement the full Bullet simulation unit test originally implemented as a C++ USD plugin.
GfVec2d v = newValue.Get<GfVec2d>();
MFnNumericData data;
MObject dataObj = data.create(MFnNumericData::k2Double);
MObject dataObj = data.create(MFnNumericData::k2Double);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values directly translated from a Python tuple will be a Vec2d. Do some conversion if assigning to a Vec2f value.

@@ -231,21 +328,41 @@ void wrapAdaptor()
.def("RegisterTypedSchemaConversion", &::RegisterTypedSchemaConversion)
.staticmethod("RegisterTypedSchemaConversion");

class_<UsdMayaSchemaAdaptor, UsdMayaSchemaAdaptorPtr>("SchemaAdaptor")
class_<UsdMayaAttributeAdaptor>("AttributeAdaptor")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attribute adaptor class was moved from below. Unchanged.

.def("Set", _AttributeAdaptor_Set)
.def("GetAttributeDefinition", &UsdMayaAttributeAdaptor::GetAttributeDefinition);

class_<SchemaAdaptorWrapper, boost::noncopyable> c("SchemaAdaptor", boost::python::no_init);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema adaptor needs to expose its virtual functions if we want the Python wrapper for schema API adaptor to be able to use it as base class in Python-land.

.def_readonly("materialConversion", &UsdMayaJobImportArgs::ShadingMode::materialConversion);

c.add_property(
"assemblyRep",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now returning the full JobImportArgs struct as read-only properties. Some USD classes could not be returned by reference, so a make_getter was used to properly implement the required pass-by-value.

{
return boost::python::object(
return MayaUsdLibSparseValueWriter(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping the sparse value writer because that class can not be passed-by-value, which is the default in Python. See the wrapper include file for details.

.add_property("convertMaterialsTo", &::getconvertMaterialsTo);
using namespace boost::python;

class_<UsdMayaJobExportArgs>("JobExportArgs", no_init)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting the whole JobExportArgs struct.

@@ -80,6 +82,26 @@ static void _SetMayaAttrKeyableState(const std::string& attrPath, const SdfVaria
UsdMayaReadUtil::SetMayaAttrKeyableState(plug, variability);
}

bool _ReadUsdAttribute(
const UsdAttribute& usdAttr,
const MObject& obj,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a MObject instead of a MFnDependencyNode which has no copy semantics.

// Python will create copies of classes to prevent dangerous access to ephemeral pointers. This does
// not work well for USD's UsdUtilsSparseValueWriter because it builds up a memory of all previously
// set values, which means it must be passed at least by reference. We will wrap the USD class in a
// way that allows seamless copies in Python land without duplicating the USD class.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course this means keeping a sparse value writer in Python land past its life scope in C++ can lead to a crash. Should I try using a weak_ptr somehow as a way to survive this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make it cleaner, if it doesn't make the binding more complex.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Too complex and requires the tracked class to be managed via shared_ptr.

mayaUsdLib.SchemaApiAdaptor.Register(TestBulletRigidBodyShemaAdaptor, "shape", "PhysicsRigidBodyAPI")

# Build a scene (and exercise the adaptor in a freeform context)
cmds.file(f=True, new=True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same code as found in test\lib\usd\translators\testUsdExportSchemaApi.py meaning the Python code can do what the C++ code does.

@@ -188,7 +188,8 @@ def __testUsdPreviewSurfaceRoundtrip(self,
imported_path = cmds.getAttr(file_node+".fileTextureName")
# imported path will be absolute:
self.assertFalse(imported_path.startswith(".."))
self.assertEqual(imported_path.lower(), original_path.lower())
self.assertEqual(os.path.normpath(imported_path.lower()),
os.path.normpath(original_path.lower()))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On some Windows configurations, normpath can be used to make sure both paths use the same directory separators.

@JGamache-autodesk JGamache-autodesk added the ready-for-merge Development process is finished, PR is ready for merge label Jan 26, 2022
@kxl-adsk kxl-adsk merged commit 08bf007 into dev Jan 26, 2022
@kxl-adsk kxl-adsk deleted the t_gamaj/MAYA-114335/expand_unit_test branch January 26, 2022 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
import-export Related to Import and/or Export ready-for-merge Development process is finished, PR is ready for merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants