[英]The Foundry Nuke – Issues with `elif` and `else` statements
所以為了簡單起見,我在 NUKE 中編寫了一個腳本,它將節點圖中的選定節點對齊成 Y 軸上的一條直線。 我在編寫elif
語句時遇到問題,該語句要么無法正常運行,要么給我一個語法錯誤。
所以函數的基礎是:
ELSE STATEMENT
- 當只選擇一個節點時 - 彈出錯誤消息說用戶必須選擇多個節點
ELIF STATEMENT
- when selected two or more nodes which are in the same Y-axis - message showing they are already aligned
IF STATEMENT
- 當在不同的 Y 軸上選擇兩個或多個節點時 - 它應該正確地將所有節點對齊成一條直線
# Getting selected nodes and making them into a list
selNodes = nuke.selectedNodes()
list = []
for node in selNodes:
n = node['ypos'].value()
list.append(n)
# Defining the actual function
def alignY():
# Aligning the selected nodes using average position of every node.
# Must select more than one node in order to get an average.
if len(selNodes) > 1:
total = sum(list)
average = total / len(selNodes)
for node in selNodes:
n = node['ypos'].setValue(average)
# Getting the position of a single node from the list
firstNodePostion = list[0]
# Checking position of the single node is equivalent to the average
# To prevent nodes aligning again)
elif average == firstNodePostion:
nuke.message("Nodes already Aligned")
# When no nodes or only one node is selected this message pops up
else:
nuke.message("Select Two or more Nodes")
alignY()
您必須根據 Python 規則縮進代碼行。
所以你需要在每個縮進級別使用 4 個空格——看看PEP 8 。
import nuke
selNodes = nuke.selectedNodes()
list = []
for node in selNodes:
n = node['ypos'].value()
list.append(n)
def alignY():
if len(selNodes) > 1:
total = sum(list)
average = total / len(selNodes)
for node in selNodes:
n = node['ypos'].setValue(average)
firstNodePostion = list[0]
elif average == firstNodePostion:
nuke.message("Nodes already Aligned")
else:
nuke.message("Select Two or more Nodes")
alignY()
現在alignY()
方法按預期工作。
您的問題是您有一個位於 if 和 elif 之間的語句,這可能會導致語法錯誤。
但是很難說,因為您沒有提供確切的錯誤消息,但是從語法的角度來看,不應該有另一個語句將 if 和 elif 分開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.