2017-04-11 58 views
0

我已經在python中編寫了一些代碼,其中將創建圓柱體形狀的土地。半徑值將來自用戶輸入。但是,我想要從半徑計算圓柱體的高度。我已經想出了一個可以這樣做的提議,但我不知道如何將它放入代碼中。它不斷給我h沒有定義的錯誤。在Python中計算用戶輸入半徑的圓柱體高度

def createLand(landRadius): 
    '''Creates flat land, circle-shaped for city to be built on. 
    Extruded tunnels for rivers. 

    landHeight(h)  : the height/thickness of the Land 
    landRadius(r)  : the size/radius of the land 
    zsubdivisions(sz) : the number of subdivisions along z axis for tunnel extrusion 
    ''' 

    #Creates circle-shaped land, moves edges and faces around for tunnels 
    land = cmds.polyCylinder(name='Land', sx=0, sy=0, sz=5, h=landRadius*0.2/4, r=landRadius); 
    cmds.polyMoveEdge('Land.e[160:179]', s=(0.57, 0.57, 0.57)); 
    cmds.polyMoveEdge('Land.e[120:139]', s=(1.08, 1.08, 1.08)); 
    cmds.polyMoveEdge('Land.e[140:159]', s=(1.32, 1.32, 1.32)); 
    cmds.rotate(0, '15deg', 0, 'Land'); 

    #Tunnels extrusion 
    cmds.polyExtrudeFacet('Land.f[120:139]', 'Land.f[160:179]', 'Land.f[105:106]', 'Land.f[115:116]', 'Land.f[100:101]', 'Land.f[110:111]', 'Land.f[145:146]', 'Land.f[155:156]', 'Land.f[140:141]', 'Land.f[150:151]', kft=True, ty=-h/2); 
    cmds.polyMoveEdge('Land.e[110:111]', 'Land.e[115:116]', 'Land.e[100:101]', 'Land.e[105:106]', ty=-h/2); 
    cmds.move(0, -h/2, 0, 'Land'); 
+0

這是不明確。圓柱體的高度與基座的半徑無關。 'h is not defined'意味着你正在使用一個名爲'h'的變量沒有被賦值。實際上,你的函數'createLand'不帶'h'參數,儘管它在它的主體中使用。 – JulienD

+0

你在哪裏調用這個函數?你可以發佈你的代碼嗎? – Dadep

回答

0

看起來像你沒有定義'h'作爲變量。但是,您可以在polyCylinder()函數中設置'h'參數。你可能想先聲明'h'。

h = landRadius*0.2/4; 
land = cmds.polyCylinder(name='Land', sx=0, sy=0, sz=5, h=h, r=landRadius); 

然後在後面的代碼,當你有以下

cmds.polyExtrudeFacet('Land.f[120:139]', 'Land.f[160:179]', 'Land.f[105:106]', 'Land.f[115:116]', 
'Land.f[100:101]', 'Land.f[110:111]', 'Land.f[145:146]', 'Land.f[155:156]', 'Land.f[140:141]', 
'Land.f[150:151]', kft=True, ty=-h/2); 

,你必須ty=-h/2, 'h' 的定義。

+0

謝謝。這是我所需要的。現在代碼按預期工作。 –

0

你有

land = cmds.polyCylinder(name='Land', sx=0, sy=0, sz=5, h=landRadius*0.2/4, r=landRadius); <snip> cmds.move(0, -h/2, 0, 'Land');

注意的是,在第一線的h=landRadius*0.2/4設置H參數cmds.polyCylinder(我假設它看起來像

def polyCylinder(self,name,sx,sy,sz,h,r): #stuff

這在你的代碼的其餘部分沒有設置h,我看起來像你想要調整高度,你可以做

cmds.move(0, -landRadius*0.2/8, 0, 'Land');

或可能 cmds.move(0, -land.h/2, 0, 'Land');