Last active
June 11, 2021 18:45
-
-
Save tpoveda/19ad4cb9d9459fc4d284cbb8aed5cea6 to your computer and use it in GitHub Desktop.
Functions to close/disable Maya Node Editors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
This gist contains: | |
- Function to close all currently opened Node Editors | |
- Python context that allows to disable new nodes addition to Node Editor while a function is being executed | |
""" | |
import contextlib | |
import maya.cmds as cmds | |
def get_node_editors(): | |
""" | |
Returns all node editors panels opened in Maya. | |
:return: list of node editors UI names. | |
:rtype: list(str) | |
""" | |
found = list() | |
for panel in cmds.getPanel(type='scriptedPanel'): | |
if not cmds.scriptedPanel(panel, query=True, type=True) == 'nodeEditorPanel': | |
continue | |
found.append(panel + 'NodeEditorEd') | |
return found | |
@contextlib.contextmanager | |
def disable_node_editor_addition(): | |
""" | |
Python context decorator that disables Node Editor nodes addition inside context. | |
usage: | |
with disable_node_editor_addition(): | |
# this code will not add new nodes to currently opened Maya node editors | |
pass | |
""" | |
node_editors = get_node_editors() | |
additive_state_dict = dict() | |
for editor in node_editors: | |
current_value = cmds.nodeEditor(editor, query=True, ann=True) | |
additive_state_dict[editor] = current_value | |
try: | |
for editor in node_editors: | |
cmds.nodeEditor(editor, e=True, ann=False) | |
yield | |
finally: | |
for editor in node_editors: | |
cmds.nodeEditor(editor, e=True, ann=additive_state_dict[editor]) | |
def close_opened_node_editors(): | |
""" | |
Closes all currently opened Maya Node Editors UIs. | |
""" | |
for editor in get_node_editors(): | |
cmds.deleteUI(editor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment