I’ve recently returned to the idea of using scriptNodes to run scriptJobs, I have a test scene which works when loaded and when referenced once, but when you try to reference it a second time doesn’t work properly.
The test script is really two parts; the first part creates geometry and attributes and the second part creates the scriptNode and the scriptJobs.
The final scene is a cube and a circle (which is parented to the cube) which has an attribute on it which when changed prints a specified local translation axis value of the cube.
Here’s the script:
import maya.cmds as cmds
theCube = cmds.polyCube()
theCircle = cmds.circle()
cmds.parent(theCircle[0], theCube[0])
cmds.addAttr(theCircle[0], ln="printTranslate", nn="Print Translate", at="enum", en="X:Y:Z", h=False, k=True)
cmds.addAttr(theCircle[0], ln="theCube", nn="The Cube", at="message")
cmds.connectAttr(theCube[0] + ".message", theCircle[0] + ".theCube")
# theString will be used in the scriptNode
theString = '''import maya.cmds as cmds
# Get all transform nodes in the scene
theObjectList = cmds.ls(tr=True)
# Create an empty list
theCircleList = []
# For every object in theObjectList, if the object contains a printTranslate attribute, append it to theCircleList
for theObject in theObjectList:
if cmds.attributeQuery("printTranslate", n=theObject, ex=True):
theCircleList.append(theObject)
# Get all the current scriptJobs in the scene
theJobList = cmds.scriptJob(lj=True)
# For every object in theCircleList, if no scriptJobs exist containing it's name, then create a scriptJob with the
# printTranslate definition
for theCircle in theCircleList:
# Get the parent cube through the message attribute
theCubeMsg = cmds.listConnections(theCircle + ".theCube")[0]
# Create local variable with a zero value
exists = 0
# For every job in theJob list, if theJob contains theCircle's name then add 1 to the exists variable
for theJob in theJobList:
if theJob.find(theCircle) != -1:
exists += 1
# If exists is zero create a definition which prints one of theCubeMsg's translation axis values based on
# the printTranslate attribute value
if exists == 0:
def printTranslate():
theAttr = cmds.getAttr(theCircle + ".printTranslate")
theStringAttr = cmds.getAttr(theCircle + ".printTranslate", asString=True)
theObjectTranslate = cmds.getAttr(theCubeMsg + ".translate")[0][theAttr]
# Format the string which will be printed
print(theCubeMsg + " translate " + theStringAttr + ": " + str(theObjectTranslate))
cmds.scriptJob(ac=[theCircle + ".printTranslate", printTranslate])'''
cmds.scriptNode(st=2, bs=theString, n="test", stp="python")
The problem which happens when referencing a second time is this:
Lets say we use the namespace string “test1”, when you change the circle’s printTranslate value to, say, “X” we get “test1_:pCube1 translate X: 0” as expected. When we reference the file again using the namespace string “test2” and change the test2:nurbsCircle1.printTranslate it prints “test1_:pCube1 translate X: 0”, it always test1_:pCube1 using the current printTranslate attribute on test1_:nurbsCircle1.
I’ve tried a bunch of stuff but I just can’t seem to get both to work when referenced! Any help would be much appreciated.
Thanks,
-Harry