gpt4 book ai didi

python - Nuke - 如何从函数内部修改节点?

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:14 24 4
gpt4 key购买 nike

为了熟悉 Nuke 的 Python,我正在创建一个在节点图中进行的小游戏,但在尝试使用函数移动我的“角色”时遇到了障碍。该角色是一个点,该函数尝试读取其在 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”节点输入到函数中,但我找不到任何其他选项说得很清楚的资源。任何帮助将不胜感激!

最佳答案

我创建了一个简化的函数,其中包含一些有用的 nuke 命令,可能对您有用。例如,类外部的函数不需要 self 参数。此代码仅创建一个不存在的字符点,因此您可以多次执行它并看到该点进一步移动。

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 的一部分,可能会涉及大量选择、创建和取消选择节点 - 因此我添加了一些代码,可以根据其唯一名称再次找到该点。

我的建议是为字符点创建一个类,它具有移动函数,可以找到它然后移动它。

请告诉我这是否有帮助,或者您是否可以提供稍微复杂一点的演示来说明您遇到的问题!

关于python - Nuke - 如何从函数内部修改节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com