Skip to content

Instantly share code, notes, and snippets.

@Alan-Green337
Created November 21, 2020 21:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Alan-Green337/bd62ab2f98cdfa9772720df3540f111a to your computer and use it in GitHub Desktop.
Save Alan-Green337/bd62ab2f98cdfa9772720df3540f111a to your computer and use it in GitHub Desktop.
fix to prevent script nodes from executing automatically in autodesk Maya
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# PLEASE READ!
# put this script in this directory (OR add it to your userSetup.py file. this code MUST be in userSetup.py to work):
# "%USERPROFILE%\Documents\maya\####\scripts"
# or
# "~/maya/####/scripts"
# this script creates a maya "callback" that executes every time ANY file is created/loaded/referenced in maya.
# The "callback" kills all <SCRIPT NODES>, making sure the scene is SAFE for users
# >>> Autodesk Maya Scanner tool does NOT clean <SCRIPT NODES> in references <<<
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
import maya.cmds as cmds
import maya.OpenMaya
def killAllScriptNodes(clientData):
attributeSettings = {
"scriptType": 0, # enum is "Demand" (dont want script to run randomly even if its empty)
"before": "", # clear before script
"after": "", # clear after script
"sourceType": 0, # MEL
"ignoreReferenceEdits": 0} #Record reference edits (so the scipt works with references)
scriptNodes = cmds.ls(typ="script") #check for any scriptnodes are in scene
scriptNodesBefore = scriptNodes
cmds.delete(scriptNodes) #delete them! theyre not safe for work
scriptNodes = cmds.ls(typ="script") #check again
scriptNodesAfter = scriptNodes
scriptNodesDeleted = len(scriptNodesBefore) - len(scriptNodesAfter)
if scriptNodesDeleted:
print(str(scriptNodesDeleted) + " Script Nodes deleted")
for sNode in scriptNodes:
for attr, val in attributeSettings.items():
attrType = cmds.attributeQuery(attr, node=sNode, attributeType=1)
if attrType == "typed":
cmds.setAttr(sNode + "." + attr, val, typ="string")
else:
cmds.setAttr(sNode + "." + attr, val)
scriptNodesEmptied = len(cmds.ls(typ="script"))
if scriptNodesEmptied:
print(str(scriptNodesEmptied) + " Script Nodes emptied")
else:
cmds.confirmDialog(m="Could not fix script nodes!", t="Error")
maya.OpenMaya.MSceneMessage.addCallback(maya.OpenMaya.MSceneMessage.kAfterSceneReadAndRecordEdits, killAllScriptNodes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment