項目の選択(ラジオボタン)

ラジオボタン

いくつかの項目中から 1 つだけ選びたい場合はラジオボタンを使用します。

ラジオボタンを作る関数

pymel.core.radioButton()
ラジオボタンを作る関数です。
ラジオボタンは複数のボタンのうち 1 つだけがオンになるものなので、 どのボタンが、どのグループに属するかを決める必要があります。 そこで、ラジオボタンをグループとしてまとめるには pymel.core.radioCollection() 関数を使用します。
radioButton() 関数と radioCollection() 関数は checkbox() 関数と同じように名前を自分で決めて作ることもできるし、 Maya が決めた名前を変数に代入して利用することもできます。
現在どのラジオボタンが選ばれているかを調べるには以下の関数を使用します。
pymel.core.radioButton('radioCollection名', q=True, select=True)
また、変数に radioCollection 名が入っている場合は、以下のように実行します。
pymel.core.radioButton(変数名, q=True, select=True)
これらの関数を実行することよって、オンになっている radioButton の名前が返されます。
具体的な使用例は以下の "ラジオボタンの使用例"を参照してください。
pymel.core.radioButtonGrp
複数(1 〜 4)個のラジオボタンを横に並べて一つのグループにしたものです。
具体的な使用例は radioButtonGrp のプログラム例を参考にしてください。

ラジオボタンの使用例

ラジオボタンによって 2 種類のプリミティブ、NURBS の球体・キューブのうちのどちらかを作る GUI を作ってみましょう。
今回はラジオボタンに radioButton1 などの適当な名前をつけてゆく方法で作成してみます。

  1. 以下の MEL スクリプトを Python スクリプトに書き直して、 makePrimitive1.py という名前で保存します。
    global proc makePrimitive1()
    {
    	global string $radioCollection1;
    	string $selected;
    
    	$selected = `radioCollection -q -select $radioCollection1`;
    	if($selected == "radioButton1")
    	{
    		sphere;
    	}
    	else if($selected == "radioButton2")
    	{
    		nurbsCube;
    	}
    }
    
    string $windowName = `window -title "radioButton1"`;
    columnLayout;
    text -label "make Primitive";
    string $radioCollection1 = `radioCollection`;
    	radioButton -label "Sphere" -select radioButton1;
    	radioButton -label "Cube" radioButton2;
    button -label "OK" -command "makePrimitive1()";
    button -label "Close" -command ("deleteUI " + $windowName);
    showWindow;
    
  2. 以下の Python スクリプトを makePrimitive1.py という名前で作ります。
    import pymel.core
    
    def makePrimitive1():
        selected = pymel.core.radioCollection(radioCollection1, q=True, select=True)
        if selected == 'radioButton1':
            pymel.core.sphere()
        elif selected == 'radioButton2':
            pymel.core.nurbsCube()
    
    windowName = pymel.core.window(title='radioButton1')
    pymel.core.columnLayout()
    pymel.core.text(label=u'プリミティブ作成')
    radioCollection1 = pymel.core.radioCollection()
    pymel.core.radioButton('radioButton1', label=u'球体', select=True)
    pymel.core.radioButton('radioButton2', label=u'立方体')
    pymel.core.button(label=u'作成', command='makePrimitive1()')
    pymel.core.button(label=u'閉じる', command=('pymel.core.deleteUI("' + windowName + '")'))
    pymel.core.showWindow()
    
  3. スクリプト エディタ の ファイル → スクリプトのロード によって makePrimitive1.py を読み込んで、テンキーの Enter キーなどで実行します。
  4. 以下のようなウインドウが表示されます。
    [radio1.melの実行結果]
  5. 立方体 ボタンにチェックを入れて、 作成 ボタンを押すと 立方体 が作られます。
    [ラジオボタンを押した結果]
    [作成 ボタンを押した結果]

スクリプトの解説

import pymel.core
pymel.core モジュールをインポートします。
def makePrimitive1():
makePrimitive1 という名前の関数の宣言です。
この関数が 作成 ボタンによって実行されます。
selected = radioCollection(radioCollection1, q=True, select=True)
radioCollection1 という変数に代入されている名前のラジオコレクションの中で、 どのラジオボタンが選ばれているかを調べて、 その名前を selected という変数に代入します。
MEL と違って global などの宣言がなくても、 radioCollection1 という外部変数を参照することができます。
if selected == 'radioButton1':
  pymel.core.sphere()
radioButton1 (1つ目のラジオボタンの名前)が選ばれているかどうか調べて、 選ばれていれば sphere() 関数を実行します。
elif selected == 'radioButton2':
  pymel.core.nurbsCube()
radioButton2 (2つ目のラジオボタンの名前)が選ばれているかどうか調べて、 選ばれていれば nurbsCube() 関数を実行します。
windowName = pymel.core.window(title='radioButton1')
radioButton1 というタイトルのついたウインドウを作ります。
作成されたウインドウの名前は windowName という変数に代入されます。
pymel.core.columnLayout()
部品を縦 1 列に並べるレイアウト関数です。
pymel.core.text(label=u'プリミティブ作成')
"プリミティブ作成" というテキストを表示します。
radioCollection1 = pymel.core.radioCollection()
ラジオコレクションを作り、その名前を radioCollection1 という変数に代入します。
これ以降に作られたラジオボタンがひとつのグループになり、 次のラジオコレクションが作られるまでか、 または現在のレイアウトが終るまでがひとつのグループになります。
pymel.core.radioButton('radioButton1', label=u'球体', select=True)
ラジオボタンを作り、そのラジオボタンの名前を radioButton1 にします。
select=True フラグがついていると最初にこのボタンが選ばれた状態で表示されます。
pymel.core.radioButton('radioButton2', label=u'立方体')
ラジオボタンを作り、そのラジオボタンの名前を radioButton2 にします。
pymel.core.button(label=u'作成', command='makePrimitive1()')
作成 ボタンを作ります。
ボタンを押すと makePrimitive1() が実行されます。
pymel.core.button(label=u'閉じる', command=('pymel.core.deleteUI("' + windowName + '")'))
閉じる ボタンを作ります。
ボタンを押すと、deleteUI 関数によって、 windowName に入っている名前のウインドウ(自分自身)が消去されます。
pymel.core.showWindow()
ウインドウを表示します。

練習

まとめ

参考


Prev | Next
Home | Contents
Mail