本段以下的所有内容均来自《实用 Maya 编程》一书。在倒数第二行中,作者说print
带有参数的语句t
隐式调用str(t)
,我想知道为什么,在第二个代码块中作者创建vect
并将其分配给 value xform.translate.get()
,他不能继续使用t
哪个也分配给xform.translate.get()
?
>>> xform.translate
Attribute(u'pSphere1.translate')
>>> t = xform.translate.get()
>>> print t
[0.0, 0.0, 0.0]
突出显示的球体变换的平移值似乎是一个列表。它不是。翻译值是 pymel.core.datatypes.Vector 的一个实例。有时我们需要更积极地内省对象。我认为这是 PyMEL 犯错误的少数几个领域之一。调用 str(t) 会返回一个看起来像是来自列表的字符串,而不是看起来像是来自 Vector 的字符串。确保你有正确的类型。我花了几个小时寻找使用 Vector 而不是列表的错误,反之亦然。
>>> vect = xform.translate.get()
>>> lst = [0.0, 0.0, 0.0]
>>> str(vect)
'[0.0, 0.0, 0.0]'
>>> str(lst)
'[0.0, 0.0, 0.0]'
>>> print t, lst # The print implicitly calls str(t)
[0.0, 0.0, 0.0] [0.0, 0.0, 0.0]