Skip to content

Commit

Permalink
HYDRA-934 : Fix reload of a scene with a Usd stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanierd-adsk committed Mar 29, 2024
1 parent ef17d50 commit 2161117
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
55 changes: 47 additions & 8 deletions lib/mayaHydra/hydraExtensions/sceneIndex/registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#include <maya/MFnDependencyNode.h>
#include <maya/MItDag.h>
#include <maya/MMessage.h>
#include <maya/MNodeMessage.h>
#include <maya/MSceneMessage.h>
#include <maya/MFileIO.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>

Expand Down Expand Up @@ -218,11 +219,16 @@ MayaHydraSceneIndexRegistry::MayaHydraSceneIndexRegistry(const std::shared_ptr<F
id = MDGMessage::addNodeAddedCallback(
_SceneIndexNodeAddedCallback, kMayaUsdProxyShapeNode, this, &status);//We need only to monitor the MayaUsdProxyShapeNode
if (TF_VERIFY(status == MS::kSuccess, "NodeAdded callback registration failed."))
_sceneIndexDagNodeMessageCallbacks.append(id);
_DGCallbackIds.append(id);
id = MDGMessage::addNodeRemovedCallback(
_SceneIndexNodeRemovedCallback, kMayaUsdProxyShapeNode, this, &status);//We need only to monitor the MayaUsdProxyShapeNode
if (TF_VERIFY(status == MS::kSuccess, "NodeRemoved callback registration failed."))
_sceneIndexDagNodeMessageCallbacks.append(id);
_DGCallbackIds.append(id);

//Because we cannot process a node while loading a maya file, we are storing them in an array in _SceneIndexNodeAddedCallback
// and process them once the after load has completed through _AfterOpenCallback.
_AfterOpenCBId = MSceneMessage::addCallback(MSceneMessage::kAfterOpen, _AfterOpenCallback, this, &status);
TF_VERIFY(status == MS::kSuccess, "MSceneMessage::kAfterOpen callback registration failed.");

static const MTypeId MAYAUSD_PROXYSHAPE_ID(0x58000095); //Hardcoded

Expand Down Expand Up @@ -267,8 +273,12 @@ MayaHydraSceneIndexRegistry::GetRegistrations() const

MayaHydraSceneIndexRegistry::~MayaHydraSceneIndexRegistry()
{
MDGMessage::removeCallbacks(_sceneIndexDagNodeMessageCallbacks);
_sceneIndexDagNodeMessageCallbacks.clear();
MDGMessage::removeCallbacks(_DGCallbackIds);
_DGCallbackIds.clear();
if (_AfterOpenCBId){
MSceneMessage::removeCallback(_AfterOpenCBId);
}
_AfterOpenCBId = 0;
_registrationsByObjectHandle.clear();
_registrations.clear();
}
Expand Down Expand Up @@ -451,10 +461,28 @@ void MayaHydraSceneIndexRegistry::_AddSceneIndexForNode(MObject& dagNode)

void MayaHydraSceneIndexRegistry::_SceneIndexNodeAddedCallback(MObject& dagNode, void* clientData)
{
if (dagNode.isNull() || dagNode.apiType() != MFn::kPluginShape)
if (dagNode.isNull() || dagNode.apiType() != MFn::kPluginShape){
return;
auto renderOverride = static_cast<MayaHydraSceneIndexRegistry*>(clientData);
renderOverride->_AddSceneIndexForNode(dagNode);
}

auto mayaHydraSceneIndexRegistry = static_cast<MayaHydraSceneIndexRegistry*>(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<MayaHydraSceneIndexRegistry*>(clientData);
mayaHydraSceneIndexRegistry->_ProcessNodesAfterOpen();
}

void MayaHydraSceneIndexRegistry::_SceneIndexNodeRemovedCallback(MObject& dagNode, void* clientData)
Expand All @@ -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
15 changes: 14 additions & 1 deletion lib/mayaHydra/hydraExtensions/sceneIndex/registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <maya/MNodeMessage.h>
#include <maya/MObject.h>
#include <maya/MObjectHandle.h>
#include <maya/MObjectArray.h>

#include <ufe/rtid.h>
#include <ufe/ufe.h>

Expand Down Expand Up @@ -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<Fvp::RenderIndexProxy> _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
Expand Down
8 changes: 4 additions & 4 deletions lib/mayaHydra/mayaPlugin/renderOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2161117

Please sign in to comment.