为了对Nuke的Python熟悉,我正在创建一个在Node Graph中进行的小游戏,但是在尝试使用函数来移动我的“字符”时遇到了障碍。角色是一个圆点,函数正在尝试读取其在X和Y中的位置,以确定它可以朝哪个方向移动,然后为玩家提供这些选项,最后将角色朝所选方向移动。该函数必须接收字符作为输入,但这就是我遇到的麻烦,这是该部分代码的简化版本:
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)
我已经尝试了很多事情,并且您在这里看到的这段代码是我无法想到的其他选项的地方,我相信问题在于如何在函数中输入“ char”节点,但找不到任何明确的资源。任何帮助将不胜感激!
答案 0 :(得分:0)
我创建了一个简化的函数,其中包含一些有用的nuke命令,这些命令可能对您有用。例如,类外部的函数不需要自变量。该代码仅在不存在一个characterDot的情况下创建它,因此您将能够多次执行它,并看到该点进一步移动。
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)
除了一些语法错误外,您的原始代码并没有太大的问题-尽管您实际上从未(使用setValue)为节点设置新值,而只是查询节点的位置。就我而言,传递整个对象是可以接受的做法!尽管在使用nuke的过程中可能会涉及很多选择,创建和取消选择节点的操作,所以我添加了一些代码,可以根据其唯一名称再次找到该点。
我的建议是为字符点创建一个类,该类具有先找到然后移动的移动功能。
让我知道这是否有帮助,或者您是否可以对您遇到的问题进行稍微复杂的演示!