プログラムを作成する時に、ある条件にしたがってコマンドの実行をするかしないかを決めたり、 この条件の時にはこのコマンド、違う条件の場合には別のコマンドを実行したいと思った場合には、 条件によってコマンドの実行を制御します。 Python においては、ある条件が満たされる場合だけコマンドを実行するために if 文と呼ばれるものを使用します。
以下の条件式の部分には、前章「制御構造(ループ)」で説明したものをそのまま使用できます。
if 条件式:
Python 文
if 条件式:
Python 文 1
else:
Python 文 2
if 条件式1:
Python 文 1
elif 条件式2:
Python 文 2
.
. ← elif が並ぶ
.
else:
Python 文 n
if 文の使用例として半径 rad が 5.0 以下の場合は円上に球を、 半径が 5.0 以上の場合はキューブを num 個並べる Python スクリプトを作ってみましょう。
global proc circleObjects1(float $rad, int $num)
{
int $i;
float $r = 0.0;
float $add = 360.0 / $num;
for($i = 0; $i < $num; $i ++)
{
if($rad <= 5.0)
{
sphere;
}
else
{
nurbsCube;
}
move $rad 0 0;
rotate -ws -p 0 0 0 0 $r 0;
$r += $add;
}
}
import maya.cmds
def circleObjects1(rad, num):
r = 0.0
add = 360.0 / num
for i in range(num):
if rad <= 5.0:
maya.cmds.sphere()
else:
maya.cmds.nurbsCube()
maya.cmds.move(rad, 0, 0)
maya.cmds.rotate(0, r, 0, ws=True, p=(0, 0, 0))
r += add
import maya.cmds
def circleObjects1(rad, num):
r = 0.0
add = 360.0 / num
for i in range(num):
if rad <= 5.0:
maya.cmds.sphere()
else:
maya.cmds.nurbsCube()
maya.cmds.move(rad, 0, 0)
maya.cmds.rotate(0, r, 0, ws=True, p=(0, 0, 0))
r += add
circleObjects3(作るプリミティブの種類, プリミティブを並べる円の半径, プリミティブの個数)