Reputation: 31
I'm currently writing a Python based tool for Maya. I'm using a line of code which I have used in countless other sections of other tools, and for some reason it refuses to work this time round. I cannot see any reason why it isn't.
def generateClothSkeleton(cloth):
print "Generating Cloth Skeleton"
objects = cmds.textScrollList("clothList", ai=True, q=True)
for x in range(0, len(objects)):
numVerts = cmds.polyEvaluate((objects[x]), v=True)
vertPosList = []
for y in xrange(1, numVerts):
object = objects[x]
xformString = object + ".vtx[" + str(y) + "]"
vertPos = cmds.xform(xformString, t=True, ws=True, a=True, q=True)
vertPosList.extend([vertPos])
...
When run, Python returns an error on the object = objects[x] line: 'list' object is not callable. Which is odd, considering there's no call being made...
Any ideas what's causing this infuriating issue?
EDIT: Its worth noting, that if I run print objects[x], it returns the name of the object as expected...
Upvotes: 3
Views: 9319
Reputation: 394965
This Question has sat out for a long time unanswered, so I'll try to help anyone trying to solve a similar problem. Given the passage of time and the fact that you didn't post a stack-trace or format your code properly, I'll ignore that you're indicating a particular line of your code is responsible for the error. First I'll demonstrate some code that raises this error, and explain why.
>>> a_list = []
>>> a_list()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
This error is raised because the variable a_list points to a specific empty list in memory. The list object itself isn't callable. Callable means you can execute it like a function or class constructor (or an object that has implemented the __call__
magic method, rather uncommon). You don't execute an actual list.
The proper way to add on a single item to a list is with append (I see you're wrapping an item in a list and then using the extend method, which is not the straightforward way to do it, and thus likely less efficient.)
>>> a_list.append('foo')
We can retrieve that item with an index:
>>> a_list[0]
'foo'
A common early mistake for new Python learners (or experts typing too quickly) is to use parentheses instead of brackets, and we see we get the same error.
>>> a_list(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
In an interpreter, this is caught quickly, but there is likely something in Maya that is not executed until it runs your code, and so this issue was never caught.
Another possibility, I see that you're overwriting the builtin object
. You get a similar error if you attempt to call an instantiated object.
>>> an_object = object()
>>> an_object()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'object' object is not callable
So, I wonder if perhaps you've overwritten a list constructor, like the builtin list
, or perhaps cmds.textScrollList
. If that may be the case, look through your code for any possible cases where you're assigning a list like this:
list = ['foo', 'bar'] # Don't do this!!!
fix it, and restart your Python session.
As a counter example to help you understand calling a list, it can (but shouldn't) be implemented.
To actually make a list
object callable, subclass it and implement a __call__
method (again, you probably shouldn't do it in production code), in this case we extend the list with the arguments we pass to the list:
class MyList(list):
def __call__(self, *args):
self.extend(args)
>>> a_list = MyList(['foo', 'bar', 'baz'])
>>> a_list('ham', 'spam', 'eggs', 'spam')
['foo', 'bar', 'baz', 'ham', 'spam', 'eggs', 'spam']
Don't do this.
Upvotes: 9