首页 > 解决方案 > Nuke - 如何从函数内部修改节点?

问题描述

为了熟悉 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”节点输入到函数中,但我找不到任何资源很清楚。任何帮助,将不胜感激!

标签: pythonfunctionnuke

解决方案


我创建了一个简化的函数,其中包含一些可能对您有用的有用的 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 的一部分,可能会涉及大量选择、创建和取消选择节点 - 所以我添加了一些代码,可以根据其唯一名称再次找到该点。

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

让我知道这是否有帮助,或者您是否可以提供更复杂的演示来说明您遇到的问题!


推荐阅读