Skip to content

Commit

Permalink
HYDRA-1058/HYDRA-970 : GeomSubsets picking filter + Fix multi-selecti…
Browse files Browse the repository at this point in the history
…on when single-picking (#143)

* HYDRA-1058 : Rough basic structure for GeomSubset picking

* HYDRA-1058 : Attempt to add menu item to USD selection mode menu

* HYDRA-1058 : Functioning registering of menu items in USD selection menu

* HYDRA-1058 : Functional picking mode menu

* HYDRA-1058 : Cleanup picking code

* HYDRA-1058 : Cleanup pick interpretation code

* HYDRA-1058 : Move comment

* HYDRA-1058 : Avoid unnecessary idle events to create pick mode UI

* HYDRA-1058 : Move all GeomSubset MEL code into same script

* HYDRA-1058 : Cleanup GeomSubsets UI setup code

* HYDRA-1058 : Tear down UI on plugin unload and if MayaUSD is unloaded, and reconstruct it if MayaUSD is reloaded

* HYDRA-1058 : Simplify data keeping for UI teardown

* HYDRA-1058 : Remove print debugging statements

* HYDRA-1058 : Reorder operations

* HYDRA-1058 : Cleanup headers and rename method

* HYDRA-1058 : Add comments to explain how the pick mode menu is hooked up

* HYDRA-1058 : Remove headers

* HYDRA-1058 : Rename prims picking mode

* HYDRA-1058 : Adjust UI label

* HYDRA-1058 : Clarify naming

* HYDRA-1058 : Add comments

* HYDRA-1058 : Handle single selection pick mode

* HYDRA-1058 : Fix UI deletion and re-creation

* HYDRA-1058 : Remove select menu callback on UI teardown

* HYDRA-1058 : Fix callbacks clear command

* HYDRA-1058 : Fix UI deletion on plugin unload and re-load

* HYDRA-1058 : Remove prints

* HYDRA-1058 : Extend check for UI creation for consistency

* HYDRA-1058 : Add example scene

* HYDRA-1058 : Fix GeomSubsets pick resolution for Xform parent

* HYDRA-1058 : Use anonymous namespace variables instead of static

* HYDRA-1058 : Refactor plugin loading callbacks registration

* HYDRA-1058 : Remove superfluous UI code changes

* HYDRA-1058 : Add upper half geom subset sphere scene

* HYDRA-1058 : Simplify GeomSubset prim

* HYDRA-1058 : Add upper half geom subset cube scene

* HYDRA-1058 : Simplify cube scene

* HYDRA-1058 : Fixup

* HYDRA-1058 : Scene with instancing and materials

* HYDRA-1058 : Adjust scene

* HYDRA-1058 : Add test files

* HYDRA-1058 : Adjust test scene

* HYDRA-1058 : GeomSubsets picking marquee select test

* HYDRA-1058 : GeomSubset picking test

* HYDRA-1058 : Fallback picking test

* HYDRA-1058 : Instance GeomSubset picking test

* HYDRA-1058 : Instance fallback picking test

* HYDRA-1058 : Refactor and cleanup testUtils

* HYDRA-1058 : Cleanup testGeomSubsetsPicking.py

* HYDRA-1058 : Remove dead code

* HYDRA-1058 : Fixup

* HYDRA-1058 : Extract out common test code

* HYDRA-1058 : Extract out common test code for instance tests

* HYDRA-1058 : Cleanup tests

* HYDRA-1058 : Cleanup test headers and increase thoroughness

* HYDRA-1058 : Remove unused file

* HYDRA-1058 : Rename picking modes and use token-based approach for extensibility

* HYDRA-1058 : Small touchups

* HYDRA-1058 : Cleanup GeomSubsets mel file

* HYDRA-1058 : Add localization support

* HYDRA-1058 : Add copyright notices

* HYDRA-1058 : Add comments in test

* HYDRA-1058 : Handle build support for USD < 2403

* HYDRA-1058 : Skip GeomSubset picking tests if USD version does not support GeomSubset prims

* HYDRA-1058 : Debug OSX and Linux failure #1

* HYDRA-1058 : Debug OSX and Linux #2

* MAYA-xxxx <one line description>

* HYDRA-1058 : Debug OSX #1

* HYDRA-1058 : Adjust test setup to use markers for clicking

* HYDRA-1058 : Rename resolvePrimAndInstancePicking

* HYDRA-1058 : Fix typo

* HYDRA-1058 : Revert obsolete changes

* HYDRA-1058 : Cleanup testGeomSubsetsPicking.cpp

* HYDRA-1058 : Add link to MayaUSD register strings MEL file

* HYDRA-1058 : Add comment
  • Loading branch information
debloip-adsk authored Jul 9, 2024
1 parent 1a0384c commit 214f104
Show file tree
Hide file tree
Showing 25 changed files with 1,049 additions and 79 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ if(BUILD_HDSB_PLUGIN)
endif()
endif()

#------------------------------------------------------------------------------
# scripts
#------------------------------------------------------------------------------
add_subdirectory(scripts)

#------------------------------------------------------------------------------
# install
#------------------------------------------------------------------------------
Expand Down
110 changes: 97 additions & 13 deletions lib/mayaHydra/mayaPlugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MSceneMessage.h>

#include <memory>
#include <vector>
Expand All @@ -46,13 +47,6 @@

using namespace MayaHydra;

// Don't use smart pointers in the static vector: when Maya is doing its
// default "quick exit" that does not uninitialize plugins, the atexit
// destruction of the overrides in the vector will crash on destruction,
// because Hydra has already destroyed structures these rely on. Simply leak
// the render overrides in this case.
static std::vector<PXR_NS::MtohRenderOverride*> gsRenderOverrides;

#if defined(MAYAHYDRA_VERSION)
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
Expand All @@ -62,6 +56,20 @@ static std::vector<PXR_NS::MtohRenderOverride*> gsRenderOverrides;
#define PLUGIN_VERSION "Maya-Hydra experimental"
#endif

namespace {
const std::string kMayaHydraPluginName = "mayaHydra";
const std::string kMayaUsdPluginName = "mayaUsdPlugin";

// Don't use smart pointers in the static vector: when Maya is doing its
// default "quick exit" that does not uninitialize plugins, the atexit
// destruction of the overrides in the vector will crash on destruction,
// because Hydra has already destroyed structures these rely on. Simply leak
// the render overrides in this case.
std::vector<PXR_NS::MtohRenderOverride*> _renderOverrides;

std::vector<MCallbackId> _pluginLoadingCallbackIds;
}

void initialize()
{
Fvp::InitializationParams fvpInitParams;
Expand All @@ -83,6 +91,35 @@ void finalize()
MayaHydra::MayaColorPreferencesTranslator::deleteInstance();
}

void afterPluginLoadCallback( const MStringArray& strs, void* clientData )
{
for (const auto& str : strs) {
// If MayaUSD is being loaded, set up our GeomSubsets picking mode UI.
// This will re-create the "Select" menu callback if it has been previously torn down.
if (str.asChar() == kMayaUsdPluginName) {
MGlobal::executeCommand("if (`exists mayaHydra_GeomSubsetsPickMode_SetupUI`) { mayaHydra_GeomSubsetsPickMode_SetupUI; }");
break;
}
}
}

void beforePluginUnloadCallback( const MStringArray& strs, void* clientData )
{
for (const auto& str : strs) {
// If MayaUSD is being unloaded, tear down our GeomSubsets picking mode UI.
// This resets the variables used to keep track of the UI elements' existence,
// and allows us to recreate them if MayaUSD is reloaded.
// We also do the same if mayaHydra is about to be unloaded : we can't rely on
// the deletion procedure registered through registerUI, as it seems the global
// variables tracking our UI elements have been reset at that point for some reason.
auto strChar = str.asChar();
if (strChar == kMayaUsdPluginName || strChar == kMayaHydraPluginName) {
MGlobal::executeCommand("mayaHydra_GeomSubsetsPickMode_TeardownUI");
break;
}
}
}

PLUGIN_EXPORT MStatus initializePlugin(MObject obj)
{
MString experimental("mayaHydra is experimental.");
Expand Down Expand Up @@ -118,11 +155,54 @@ PLUGIN_EXPORT MStatus initializePlugin(MObject obj)
auto mtohRenderer = std::make_unique<PXR_NS::MtohRenderOverride>(desc);
MStatus status = renderer->registerOverride(mtohRenderer.get());
if (status == MS::kSuccess) {
gsRenderOverrides.push_back(mtohRenderer.release());
_renderOverrides.push_back(mtohRenderer.release());
}
}
}

if (!plugin.registerUIStrings(nullptr, "mayaHydra_registerUIStrings")) {
ret = MS::kFailure;
ret.perror("Error registering mayaHydra UI string resources.");
return ret;
}

if (!plugin.registerUI(
"mayaHydra_registerUI_load",
"mayaHydra_registerUI_unload",
"mayaHydra_registerUI_batch_load",
"mayaHydra_registerUI_batch_unload"))
{
ret = MS::kFailure;
ret.perror("Error registering mayaHydra UI procedures.");
return ret;
}

auto registerPluginLoadingCallback = [&](MSceneMessage::Message pluginLoadingMessage, MMessage::MStringArrayFunction callback) {
MStatus callbackStatus;
MCallbackId callbackId = MSceneMessage::addStringArrayCallback(
pluginLoadingMessage,
callback,
nullptr,
&callbackStatus);
if (callbackStatus) {
_pluginLoadingCallbackIds.push_back(callbackId);
} else {
ret = MS::kFailure;
ret.perror("Error registering mayaHydra plugin loading callback.");
}
};

std::vector<std::pair<MSceneMessage::Message, MMessage::MStringArrayFunction>> pluginLoadingCallbacks = {
{MSceneMessage::Message::kAfterPluginLoad, afterPluginLoadCallback},
{MSceneMessage::Message::kBeforePluginUnload, beforePluginUnloadCallback}
};
for (const auto& pluginLoadingCallback : pluginLoadingCallbacks) {
registerPluginLoadingCallback(pluginLoadingCallback.first, pluginLoadingCallback.second);
if (!ret) {
return ret;
}
}

initialize();

return ret;
Expand All @@ -132,19 +212,23 @@ PLUGIN_EXPORT MStatus uninitializePlugin(MObject obj)
{
finalize();

for (const auto& callbackId : _pluginLoadingCallbackIds) {
MSceneMessage::removeCallback(callbackId);
}

MFnPlugin plugin(obj, "Autodesk", PLUGIN_VERSION, "Any");
MStatus ret = MS::kSuccess;
if (auto* renderer = MHWRender::MRenderer::theRenderer()) {
for (unsigned int i = 0; i < gsRenderOverrides.size(); i++) {
renderer->deregisterOverride(gsRenderOverrides[i]);
delete gsRenderOverrides[i];
for (unsigned int i = 0; i < _renderOverrides.size(); i++) {
renderer->deregisterOverride(_renderOverrides[i]);
delete _renderOverrides[i];
}
}

gsRenderOverrides.clear();
_renderOverrides.clear();

// Clear any registered callbacks
MGlobal::executeCommand("callbacks -cc mayaHydra;");
MGlobal::executeCommand("callbacks -cc -owner mayaHydra;");

if (!plugin.deregisterCommand(MtohViewCmd::name)) {
ret = MS::kFailure;
Expand Down
Loading

0 comments on commit 214f104

Please sign in to comment.