【问题标题】:Generating random number of polygons and their XYZ values using for-loop使用 for 循环生成随机数量的多边形及其 XYZ 值
【发布时间】:2017-02-23 02:32:58
【问题描述】:

我创建了一个脚本,它生成三种不同的多边形,它们的 x y z 坐标是随机的。当前代码根据需要生成这些,但它总是每个生成 40 个。

下一步将使用整数作为random number generator 来生成每种多边形类型的随机数。这需要一个for 循环,其中包含一个if 语句、一个else-if 语句和一个else 语句。代码将完整地执行上述参数(因为我已经取消了它们),除了它只会做一种类型的多边形(环面不可触发)。

我对两件事持怀疑态度:

1:如果 int $rng=rand(1,4); 正确指定创建一个 1-4 的范围以作为 random numbers 使用。

2:如果需要一个带有if-else 语句的for 循环首先获得所有形状的随机数。

This is a desired result I'm trying to get. This is the most recently-executed result of the code.

int $num = 40 ;
int $rng = rand( 1, 4 ) ;

for ( $i = 1; $i <= $num; $i++ ) {

    if ( $rng == 1 ) {

        polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1 ;
        int $xpos = rand( -20, 100 ) ;
        int $ypos = rand( 20, 80 ) ;
        int $zpos = rand( 20, 50 ) ;
        move -r $xpos $ypos $zpos ;
        print ( $i + "sphere \n" ) ;
    }   

    else if ( $rng == 4 ) {

        polyTorus -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1 ;
        int $xpos = rand( -20, 100 ) ;
        int $ypos = rand( 20, 80 ) ;
        int $zpos = rand( 20, 50 ) ;
        move -r $xpos $ypos $zpos ;        
        print ($i + "torus \n");    
    }   

    else {

        polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1 ;
        int $xpos = rand( -20, 100 ) ;
        int $ypos = rand( 20, 80 ) ;
        int $zpos = rand( 20, 50 ) ;
        move -r $xpos $ypos $zpos ;
        print ( $i + "cube \n" ) ;
    }      
}

【问题讨论】:

    标签: if-statement maya modeling autodesk mel


    【解决方案1】:

    您需要将 $rng 放在 for 循环中,否则它将选择一个随机几何图形并创建 40 次,而不是每次都随机选择一个。

    在这种情况下,您可以使用switch 而不是使用if else 来确定不同的情况。您也不需要在所有情况下重复获取位置和移动它们,因为它们在做同样的事情。删除它们将使脚本不那么臃肿。

    这是 MEL 中的一个示例:

    int $num = 40;
    
    for ($i = 0; $i < $num; $i++) {
        int $rng = rand(0, 3); 
    
        float $xpos = rand(-20, 100);
        float $ypos = rand(20, 80);
        float $zpos = rand(20, 50);
    
        switch ($rng) {
            case 0:
                polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1;
                print ($i+1 + ": sphere \n");
                break;
            case 1:
                polyTorus -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1;
                print ($i+1 + ": torus \n");
                break;
            case 2:
                polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
                print ($i+1 + ": cube \n");
                break;
        }
    
        move -r $xpos $ypos $zpos;
    }
    

    同样,这是 Python 中的相同代码,我建议您改用它,因为该语言更灵活,拥有更大的社区,并且更容易学习:

    import random
    import maya.cmds as cmds
    
    num = 40
    
    for i in range(40):
        rng = random.randint(0, 3)
    
        xpos = random.uniform(-20, 100)
        ypos = random.uniform(20, 80)
        zpos = random.uniform(20, 50)
    
        if rng == 0:
            cmds.polySphere(r=1, sx=20, sy=20, ax=[0, 1, 0], cuv=2, ch=1)
            print "{0}: sphere \n".format(i+1)
        elif rng == 1:
            cmds.polyTorus(r=1, sx=20, sy=20, ax=[0, 1, 0], cuv=2, ch=1)
            print "{0}: torus \n".format(i+1)
        else:
            cmds.polyCube(w=1, h=1, d=1, sx=1, sy=1, sz=1, ax=[0, 1, 0], cuv=4, ch=1)
            print "{0}: cube \n".format(i+1)
    
        cmds.move(xpos, ypos, zpos, r=True)
    

    不要忘记缩进和格式;由于它,您的代码目前有点难以阅读:)

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 2014-02-14
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多