简体   繁体   中英

Python in Nuke: except StopIteration

Basically what I'm trying to do currently is set up a script in Nuke using python that takes nodes selected by the user and adds shuffle nodes to them for an easy compositing workflow. However I'm stuck on getting Nuke to add the shuffles onto the selected nodes. It works on 1 node when selected but if multiple are selected it only works on the first one selected. I asked a friend about it and she said to try a while loop so this is the code as follows:

while True:
    if n in nuke.selectedNodes():
        name = n.name()
        node = nuke.toNode(name)
        blue.setInput(0,node)
        green.setInput(0,node)
        red.setInput(0,node)
    except StopIteration :
        break

This all works well until the except part. I've ran the script while there wasn't an except and it froze Nuke which shows it's running infinitely but I need it to stop. Nuke tells me the except is a invalid syntax. Does anyone know how I can fix this or create a better work around for my process I'm trying to go for?

Maybe you should think about what you're doing instead of just throwing syntax at the problem and hoping it goes away. except doesn't make sense without a try block, and you aren't assigning to n anywhere. Presumably you meant something like this:

for n in nuke.selectedNodes():
    name = n.name()
    node = nuke.toNode(name)
    blue.setInput(0,node)
    green.setInput(0,node)
    red.setInput(0,node)

There's no need to catch StopIteration if you're using iterators like this.

The correct syntax to catch an exception with the name <exception_name> is:

try:
    #code here...
except <exception_name>:
    #code here...

EDIT:
It seams your code has also other problems...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM