2016-10-13 1 views
1

xyz 좌표가 랜덤 화 된 다른 다각형 세 가지 유형을 생성하는 스크립트를 만들었습니다. 현재 코드는 원하는대로 코드를 생성하지만 항상 각각 40 개가됩니다.for 루프를 사용하여 임의의 수의 다각형과 XYZ 값 생성

다음 단계는 random number generator으로 정수를 사용하여 각 다각형 유형의 난수를 생성하는 것입니다. 이 경우 하나의 if 문과 else-if 문과 else 문이있는 for 루프가 필요합니다. 위의 인수를 사용하여 코드가 실행됩니다 (취소 된 이후부터). 단 하나의 유형의 다각형 (토러스는 트리거 할 수 없음) 만 수행한다는 점만 다릅니다.

1 :

나는이 일에 대해 회의 해요 int $rng=rand(1,4); 제대로 random numbers 등의 작업을 1 ~ 4의 범위를 만들 지정하는 경우.

2 : for 루프가 if-else으로 반복되는 경우 처음부터 모든 모양의 임의 번호를 가져와야합니다. 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") ; 
    }  
} 

답변

0

This is a desired result I'm trying to get.

당신은 그렇지 않으면 임의의 형상을 데리러 갈게 그냥 대신 임의의 하나마다 따기의 그것을 40 배를 만들 for 루프 내부에 $rng을 넣어해야합니다.

이 경우 ifelse 대신에 switch을 사용하여 다른 경우를 확인할 수 있습니다. 또한 같은 일을하기 때문에 모든 경우에 위치를 가져오고 이동하는 것을 반복 할 필요가 없습니다. 스크립트를 제거하면 스크립트가 덜 부 풀릴 수 있습니다. 여기

은 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; 
} 

과 같은 현명한, 여기 당신이 언어, 더 유연 더 큰 커뮤니티를 가지고 대신으로 픽업 추천 파이썬에서 동일한 코드가, 그리고이다 배우기가 훨씬 쉽습니다.

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) 

들여 쓰기 및 형식 지정을 잊지 마십시오. 그것 때문에 그 순간에 당신의 코드는 읽기가 조금 어렵습니다. :)