-
Notifications
You must be signed in to change notification settings - Fork 202
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-101 Override primWriter #3189
Merged
seando-adsk
merged 12 commits into
dev
from
samuelliu-adsk/EMSUSD-101/override-primwriter
Jul 28, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ae65adc
Add CanImport function for prim writers
samuelliu-adsk d6b7d8a
Update primwriter functions to add ContextSupport
samuelliu-adsk fa18274
Add poke function to primWriterRegistry for loading writer plugin
samuelliu-adsk 0ede36e
Add MFnDependencyNode as an input to CanExport function so user can i…
samuelliu-adsk ac4f5e0
clang format fix
samuelliu-adsk a43f3e8
Change CanExport function to take MObject and updated python wrapper
samuelliu-adsk cc0e561
Clang format fix
samuelliu-adsk 84cff9f
Add default context support for cases CanExport not implemented
samuelliu-adsk 1d5fabe
Update unit test for primWriter and shaderWriter
samuelliu-adsk 3f18188
Add classmethod decorator to CanExport
samuelliu-adsk d9e1001
Rename poke function and refine comments
samuelliu-adsk 2725e96
Bug fix for string conversion
samuelliu-adsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,9 +42,75 @@ TF_DEFINE_PRIVATE_TOKENS( | |
); | ||
// clang-format on | ||
|
||
typedef std::map<std::string, UsdMayaPrimWriterRegistry::WriterFactoryFn> _Registry; | ||
static _Registry _reg; | ||
static std::set<std::string> _mayaTypesThatDoNotCreatePrims; | ||
namespace { | ||
struct _RegistryEntry | ||
{ | ||
UsdMayaPrimWriterRegistry::ContextPredicateFn _pred; | ||
UsdMayaPrimWriterRegistry::WriterFactoryFn _writer; | ||
int _index; | ||
}; | ||
|
||
// typedef std::map<std::string, UsdMayaPrimWriterRegistry::WriterFactoryFn> _Registry; | ||
typedef std::unordered_multimap<std::string, _RegistryEntry> _Registry; | ||
static _Registry _reg; | ||
static std::set<std::string> _mayaTypesThatDoNotCreatePrims; | ||
static int _indexCounter = 0; | ||
|
||
_Registry::const_iterator _Find( | ||
const std::string& mayaTypeName, | ||
const UsdMayaJobExportArgs& exportArgs, | ||
const MObject& exportObj) | ||
{ | ||
using ContextSupport = UsdMayaPrimWriter::ContextSupport; | ||
|
||
_Registry::const_iterator ret = _reg.cend(); | ||
_Registry::const_iterator first, last; | ||
std::tie(first, last) = _reg.equal_range(mayaTypeName); | ||
while (first != last) { | ||
ContextSupport support = first->second._pred(exportArgs, exportObj); | ||
// Look for a "Supported" reader. If no "Supported" reader is found, use a "Fallback" reader | ||
if (support == ContextSupport::Supported) { | ||
ret = first; | ||
break; | ||
} else if (support == ContextSupport::Fallback && ret == _reg.end()) { | ||
ret = first; | ||
} | ||
++first; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
} // namespace | ||
|
||
/* static */ | ||
void UsdMayaPrimWriterRegistry::Register( | ||
const std::string& mayaTypeName, | ||
UsdMayaPrimWriterRegistry::ContextPredicateFn pred, | ||
UsdMayaPrimWriterRegistry::WriterFactoryFn fn, | ||
bool fromPython) | ||
{ | ||
TF_DEBUG(PXRUSDMAYA_REGISTRY) | ||
.Msg("Registering UsdMayaPrimWriter for maya type %s.\n", mayaTypeName.c_str()); | ||
|
||
int index = _indexCounter++; | ||
_reg.insert(std::make_pair(mayaTypeName, _RegistryEntry { pred, fn, index })); | ||
|
||
// The unloader uses the index to know which entry to erase when there are | ||
// more than one for the same mayaTypeName. | ||
UsdMaya_RegistryHelper::AddUnloader( | ||
[mayaTypeName, index]() { | ||
_Registry::const_iterator it, itEnd; | ||
std::tie(it, itEnd) = _reg.equal_range(mayaTypeName); | ||
for (; it != itEnd; ++it) { | ||
if (it->second._index == index) { | ||
_reg.erase(it); | ||
break; | ||
} | ||
} | ||
}, | ||
fromPython); | ||
} | ||
|
||
/* static */ | ||
void UsdMayaPrimWriterRegistry::Register( | ||
|
@@ -55,14 +121,31 @@ void UsdMayaPrimWriterRegistry::Register( | |
TF_DEBUG(PXRUSDMAYA_REGISTRY) | ||
.Msg("Registering UsdMayaPrimWriter for maya type %s.\n", mayaTypeName.c_str()); | ||
|
||
std::pair<_Registry::iterator, bool> insertStatus | ||
= _reg.insert(std::make_pair(mayaTypeName, fn)); | ||
if (insertStatus.second) { | ||
UsdMaya_RegistryHelper::AddUnloader( | ||
[mayaTypeName]() { _reg.erase(mayaTypeName); }, fromPython); | ||
} else { | ||
TF_CODING_ERROR("Multiple writers for type %s", mayaTypeName.c_str()); | ||
} | ||
int index = _indexCounter++; | ||
|
||
// Use default ContextSupport if not specified | ||
_reg.insert(std::make_pair( | ||
mayaTypeName, | ||
_RegistryEntry { [](const UsdMayaJobExportArgs&, const MObject&) { | ||
return UsdMayaPrimWriter::ContextSupport::Fallback; | ||
}, | ||
fn, | ||
index })); | ||
|
||
// The unloader uses the index to know which entry to erase when there are | ||
// more than one for the same mayaTypeName. | ||
UsdMaya_RegistryHelper::AddUnloader( | ||
[mayaTypeName, index]() { | ||
_Registry::const_iterator it, itEnd; | ||
std::tie(it, itEnd) = _reg.equal_range(mayaTypeName); | ||
for (; it != itEnd; ++it) { | ||
if (it->second._index == index) { | ||
_reg.erase(it); | ||
break; | ||
} | ||
} | ||
}, | ||
fromPython); | ||
} | ||
|
||
/* static */ | ||
|
@@ -74,32 +157,59 @@ void UsdMayaPrimWriterRegistry::RegisterRaw( | |
} | ||
|
||
/* static */ | ||
UsdMayaPrimWriterRegistry::WriterFactoryFn | ||
UsdMayaPrimWriterRegistry::Find(const std::string& mayaTypeName) | ||
UsdMayaPrimWriterRegistry::WriterFactoryFn UsdMayaPrimWriterRegistry::Find( | ||
const std::string& mayaTypeName, | ||
const UsdMayaJobExportArgs& exportArgs, | ||
const MObject& exportObj) | ||
{ | ||
TfRegistryManager::GetInstance().SubscribeTo<UsdMayaPrimWriterRegistry>(); | ||
|
||
// unfortunately, usdTypeName is diff from the tfTypeName which we use to | ||
// register. do the conversion here. | ||
WriterFactoryFn ret = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK what that erased comment about usdTypeName vs tfTypeName was about... I guess it is now irrelevant? |
||
if (TfMapLookup(_reg, mayaTypeName, &ret)) { | ||
return ret; | ||
_Registry::const_iterator it = _Find(mayaTypeName, exportArgs, exportObj); | ||
|
||
if (it != _reg.end()) { | ||
return it->second._writer; | ||
} | ||
|
||
static const TfTokenVector SCOPE = { _tokens->UsdMaya, _tokens->PrimWriter }; | ||
UsdMaya_RegistryHelper::FindAndLoadMayaPlug(SCOPE, mayaTypeName); | ||
|
||
// ideally something just registered itself. if not, we at least put it in | ||
it = _Find(mayaTypeName, exportArgs, exportObj); | ||
|
||
if (it != _reg.end()) { | ||
return it->second._writer; | ||
} | ||
|
||
// if nothing was found and nothing was registered, we at least put it in | ||
// the registry in case we encounter it again. | ||
pierrebai-adsk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!TfMapLookup(_reg, mayaTypeName, &ret)) { | ||
TF_DEBUG(PXRUSDMAYA_REGISTRY) | ||
.Msg( | ||
"No usdMaya writer plugin for maya type %s. No maya plugin found.\n", | ||
mayaTypeName.c_str()); | ||
_reg[mayaTypeName] = nullptr; | ||
TF_DEBUG(PXRUSDMAYA_REGISTRY) | ||
.Msg("No usdMaya writer plugin for TfType %s. No maya plugin.\n", mayaTypeName.c_str()); | ||
if (_reg.count(mayaTypeName) == 0) { | ||
// Nothing registered at all, remember that: | ||
_reg.insert(std::make_pair( | ||
mayaTypeName, | ||
_RegistryEntry { [](const UsdMayaJobExportArgs&, const MObject&) { | ||
return UsdMayaPrimWriter::ContextSupport::Fallback; | ||
}, | ||
nullptr, | ||
-1 })); | ||
} | ||
|
||
return ret; | ||
return nullptr; | ||
} | ||
|
||
/* static */ | ||
void UsdMayaPrimWriterRegistry::CheckForWriterPlugin(const std::string& mayaTypeName) | ||
{ | ||
TfRegistryManager::GetInstance().SubscribeTo<UsdMayaPrimWriterRegistry>(); | ||
|
||
_Registry::const_iterator first, last; | ||
std::tie(first, last) = _reg.equal_range(mayaTypeName); | ||
|
||
if (first == last) { | ||
// If the type name is not currently in our registry, check for plugin registry | ||
static const TfTokenVector SCOPE = { _tokens->UsdMaya, _tokens->PrimWriter }; | ||
UsdMaya_RegistryHelper::FindAndLoadMayaPlug(SCOPE, mayaTypeName); | ||
} | ||
} | ||
|
||
/* static */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this typedef in comment was left-over and probably should be removed.