カテゴリー

アーカイブ






Archive for the 'Maya' Category

MEL sortをpythonでラップ

Author: あきを
06 21st, 2008

MELのsortが思った通りの動きをしないときが
あったのでpythonでラッピングしてみた

global proc string[] pysort(string $array[]){
  python("strArray = '"+`stringArrayToString $array " "`+"'.split()¥nstrArray.sort()");
  return python("strArray");
}

電車の中なのでpython CEでしか動作確認してませんが
大体こんな感じ



pymel

Author: あきを
05 10th, 2008

Pymel Python Module - Syntax / Scripting Tools for Maya
http://www.highend3d.com/maya/downloads/tools/syntax_scripting/Pymel-4844.html

maya pyhtonのラッパー

maya pythonをよりpythonぽい書き方でかけます
maya python:

import maya.cmds as mc
nodes = mc.ls(sl=1)
mc.connectAttr(nodes[0]+".sx", nodes[1]+".sx")
pos = mc.getAttr(nodes[0]+".t")
mc.setAttr(nodes[1]+".t", pos[0][0], pos[0][1], pos[0][2])

pymel:

from pymel import *
nodes=ls(sl=1)
nodes[0].sx >> nodes[1].sx
pos = nodes[0].translate.get()
nodes[1].translate = pos

うん、mayaPythonでgetAttrしたときタプルの中の配列として返すのは仕様としておかしいと思うわ

便利なのは分かるんですけど
こういうものってデフォルトで用意されてないと仕事で使う気がしないんですよねー

でもこの書き方はかなり魅力的
オブジェクトのメソッドでgetとかsetとかするのがやっぱりしっくりくるし
connectAttrの書き方も直感的で良いです

昨日は久々にたくさんのコードを書いた。
UIをゼロから作ったんで無駄にformLayoutに行数かかっただけなんだけど
なんとも無駄な事をしているなぁ
とにかくキーボードを打つ回数を減らしたい



system関数

Author: あきを
04 16th, 2008

テキスト抽出するコードをpythonで色々いじったのだが
結局sedとgrepを使うシェルスクリプトを書いた方が早かった。
pythonでテキストを扱うライブラリとかあるのかな?

pythonのos.systemでgrepを実行した場合
windowsだと常に0が返るので、結果を受け取れないのだ。
結果が欲しい場合、リダイレクションさせて一回ファイルに落とさないといけない

os.system("grep '***' './script.txt' > temp.txtx")

mynzさんに指摘してもらって、パイプを使ったらうまくいった

os.popen("grep '***' './script.txt'").read().split("\n")

MELのsystem関数だとちゃんと結果が返ってきて
stringに格納できる

string $result = system("grep '***' './script.txt'")


MayaTipsサイト

Author: あきを
04 10th, 2008

最近見つけたMayaTipsサイト

TWiki
http://www.tokeru.com/t/bin/view

mel wiki - - Your Maya mel resource on the web -
http://mayamel.tiddlyspot.com/

djx blog
http://www.djx.com.au/blog/



MayaでlineComprehension

Author: あきを
04 2nd, 2008

mayapythonの方がフィルタ処理をはさむことが多いので
line Comprehensionが役立つんじゃね、っていう話

あるノードxxxから接続しているnurbsCurveを知りたいとき。
ただし、xxxとコネクションしているのはnurbsCurveの親トランスフォームノードである

MELで考えると
まずxxxからlistConnectionsで接続ノードリストを得る

そのリストからlistRelatives -shapeで得られたオブジェクトを
nodeTypeで”nurbsCurve”なものに絞り込む

python line Comprehensionだと

import maya.cmds as mc
[x
for x in mc.listConnections(type="transform")
if mc.nodeType(mc.listRelatives(x,s=1)[0])=="nurbsCurve"]

ただし、これだと同じノードと多重接続していると
返ってくるリストにノードが重複するので

import maya.cmds as mc
list(set([x
for x in mc.listConnections(type="transform")
if mc.nodeType(mc.listRelatives(x,s=1)[0])=="nurbsCurve"
]))

としてやるのがよいです