カテゴリー : maya

アニメーションノードの取得

ノード**に接続されたアニメーションノードを取得する関数です。
ある場合は「アニメーションノードが配列」で返り、ない場合は「空の配列」で返ります。

import maya.cmds as cmds

# アニメーションノードの取得
def getAnimeNode( object ):
	result = []
	listAttr = cmds.listAttr( object, k=True )
	if listAttr != [] and listAttr != None:
		for attr in listAttr:
			isTU = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTU" )
			isTA = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTA" )
			isTL = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTL" )

			if isTU != None:
				result.append( isTU[0] )
			if isTA != None:
				result.append( isTA[0] )
			if isTL != None:
				result.append( isTL[0] )

	return result

アニメーションがあるアトリビュートを取得

アニメーションがあるアトリビュートを取得する関数です。
ある場合は「アトリビュートが配列」で返り、ない場合は「空の配列」で返ります。

import maya.cmds as cmds

# アニメーションがあるアトリビュートを取得
def getAnimeChannel( object ):
	listAttr = cmds.listAttr( object, k=True )
	result   = []
	if listAttr != [] and listAttr != None:
		for attr in listAttr:
			isTU = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTU" )
			isTA = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTA" )
			isTL = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTL" )

			if isTU != None or isTA != None or isTL != None:
				result.append( attr )

	return result

アニメーションがあるか確認

アニメーションがあるか確認する関数です。
あれば「True」、なければ「False」が返ります。

import maya.cmds as cmds

# アニメーションがあるか
def isAnime( object ):
	listAttr = cmds.listAttr( object, k=True )
	if listAttr != [] and listAttr != None:
		for attr in listAttr:

			if len( attr.split(".") ) >= 2:
				continue

			isTU = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTU" )
			isTA = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTA" )
			isTL = cmds.listConnections( "%s.%s" % ( object, attr ), t="animCurveTL" )

			if isTU != None or isTA != None or isTL != None:
				return True

	return False

メッセージの出力

PythonからMayaへメッセージを出力する関数です。
Windows環境で、OUTPUTウインドウへ日本語を出力する際は
「cp932」か「shift-jis」にエンコードしないと文字化けします。

import maya.OpenMaya as OpenMaya
import sys

# OUTPUTウインドウへメッセージを、出力
def out( msg, encode="cp932" ):
	sys.__stdout__.write( msg.encode( encode ) )

# Mayaへメッセージを、通常出力
def info( msg ):
	OpenMaya.MGlobal.displayInfo( msg )

# Mayaへメッセージを、ワーニング出力
def warning( msg ):
	OpenMaya.MGlobal.displayWarning( msg )

# Mayaへメッセージを、エラー出力
def error( msg ):
	OpenMaya.MGlobal.displayError( msg )

findRelatedのPython版

MEL関数で用意されている「findRelated」のPython版です。

import maya.cmds as cmds

def findRelated( skinObject ):

    skinShape = None
    skinShapeWithPath = None
    hiddenShape = None
    hiddenShapeWithPath = None

    cpTest = cmds.ls( skinObject, typ="controlPoint" )
    if len( cpTest ):
        skinShape = skinObject

    else:
        rels = cmds.listRelatives( skinObject )
        for r in rels :
            cpTest = cmds.ls( "%s|%s" % ( skinObject, r ), typ="controlPoint" )
            if len( cpTest ) == 0:
                continue

            io = cmds.getAttr( "%s|%s.io" % ( skinObject, r ) )
            if io:
                continue

            visible = cmds.getAttr( "%s|%s.v" % ( skinObject, r ) )
            if not visible:
                hiddenShape = r
                hiddenShapeWithPath = "%s|%s" % ( skinObject, r )
                continue

            skinShape = r
            skinShapeWithPath = "%s|%s" % ( skinObject, r )
            break

    if len( skinShape ) == 0:
        if len( hiddenShape ) == 0:
            return None

        else:
            skinShape = hiddenShape
            skinShapeWithPath = hiddenShapeWithPath

    clusters = cmds.ls( typ="skinCluster" )
    for c in clusters:
        geom = cmds.skinCluster( c, q=True, g=True )
        for g in geom:
            if g == skinShape or g == skinShapeWithPath:
                return c

    return None

イメージパネルの取得

カメラのトランスノードか、カメラのシェイプノードから、イメージパネルの取得関数です。
配列で返ります。

import maya.cmds as cmds

# イメージパネルの取得
def getImagePlane( camera ):
	if cmds.objectType( camera ) == "transform":
		camera= cmds.listRelatives( camera , s=True, f=True )
		if camera== [] or camera== None:
			return None

		camera = camera[0]

	imagePlanes = cmds.listConnections( "%s.imagePlane" % camera )
	return imagePlanes

PythonのuserSetup

MELでは、起動時に***を実行したい場合は「userSetup.mel」に記入すればOKでしたが
Pythonの場合、それではダメな時があります。

具体的には、実行したい***がMAYAに依存する場合で、
起動したら、メインウインドウにメニューを追加するなどが該当します。

対応方法は、「maya.utils.executeDeferred」を使用します。
このコマンドを使用すると、MAYAの初期化が終わるまでコードが遅延されます。

import maya.utils as utils
import maya.OpenMaya as openMaya
def startup( msg ):
	openMaya.MGlobal.displayInfo( msg )

utils.executeDeferred( startup, u"起動完了" )

アサインされたマテリアルの取得

Hypershadeのコマンドで、アサインされたマテリアルを取得できますが
選択状況が変わってしまうので、関数を作りました。

「getAssignedMaterial( “pCube” )」のようにすると、
アサインされたマテリアル名がリストで返ってきます。

import maya.cmds as cmds

# アサインされたマテリアルを取得
def getAssignedMaterial( object ):
	try:
		result = []
		currentSelects = cmds.ls( sl=True )

		cmds.select( object )
		cmds.hyperShade( smn=True )
		result = cmds.ls( sl=True )

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

		return result

	except:
		return False

プラグインのロード関連

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

関数「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