diff --git a/lib/mayaHydra/hydraExtensions/sceneIndex/registration.cpp b/lib/mayaHydra/hydraExtensions/sceneIndex/registration.cpp index db61074e41..ba8b1fed29 100644 --- a/lib/mayaHydra/hydraExtensions/sceneIndex/registration.cpp +++ b/lib/mayaHydra/hydraExtensions/sceneIndex/registration.cpp @@ -41,7 +41,8 @@ #include #include #include -#include +#include +#include #include #include @@ -218,11 +219,16 @@ MayaHydraSceneIndexRegistry::MayaHydraSceneIndexRegistry(const std::shared_ptr(clientData); - renderOverride->_AddSceneIndexForNode(dagNode); + } + + auto mayaHydraSceneIndexRegistry = static_cast(clientData); + if (MFileIO::isOpeningFile()){ + //We cannot process a node while loading a file + mayaHydraSceneIndexRegistry->_AppendNodeToProcessAfterOpenScene(dagNode); + }else{ + mayaHydraSceneIndexRegistry->_AddSceneIndexForNode(dagNode); + } +} + +//We need to check if some nodes that need to be processed were added to our array during a file load +void MayaHydraSceneIndexRegistry::_AfterOpenCallback(void *clientData) +{ + if (! clientData){ + return; + } + + auto mayaHydraSceneIndexRegistry = static_cast(clientData); + mayaHydraSceneIndexRegistry->_ProcessNodesAfterOpen(); } void MayaHydraSceneIndexRegistry::_SceneIndexNodeRemovedCallback(MObject& dagNode, void* clientData) @@ -465,4 +493,15 @@ void MayaHydraSceneIndexRegistry::_SceneIndexNodeRemovedCallback(MObject& dagNod renderOverride->_RemoveSceneIndexForNode(dagNode); } +void MayaHydraSceneIndexRegistry::_ProcessNodesAfterOpen() +{ + for (auto& dagNode : _nodesToProcessAfterOpenScene){ + if (dagNode.isNull() || dagNode.apiType() != MFn::kPluginShape){ + continue; + } + _AddSceneIndexForNode(dagNode); + } + _nodesToProcessAfterOpenScene.clear(); +} + PXR_NAMESPACE_CLOSE_SCOPE diff --git a/lib/mayaHydra/hydraExtensions/sceneIndex/registration.h b/lib/mayaHydra/hydraExtensions/sceneIndex/registration.h index ced37f40d0..761015f546 100644 --- a/lib/mayaHydra/hydraExtensions/sceneIndex/registration.h +++ b/lib/mayaHydra/hydraExtensions/sceneIndex/registration.h @@ -30,6 +30,8 @@ #include #include #include +#include + #include #include @@ -98,9 +100,20 @@ class MayaHydraSceneIndexRegistry bool _RemoveSceneIndexForNode(const MObject& dagNode); static void _SceneIndexNodeAddedCallback(MObject& obj, void* clientData); static void _SceneIndexNodeRemovedCallback(MObject& obj, void* clientData); + static void _AfterOpenCallback (void *clientData); + + // Append a node to the list of nodes that need to be processed after the scene is opened. + void _AppendNodeToProcessAfterOpenScene(const MObject& node) {_nodesToProcessAfterOpenScene.append(node);} + //We need to check if some nodes that need to be processed were added to our array during a file load + void _ProcessNodesAfterOpen(); + const std::shared_ptr _renderIndexProxy; - MCallbackIdArray _sceneIndexDagNodeMessageCallbacks; + MCallbackIdArray _DGCallbackIds; + MCallbackId _AfterOpenCBId {0}; + + // Maintain a list of nodes that need to be processed after the scene is opened. We cannot process them during file load. + MObjectArray _nodesToProcessAfterOpenScene; Registrations _registrations; // Maintain alternative way to retrieve registration based on MObjectHandle. This is faster to diff --git a/lib/mayaHydra/mayaPlugin/renderOverride.cpp b/lib/mayaHydra/mayaPlugin/renderOverride.cpp index 24dc772198..51c0ee33a6 100644 --- a/lib/mayaHydra/mayaPlugin/renderOverride.cpp +++ b/lib/mayaHydra/mayaPlugin/renderOverride.cpp @@ -1218,6 +1218,10 @@ void MtohRenderOverride::_InitHydraResources(const MHWRender::MDrawContext& draw //Add the scene index as an input scene index of the merging scene index _renderIndexProxy->InsertSceneIndex(_mayaHydraSceneIndex, SdfPath::AbsoluteRootPath()); + if (!_sceneIndexRegistry) { + _sceneIndexRegistry.reset(new MayaHydraSceneIndexRegistry(_renderIndexProxy)); + } + _CreateSceneIndicesChainAfterMergingSceneIndex(drawContext); if (auto* renderDelegate = _GetRenderDelegate()) { @@ -1321,10 +1325,6 @@ void MtohRenderOverride::_CreateSceneIndicesChainAfterMergingSceneIndex(const MH _selectionSceneIndex->SetDisplayName("Flow Viewport Selection Scene Index"); _lastFilteringSceneIndexBeforeCustomFiltering = _selectionSceneIndex; - if (!_sceneIndexRegistry) { - _sceneIndexRegistry.reset(new MayaHydraSceneIndexRegistry(_renderIndexProxy)); - } - // Add display style scene index _lastFilteringSceneIndexBeforeCustomFiltering = _displayStyleSceneIndex = Fvp::DisplayStyleOverrideSceneIndex::New(_lastFilteringSceneIndexBeforeCustomFiltering);