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

ラジオボタン

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

ラジオボタンを作るコマンド

radioButton(ラジオボタン)
複数のボタンのうち一つだけがオンになるもの。
複数のラジオボタンを一つのグループとしてまとめるには radioCollection コマンドを使用する。
radioButton コマンドと radioCollection コマンドは checkbox コマンドと同じように名前を自分で決めて作ることもできるし、MEL が決めた名前を変数に代入して利用することもできる。
どのラジオボタンが選ばれているかは以下のコマンドで得ることができる。
radioButton -q -select radioCollection名;
または
radioButton -q -select radioCollection名の入っている変数名;
これらによってオンになっている radioButton の名前が返される。
具体的な使用例は以下の "ラジオボタンの使用例"を参照すること。
radioButtonGrp
複数(1 〜 4)個のラジオボタンを横に並べて一つのグループにしたもの。

ラジオボタンの使用例

ラジオボタンによってプリミティブ(球、キューブのうちのどちらか)を作るGUIを作ってみよう。
今回はラジオボタンに自分で適当な名前(radioButton1 など)をつけてゆく方法で作成している。

  1. 以下のMELスクリプトを radio1.mel という名前で作る。
    global proc makePrimitive()
    {
    	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 "makePrimitive()";
    button -label "Close" -command "deleteUI $windowName";
    showWindow;
    
  2. Script Editorの File → Source Script によってradio1.melを読み込む。
  3. 以下のようなウインドウが表示される。
    [radio1.melの実行結果]
  4. Cube ボタンにチェックを入れてOKボタンを押すと Cube が作られる。
    [ラジオボタンを押した結果]
    [OKボタンを押した結果]

スクリプトの解説

global proc makePrimitive()
このプロシージャがOKボタンによって実行される。
以下はmakePrimitiveの中身。
global string $radioCollection1;
外部変数の宣言。
このプロシージャ(makePrimitive)の外側で宣言された(される)変数をプロシージャ内で使用するときはglobalをつける。
string $selected;
選ばれたラジオボタンの名前を代入するための変数の宣言。
$selected = `radioCollection -q -select $radioCollection1`;
$radioCollection1 という変数に代入されている名前のラジオコレクションの中で選ばれているラジオボタンを調べて、その名前を$selectedという変数に代入する。
if($selected == "radioButton1")
{
	sphere;
}
radioButton1 (1つ目のラジオボタンの名前)が選ばれていれば、sphere コマンドを実行する。
else if($selected == "radioButton2")
{
	nurbsCube;
}
radioButton2 (2つ目のラジオボタンの名前)が選ばれていれば、nurbsCube コマンドを実行する。
string $windowName = `window -title "radioButton1"`;
radio1というタイトルのついたウインドウを作る。
作られたウインドウの名前は $windowName という変数に代入される。
columnLayout;
部品を縦1列に並べるレイアウトコマンド。
text -label "make Primitive";
make Primitive というテキストを表示。
string $radioCollection1 = `radioCollection`;
ラジオコレクションを作り、その名前を $radioCollection1 という変数に代入する。
これ以降に作られたラジオボタンが、ひとつのグループになる。
(次のラジオコレクションが作られるまで、または現在のレイアウトが終るまでが、ひとつのグループになる)
radioButton -label "Sphere" -select radioButton1;
ラジオボタンを作り、そのラジオボタンの名前を radioButton1 にする。
-selectフラグがついていると最初にこのボタンが選ばれた状態で表示される。
radioButton -label "Cube" radioButton2;
ラジオボタンを作り、そのラジオボタンの名前を radioButton2 にする。
button -label "OK" -command "makePrimitive()";
OK ボタンを作る。
ボタンを押すと makePrimitive() が実行される。
button -label "Close" -command "deleteUI $windowName";
Close ボタンを作る。
ボタンを押すと、deleteUI コマンドによって $windowName に入っている名前のウインドウが消去される。
showWindow;
ウインドウを表示する。

練習

まとめ

練習課題

参考


Prev
Home | Contents
Mail