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

EMSUSD-957 - Call CanExport() per object instead of per export #3592

Merged
merged 1 commit into from
Feb 6, 2024
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
6 changes: 6 additions & 0 deletions lib/mayaUsd/fileio/primWriterRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ _Registry::const_iterator _Find(

} // namespace

/* static */
bool UsdMayaPrimWriterRegistry::HasMultipleWriters(const std::string& mayaTypeName)
{
return _reg.count(mayaTypeName) > 1;
}

/* static */
void UsdMayaPrimWriterRegistry::Register(
const std::string& mayaTypeName,
Expand Down
37 changes: 37 additions & 0 deletions lib/mayaUsd/fileio/primWriterRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ struct UsdMayaPrimWriterRegistry
using ContextPredicateFn = std::function<
UsdMayaPrimWriter::ContextSupport(const UsdMayaJobExportArgs&, const MObject&)>;

// Return true if this mayaTypeName has multiple writers registered.
// In that case, call the Find function each time to find the correct writer for that object.
MAYAUSD_CORE_PUBLIC
static bool HasMultipleWriters(const std::string& mayaTypeName);

/// \brief Register \p fn as a factory function providing a
/// UsdMayaPrimWriter subclass that can be used to write \p mayaType.
/// Provide a supportability of the primWriter. Use "supported" to
Expand Down Expand Up @@ -235,6 +240,38 @@ struct UsdMayaPrimWriterRegistry
}); \
}

/// \brief Registers a custom pre-existing writer class for the given Maya type;
/// the writer class should be a subclass of UsdMayaPrimWriter with a three-place
/// constructor that takes <tt>(const MFnDependencyNode& depNodeFn,
/// const SdfPath& usdPath, UsdMayaWriteJobContext& jobCtx)</tt> as arguments.
/// Use CanExport(const UsdMayaJobExportArgs& exportArgs, const MObject& exportObj) function to
/// return the level of support the writer can offer for a given context.
///
/// Example:
/// \code{.cpp}
/// class MyWriter : public UsdMayaPrimWriter {
/// MyWriter(
/// const MFnDependencyNode& depNodeFn,
/// const SdfPath& usdPath,
/// UsdMayaWriteJobContext& jobCtx) {
/// // ...
/// }
/// };
/// PXRUSDMAYA_REGISTER_CUSTOM_WRITER(myCustomMayaNode, MyWriter);
/// \endcode
#define PXRUSDMAYA_REGISTER_CUSTOM_WRITER(mayaTypeName, writerClass) \
TF_REGISTRY_FUNCTION_WITH_TAG(UsdMayaPrimWriterRegistry, mayaTypeName##_##writerClass) \
{ \
UsdMayaPrimWriterRegistry::Register( \
#mayaTypeName, \
writerClass::CanExport, \
[](const MFnDependencyNode& depNodeFn, \
const SdfPath& usdPath, \
UsdMayaWriteJobContext& jobCtx) { \
return std::make_shared<writerClass>(depNodeFn, usdPath, jobCtx); \
}); \
}

PXR_NAMESPACE_CLOSE_SCOPE

#endif
5 changes: 4 additions & 1 deletion lib/mayaUsd/fileio/writeJobContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,11 @@ UsdMayaWriteJobContext::_FindWriter(const MFnDependencyNode& mayaNode)
const std::string mayaNodeType(mayaNode.typeName().asChar());

// Check if type is already cached locally.
// If this type has multiple writers, we need to call CanExport again to determine which writer
// to use
auto iter = mWriterFactoryCache.find(mayaNodeType);
if (iter != mWriterFactoryCache.end()) {
if (iter != mWriterFactoryCache.end()
&& !UsdMayaPrimWriterRegistry::HasMultipleWriters(mayaNodeType)) {
return iter->second;
}

Expand Down
Loading