1

我正在为 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)

该脚本有效,我只是觉得应该有一种更简洁的方式来编写它。

4

3 回答 3

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
于 2017-01-14T13:11:45.070 回答
1

当然有,即使因为 for n > 100,您的代码将逐字执行if语句中的每个块。

无论如何,您的xNumberyNumber值似乎是 的简单函数n。那么呢:

import math

factor = math.floor(n/5)
xNumber = (i - (factor * 5)) * 200
yNumber = factor * 200
于 2016-12-07T16:55:56.640 回答
0

您可以使用这个简单的数学来删除所有条件语句。它假定 i 不一定是整数(取决于您的 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)
于 2016-12-07T16:57:35.207 回答