Skip to main content

Maya Python API Learning Example, inMesh -> outMesh

Submitted by root on

I made my first attempt at playing with the Maya API using Python as my C++ is still too sketchy, and doing stuff in python is far quicker, no need to reload Maya.

Anyway the example is lifted from the animCubeNode.py from the maya dev kit

I just added a new input plug.

The intention is to add three mesh inputs, and output a final mesh using thier properties as a feather/scale system:

  1. feather/scale geo mesh, to define shape of scales geometry
  2. skin mesh, to define UV, P and N
  3. feather layout mesh, to define a vert for each quill, in proximity to the skin mesh

This way you can simply use mesh editing tools to determine a placement of feathers and scales. I am still n00bing it up for now, but the maths should be fairly straight forward, cross products and barycentric coordinates should be as hardcore as it gets for now.

Adding a few mappable inputs should get interesting to control: scale and rotation.

Anyway this is the first step, if I do it outside of working hours it will appear here.

here are some links that helped me out on the learning curve:


#Sam Hodge's Lame Scripted Plugin

"""
#TEST turn a pipe into a cube
import os
import maya


devPath = "/home/samh/dev"

maya.cmds.file(new=True,force=True)

for i in os.environ:
	if "MAYA" and "PLUG" in i:
		if devPath not in os.environ[i]:
			os.environ[i] =  "%s:%s" % (devPath ,os.environ[i])
			print "Setup Dev: %s : %s" % (i,os.environ[i])
		print "Settings: %s : %s" % (i,os.environ[i])

maya.cmds.unloadPlugin("duplicator.py")
maya.cmds.loadPlugin("duplicator.py")



cubeTrans = maya.cmds.polyCube(ch=0,o=1,w=12.380629,h=9.432578,d=7.009036,cuv=4)[0]
maya.cmds.createNode("shDuplicator")
pipeTrans = maya.cmds.polyPipe(ch=0,o=1,r=10.80581,h=22.165043)[0]
maya.cmds.connectAttr("pCubeShape1.outMesh","shDuplicator1.inputMesh",force=True)
maya.cmds.connectAttr("shDuplicator1.outputMesh","pPipeShape1.inMesh",force=True)
"""


import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx

kPluginNodeName = "shDuplicator"
kPluginNodeId = OpenMaya.MTypeId(0x8700B)

class meshReplicator(OpenMayaMPx.MPxNode):
	inputMesh = OpenMaya.MObject()
	outputMesh = OpenMaya.MObject()

	def __init__(self):
		OpenMayaMPx.MPxNode.__init__(self)

	def dupeMesh(self, inMesh, outData):
		OpenMaya.MFnMesh().copy(inMesh,outData)
		meshFS = OpenMaya.MFnMesh(inMesh)
		#Comment this out to hush the noise
		sys.stderr.write("Edge Count :" + repr(meshFS.numEdges())+"\n")
		sys.stderr.write("Vert Count :" + repr(meshFS.numVertices())+"\n")
		sys.stderr.write("Poly Count :" + repr(meshFS.numPolygons())+"\n")
		return meshFS

	def compute(self, plug, data):
		if plug == meshReplicator.outputMesh:
			inputData = data.inputValue(meshReplicator.inputMesh)
			inMesh = inputData.data()


			sys.stderr.write(inMesh.apiTypeStr() + "\n")
			outputHandle = data.outputValue(meshReplicator.outputMesh)


			dataCreator = OpenMaya.MFnMeshData()
			newOutputData = dataCreator.create()

			self.dupeMesh(inMesh, newOutputData)
			outputHandle.setMObject(newOutputData)
			data.setClean(plug)
		else:
			return OpenMaya.kUnknownParameter

def nodeCreator():
	return OpenMayaMPx.asMPxPtr( meshReplicator() )

def nodeInitializer():
	inTypedAttr = OpenMaya.MFnTypedAttribute()
	outTypedAttr = OpenMaya.MFnTypedAttribute()

	meshReplicator.inputMesh = inTypedAttr.create("inputMesh", "in", OpenMaya.MFnData.kMesh)
	meshReplicator.outputMesh = outTypedAttr.create("outputMesh", "out", OpenMaya.MFnData.kMesh)

	meshReplicator.addAttribute(meshReplicator.inputMesh)
	meshReplicator.addAttribute(meshReplicator.outputMesh)

	meshReplicator.attributeAffects(meshReplicator.inputMesh, meshReplicator.outputMesh)


# initialize the script plug-in
def initializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
		mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator, nodeInitializer)
	except:
		sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
		raise

# uninitialize the script plug-in
def uninitializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
		mplugin.deregisterNode( kPluginNodeId )
	except:
		sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
		raise


Comments