2011年12月14日水曜日

scriptのすすめ9 ~if文、spinner、radio button~ maxscript tips

日本語Ver張り忘れていました。。

Before I revise last code, I will show you how to use "if" and "else".
Here is a simple example.

a = 1
if a == 1 then
(
    print "a is one"
)
else
(
    print "a is not one"
)

--------------------------------------------------------------------------------------------------------------------------------------
when we want to separate some actions,we use if and else.
if you change "a = 1" to "a = 5","a is not one" will be printed on lisner.
So,this time I'll show you how to use "Spinner" and "radioButtons".

First let's make a radio button to choose geometry.
Here is an example.









rollout myUI "simpleUI"
(
    radiobuttons rb_type labels:#( "Box","Sphere","Teapot")
    spinner spn_size "Size:" range:[0, 10000, 25]

    button myBtn "Do!!" width:200 height:50
  
    on myBtn pressed do
    (
        if rb_type.state == 1 do
        (
            b = box length:spn_size.value  width:spn_size.value height:spn_size.value
        )
        if rb_type.state == 2 do
        (
            b = sphere radius:(spn_size.value/2)
        )
        if rb_type.state == 3 do
        (
            b = teapot radius:(spn_size.value/2)
        )
      
        for i = 1 to 10 do
        (
            for j =1 to 10 do
            (
                for k=1 to 10 do
                (
                    ib = instance b
                    ib.pos = [i,j,k]*spn_size.value
                )
            ) 
         
        )
        delete b
    )
)
createdialog myUI  240 100

--------------------------------------------------------------------------------------------------------------------------------------
I added 2 UI parts and modify some code.
First one is a radio button.

radiobuttons rb_type labels:#( "Box","Sphere","Teapot")

"rad_type" is the radio button's name whitch is used for access its state.
if you choose "Box", it will return 1, if you choose "Sphere", it will return 2,if you choose "Teapot"it will return 3.
We can access the number by using ".state".
radio buttons is for just returning a number whitch button is selected.

spinner spn_size "Size:" range:[0, 10000, 25]

"spn_size" is the spineer"s name whitch is used for access its value is the same as radio buttons.
spineer needs 3 numbers,in this case,"0" is minimum value of this spinner,"10000" is maximum value and 25 is default value.
We can access the spinner's value by using  ".value".

Lastly let's add spinners to control how many objects is created along the each axis.













--------------------------------------------------------------------------------------------------------------------------------------
rollout myUI "simpleUI"
(
    radiobuttons rb_type labels:#( "Box","Sphere","Teapot")
    spinner spn_size "Size:" range:[0, 10000, 25]
  
    spinner spn_x "X:" type:#integer range:[1, 10000, 10]
    spinner spn_y "Y:" type:#integer range:[1, 10000, 10]
    spinner spn_z "Z:" type:#integer range:[1, 10000, 10]
  
    button myBtn "Do!!" width:200 height:50
  
    on myBtn pressed do
    (
        if rb_type.state == 1 do
        (
            b = box length:spn_size.value  width:spn_size.value height:spn_size.value
        )
        if rb_type.state == 2 do
        (
            b = sphere radius:(spn_size.value/2)
        )
        if rb_type.state == 3 do
        (
            b = teapot radius:(spn_size.value/2)
        )
      
        for i = 1 to spn_x.value do
        (
            for j =1 to spn_y.value do
            (
                for k=1 to spn_z.value do
                (
                    ib = instance b
                    ib.pos = [i,j,k]*spn_size.value
                )
            ) 
         
        )
        delete b
    )
)
createdialog myUI  240 170

--------------------------------------------------------------------------------------------------------------------------------------
That's it this time.
See you then, next time!!
Thank you.
--------------------------------------------------------------------------------------------------------------------------------------
前回のコードを拡張していく前に、if文elseの使い方を説明しておこうと思います。
こちら、簡単な例です。

a = 1
if a == 1 then
(
    print "a is one"
)
else
(
    print "a is not one"
)

--------------------------------------------------------------------------------------------------------------------------------------

a が1のときは、a is one それ以外のときは,"a is not one"とプリントされます。
何かの条件で分岐したいときに使うというだけです。
試しにa=1 をa = 5に変えてみると、,"a is not one"とプリントされると思います。

では、ここからはスピナーとラジオボタンの使い方を説明しようと思います。
まずは、ジオメトリを選ぶ為のラジオボタンを作ってみようと思います。
例です。










rollout myUI "simpleUI"
(
    radiobuttons rb_type labels:#( "Box","Sphere","Teapot")
    spinner spn_size "Size:" range:[0, 10000, 25]

    button myBtn "Do!!" width:200 height:50
  
    on myBtn pressed do
    (
        if rb_type.state == 1 do
        (
            b = box length:spn_size.value  width:spn_size.value height:spn_size.value
        )
        if rb_type.state == 2 do
        (
            b = sphere radius:(spn_size.value/2)
        )
        if rb_type.state == 3 do
        (
            b = teapot radius:(spn_size.value/2)
        )
      
        for i = 1 to 10 do
        (
            for j =1 to 10 do
            (
                for k=1 to 10 do
                (
                    ib = instance b
                    ib.pos = [i,j,k]*spn_size.value
                )
            ) 
         
        )
        delete b
    )
)
createdialog myUI  240 100


--------------------------------------------------------------------------------------------------------------------------------------
2つのUIと少しコードを足してみました。
1つ目はラジオボタンです。

radiobuttons rb_type labels:#( "Box","Sphere","Teapot")

"rad_type" はこのラジオボタンの名前でどの状態にあるか取得する為に使います。
 "Box"を選択していれば1を返し、"Sphere"なら 2、"Teapot"なら3の値を返します。
".state"を使って、このラジオボタンにアクセスできます。
ラジオボタンはただどのボタンが選択されているか、ということをナンバーで返すものです。

spinner spn_size "Size:" range:[0, 10000, 25]

"spn_size" はこのスピナの名まで、ラジオボタン同様値を得る為に使います。
"0"は最小値"10000"最大値で 25はデフォルトの値です。
スピナの値には".value"を使ってアクセスできます。

最後に、軸それぞれに幾つオブジェクトを並べるか決めるスピナーを追加してみましょう。













--------------------------------------------------------------------------------------------------------------------------------------
rollout myUI "simpleUI"
(
    radiobuttons rb_type labels:#( "Box","Sphere","Teapot")
    spinner spn_size "Size:" range:[0, 10000, 25]
  
    spinner spn_x "X:" type:#integer range:[1, 10000, 10]
    spinner spn_y "Y:" type:#integer range:[1, 10000, 10]
    spinner spn_z "Z:" type:#integer range:[1, 10000, 10]
  
    button myBtn "Do!!" width:200 height:50
  
    on myBtn pressed do
    (
        if rb_type.state == 1 do
        (
            b = box length:spn_size.value  width:spn_size.value height:spn_size.value
        )
        if rb_type.state == 2 do
        (
            b = sphere radius:(spn_size.value/2)
        )
        if rb_type.state == 3 do
        (
            b = teapot radius:(spn_size.value/2)
        )
      
        for i = 1 to spn_x.value do
        (
            for j =1 to spn_y.value do
            (
                for k=1 to spn_z.value do
                (
                    ib = instance b
                    ib.pos = [i,j,k]*spn_size.value
                )
            ) 
         
        )
        delete b
    )
)
createdialog myUI  240 170

--------------------------------------------------------------------------------------------------------------------------------------
今回は以上です。
ではでは、また次回。

0 件のコメント :