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-1971 Make job context UI registeration be order-neutral #4054

Merged
merged 1 commit into from
Dec 19, 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
155 changes: 90 additions & 65 deletions lib/mayaUsd/fileio/jobContextRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,39 @@ void UsdMayaJobContextRegistry::RegisterExportJobContext(
bool fromPython)
{
TF_DEBUG(PXRUSDMAYA_REGISTRY).Msg("Registering export job context %s.\n", jobContext.c_str());

TfToken key(jobContext);
ContextInfo newInfo { key, TfToken(niceName), TfToken(description), enablerFct, {}, {} };
auto itFound = _jobContextReg.find(newInfo);
ContextInfo newInfo { key, TfToken(niceName), TfToken(description) };
newInfo.exportEnablerCallback = enablerFct;

auto itFound = _jobContextReg.find(newInfo);
if (itFound == _jobContextReg.end()) {
_jobContextReg.insert(newInfo);
UsdMaya_RegistryHelper::AddUnloader(
[key]() {
ContextInfo toErase { key, {}, {}, {}, {}, {} };
_jobContextReg.erase(toErase);
},
fromPython);
[key]() { _jobContextReg.erase(ContextInfo { key }); }, fromPython);
} else {
if (!itFound->exportEnablerCallback) {
if (niceName != itFound->niceName) {
TF_CODING_ERROR(
"Export enabler has differing nice name: %s != %s",
niceName.c_str(),
itFound->niceName.GetText());
}
// Fill the export part:
ContextInfo updatedInfo(*itFound);
updatedInfo.exportDescription = TfToken(description);
updatedInfo.exportEnablerCallback = enablerFct;
_jobContextReg.erase(updatedInfo);
_jobContextReg.insert(updatedInfo);
} else {
if (itFound->exportEnablerCallback) {
TF_CODING_ERROR("Multiple enablers for export job context %s", jobContext.c_str());
}

if (itFound->niceName.size() > 0 && niceName != itFound->niceName) {
TF_CODING_ERROR(
"Export enabler has differing nice name: %s != %s",
niceName.c_str(),
itFound->niceName.GetText());
}

// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.
ContextInfo updatedInfo(*itFound);
if (niceName.size() > 0)
updatedInfo.niceName = TfToken(niceName);
if (description.size() > 0)
updatedInfo.exportDescription = TfToken(description);
updatedInfo.exportEnablerCallback = enablerFct;
_jobContextReg.erase(updatedInfo);
_jobContextReg.insert(updatedInfo);
}
}

Expand All @@ -98,22 +103,27 @@ void UsdMayaJobContextRegistry::SetExportOptionsUI(
UIFn uiFct,
bool fromPython)
{
const ContextInfo key { TfToken(jobContext), {}, {}, {}, {}, {} };
auto iter = _jobContextReg.find(key);
if (iter == _jobContextReg.end()) {
TF_CODING_ERROR("Export job context %s does not exists", jobContext.c_str());
return;
}
TF_DEBUG(PXRUSDMAYA_REGISTRY).Msg("Adding export job context %s UI.\n", jobContext.c_str());

// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.
TfToken key(jobContext);
ContextInfo newInfo { key };
newInfo.exportUICallback = uiFct;

ContextInfo updatedInfo(*iter);
updatedInfo.exportUICallback = uiFct;
UsdMaya_RegistryHelper::AddUnloader([key]() { _jobContextReg.erase(key); }, fromPython);
_jobContextReg.erase(iter);
_jobContextReg.insert(updatedInfo);
auto itFound = _jobContextReg.find(newInfo);
if (itFound == _jobContextReg.end()) {
_jobContextReg.insert(newInfo);
UsdMaya_RegistryHelper::AddUnloader(
[key]() { _jobContextReg.erase(ContextInfo { key }); }, fromPython);
} else {
// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.

ContextInfo updatedInfo(*itFound);
updatedInfo.exportUICallback = uiFct;
_jobContextReg.erase(itFound);
_jobContextReg.insert(updatedInfo);
}
}

void UsdMayaJobContextRegistry::RegisterImportJobContext(
Expand All @@ -124,9 +134,12 @@ void UsdMayaJobContextRegistry::RegisterImportJobContext(
bool fromPython)
{
TF_DEBUG(PXRUSDMAYA_REGISTRY).Msg("Registering import job context %s.\n", jobContext.c_str());

TfToken key(jobContext);
ContextInfo newInfo { key, TfToken(niceName), {}, {}, TfToken(description), enablerFct };
auto itFound = _jobContextReg.find(newInfo);
ContextInfo newInfo { key, TfToken(niceName), {}, {}, {}, TfToken(description) };
newInfo.importEnablerCallback = enablerFct;

auto itFound = _jobContextReg.find(newInfo);
if (itFound == _jobContextReg.end()) {
_jobContextReg.insert(newInfo);
UsdMaya_RegistryHelper::AddUnloader(
Expand All @@ -136,22 +149,29 @@ void UsdMayaJobContextRegistry::RegisterImportJobContext(
},
fromPython);
} else {
if (!itFound->importEnablerCallback) {
if (niceName != itFound->niceName) {
TF_CODING_ERROR(
"Import enabler has differing nice name: %s != %s",
niceName.c_str(),
itFound->niceName.GetText());
}
// Fill the import part:
ContextInfo updatedInfo(*itFound);
updatedInfo.importDescription = TfToken(description);
updatedInfo.importEnablerCallback = enablerFct;
_jobContextReg.erase(updatedInfo);
_jobContextReg.insert(updatedInfo);
} else {
if (itFound->importEnablerCallback) {
TF_CODING_ERROR("Multiple enablers for import job context %s", jobContext.c_str());
}

if (itFound->niceName.size() > 0 && niceName.size() > 0 && niceName != itFound->niceName) {
TF_CODING_ERROR(
"Import enabler has differing nice name: %s != %s",
niceName.c_str(),
itFound->niceName.GetText());
}

// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.

ContextInfo updatedInfo(*itFound);
if (niceName.size() > 0)
updatedInfo.niceName = TfToken(niceName);
if (description.size() > 0)
updatedInfo.importDescription = TfToken(description);
updatedInfo.importEnablerCallback = enablerFct;
_jobContextReg.erase(updatedInfo);
_jobContextReg.insert(updatedInfo);
}
}

Expand All @@ -160,22 +180,27 @@ void UsdMayaJobContextRegistry::SetImportOptionsUI(
UIFn uiFct,
bool fromPython)
{
const ContextInfo key { TfToken(jobContext), {}, {}, {}, {}, {} };
auto iter = _jobContextReg.find(key);
if (iter == _jobContextReg.end()) {
TF_CODING_ERROR("Import job context %s does not exist", jobContext.c_str());
return;
}
TF_DEBUG(PXRUSDMAYA_REGISTRY).Msg("Adding import job context %s UI.\n", jobContext.c_str());

// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.
TfToken key(jobContext);
ContextInfo newInfo { key, {}, {}, {}, {}, {}, {}, uiFct };
newInfo.importUICallback = uiFct;

ContextInfo updatedInfo(*iter);
updatedInfo.importUICallback = uiFct;
UsdMaya_RegistryHelper::AddUnloader([key]() { _jobContextReg.erase(key); }, fromPython);
_jobContextReg.erase(iter);
_jobContextReg.insert(updatedInfo);
auto itFound = _jobContextReg.find(newInfo);
if (itFound == _jobContextReg.end()) {
_jobContextReg.insert(newInfo);
UsdMaya_RegistryHelper::AddUnloader(
[key]() { _jobContextReg.erase(ContextInfo { key }); }, fromPython);
} else {
// Note: the container for the plugin info is a set so it cannot be modified.
// We need to copy the entry, modify the copy, remove the old entry and
// insert the newly updated entry.

ContextInfo updatedInfo(*itFound);
updatedInfo.importUICallback = uiFct;
_jobContextReg.erase(itFound);
_jobContextReg.insert(updatedInfo);
}
}

TfTokenVector UsdMayaJobContextRegistry::_ListJobContexts()
Expand Down
18 changes: 0 additions & 18 deletions lib/mayaUsd/fileio/jobContextRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@ class UsdMayaJobContextRegistry : public TfWeakBase
TfToken importDescription;
EnablerFn importEnablerCallback;
UIFn importUICallback;

ContextInfo() = default;

ContextInfo(
const TfToken& jc,
const TfToken& nn,
const TfToken& edsc,
EnablerFn eef,
const TfToken& idsc,
EnablerFn ief)
: jobContext(jc)
, niceName(nn)
, exportDescription(edsc)
, exportEnablerCallback(eef)
, importDescription(idsc)
, importEnablerCallback(ief)
{
}
};

/// Gets the conversion information associated with \p jobContext on export and import
Expand Down
22 changes: 11 additions & 11 deletions test/lib/usd/plugin/nullApiExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ REGISTER_EXPORT_JOB_CONTEXT_FCT(
return extraArgs;
}

REGISTER_EXPORT_JOB_CONTEXT_FCT(
Curly,
"Curly's special",
"Test coverage of error handling part deux")
{
VtDictionary extraArgs;
// Incorrect type:
extraArgs[UsdMayaJobExportArgsTokens->apiSchema] = VtValue(std::string("testApi"));
return extraArgs;
}

REGISTER_EXPORT_JOB_CONTEXT_UI_FCT(Curly)
{
VtDictionary forcedSettings;
Expand All @@ -129,6 +118,17 @@ REGISTER_EXPORT_JOB_CONTEXT_UI_FCT(Curly)
return forcedSettings;
}

REGISTER_EXPORT_JOB_CONTEXT_FCT(
Curly,
"Curly's special",
"Test coverage of error handling part deux")
{
VtDictionary extraArgs;
// Incorrect type:
extraArgs[UsdMayaJobExportArgsTokens->apiSchema] = VtValue(std::string("testApi"));
return extraArgs;
}

Comment on lines +121 to +131
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see any changes other than moving the macro. Is there a reason this macro moved below the other one?

Copy link
Collaborator Author

@pierrebai-adsk pierrebai-adsk Dec 18, 2024

Choose a reason for hiding this comment

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

To test the fact that order no longer matters. Before my fix, this swap made the test fail for me locally as the UI callback was registered before the job context. Now it passes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yes perfect. Thanks for thinking of that.

REGISTER_EXPORT_JOB_CONTEXT_FCT(Moe, "Moe's special", "Test coverage of error handling part funf")
{
VtDictionary extraArgs;
Expand Down