3DCGbook

映像プロダクションに所属しています。主に毎日のアウトプットを配信していこうと思います。

OpenMaya 勉強中 その3

フラグをつける方法をなんとなく理解。

しかし先は長い!
これで、ワールドかローカル座標を返すだけですからね!(笑)
スクリプトだったら一行とかですよ!

この先のステップまで理解できるように早くなりたい…

import maya.OpenMaya as om
import maya.OpenMayaMPx as ommpx

class GetTranslation(ommpx.MPxCommand):

	kPluginCmdName = 'GetTranslation'

	kWorldSpaceFlag = '-ws'
	kWorldSpaceLongFlag = '-worldSpace'
	
	def __init__(self):
		ommpx.MPxCommand.__init__(self)
		self.__transformPath = om.MDagPath()
		self.__transformFn   = om.MFnTransform()
		self.__space         = om.MSpace.kTransform
	
	def doIt(self,args):
		try: argData = om.MArgDatabase(self.syntax(), args)
		except: pass
		else:
			if argData.isFlagSet(GetTranslation.kWorldSpaceFlag):
				self.__space = om.MSpace.kWorld

			sList = om.MSelectionList()
			argData.getObjects(sList)
			iter = om.MItSelectionList(sList, om.MFn.kTransform)
			iter.getDagPath(self.__transformPath)
			self.__transformFn.setObject(self.__transformPath)
			
			self.doItQuery()
			
	def doItQuery(self):
		doubleArray = om.MScriptUtil()
		translation = self.__transformFn.getTranslation(self.__space)
		doubleArray.createFromDouble(translation.x,translation.y,translation.z)
		self.setResult(om.MDoubleArray(doubleArray.asDoublePtr(),3))
	
	
	@classmethod
	def creator(cls):
		return ommpx.asMPxPtr(cls())
		
	@classmethod
	def syntaxCreator(cls):
		syntax = om.MSyntax()
		syntax.addFlag(cls.kWorldSpaceFlag,cls.kWorldSpaceLongFlag, om.MSyntax.kNoArg)
		syntax.useSelectionAsDefault(True)
		syntax.setObjectType(om.MSyntax.kSelectionList,1,1)
		return syntax
		
def initializePlugin(obj):
	plugin = ommpx.MFnPlugin(obj,'ShuheiArai')
	try:
		plugin.registerCommand(GetTranslation.kPluginCmdName,GetTranslation.creator,GetTranslation.syntaxCreator)
	except:
		raise Exception("Failed Load %s" % GetTranslation.kPluginCmdName)
		
def uninitializePlugin(obj):
	plugin = ommpx.MFnPlugin(obj)
	try:
		plugin.deregisterCommand(GetTranslation.kPluginCmdName)
	except:
		raise Exception("Failed to unregister command: %s" % GetTranslation.kPluginCmdName)