2

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

Tom Rowell
  • 21
  • 1
  • 2
  • 2
    Please provide the actual traceback message. Copy and past it, properly formatted. The whole traceback stack is very important. – S.Lott May 26 '11 at 22:11
  • 2
    Are you sure the error is being thrown on that line? As it is, this won't run because of an indentation error. The method body needs to be indented. – recursive May 26 '11 at 22:11
  • 7
    Also, just for kicks, I'll mention that `object` is a very important name that you probably shouldn't overwrite. – senderle May 26 '11 at 22:16
  • Also just for kicks, I'll mention that `print` statements don't return anything, and if `print objects[x]` prints an object name, then your variable names are confusing; try something more meaningful, like `object_names` and `object_name`. By the way, are you sure that the code that you are actually running doesn't have `object = objects(x)` ? – John Machin May 26 '11 at 22:24
  • Please edit your code so you aren't using object/objects as a variable name – Dhaivat Pandya May 26 '11 at 23:00
  • The issue, as others have said, is probably that you're overwriting `object`. (don't do that) – Sri Raghavan May 26 '11 at 23:06
  • @recursive - the code is correctly indented, it just was lost in the transfer to S.O (an oversight on my part in formatting) – Tom Rowell May 27 '11 at 03:10
  • @John Machin/Senderle/Dhaivat Pandya/Sri Raghavan - I've tried running the code with different variable names with no success. It still returns the error. – Tom Rowell May 27 '11 at 03:11
  • 1
    Bizarrely, if I run the script in an older version of Maya (2011, instead of 2012) the error doesn't appear. Its clearly just an issue with that version of Maya. Since I don't need it to be specifically compatible with 2012, I'll just stick with using 2011. – Tom Rowell May 27 '11 at 03:18
  • Unreladed note: change vertPosList.extend([vertPos]) into .append(), might be a little bit faster. – liori May 27 '11 at 10:53
  • 1
    You should file a bug on Maya 2012, to help other users. – smci Jul 04 '11 at 10:21

1 Answers1

9

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.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331