diff --git a/lib/mayaUsd/listeners/notice.cpp b/lib/mayaUsd/listeners/notice.cpp index bb011074e2..6f7d433113 100644 --- a/lib/mayaUsd/listeners/notice.cpp +++ b/lib/mayaUsd/listeners/notice.cpp @@ -24,6 +24,11 @@ PXR_NAMESPACE_OPEN_SCOPE namespace { +// Simple ref count for how many plugins have initialized the callback. +// Used to keep the listener around until the last plugin asks for it +// to be removed. +static int _newOrOpenRegistrationCount = 0; + static void _OnMayaNewOrOpenSceneCallback(void* /*clientData*/) @@ -53,6 +58,10 @@ UsdMayaSceneResetNotice::UsdMayaSceneResetNotice() void UsdMayaSceneResetNotice::InstallListener() { + if (_newOrOpenRegistrationCount++ > 0) { + return; + } + // Send scene reset notices when changing scenes (either by switching // to a new empty scene or by opening a different scene). We do not listen // for kSceneUpdate messages since those are also emitted after a SaveAs @@ -79,6 +88,10 @@ UsdMayaSceneResetNotice::InstallListener() void UsdMayaSceneResetNotice::RemoveListener() { + if (_newOrOpenRegistrationCount-- > 1) { + return; + } + if (_afterNewCallbackId != 0) { MMessage::removeCallback(_afterNewCallbackId); } diff --git a/plugin/adsk/plugin/plugin.cpp b/plugin/adsk/plugin/plugin.cpp index 13007dd27b..c65adf2460 100644 --- a/plugin/adsk/plugin/plugin.cpp +++ b/plugin/adsk/plugin/plugin.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -134,6 +135,8 @@ MStatus initializePlugin(MObject obj) } } + UsdMayaSceneResetNotice::InstallListener(); + return status; } @@ -180,5 +183,7 @@ MStatus uninitializePlugin(MObject obj) CHECK_MSTATUS(status); #endif + UsdMayaSceneResetNotice::RemoveListener(); + return status; }