From e6a94708eabd5be9f60ba23bb0fef91703a3c212 Mon Sep 17 00:00:00 2001 From: Pierre Baillargeon Date: Tue, 10 Oct 2023 12:12:56 -0400 Subject: [PATCH 1/2] EMSUSD-676 use the outliner menu in the viewport --- plugin/adsk/scripts/USDMenuProc.mel | 293 +--------------------------- 1 file changed, 3 insertions(+), 290 deletions(-) diff --git a/plugin/adsk/scripts/USDMenuProc.mel b/plugin/adsk/scripts/USDMenuProc.mel index dc9fe2bb82..6eb3e65807 100644 --- a/plugin/adsk/scripts/USDMenuProc.mel +++ b/plugin/adsk/scripts/USDMenuProc.mel @@ -24,302 +24,15 @@ proc string expandToSelection(string $obj) } } -proc int canEditAsMaya(string $obj) -{ - if (!hasPrimUpdater()) - return 0; - - if (size($obj) != 0) { - return `python("from mayaUsd.lib import PrimUpdaterManager; PrimUpdaterManager.canEditAsMaya('" + $obj + "')")`; - } - return 0; -} - -proc int canDupAsMaya(string $obj) -{ - string $typ = `nodeType $obj`; - return $typ != "MayaReference"; -} - -global proc mayaUsdMenu_editAsMaya(string $obj) -{ - if (!hasPrimUpdater()) - return; - - if (size($obj) != 0) { - mayaUsdEditAsMaya $obj; - } -} - -global proc mayaUsdMenu_duplicate(string $ufePath) -{ - if (!hasPrimUpdater()) - return; - - if (size($ufePath)) { - mayaUsdDuplicate $ufePath "|world"; - } -} - -global proc mayaUsdMenu_assignNewMaterial(string $ufePath, string $material) -{ - if (!hasPrimUpdater()) - return; - - if (size($ufePath) != 0 && size($material) != 0) { - string $temp = `python("import ufe;\ - ufePath = ufe.PathString.path('" + $ufePath + "');\ - item = ufe.Hierarchy.createItem(ufePath);\ - contextOps = ufe.ContextOps.contextOps(item);\ - cmd = contextOps.doOpCmd(['Assign New Material', '', '" + $material + "']);\ - ufe.UndoableCommandMgr.instance().executeCmd(cmd);")`; - } -} - -global proc mayaUsdMenu_assignExistingMaterial(string $ufePath, string $material) -{ - if (!hasPrimUpdater()) - return; - - if (size($ufePath) != 0 && size($material) != 0) { - string $temp = `python("import ufe;\ - ufePath = ufe.PathString.path('" + $ufePath + "');\ - item = ufe.Hierarchy.createItem(ufePath);\ - contextOps = ufe.ContextOps.contextOps(item);\ - cmd = contextOps.doOpCmd(['Assign Existing Material', '', '" + $material + "']);\ - ufe.UndoableCommandMgr.instance().executeCmd(cmd);")`; - } -} - -global proc mayaUsdMenu_addNewMaterials(string $ufePath) -{ - string $materials[] = `mayaUsdGetMaterialsFromRenderers`; - string $previousRendererString; - string $menuItem; - for ($material in $materials) - { - // We want our materials grouped into submenus according to their path in the hierarchy. - string $rendererAndMaterial[]; - $numTokens = `tokenize $material "/" $rendererAndMaterial`; - - // Expects tokenized string in the form "RendererName/Material Name|MaterialIdentifer" - if ($numTokens != 2) - continue; - - string $materialName = $rendererAndMaterial[$numTokens - 1]; - - // Get a string containing the path to the material without the name of the material itself. - string $renderer[] = $rendererAndMaterial; - stringArrayRemoveAtIndex($numTokens - 1, $renderer); - string $rendererString = stringArrayToString($renderer, "/"); - - // Create a new submenu for each path. - if ($previousRendererString != $rendererString) - { - if ($menuItem != "") - { - setParent -menu ..; - } - - $previousRendererString = $rendererString; - $menuItem = `menuItem -subMenu true -tearOff false -label $rendererString`; - } - - string $labelAndIdentifier[]; - $numTokens = `tokenize $materialName "|" $labelAndIdentifier`; - - // Expects tokenized string in the form "Material Name|MaterialIdentifer" - if ($numTokens != 2) - continue; - - menuItem -label $labelAndIdentifier[0] -command ("mayaUsdMenu_assignNewMaterial \"" + $ufePath + "\" \"" + $labelAndIdentifier[1] + "\""); - } - setParent -menu ..; -} - -global proc mayaUsdMenu_addExistingMaterials(string $ufePath) -{ - string $materials[] = `mayaUsdGetMaterialsInStage $ufePath`; - string $previousPathString; - string $menuItem; - for ($material in $materials) - { - // We want our materials grouped into submenus according to their path in the hierarchy. - string $pathAndMaterial[]; - $numTokens = `tokenize $material "/" $pathAndMaterial`; - - string $materialName = $pathAndMaterial[$numTokens - 1]; - - // Get a string containing the path to the material without the name of the material itself. - string $path[] = $pathAndMaterial; - stringArrayRemoveAtIndex($numTokens - 1, $path); - string $pathString = "/" + stringArrayToString($path, "/"); - - // Create a new submenu for each path. - if ($previousPathString != $pathString) - { - if ($menuItem != "") - { - setParent -menu ..; - } - - $previousPathString = $pathString; - $menuItem = `menuItem -subMenu true -tearOff false -label $pathString`; - } - - menuItem -label $materialName -parent $menuItem -command ("mayaUsdMenu_assignExistingMaterial \"" + $ufePath + "\" \"" + $material + "\""); - } - setParent -menu ..; -} - -global proc mayaUsdMenu_unassignMaterial(string $ufePath) -{ - if (size($ufePath) != 0) { - string $temp = `python("import ufe;\ - ufePath = ufe.PathString.path('" + $ufePath + "');\ - item = ufe.Hierarchy.createItem(ufePath);\ - contextOps = ufe.ContextOps.contextOps(item);\ - cmd = contextOps.doOpCmd(['Unassign Material']);\ - ufe.UndoableCommandMgr.instance().executeCmd(cmd);")`; - } -} - -global proc mayaUsdMenu_EditAsMayaDataOptionsCallback(string $optionsString) -{ - optionVar -stringValue usdMaya_EditAsMayaDataOptions $optionsString; -} - -global proc mayaUsdMenu_EditAsMayaDataOptionsReset(string $obj) -{ - optionVar -remove usdMaya_EditAsMayaDataOptions; - mayaUsdMenu_EditAsMayaDataOptions($obj); -} - -global proc mayaUsdMenu_ApplyEditAsMayaDataOptions(string $obj) -{ - mayaUsdTranslatorImport ("", "query", "", "mayaUsdMenu_EditAsMayaDataOptionsCallback"); - hideOptionBox; - - if (size($obj)) { - waitCursor -state 1; - mayaUsdMenu_editAsMaya $obj; - waitCursor -state 0; - } -} - -global proc mayaUsdMenu_EditAsMayaDataOptions(string $obj) -{ - global string $gOptionBoxOptions; - $gOptionBoxOptions = "noToolAndAction"; - string $layout = getOptionBox(); - setParent $layout; - - setOptionBoxTitle(getMayaUsdString("kEditAsMayaDataOptions")); - - string $optionsString = ";readAnimData=1"; - if (`optionVar -exists usdMaya_EditAsMayaDataOptions`) { - $optionsString = `optionVar -query usdMaya_EditAsMayaDataOptions`; - } - - string $optionsScroll = `scrollLayout -cr true editAsMayaDataOptionsFrame`; - mayaUsdTranslatorImport ($optionsScroll, "post", $optionsString, "mayaUsdMenu_EditAsMayaDataOptionsCallback"); - - string $applyCloseBtn = getOptionBoxApplyAndCloseBtn(); - button -edit -label `getMayaUsdString("kEditAsMayaData")` - -command ("mayaUsdMenu_ApplyEditAsMayaDataOptions(\"" + $obj + "\");") - $applyCloseBtn; - - string $applyBtn = getOptionBoxApplyBtn(); - button -edit - -command ("mayaUsdMenu_ApplyEditAsMayaDataOptions(\"\");") - $applyBtn; - - string $resetBtn = getOptionBoxResetBtn(); - button -edit - -command ("mayaUsdMenu_EditAsMayaDataOptionsReset(\"" + $obj + "\");") - $resetBtn; - - string $saveBtn = getOptionBoxSaveBtn(); - button -edit - -command "mayaUsdTranslatorImport(\"\", \"query\", \"\", \"mayaUsdMenu_EditAsMayaDataOptionsCallback\")" - $saveBtn; - - string $helpItem = getOptionBoxHelpItem(); - if ($helpItem != "") { - menuItem -edit - -label `getMayaUsdString("kHelpOnEditAsMayaDataOptions")` - -command ("python(\"from mayaUsdUtils import showHelpMayaUSD; showHelpMayaUSD('UsdImportOptionsConfig');\")") - $helpItem; - } - - showOptionBox(); - - global string $gOptionBox; - window -e -height 260 $gOptionBox; -} - // On right click in the viewport, either through selection or through a pick // hit, Maya will look for a procedure called $runTimeName + "MenuProc". // and call it. When the selection or pick hit is a USD prim, this calls the // following procedure. global proc USDMenuProc(string $parent, string $obj) { - if (!hasPrimUpdater()) - return; - $obj = expandToSelection($obj); - if (size($obj) != 0) - { - popupMenu -e -dai $parent; - setParent -menu $parent; - - setParent -menu ..; - if (canEditAsMaya($obj)) { - menuItem -label `getMayaUsdString("kEditAsMayaData")` -image "edit_as_Maya.png" -command ("{waitCursor -state 1; mayaUsdMenu_editAsMaya \"" + $obj + "\"; waitCursor -state 0;}"); - menuItem -command ("{mayaUsdMenu_EditAsMayaDataOptions \"" + $obj + "\";}") -optionBox true; - } - if (canDupAsMaya($obj)) { - menuItem -label "Duplicate As Maya Data" -command ("{waitCursor -state 1; mayaUsdMenu_duplicate \"" + $obj + "\"; waitCursor -state 0;}"); - } - - menuItem -divider true; - - // Material-related options should only be shown if the right-clicked object is of a type known to support material bindings. - int $allowMaterialFunctions = `mayaUsdMaterialBindings $obj -canAssignMaterialToNodeType true`; - if ($allowMaterialFunctions) - { - // "Assign New Material" from a list of materials provided by the renderers. - menuItem -subMenu true -tearOff false -label "Assign New Material"; - { - mayaUsdMenu_addNewMaterials($obj); - } - setParent -menu ..; - - // Only show if we have materials in the stage. - string $materials[] = `mayaUsdGetMaterialsInStage $obj`; - if (size($materials) > 0) - { - // "Assign Existing Material" from a list of materials in the selected item's stage. - menuItem -subMenu true -tearOff false -label "Assign Existing Material"; - { - mayaUsdMenu_addExistingMaterials($obj); - } - setParent -menu ..; - } - - // Only show the following entry if the object actually has a material assigned. - int $hasMaterialBindingAPI = `mayaUsdMaterialBindings $obj -hasMaterialBinding true`; - if ($hasMaterialBindingAPI) - { - // Unassign (aka "unbind") the material from the selected object. - menuItem -label "Unassign Material" -command ("{waitCursor -state 1; mayaUsdMenu_unassignMaterial \"" + $obj + "\"; waitCursor -state 0;}"); - } - } - } + if (size($obj) == 0) + return; - // Allow the user to define a proc with additional menu items - if (`exists USDUserMenuProc`) - { - USDUserMenuProc($parent,$obj); - } + python("import maya.app.ufe.outlinerSupport; maya.app.ufe.outlinerSupport.buildNonMayaContextMenu('''" + $obj + "''')"); } \ No newline at end of file From 64c29183e675d04da7a11a4815b1d289c070c21a Mon Sep 17 00:00:00 2001 From: Pierre Baillargeon Date: Wed, 11 Oct 2023 11:05:25 -0400 Subject: [PATCH 2/2] EMSUSD-676 put back necessary code used elsewhere --- plugin/adsk/scripts/USDMenuProc.mel | 82 ++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/plugin/adsk/scripts/USDMenuProc.mel b/plugin/adsk/scripts/USDMenuProc.mel index 6eb3e65807..075a815db4 100644 --- a/plugin/adsk/scripts/USDMenuProc.mel +++ b/plugin/adsk/scripts/USDMenuProc.mel @@ -24,6 +24,80 @@ proc string expandToSelection(string $obj) } } +global proc mayaUsdMenu_EditAsMayaDataOptionsCallback(string $optionsString) +{ + optionVar -stringValue usdMaya_EditAsMayaDataOptions $optionsString; +} + +global proc mayaUsdMenu_EditAsMayaDataOptionsReset(string $obj) +{ + optionVar -remove usdMaya_EditAsMayaDataOptions; + mayaUsdMenu_EditAsMayaDataOptions($obj); +} + +global proc mayaUsdMenu_ApplyEditAsMayaDataOptions(string $obj) +{ + mayaUsdTranslatorImport ("", "query", "", "mayaUsdMenu_EditAsMayaDataOptionsCallback"); + hideOptionBox; + + if (size($obj)) { + waitCursor -state 1; + mayaUsdMenu_editAsMaya $obj; + waitCursor -state 0; + } +} + +global proc mayaUsdMenu_EditAsMayaDataOptions(string $obj) +{ + global string $gOptionBoxOptions; + $gOptionBoxOptions = "noToolAndAction"; + string $layout = getOptionBox(); + setParent $layout; + + setOptionBoxTitle(getMayaUsdString("kEditAsMayaDataOptions")); + + string $optionsString = ";readAnimData=1"; + if (`optionVar -exists usdMaya_EditAsMayaDataOptions`) { + $optionsString = `optionVar -query usdMaya_EditAsMayaDataOptions`; + } + + string $optionsScroll = `scrollLayout -cr true editAsMayaDataOptionsFrame`; + mayaUsdTranslatorImport ($optionsScroll, "post", $optionsString, "mayaUsdMenu_EditAsMayaDataOptionsCallback"); + + string $applyCloseBtn = getOptionBoxApplyAndCloseBtn(); + button -edit -label `getMayaUsdString("kEditAsMayaData")` + -command ("mayaUsdMenu_ApplyEditAsMayaDataOptions(\"" + $obj + "\");") + $applyCloseBtn; + + string $applyBtn = getOptionBoxApplyBtn(); + button -edit + -command ("mayaUsdMenu_ApplyEditAsMayaDataOptions(\"\");") + $applyBtn; + + string $resetBtn = getOptionBoxResetBtn(); + button -edit + -command ("mayaUsdMenu_EditAsMayaDataOptionsReset(\"" + $obj + "\");") + $resetBtn; + + string $saveBtn = getOptionBoxSaveBtn(); + button -edit + -command "mayaUsdTranslatorImport(\"\", \"query\", \"\", \"mayaUsdMenu_EditAsMayaDataOptionsCallback\")" + $saveBtn; + + string $helpItem = getOptionBoxHelpItem(); + if ($helpItem != "") { + menuItem -edit + -label `getMayaUsdString("kHelpOnEditAsMayaDataOptions")` + -command ("python(\"from mayaUsdUtils import showHelpMayaUSD; showHelpMayaUSD('UsdImportOptionsConfig');\")") + $helpItem; + } + + showOptionBox(); + + global string $gOptionBox; + window -e -height 260 $gOptionBox; +} + // On right click in the viewport, either through selection or through a pick // hit, Maya will look for a procedure called $runTimeName + "MenuProc". // and call it. When the selection or pick hit is a USD prim, this calls the @@ -31,8 +105,12 @@ proc string expandToSelection(string $obj) global proc USDMenuProc(string $parent, string $obj) { $obj = expandToSelection($obj); - if (size($obj) == 0) - return; python("import maya.app.ufe.outlinerSupport; maya.app.ufe.outlinerSupport.buildNonMayaContextMenu('''" + $obj + "''')"); + + // Allow the user to define a proc with additional menu items + if (`exists USDUserMenuProc`) + { + USDUserMenuProc($parent,$obj); + } } \ No newline at end of file