Samiam’s Scribble Pad

January 10, 2010

Maya Python API Learning Example, inMesh -> outMesh

Filed under: Uncategorized — Tags: , , — admin @ 10:53 pm

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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

2 Comments »

  1. Your example is appreciated!

    Comment by remik — June 15, 2011 @ 5:11 pm

  2. Thanks dude! You just saved my day.

    Comment by xcx — November 20, 2012 @ 7:20 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress