Python 如何从函数内部修改节点?
为了熟悉Python for Nuke,我正在创建一个发生在节点图中的小游戏,但是我在尝试使用函数移动我的“角色”时遇到了一个障碍。角色是一个点,函数试图读取其在X和Y方向上的位置,以确定其可以移动的方向,然后向玩家提供这些选项,最后将角色移动到选定的方向。函数必须接收字符作为输入,但这就是我遇到的问题,这是该部分代码的简化版本:Python 如何从函数内部修改节点?,python,function,nuke,Python,Function,Nuke,为了熟悉Python for Nuke,我正在创建一个发生在节点图中的小游戏,但是我在尝试使用函数移动我的“角色”时遇到了一个障碍。角色是一个点,函数试图读取其在X和Y方向上的位置,以确定其可以移动的方向,然后向玩家提供这些选项,最后将角色移动到选定的方向。函数必须接收字符作为输入,但这就是我遇到的问题,这是该部分代码的简化版本: global currentPosX global currentPosY currentPosX = 0 currentPosY = 0 def moveArea
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”节点输入到函数中,但我找不到任何明确的资源。任何帮助都将不胜感激 我创建了一个简化的函数,其中包含一些有用的nuke命令,可能对您有用。例如,类之外的函数不需要自参数。这段代码只在characterDot还不存在时创建一个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时可能会涉及到大量的选择、创建和取消选择节点,所以我添加了一些代码,可以根据点的唯一名称再次找到它
我的建议是为字符点创建一个类,该类具有查找字符点然后移动字符点的移动函数
让我知道这是否有帮助,或者你是否可以提供一个稍微复杂一点的演示你的问题 我创建了一个简化的函数,其中包含一些有用的nuke命令,可能对您有用。例如,类之外的函数不需要自参数。这段代码只在characterDot还不存在时创建一个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时可能会涉及到大量的选择、创建和取消选择节点,所以我添加了一些代码,可以根据点的唯一名称再次找到它
我的建议是为字符点创建一个类,该类具有查找字符点然后移动字符点的移动函数
让我知道这是否有帮助,或者你是否可以提供一个稍微复杂一点的演示你的问题 您的简化代码似乎已损坏,例如“print=currentPosX”将返回语法错误。您的简化代码似乎已损坏,例如“print=currentPosX”将返回语法错误。