0

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?

mhlester
  • 22,781
  • 10
  • 52
  • 75
  • Nah that doesn't do it. It was there for viewing it better but that wasn't the problem. – David Charvat Mar 05 '13 at 23:47
  • The `StopIteration` exception is part of the [Python iterator protocol](http://docs.python.org/2/library/stdtypes.html#iterator-types). It's raised when you call `next` on an iterator that has no more values to yield. I'm completely ignorant of Nuke, so can you spell out which of the function calls you're expecting to raise that exception? Nothing seems obvious to me, looking at the code. – Blckknght Mar 06 '13 at 01:32

2 Answers2

1

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.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
0

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...

sissi_luaty
  • 2,839
  • 21
  • 28
  • I just tried to define n by making it n = nuke.selectedNodes() before the while statement and now it's crashing on me. – David Charvat Mar 06 '13 at 00:11
  • @david-charvat have you tried i=0; n = nuke.selectedNodes()[i]; i+=1, for instance? It's not clear for me what you are trying to do. – sissi_luaty Mar 06 '13 at 00:35