Skip to content

Commit

Permalink
General mechanism for storing USD-specific data in "USD_" Maya attrib…
Browse files Browse the repository at this point in the history
…utes

(Internal change: 1661180)
  • Loading branch information
sdao authored and pixar-oss committed Oct 17, 2016
1 parent 294a816 commit 7bad1b0
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 24 deletions.
62 changes: 62 additions & 0 deletions third_party/maya/lib/usdMaya/AttributeConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "usdMaya/AttributeConverter.h"

#include "pxr/usd/usd/prim.h"

#include <maya/MFnDependencyNode.h>

FunctionalAttributeConverter::FunctionalAttributeConverter(
MayaToUsdFn mayaToUsdFn, UsdToMayaFn usdToMayaFn)
: _mayaToUsdFn(mayaToUsdFn), _usdToMayaFn(usdToMayaFn)
{
}

bool
FunctionalAttributeConverter::MayaToUsd(
const MFnDependencyNode& srcNode,
UsdPrim& destPrim,
const UsdTimeCode usdTime) const
{
if (_mayaToUsdFn) {
return _mayaToUsdFn(srcNode, destPrim, usdTime);
}
else {
return false;
}
}

bool
FunctionalAttributeConverter::UsdToMaya(
const UsdPrim& srcPrim,
MFnDependencyNode& destNode,
const UsdTimeCode usdTime) const
{
if (_usdToMayaFn) {
return _usdToMayaFn(srcPrim, destNode, usdTime);
}
else {
return false;
}
}
97 changes: 97 additions & 0 deletions third_party/maya/lib/usdMaya/AttributeConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXRUSDMAYA_ATTRIBUTECONVERTER_H
#define PXRUSDMAYA_ATTRIBUTECONVERTER_H

/// \file AttributeConverter.h

#include "pxr/usd/usd/prim.h"
#include "pxr/usd/usd/timeCode.h"

#include <maya/MFnDependencyNode.h>

#include <functional>

/// \brief Provides a way to store USD-specific information on a Maya node
/// as an extra Maya attribute (e.g. "USD_hidden" for the hidden state) when no
/// existing Maya attribute maps cleanly.
///
/// This way, we can store information such as the hidden state or the model
/// kind on a Maya node so that the information isn't lost in the USD to Maya to
/// USD roundtrip.
///
/// This class encapsulates:
/// (1) a way to store non-attribute metadata or information (such as the hidden
/// state) on a Maya node when importing, and
/// (2) a way to write out the same metadata or information from a Maya node
/// into a \class UsdPrim when exporting.
class AttributeConverter {
public:
/// \brief Takes attribute(s) from a Maya node and modifies a
/// \class UsdPrim accordingly.
virtual bool MayaToUsd(
const MFnDependencyNode& depNode,
UsdPrim& destPrim,
const UsdTimeCode usdTime) const = 0;

/// \brief Stores information about a source \class UsdPrim in a Maya node.
virtual bool UsdToMaya(
const UsdPrim& srcPrim,
MFnDependencyNode& destNode,
const UsdTimeCode usdTime) const = 0;
};

/// \brief An implementation of \class AttributeConverter that allows passing in
/// functions or lambdas to handle the conversion between Maya and USD.
class FunctionalAttributeConverter : public AttributeConverter {
public:
typedef std::function< bool (const MFnDependencyNode&, UsdPrim&,
UsdTimeCode) > MayaToUsdFn;
typedef std::function< bool (const UsdPrim&, MFnDependencyNode&,
UsdTimeCode) > UsdToMayaFn;

virtual bool MayaToUsd(
const MFnDependencyNode& depNode,
UsdPrim& destPrim,
const UsdTimeCode usdTime) const override;
virtual bool UsdToMaya(
const UsdPrim& srcPrim,
MFnDependencyNode& destNode,
const UsdTimeCode usdTime) const override;

/// \brief Constructs a FunctionalAttributeConverter that forwards calls to
/// MayaToUsd and UsdToMaya to the given functions.
///
/// The functions can be empty functions, in which case MayaToUsd and/or
/// UsdToMaya will return false.
FunctionalAttributeConverter(
MayaToUsdFn mayaToUsdFn,
UsdToMayaFn usdToMayaFn);

private:
const MayaToUsdFn _mayaToUsdFn;
const UsdToMayaFn _usdToMayaFn;
};

#endif // PXRUSDMAYA_ATTRIBUTECONVERTER_H
45 changes: 45 additions & 0 deletions third_party/maya/lib/usdMaya/AttributeConverterRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "usdMaya/AttributeConverterRegistry.h"

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

static std::vector<AttributeConverter*> _reg;

/* static */
void
AttributeConverterRegistry::Register(AttributeConverter* converter) {
_reg.push_back(converter);
}

/* static */
std::vector<const AttributeConverter*>
AttributeConverterRegistry::GetAllConverters() {
TfRegistryManager::GetInstance().SubscribeTo<AttributeConverterRegistry>();
std::vector<const AttributeConverter*> ret;
for (AttributeConverter* converter : _reg) {
ret.push_back(converter);
}
return ret;
}
45 changes: 45 additions & 0 deletions third_party/maya/lib/usdMaya/AttributeConverterRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXRUSDMAYA_ATTRIBUTECONVERTERREGISTRY_H
#define PXRUSDMAYA_ATTRIBUTECONVERTERREGISTRY_H

/// \file AttributeConverterRegistry.h

#include <vector>

class AttributeConverter;

/// \brief A registry of all the converters used to import and export
/// USD-specific information stored in Maya attributes (e.g. "USD_hidden").
struct AttributeConverterRegistry {
/// \brief Registers the given attribute converter.
/// Ownership of the converter \p converter transfers to
/// AttributeConverterRegistry.
static void Register(AttributeConverter* converter);

/// \brief Gets a copy of the list of all registered converters.
static std::vector<const AttributeConverter*> GetAllConverters();
};

#endif // PXRUSDMAYA_ATTRIBUTECONVERTERREGISTRY_H
4 changes: 4 additions & 0 deletions third_party/maya/lib/usdMaya/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pxr_shared_library(${PXR_PACKAGE}

Chaser
ChaserRegistry

AttributeConverter
AttributeConverterRegistry

PRIVATE_CLASSES
registryHelper
Expand Down Expand Up @@ -97,6 +100,7 @@ pxr_shared_library(${PXR_PACKAGE}
translatorMesh_SubDiv.cpp
translatorXformable_decompose.cpp
usdReadJob_ImportWithProxies.cpp
UsdMetadataAttributeConverters.cpp

PYMODULE_CPPFILES
module.cpp
Expand Down
35 changes: 11 additions & 24 deletions third_party/maya/lib/usdMaya/MayaPrimWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "usdMaya/writeUtil.h"
#include "usdMaya/translatorGprim.h"
#include "usdMaya/primWriterContext.h"
#include "usdMaya/AttributeConverter.h"
#include "usdMaya/AttributeConverterRegistry.h"

#include "pxr/base/gf/gamma.h"

Expand Down Expand Up @@ -65,33 +67,12 @@ MayaPrimWriter::MayaPrimWriter(MDagPath & iDag,
}
}


void
_writeUsdInfo(
const MDagPath& dag,
const UsdTimeCode& usdTime,
const UsdPrim& prim)
{
MFnDependencyNode depFn(dag.node());

bool instanceable = false;
if (PxrUsdMayaUtil::getPlugValue(depFn, "USD_instanceable", &instanceable)) {
prim.SetInstanceable(instanceable);
}

// We only author hidden if it's to set it to true.
bool hidden = false;
if (PxrUsdMayaUtil::getPlugValue(depFn, "USD_hidden", &hidden) and hidden) {
prim.SetHidden(hidden);
}
}

bool
MayaPrimWriter::writePrimAttrs(const MDagPath &dagT, const UsdTimeCode &usdTime, UsdGeomImageable &primSchema)
{
MStatus status;
MFnDependencyNode depFn(getDagPath().node());
MFnDependencyNode depFn2(dagT.node()); // optionally also scan a shape's transform if merging transforms
MFnDependencyNode depFnT(dagT.node()); // optionally also scan a shape's transform if merging transforms

if (getArgs().exportVisibility) {
bool isVisible = true; // if BOTH shape or xform is animated, then visible
Expand All @@ -101,7 +82,7 @@ MayaPrimWriter::writePrimAttrs(const MDagPath &dagT, const UsdTimeCode &usdTime,

if ( dagT.isValid() ) {
bool isVis, isAnim;
if (PxrUsdMayaUtil::getPlugValue(depFn2, "visibility", &isVis, &isAnim)){
if (PxrUsdMayaUtil::getPlugValue(depFnT, "visibility", &isVis, &isAnim)){
isVisible = isVisible and isVis;
isAnimated = isAnimated or isAnim;
}
Expand Down Expand Up @@ -132,7 +113,13 @@ MayaPrimWriter::writePrimAttrs(const MDagPath &dagT, const UsdTimeCode &usdTime,

}

_writeUsdInfo(dagT, usdTime, usdPrim);
// Process special "USD_" attributes.
std::vector<const AttributeConverter*> converters =
AttributeConverterRegistry::GetAllConverters();
for (const AttributeConverter* converter : converters) {
// We want the node for the xform (depFnT).
converter->MayaToUsd(depFnT, usdPrim, usdTime);
}

// Write user-tagged export attributes. Write attributes on the transform
// first, and then attributes on the shape node. This means that attribute
Expand Down
Loading

0 comments on commit 7bad1b0

Please sign in to comment.