In order to get well acquainted with Python for Nuke I am creating a small game that takes place in the Node Graph, but I hit a snag while trying to move my "character" around using a function. The character is a Dot and the function is trying to read its position in X and Y to determine in which direction it can move, then provide the player with those options and finally move the character in the direction chosen. The function must receive the character as an input but that's where I am having trouble, this is the simplified version of that part of the code:
global currentPosX
global currentPosY
currentPosX = 0
currentPosY = 0
def moveArea(self, node):
charT = node
print = currentPosX
currentPosX = charT['xpos'].value()
currentPosY = charT['ypos'].value()
char = nuke.nodes.Dot(hide_input=1, xpos=490, ypos=70)
moveArea(char)
I have tried many things and this code you see here is where I just couldn't think of any other options, I believe the problem is in how I input the 'char' node into the function but I could not find any resource that made it clear. Any help would be appreciated!
I've created a simplified function with some useful nuke commands thrown in that might be useful to you. For example, a function outside of a class doesn't need a self argument. This code only creates a characterDot if one doesn't already exist, so you'll be able to execute it multiple times and see the dot move further.
def moveArea(node, moveX=0, moveY=0):
# query current position of input node
currentPosX = node['xpos'].value()
currentPosY = node['ypos'].value()
# calculate new position based on arguments
newPosX = currentPosX + moveX
newPosY = currentPosY + moveY
# actually move the node
print "moving %s from (%s,%s) to (%s,%s)" % (node.name(), currentPosX, currentPosY, newPosX, newPosY)
node['xpos'].setValue(newPosX)
node['ypos'].setValue(newPosY)
# create the node for the very first time with a unique name (if it doesn't exist)
if not nuke.exists('characterDot'):
characterDot = nuke.nodes.Dot(hide_input=1, xpos=490, ypos=70, name='characterDot')
# find the node based on the name whenever you want
char = nuke.toNode('characterDot')
# run the move function on the character
moveArea(char, 100, 20)
Some syntax errors aside, your original code wasn't hugely problematic - though you never actually set new values to the node (using setValue) and only queried the nodes positions. Passing in the entire object is acceptable practice as far as I'm concerned! Though as part of working with nuke will likely involve lots of selecting, creating and deselecting nodes - so I've added a bit of code that will find the dot again based on its unique name.
My advice would be to create a class for the character dot, which has move functions that find it then move it.
Let me know if that helps, or if you can provide a slightly more complex demonstration of the issue you're having!