2015-06-30 30 views
0

我正嘗試使用python循環函數在XY平面中的中心球體周圍創建一個六角形球體陣列(無法弄清楚如何使用重複特殊)。它應該最終看起來像這樣:在Maya + Python中使用trig函數轉換的重複對象

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

關於發生了什麼的任何想法?

+0

好,'createSphere'接受3個參數,但你只有通過它1'duplicateSphere(createSphere(1.03))' – theodox

+0

你也是從來沒有使用maya.cmds api來創建一個新球體......我想你想'cmds.sphere()' – lemonhead

回答

2

好吧,首先回答你的問題,SyntaxError是由不正確的類實例化引起的。類聲明必須從構造方法在Python中分離出來,就像這樣:

class Sphere(object): 
    def __init__(self, radius, tx=0, ty=0, tz=0, sx=0, sy=0, sz=0): 
     self.diameter = 2 * 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指出,你有一個TypeErrorcreateSphere方法,這需要3個PARAMS(他們都不是現在,因此沒有關鍵字參數可選),你只在1

經過然而,主要問題是目前你實際上並沒有在瑪雅製造任何領域。如果意圖是你的球體對象是maya.cmds周圍的一個面向對象的包裝器,那麼你將需要它在某處調用cmds.sphere,並且你可能會想要它通過你的t *和s *值得到cmds.move()cmds.scale()。如果你在構造函數中完成了所有這些,你可以避免爲所有球體屬性設置實例變量。

這將是這個樣子:

cmds.sphere(radius=self.radius) 
cmds.move(self.tx,self.ty,self.tz) 
cmds.scale(self.sx,self.sy,self.sz) 

最後,我想你是TRIG有點過(你想每次迭代正好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) 
    # ... 

作爲最後一個音符,考慮看着像pymel一個面向對象的解決方案,以避免需要推倒重來在包裝方面對象中的maya.cmds命令。

不管怎麼說,應用所有這些修正產生類似:

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)) 
+0

謝謝你,這是非常有用的。 –

+0

不客氣,很高興幫助:) – lemonhead

相關問題