2017-04-11 331 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');