文字列の処理

全然進める時間がないのでメモ的なことを…。

文字列

 "Hello Python"
 'Hello Python'

シングルクォーテーションでも、ダブルクォーテーションでもどちらでも同じ文字列として扱える。
見やすいほうをどうぞ。

=と==

VBSとかだと、変数に入れるのも=(イコール一つ)で、比較するのも=なのでぱっと見て見分けが付かないこともあります。
Pythonの場合、変数に入れるのは=(イコール一つ)、比較するのは==(イコール二つ)なのでソースをざざーと流した時にぱっと分かりやすい。

fmt_SetICEVertexColor.py(処理)

お久しぶりくさ。

そろそろ少し進めようと書き始めています。
PPGが用意できたらスクリプトの中身ですね。

では、まずSoftimageコマンドを呼ぶときによく使うApplicationを省略します。

app = Application

次に実際の処理です。
選択しているオブジェクトをコレクションとしてoColl変数に格納。
VBScriptと比べて、Set宣言がいらないのが特徴です。

oColl = app.GetValue("SelectionList")

そしてコレクションの処理で、オブジェクトがポリゴンメッシュの時のみ実行したいのでFor文を使ってタイプ名「polymsh」の時だけ処理をさせるようにします。

for oSel in oColl:
	if oSel.Type == "polymsh":
		print oSel

とりあえずprint文でログウィンドウにログを表示させます。
ここまでを続けて実行すると、選択しているもののうちポリゴンメッシュオブジェクトだけがログに表示されるようになります。

2011から標準インストールされるPythonのおかげで、print文でログが表示されるようになっています。
注意※処理が一通り通った後、最後にログが表示されるようになっています。
Application.Logmessage のように、処理の途中を監視することは出来ません。

「合体」
インデントに意味があるPython独特の書式になりますね。

app = Application
oColl = app.GetValue("SelectionList")
for oSel in oColl:
	if oSel.Type == "polymsh":
		print oSel

fmt_SetICEVertexColor.py

ICEの頂点カラーをセットするためのPythonスクリプトを書き始めました。
とりあえず、カスタムプロパティウィザードを使ってGUIだけ決めてみる。

import win32com.client
from win32com.client import constants

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "fmt0808"
	in_reg.Name = "fmt_SetICEVertexColorPlugin"
	in_reg.Email = "fmt.0808@gmail.com"
	in_reg.URL = "http://d.hatena.ne.jp/fmt0808/"
	in_reg.Major = 0
	in_reg.Minor = 8

	in_reg.RegisterCommand("fmt_SetICEVertexColor","fmt_SetICEVertexColor")
	in_reg.RegisterProperty("fmt_SetICEVertexColor")
	in_reg.RegisterMenu(constants.siMenuTbGetPropertyID,"fmt_SetICEVertexColor_Menu",
	false,false)
	#RegistrationInsertionPoint - do not remove this line

	return true

def XSIUnloadPlugin( in_reg ):
	strPluginName = in_reg.Name
	Application.LogMessage(str(strPluginName) + str(" has been unloaded."),
	constants.siVerbose)
	return true

def fmt_SetICEVertexColor_Define( in_ctxt ):
	oCustomProperty = in_ctxt.Source
	oCustomProperty.AddParameter2("vertexcolor_R",constants.siDouble,1,0,1,0,1,
	constants.siClassifUnknown,constants.siPersistable + constants.siKeyable)
	oCustomProperty.AddParameter2("vertexcolor_G",constants.siDouble,1,0,1,0,1,
	constants.siClassifUnknown,constants.siPersistable + constants.siKeyable)
	oCustomProperty.AddParameter2("vertexcolor_B",constants.siDouble,1,0,1,0,1,
	constants.siClassifUnknown,constants.siPersistable + constants.siKeyable)
	oCustomProperty.AddParameter2("vertexcolor_A",constants.siDouble,1,0,1,0,1,
	constants.siClassifUnknown,constants.siPersistable + constants.siKeyable)
	return true

def fmt_SetICEVertexColor_DefineLayout( in_ctxt ):
	oLayout = in_ctxt.Source
	oLayout.Clear()
	oLayout.AddGroup("Set Color")
	oLayout.AddColor("vertexcolor_R","Vertex Color",true)
	oLayout.EndGroup()
	return true

def fmt_SetICEVertexColor_OnInit( ):
	Application.LogMessage("fmt_SetICEVertexColor_OnInit called",constants.siVerbose)

def fmt_SetICEVertexColor_vertexcolor_R_OnChanged( ):
	Application.LogMessage("fmt_SetICEVertexColor_vertexcolor_R_OnChanged called",
	constants.siVerbose)

def fmt_SetICEVertexColor_vertexcolor_G_OnChanged( ):
	Application.LogMessage("fmt_SetICEVertexColor_vertexcolor_G_OnChanged called",
	constants.siVerbose)

def fmt_SetICEVertexColor_vertexcolor_B_OnChanged( ):
	Application.LogMessage("fmt_SetICEVertexColor_vertexcolor_B_OnChanged called",
	constants.siVerbose)

def fmt_SetICEVertexColor_vertexcolor_A_OnChanged( ):
	Application.LogMessage("fmt_SetICEVertexColor_vertexcolor_A_OnChanged called",
	constants.siVerbose)

def fmt_SetICEVertexColor_Menu_Init( in_ctxt ):
	oMenu = in_ctxt.Source
	oMenu.AddCallbackItem("fmt_SetICEVertexColor","Onfmt_SetICEVertexColorMenuClicked")
	return true

def Onfmt_SetICEVertexColorMenuClicked( in_ctxt ):
	fmt_SetICEVertexColorPPG( )
	return 1
	
def fmt_SetICEVertexColor_OnInit( ):
	return 1

def fmt_SetICEVertexColor_Execute( ):
	fmt_SetICEVertexColorPPG( )
	return 1
	
def fmt_SetICEVertexColorPPG( ):
	oParent = Application.ActiveProject.ActiveScene.Root
	oProp = oParent.AddProperty("fmt_SetICEVertexColor")
	cancelflag = Application.InspectObj(oProp,'','',4,False)
	if cancelflag:
		Application.LogMessage("fmt_SetICEVertexColor is canceled...")
	Application.DeleteObj(oProp)
	return 1

なんか長かったので途中で改行しているところがあります。
Pythonってコマンドの引数途中で改行してもカッコ内であれば大丈夫って言うところが面白いですね。
例えばこういう書き方も出来る

app = Application
app.CreatePrim(
				"Sphere",
				"MeshSurface",
				"",
				""
				)

ソースを見やすくするためのコツをいろいろ工夫しちゃうな〜。

後は手を入れてPPGのInspectをModalにしてみました。
OKを押したらスクリプトが走るといった動作の準備です。

PluginScriptのほうがいろいろ出来るので(メニューに追加したり、カスタムコマンドとしてとうろくしたり)よく多用するのですが、どうですかね?

ワークグループにまとめちゃえば、管理も楽ですから・・・。

Softimage2011はPythonが標準装備

Softimage2011はPythonが一緒にインストールされ、それが使えるようになります。

今までのようにインストールする手間は不要です。

しかもちゃんと通常のPythonのようにprint文が使えます。

print 'Hello Softimage!'

http://www.info-event.jp/autodesk/seminar/

パイソンをインストール

SoftimageSDKドキュメントに書いてある必要なパイソンモジュール。

Python本体
http://www.python.org/

・拡張モジュール
http://sourceforge.net/projects/pywin32/

Application.Logmessage("Python was installed.")

インストールしたあとの動作テスト。
スクリプトエディタにコピペしてF5で実行してみてください。

# INFO : Python was installed.

と表示されたら成功なので思う存分Pythonって下さい。
めくるめくPythonの世界へようこそ。