プラグインのロード関連

プラグインがロードするのも、いちいちロードされていないか確認しないといけないので
関数を用意しました。

関数「load」は、事前にロードされているか確認するので
とりあえず呼び出しておけば、必ずロードされている状態になります。

import maya.cmds as cmds

# ロード済みか確認
def isLoaded( pluginName ):
	status = cmds.pluginInfo( pluginName, q=True, l=True )
	return status

# プラグインのロード
def load( pluginName ):
	status = isLoad( pluginName )
	if not status:
		status = cmds.loadPlugin( pluginName )
		if not status or status == "":
			return False

	return True

FPSの取得

maya.cmdsだけでは、扱いにくいのでMELの関数をPythonで書きました。

import maya.cmds as cmds
def getFPS():

	unit = cmds.currentUnit( q=True, t=True )
	fps  = 0

	if unit == "game":	fps = 15
	if unit == "film":	fps = 24
	if unit == "pal":	fps = 25
	if unit == "ntsc":	fps = 30
	if unit == "show":	fps = 48
	if unit == "palf":	fps = 50
	if unit == "ntscf":	fps = 60
	if unit == "2fps":	fps = 2
	if unit == "3fps":	fps = 3
	if unit == "4fps":	fps = 4
	if unit == "5fps":	fps = 5
	if unit == "6fps":	fps = 6
	if unit == "8fps":	fps = 8
	if unit == "10fps":	fps = 10
	if unit == "12fps":	fps = 12
	if unit == "16fps":	fps = 16
	if unit == "20fps":	fps = 20
	if unit == "40fps":	fps = 40
	if unit == "75fps":	fps = 75
	if unit == "80fps":	fps = 80
	if unit == "100fps":	fps = 100
	if unit == "120fps":	fps = 120
	if unit == "125fps":	fps = 125
	if unit == "150fps":	fps = 150
	if unit == "200fps":	fps = 200
	if unit == "240fps":	fps = 240
	if unit == "250fps":	fps = 250
	if unit == "300fps":	fps = 300
	if unit == "375fps":	fps = 375
	if unit == "400fps":	fps = 400
	if unit == "500fps":	fps = 500
	if unit == "600fps":	fps = 600
	if unit == "750fps":	fps = 750
	if unit == "1200fps":	fps = 1200
	if unit == "1500fps":	fps = 1500
	if unit == "3000fps":	fps = 3000
	if unit == "6000fps":	fps = 6000

	return fps

メインウインドウに、任意のメニューを追加する方法

Mayaのメインウインドウに、任意のメニューを作成したい場合は
「setParent」で、「MayaWindow」を指定してからメニューコマンドを書きます。

メインウインドウに、「Hello」というメニューを作る場合(python)

import maya.cmds as cmds

def create():
	cmds.setParent( "MayaWindow" )
	cmds.menu( l=u"Hello", to=True )
	cmds.menuItem( l=u"このようにメニューを作ります" )
	cmds.setParent( "..", m=True )

メインウインドウに、「Hello」というメニューを作る場合(Mel)

global proc create()
{
	setParent "MayaWindow";
	menu -l "Hellow" -to true;
	menuItem -l "このようにメニューを作ります";
	setParent -m "..";
}

FINAL FANTASY XIVのメイキング映像が公開 第5弾!!

第5弾は、カットシーン作成です。
俺が一番興味があるところですw

Mayaのディレクトリ取得

実行中のMayaのディレクトリの取得関数です。

import os
def getMayaDir():
	result = os.getenv( "MAYA_LOCATION" )
	result = result.replace( "\\", "/" )
	return result

選択・コンポーネントの変換

オブジェクトから、頂点に変換。頂点から、フェイスに変換など
ツールを作るときに、ノード・コンポーネントの変換をしたい場合
「polyListComponentConversion」を使いますが
色々種類が多いので、関数化しました。

Melで用意された関数「ConvertSelectionToVertices」等では、
選択状況が変わってしまい、「ls」で取得しなおす必要がある為
あまりオススメしません。

import maya.cmds as cmds

# 選択をフェイスに変換
def toFace( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, fe=1, fuv=1, fvf=1, tf=1 )
	return converted == [] and None or toFlatten( converted )

# 含むフェイスに変換
def toContainedFace( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, fe=1, fuv=1, fvf=1, tf=1, internal=1 )
	return converted == [] and None or toFlatten( converted )

# エッジに変換
def toEdge( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, ff=1, fuv=1, fvf=1, te=1 )
	return converted == [] and None or toFlatten( converted )

# 含むエッジに変換
def toContainedEdge( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, ff=1, fuv=1, fvf=1, te=1, internal=1 )
	return converted == [] and None or toFlatten( converted )

# 頂点に変換
def toVertex( list ):
	converted = cmds.polyListComponentConversion( list, ff=1, fe=1, fuv=1, fvf=1, tv=1 )
	return converted == [] and None or toFlatten( converted )

# 頂点フェイスに変換
def toVertexFace( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, fe=1, ff=1, fuv=1, tvf=1 )
	return converted == [] and None or toFlatten( converted )

# UVに変換
def toUv( list ):
	converted = cmds.polyListComponentConversion( list, fv=1, fe=1, ff=1, fvf=1, tuv=1 )
	return converted == [] and None or toFlatten( converted )

# シェルに変換
def toShell( list ):
	currentSelects = cmds.ls( sl=True )

	cmds.select( component )
	cmds.polySelectConstraint( t=0 )
	cmds.polySelectConstraint( sh=1, bo=0, m=2 )
	converted = cmds.ls( sl=True, fl=True )

	if currentSelects != []:
		cmds.select( currentSelects )

	return converted == [] and None or toFlatten( converted )

# コンポーネントリストの展開
def toFlatten( list ):
	return cmds.ls( list, fl=True )

選択取得のあれこれ

選択の取得は、いろいろな種類があってすぐ忘れるので
関数化してみました。

import maya.cmds as cmds

# アクティブな選択の取得
# 引数
#	type	種類の指定( transformやikHandle等 )
def getActiveSelection( type=None ):
	selection = []
	if type == None:
		selection = cmds.ls( sl=True, fl=True, l=True )
	else:
		try:
			selection = cmds.ls( sl=True, fl=True, l=True, type=type )
		except:
			# 失敗する場合は、typeの指定ミス
			pass
	return selection == [] and None or selection

# アクティブな選択の取得(ポリゴン)
def getPolygnonOfSelection():
	selection = cmds.filterExpand( sm=12, fp=True )
	return selection

# アクティブな選択の取得(ポリゴン頂点):
def getPolygonVertexOfSelection():
	selection = cmds.filterExpand( sm=31, fp=True )
	return selection

# アクティブな選択の取得(ポリゴンエッジ):
def getPolygonEdgeOfSelection():
	selection = cmds.filterExpand( sm=32, fp=True )
	return selection

# アクティブな選択の取得(ポリゴンフェイス):
def getPolygonFaceOfSelection():
	selection = cmds.filterExpand( sm=34, fp=True )
	return selection

# アクティブな選択の取得(ポリゴン頂点フェイス):
def getPolygonVertexFaceOfSelection():
	selection = cmds.filterExpand( sm=70, fp=True )
	return selection

# アクティブな選択の取得(ポリゴンUV):
def getPolygonUvOfSelection():
	selection = cmds.filterExpand( sm=35, fp=True )
	return selection

チャンネルボックスの選択を取得

チャンネルボックスの選択を取得したい場合は
以下の関数で取得できます。

アニメーションのキーを打つときに
チャンネルボックスを選択しているところにキーを打つ
っというような事をやりたい時に便利です。

import maya.cmds as cmds
# チャンネルボックスの選択取得
def getActiveSelectionFromChannelBox():
	selection = cmds.channelBox( "mainChannelBox", q=True, sma=True )
	return selection

ショートネームの取得

シーン内に同名のノードがあると、パスを含むノード名の取得になるので
純粋なオブジェクト名を取得したい場合は、以下の関数を使うと取得できます。

import maya.cmds as cmds
# ショートネームの取得
def getShortName( object ):
	shortName = cmds.ls( object, sn=True )
	return shortName[0]

親ノード・子ノードを取得

ノードの親ノード、子ノードを取得したい場合、以下の関数を使うと取得できます。
トップレベルで、親を取得しようとした場合は「|」、取得に失敗した場合は「False」が返ります。
※getChildは配列で返ります。

import maya.cmds as cmds
# 親ノードを取得
def getParent( object ):
	temp= cmds.listRelatives( object , p=True, f=True )
	if temp == [] or temp== None:
		return False
	if not temp:
		return "|"

	return temp[0]

# 子ノードを取得
def getChild( object ):
	childs = cmds.listRelatives( object, c=True, f=True )
	if childs == [] or childs == None:
		return False

	return childs