Maya hypershade create new tab via python

Hey
As the topic says, any idea?
I’m inside hyperShadePanel.mel trying to figure out but after 4h I got zip sigh.
Any1?
Thanks!

I don’t think you can, unfortunately.
Well, I mean, technically you can do almost anything, and it’s clearly possible already to perform this process in MEL, but what I mean is that the process involves so many interrelated and co-dependent global and local procedures inside hyperShadPanel.mel (all of which are also very naively written so that their core functionality is irretrievably intertwined with the UI functionality), that it would require you to write 10s or even maybe 100s of lines of Python to replicate it sufficiently.
And you cannot simply call the relevent mel functions from Python or MEL, as they all rely entirely on the UI and local procs (that cannot be run manually).

I figured it out aventually. Ended up being >

cmds.nodeEditor("hyperShadePrimaryNodeEditor", e=True, createTab=[-1, tabName])

Hope it helps next poor soul that try to work in maya API sigh…
GL!

ps. It was fun reading thos MEL scripts… specially when release date was like 1996…1998…2006 Like. Nice one AD nice one.

1 Like

In fact, it’s very simple.
For example:
cmds.nodeEditor('hyperShadePrimaryNodeEditor', edit = True, createTab = [-1, 'My_New_Tab'])
There are MEL/Python commands for convenient work with panels, including panels of the scriptedPanel type (the Hypershade panel belongs to this type of panels).
It is important to understand that the ‘Hypershade’ editor is just a type of Node Editor.
Therefore, you can use the MEL/Python command: nodeEditor
There are also MEL/Python commands for editing a shader in a hypergraph.
You can use any MEL commands in Python with the maya.mel.eval() command.
To do this, simply import: import maya.mel as mel.

Before creating a new tab, we need to make sure that the ‘Hypershade’ editor panel is open.
If it is not, then we will open the ‘Hypershade’ editor panel.
But, there is one nuance:
Usually, the panel name is hyperShadePanel1.
But, in different localizations, the Hypershade panel may not have the usual English label.
Therefore, this point should be checked and taken into account.

import maya.mel as mel
import maya.cmds as cmds


# Checking the availability of the 'Hypershade' panel.
# If not available, then restart the panel.
# If the panel is in a separate window, then get the name of this window.
# Instead of reinventing the wheel and writing a dozen lines of code, we will take advantage of the maya.mel.eval() command
#To get a label for Unlocalized Panel: 'unlocalizedPanelLabel'.

hsh_label = 'Hypershade'
hsh_panel = cmds.getPanel(withLabel = hsh_label)
if not hsh_panel:
    en_label = mel.eval('unlocalizedPanelLabel("{}")'.format(hsh_label))
    if(en_label != hsh_label):
        hsh_panel = cmds.getPanel(withLabel = en_label)

if hsh_panel:
    hsh_window_name = '{}Window'.format(hsh_panel)
    cmds.scriptedPanel(hsh_panel, edit = True, tearOffRestore = True)

else:
    cmds.scriptedPanel(label = hsh_label, type = 'hyperShadePanel', tearOffRestore = True)


# Check if Node Editor 'hyperShadePrimaryNodeEditor' exists
# Check if 'hyperShadePrimaryNodeEditor' allows creating new tabs.
# If this is true, then create a new tab named 'My_New_Tab'.
# If a tab with this name already exists, then an increasing number will be added to the name.
# Set the position index for the new tab outside the range: -1
# This means that the tab will be created and added to the end of the existing tabs.

hsh_name = 'hyperShadePrimaryNodeEditor'
hsh_exists = cmds.nodeEditor(hsh_name, exists = True)
if hsh_exists:
    hsh_allow_new_tabs = cmds.nodeEditor(hsh_name, query = True, allowNewTabs = True)
    if (hsh_exists and hsh_allow_new_tabs):
        hsh_query = cmds.nodeEditor(hsh_name, query = True, restoreLastClosedTab = True)
        cmds.iconTextButton('tabsRLCTButton', edit = True, enable = hsh_query)
        cmds.nodeEditor(hsh_name, edit = True, createTab = [-1, 'My_New_Tab'])

commands (Python):

scriptedPanel

nodeEditor

hyperShade

getPanel

MEL/Python communication

Important differences between MEL and Python

P.S.:
But difficulties will arise if you want a little more.
For example, getting indexes of all tabs together with their labels (including recently closed tabs :slight_smile: ).
Solutions for such problems are not so obvious…