python - 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”节点输入到函数中,但我找不到任何资源很清楚。任何帮助,将不胜感激!
解决方案
我创建了一个简化的函数,其中包含一些可能对您有用的有用的 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 的一部分,可能会涉及大量选择、创建和取消选择节点 - 所以我添加了一些代码,可以根据其唯一名称再次找到该点。
我的建议是为字符点创建一个类,它具有找到它然后移动它的移动函数。
让我知道这是否有帮助,或者您是否可以提供更复杂的演示来说明您遇到的问题!
推荐阅读
- ssl - Hyperledger Fabric SDK - https 和 TLS 证书/密钥
- angular - 角度材质单选按钮在重置后不显示所选值
- javascript - how to tell an element to change its position once it expands outside the viewport?
- python - 正则表达式在字符串对之间删除,但在多对字符串时不删除所有内容
- asp.net - 如何捕获所有组,包括可选的 URL 组?
- angular - Angular:从惰性功能模块添加多提供者
- python-3.x - 在 python 3 中操作命令时忽略反斜杠
- r - Fast greedy on graph objects
- python - Selenium-wire 找不到标头(Python)
- python - Python tkinter 中的多个选项菜单跟踪语句不起作用