1

我目前正在为 Maya 编写基于 Python 的工具。我正在使用我在其他工具的无数其他部分中使用过的代码行,由于某种原因,它这次拒绝工作。我看不出有什么理由不这样做。

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

运行时,Python 在object = objects[x]行上返回错误: 'list' object is not callable。这很奇怪,考虑到没有电话被打...

任何想法是什么导致了这个令人愤怒的问题?

编辑:值得注意的是,如果我运行print objects[x],它会按预期返回对象的名称......

4

1 回答 1

8

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.

于 2014-01-03T04:30:02.280 回答