list() gives TypeError: 'Node' object is not callable - BUT ONLY AFTER SOME CODES

806 Views Asked by At

I hope this is an easy one for you guys.

This is my script, for Nuke.

selNodes = nuke.selectedNodes()

for list in selNodes:
    if list.Class() == 'Read':
        layerArray = []
        # Get the list of layers and make unique using set
        for chanList in list.channels():
            channelLayer = chanList.split('.')
            layerArray.append(channelLayer[0])

        print list(set(layerArray))

It gives an error:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
TypeError: 'Node' object is not callable

So I tried a simpler code of the same nature:

a = [1, 1]
print list(set(a))

And it didn't work. Same error message. Now here's the strange thing: I opened up a new Nuke and ran the simpler codes again, it worked. I couldn't understand why, but I was happy. So I put in my original codes and ran it, error message. I deleted them, the editor is now clean. And ran the simpler code again, error message!!

Which means that a working code can be rendered failure after I pasted and deleted something else!

Can anyone shed some light on this issue? Nuke is a very established software I don't know if it's a software bug.

1

There are 1 best solutions below

0
On

It is because, you are using list as the loop variable, which hides the builtin function list. You are using that function in

print list(set(layerArray))

The loop variables are leaked even when the loop is over, check this program to understand better

for i in range(10):
    pass
print(i)

This will print 9. It means that, i is still available in the program even after the loop is over. The case in your program, after iterating over the selNodes, list variable has the last variable. And you are trying to call that like a function when you say

print list(set(layerArray))

That's why it fails. There are two ways to fix this.

  1. Just change the loop variable to something else.

  2. Use del list when the loop is over. Just pretend that I didn't suggest this. This is NOT recommended. Just change the loop variable to something else.