我正在为Nuke编写一个脚本来安排网格中的选定节点。每个节点都有x和y坐标。此脚本会更改这些坐标。我想让网格的水平长度为5,然后开始一个新的行。我把它写在一个范围循环中。范围由所选节点的数量决定。我有多个if语句将节点删除到新行。我的问题是,是否有办法清理if语句不那么字面,不必每5次重复一次。这是脚本:
list = []
for s in nuke.selectedNodes():
n = s['name'].value()
list.append(n)
names = iter(list)
c = len(nuke.selectedNodes())
for i in range(c):
xNumber = i*200
yNumber = 0
n = i+1
if n > 5:
xNumber = (i-5)*200
yNumber = 200
if n > 10:
xNumber = (i-10) *200
yNumber = 400
if n > 15:
xNumber = (i-15)*200
yNumber = 600
if n > 20:
xNumber = (i-20)*200
yNumber = 800
if n > 25:
xNumber = (i-25)*200
yNumber = 1000
if n > 30:
xNumber = (i-30) *200
yNumber = 1200
if n > 35:
xNumber = (i-35)*200
yNumber = 1400
if n > 40:
xNumber = (i-40)*200
yNumber = 1600
if n > 45:
xNumber = (i-45)*200
yNumber = 1800
if n > 50:
xNumber = (i-50)*200
yNumber = 2000
if n > 55:
xNumber = (i-55)*200
yNumber = 2200
if n > 60:
xNumber = (i-60) *200
yNumber = 2400
if n > 65:
xNumber = (i-65)*200
yNumber = 2600
if n > 70:
xNumber = (i-70)*200
yNumber = 2800
if n > 75:
xNumber = (i-75)*200
yNumber = 3000
if n > 80:
xNumber = (i-80) *200
yNumber = 3200
if n > 85:
xNumber = (i-85)*200
yNumber = 3400
if n > 90:
xNumber = (i-90)*200
yNumber = 3600
if n > 95:
xNumber = (i-95)*200
yNumber = 3800
if n > 100:
xNumber = (i-100)*200
yNumber = 4000
NodeName = next(names)
nuke.toNode(NodeName)['xpos'].setValue(xNumber)
nuke.toNode(NodeName)['ypos'].setValue(yNumber)
脚本有效,我觉得应该有更简洁的方法来写这个。
答案 0 :(得分:1)
当然,即使因为n > 100
,您的代码也会在if
语句中的每个块中执行。
无论如何,您的xNumber
和yNumber
值似乎是n
的简单函数。那么:
import math
factor = math.floor(n/5)
xNumber = (i - (factor * 5)) * 200
yNumber = factor * 200
答案 1 :(得分:1)
以下代码经过测试并正常运行。有关详细信息,请参阅代码注释如果这可以解决您的问题,请告诉我。
list = []
for s in nuke.selectedNodes():
n = s['name'].value()
list.append(n)
names = iter(list)
numSelectedNodes = len(nuke.selectedNodes())
#variables. You only need to change following parameters
numberOfColums = 5
xMult= 200
yMult= 40
#numberOfRows = numSelectedNodes/numberOfColums
#reminder = numSelectedNodes%numberOfColums
#if (reminder>0):
# numberOfRows += 1
# adding 1 as the following will save you above 3 lines of code.
numberOfRows = (numSelectedNodes/numberOfColums)+1
def setPos(xValue,yValue):
nodeName = next(names)
nuke.toNode(nodeName)['xpos'].setValue(xValue)
nuke.toNode(nodeName)['ypos'].setValue(yValue)
i = 0
while (i<numSelectedNodes):
for j in range(numberOfRows):
stepMult = j*numberOfColums
for k in range(numberOfColums):
xNumber = (i-stepMult)*xMult
yNumber = yMult * stepMult
setPos(xNumber,yNumber)
i += 1 # shorthand for i = i+1
答案 2 :(得分:0)
您可以使用这个简单的数学运算删除所有条件语句。它假设我不一定是整数(取决于你的python版本)
list = []
for s in nuke.selectedNodes():
n = s['name'].value()
list.append(n)
names = iter(list)
c = len(nuke.selectedNodes())
for i in range(c):
if i <= 100:
xNumber = ( i - ( ( (int) i ) / 5 ) * 5 ) * 200
yNumber = ( ( (int) i ) / 5 ) * 200
else:
xNumber = (i-100)*200
yNumber = 4000
NodeName = next(names)
nuke.toNode(NodeName)['xpos'].setValue(xNumber)
nuke.toNode(NodeName)['ypos'].setValue(yNumber)