3DCGbook

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

OpenMaya 勉強中 その4

頂点にpartcleを生成。
これからちょっとアドバンス的なのを目指す叩き台…
まずはひたすらおさらいしてます(^^;

そういや2014のAPIガイドはPythonの項目がかなり増えてましたね!いい感じ!

import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as ommpx
import maya.OpenMayaFX as omfx

class PluginCommand(ommpx.MPxCommand):
	kPluginName = "vertexParticle"
	
	kHelpFlag = '-h'
	kHelpLongFlag = '-help'
	
	helpMessage = "This command is used to attach a particle on each vertex of a poly mesh"
	
	kSparseFlag = '-s'
	kSparseLongFlag = '-sparse'
	
	sparse = None
	mObj_particle = om.MObject()
	
	def __init__(self):
		ommpx.MPxCommand.__init__(self)
	
	def argumentParser(self, args):
		parseArguments = om.MArgDatabase(self.syntax(), args)
		if parseArguments.isFlagSet(PluginCommand.kSparseFlag):
			self.sparse = parseArguments.flagArgumentDouble(PluginCommand.kSparseFlag, 0)
		if parseArguments.isFlagSet(PluginCommand.kSparseLongFlag):
			self.sparse = parseArguments.flagArgumentDouble(PluginCommand.kSparseLongFlag, 0)
		if parseArguments.isFlagSet(PluginCommand.kHelpFlag):
			self.setResult(helpMessage)
		if parseArguments.isFlagSet(PluginCommand.kHelpLongFlag):
			self.setResult(helpMessage)

	def redoIt(self):
		mSel = om.MSelectionList()
		mDagPath = om.MDagPath()
		mFnMesh  = om.MFnMesh()
		om.MGlobal.getActiveSelectionList(mSel)
		if mSel.length() >= 1:
			try:
				mSel.getDagPath(0,mDagPath)
				mFnMesh.setObject(mDagPath)
				
			except:
				print "Select a poly mesh"
		else:
			print "Select a poly mesh"
		
		mPointArray = om.MPointArray()
		mFnMesh.getPoints(mPointArray,om.MSpace.kWorld)			
		
		mFnParticle = omfx.MFnParticleSystem()
		self.mObj_particle = mFnParticle.create()
		
		# To fix Maya bug
		mFnParticle = omfx.MFnParticleSystem(self.mObj_particle)
		
		counter = 0
		for i in xrange(mPointArray.length()):
			if i%self.sparse == 0:
				mFnParticle.emit(mPointArray[i])
				counter += 1
		print "Total Points : %s" % counter
		mFnParticle.saveInitialState() 

		
	def doIt(self,args):
		self.argumentParser(args)
		if not self.sparse == None:
			self.redoIt()
		
		
	@classmethod
	def cmdCreator(cls):
		return ommpx.asMPxPtr(cls())
		
	@classmethod
	def syntaxCreator(cls):
		syntax = om.MSyntax()
		syntax.addFlag(cls.kHelpFlag, cls.kHelpLongFlag)
		syntax.addFlag(cls.kSparseFlag, cls.kSparseLongFlag, om.MSyntax.kDouble)
		return syntax
		
def initializePlugin(obj):
	mplugin = ommpx.MFnPlugin(obj)
	try:
		mplugin.registerCommand(PluginCommand.kPluginName, PluginCommand.cmdCreator, PluginCommand.syntaxCreator)
	except:
		sys.stderr.write("Failed to register command: %s\n" % PluginCommand.kPluginName)
		
def uninitializePlugin(obj):
	mplugin = ommpx.MFnPlugin(obj)
	try:
		mplugin.deregisterCommand(PluginCommand.kPluginName)
	except:
		sys.stderr.write("Failed to deregister command: %s\n" % PluginCommand.kPluginName)