![見出し画像](https://assets.st-note.com/production/uploads/images/164855324/rectangle_large_type_2_5d82535b592a10dfc14f4c8a42c02ac0.png?width=1200)
【Maya】カジュアルにスクリプトを書こう・後編【Python】
★この記事は「Maya Advent Calender2024」用に投稿しました。
3DCGデザイナーのたぶん、鵺です。
タイトルの通り、カジュアルにPythonを書こう!というのが目的です。
前編の記事を読んでから挑戦することオススメします。
▼前編はこちら
![](https://assets.st-note.com/img/1733587837-OnxmtSNfKiqUAkaIlD8PTZvF.png)
![](https://assets.st-note.com/img/1733587861-YCcEniv10JzFfA7D3axPVZdQ.png)
【実践】コードを完成させよう!
…その前に少しだけ手直しをします。
警告文を追加する
何も選択してない状態でボタンを押したときに警告文が出るようにします。
def All_freeze()の中を以下のように編集します。
def All_freeze():
selected_obj = cmds.ls(selection=True)
if not selected_obj: #if文を追加しました
cmds.warning("対象のオブジェクトを選択してください") #警告文を追加しました
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
【#コメント】
#が付いた文はコメントという機能です。
コードに影響のないテキストなので、メモとして使います。
【解説】
if not selected_obj:
if文という条件分岐を行う処理です。
この場合、if notなので「selected_objにオブジェクトの情報がない場合」という意味になります。
cmds.warning("対象のオブジェクトを選択してください")
if notの時、このコマンドが実行される仕組みです。
cmds.warning()は警告文を出すコマンドです。
"文字列"にメッセージを打ち込みましょう。
returnとは、関数の処理を止める役割を持っています。
これを書かないと、オブジェクトを選択してない状態でもFreezeの機能が動き、結果としてエラーが出てしまいます。
警告文を出す為に、returnを書いて後の処理を止めるようにするわけです。
![](https://assets.st-note.com/img/1733650193-5L3E6TYJCKb87UVXRO09tuDw.png?width=1200)
引数を受けとらないようにする
【引数について】
関数に渡す値、データのこと。
とりあえずこれだけ覚えておいてください。
使い方については、詳しく丁寧に説明してる記事が沢山あるので、調べてみてください。
Mayaの仕様でcmds.button(command=関数)を実行すると、関数にcmds.buttonの引数(UIに関するデータ)が送られるようです。
今回の関数は、cmds.buttonの引数(UIに関するデータ)を使うことはないので受け取らないようにします。
以下のように編集しました。
import maya.cmds as cmds
def All_freeze(_): #_(アンダーバー)を追加しました
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
cmds.window(title='Freeze', w=200, h=120)
cmds.columnLayout()
cmds.button(label='All', w=200, command=All_freeze) #command="文字列"から変更しました
cmds.showWindow()
【解説】
def All_freeze(_):
()内に_(アンダーバー)を追加しました。
これで「引数は受け取りません」という意味になります。
他にも(unused)という書き方もあります、好みですね。
cmds.button(label='All', w=200, command=All_freeze)
(command="All_freeze()")から
(command=All_freeze)へ変更しました。
難しい話ですが、文字列の書き方は推奨してないそうです。
デバックがしづらいとか色々あるみたいです。最初から文字列で書くなよって話ですが、引数の説明するの嫌で回避してました、すみません
手直ししたコードはこちら
import maya.cmds as cmds
def All_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
cmds.window(title='Freeze', w=200, h=120)
cmds.columnLayout()
cmds.button(label='All', w=200, command=All_freeze)
cmds.showWindow()
このコードを使ってFreezeの機能を完成させましょう!
コードを完成させよう!
と、言っても書く事はほぼ同じです。
まずはTranslateのFreezeを書きます。
import maya.cmds as cmds
def All_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
def Translate_freeze(_): #Freeze < Translate
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True) #TranslateのみTrue
cmds.window(title='Freeze', w=200, h=120)
cmds.columnLayout()
cmds.button(label='All', w=200, command=All_freeze)
cmds.button(label='Translate', w=200, command=Translate_freeze) #ボタン作成
cmds.showWindow()
【解説】
def All_freeze()と違う点について解説します。
def Translate_freeze():
関数です。ここにFreeze < Translateの機能をまとめます。
cmds.makeIdentity(selected_obj, apply=True, t=True)
TranslateのみのFreezeになるので(t=True)という書き方になります。
書き方として(t=True, r=False, s=False)でも問題はありません。
フラグは書かない場合はOFFの状態=Falseのため、書く必要はなかったりします。(コードの内容もあるので断言できないです)
cmds.button(label='All', w=200, command=Translate_freeze)
ボタンを押してdef Translate_freeze()が実行されるようにします。
コードは使いまわして、RotateとScaleも同じことを書きます。
import maya.cmds as cmds
def All_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
def Translate_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True)
def Rotate_freeze(_): #Freeze < Rotate
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, r=True) #RotateのみTrue
def Scale_freeze(_): #Freeze < Scale
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, s=True) #scaleのみTrue
cmds.window(title='Freeze', w=200, h=90) #ボタンサイズに高さ合わせました
cmds.columnLayout()
cmds.button(label='All', w=200, command=All_freeze)
cmds.button(label='Translate', w=200, command=Translate_freeze)
cmds.button(label='Rotate', w=200, command=Rotate_freeze) #ボタン作成
cmds.button(label='Scale', w=200, command=Scale_freeze) #ボタン作成
cmds.showWindow()
![](https://assets.st-note.com/img/1733647208-vBJSaCedFYLH3Qpklb8i4oXP.png)
完成したコードはこちら
import maya.cmds as cmds
def All_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True, r=True, s=True)
def Translate_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, t=True)
def Rotate_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, r=True)
def Scale_freeze(_):
selected_obj = cmds.ls(selection=True)
if not selected_obj:
cmds.warning("対象のオブジェクトを選択してください")
return
cmds.makeIdentity(selected_obj, apply=True, s=True)
cmds.window(title='Freeze', w=200, h=90)
cmds.columnLayout()
cmds.button(label='All', w=200, command=All_freeze)
cmds.button(label='Translate', w=200, command=Translate_freeze)
cmds.button(label='Rotate', w=200, command=Rotate_freeze)
cmds.button(label='Scale', w=200, command=Scale_freeze)
cmds.showWindow()
![](https://assets.st-note.com/production/uploads/images/164958079/picture_pc_e08f1dbc5c25476b7a1cde646fa8ebaa.gif?width=1200)
後編はここまで
まだPython勉強中なので、簡単に分かりやすく説明するのは難しいですね。
今回の記事のおかげで、コマンドやPythonの書き方など、さらに理解を深めることができました。人に教えると上達するというのはこの事…。
ただ、今回書いたコードはあまり綺麗とは言えません。
なぜなら重複した処理を書いてるからですね。
今回は綺麗なコードを書く事が目的ではないので、お許しくださいませ…!
![](https://assets.st-note.com/img/1733649283-ZefzIkwj1i5oU4YxDHTJBAyt.png)
「こういう機能を実装したい」「GUIを変えたい」と思うのであれば、ぜひPythonを学んでみてはいかがでしょうか。
制作が楽になること間違いなしです。現場で重宝されてチヤホヤされる
さいごに
今年はMaya Advent Calenderに参加できて本当にうれしいです!
(こういう企画があるの知らなくて…)
もうちょっとソフトに関する記事が書けたら良かったんですが…
今年はテクニカル系でいこうかなと思います。
クリスマスイブにはPythonスクリプトを配布予定です。お楽しみに!
ここまでお付き合い頂きまして、ありがとうございました。
お疲れ様でした!