在Maya+;python
我正在尝试使用python循环函数在XY平面的中心球体周围创建一个六边形球体阵列(不知道如何使用duplicate special)。结果应该是这样的:在Maya+;python,python,trigonometry,maya,Python,Trigonometry,Maya,我正在尝试使用python循环函数在XY平面的中心球体周围创建一个六边形球体阵列(不知道如何使用duplicate special)。结果应该是这样的: 0 0 0 0 0 0 0 这是我的密码。我发现一个语法错误 # Error: line 1: invalid syntax # 当我叫它的时候,虽然我很确定一号线没有问题 import maya.cmds as cmds class Sphere(radius, tx=0, ty=0, tz=0, sx=0, sy
0 0
0 0 0
0 0
这是我的密码。我发现一个语法错误
# Error: line 1: invalid syntax #
当我叫它的时候,虽然我很确定一号线没有问题
import maya.cmds as cmds
class Sphere(radius, tx=0, ty=0, tz=0, sx=0, sy=0, sz=0):
self.diameter = 2*radius
def createSphere(radius, tx, ty):
newSphere = Sphere(radius=radius, tx=tx, ty=ty)
return newSphere
def duplicateSphere(wholeSphere):
for i in range(6, 1, -1):
createSphere(tx=wholeSphere.diameter*math.cos(2*math.pi/i), ty=wholeSphere.diameter*math.sin(2*math.pi/i))
# create spheres with projections onto x and y axes as translation params
duplicateSphere(createSphere(1.03))
有什么想法吗?好的,首先回答你的问题,
语法错误是由不正确的类实例化引起的。类声明必须与python中的构造函数方法分开,如下所示:
class Sphere(object):
def __init__(self, radius, tx=0, ty=0, tz=0, sx=0, sy=0, sz=0):
self.diameter = 2 * radius
提示:在maya脚本编辑器面板中,如果启用历史->显示堆栈跟踪,可以更好地了解实际错误发生的位置
然而,还有一些其他问题在起作用。首先,您永远不会存储传递到sphere类中的参数(radius除外,它是通过存储直径隐式存储的)。你可能想要:
class Sphere(object):
def __init__(self, radius, tx=0, ty=0, tz=0, sx=1, sy=1, sz=1):
self.radius = radius
self.tx = tx
self.ty = ty
self.tz = tz
self.sx = sx
self.sy = sy
self.sz = sz
self.diameter = 2 * radius
我将“比例默认值”更改为1,而不是0,以便默认球体不可见地小
另外,正如theodox所指出的,在createSphere
方法中有一个TypeError
,它接受3个参数(目前没有一个是关键字参数,因此不是可选的),并且只传入1
但是,主要问题是,当前您实际上没有在maya中创建任何球体。如果您的球体对象是maya.cmds周围的面向对象包装器,则需要它在某处调用cmds.sphere
,并且您可能希望cmds.move()
和cmds.scale()
通过t*和s*值将其移动。如果在构造函数中执行所有这些操作,那么如果需要,实际上可以避免为所有球体属性设置实例变量
这看起来像这样:
cmds.sphere(radius=self.radius)
cmds.move(self.tx,self.ty,self.tz)
cmds.scale(self.sx,self.sy,self.sz)
最后,我认为您的三角有点偏离(您希望每次迭代都精确地改变60°或π/3弧度)。为了获得合适的角度,我想你需要更多的东西:
import math
for i in range(6,0,-1):
angle = math.pi * i / 3.0
distance = wholeSphere.diameter
tx = distance * math.cos(angle)
ty = distance * math.sin(angle)
# ...
作为最后一个注意事项,考虑一下一个面向对象的解决方案,例如避免在对象中包装MayaCMDS命令的情况下重新发明轮盘。
不管怎样,应用所有这些更正都会产生如下结果:
import maya.cmds as cmds
import math
class Sphere(object):
def __init__(self, radius, tx=0, ty=0, tz=0, sx=1, sy=1, sz=1):
self.diameter = 2*radius
self.radius = radius
self.tx = tx
self.ty = ty
self.tz = tz
self.sx = sx
self.sy = sy
self.sz = sz
cmds.sphere(radius=self.radius)
cmds.move(self.tx,self.ty,self.tz)
cmds.scale(self.sx,self.sy,self.sz)
def createSphere(radius, tx=0, ty=0):
newSphere = Sphere(radius, tx=tx, ty=ty)
return newSphere
def duplicateSphere(wholeSphere):
for i in range(6, 0, -1):
angle = math.pi * i / 3.0
distance = wholeSphere.diameter
tx = distance * math.cos(angle)
ty = distance * math.sin(angle)
createSphere(wholeSphere.radius, tx=tx, ty=ty)
duplicateSphere(createSphere(1.03))
好的,createSphere
接受3个参数,但在duplicateSphere(createSphere(1.03))中只传递1个参数。
您也从未使用maya.cmds api创建新的球体。。。我想您需要cmds.sphere()
谢谢,这非常有帮助。