Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as omPx
class createPolyMesh(omPx.MPxNode):
widthHeight = om.MObject()
outputMesh = om.MObject()
#pluginコマンド名
kPluginNodeName = "createPoly"
kPluginNodeId = om.MTypeId(0x87008)
def __init__(self):
omPx.MPxNode.__init__(self)
def _cMesh(self,planeSize,outData):
numFace = 1
#頂点座標をセット
vtxs = []
vtxs.append(om.MFloatPoint( planeSize , 0.0 , planeSize))
vtxs.append(om.MFloatPoint( planeSize , 0.0 ,-planeSize))
vtxs.append(om.MFloatPoint(-planeSize , 0.0 ,-planeSize))
vtxs.append(om.MFloatPoint(-planeSize , 0.0 , planeSize))
numVtx = len(vtxs)
points = om.MFloatPointArray()
points.setLength(numVtx)
for i in range(numVtx):
points.set(vtxs[i],i)
#Faceのコネクト情報
faceConnect = om.MIntArray()
faceConnect.setLength(4)
# 頂点を1->2->3->0 の順でつないだFaceを作成する
faceConnect[0] = 1
faceConnect[1] = 2
faceConnect[2] = 3
faceConnect[3] = 0
faceCount = om.MIntArray()
faceCount.setLength(numFace)
faceCount.set(4,0)
#Meshの関数セットを作成
meshFS = om.MFnMesh()
# 頂点数,Face数,頂点座標配列,ポリゴンカウント配列,結線情報,出力用Mesh
newMesh = meshFS.create(numVtx,numFace,points,faceCount,faceConnect,outData)
return newMesh
def compute(self,plug,data):
if plug == createPolyMesh.outputMesh:
dataHandle = data.inputValue(createPolyMesh.widthHeight)
size = dataHandle.asFloat()
dataCreator = om.MFnMeshData()
newOutputData = dataCreator.create()
self._cMesh(size,newOutputData)
#outputに作成したMesh情報を出力する
outputHandle = data.outputValue(createPolyMesh.outputMesh)
outputHandle.setMObject(newOutputData)
data.setClean(plug)
else:
return om.kUnknownParameter
@classmethod
def nodeCreator(cls):
return omPx.asMPxPtr( cls() )
@classmethod
def nodeInitializer(cls):
nAttr = om.MFnNumericAttribute()
cls.widthHeight = nAttr.create("widthHeight","wh",om.MFnNumericData.kFloat,1.0)
nAttr.setStorable(True)
typedAttr = om.MFnTypedAttribute()
cls.outputMesh = typedAttr.create("outputMesh","out",om.MFnData.kMesh)
cls.addAttribute(cls.widthHeight)
cls.addAttribute(cls.outputMesh)
cls.attributeAffects(cls.widthHeight,cls.outputMesh)
def initializePlugin(obj):
plugin = omPx.MFnPlugin(obj,
"Remiria",
"1.0",
"1.0")
try:
plugin.registerNode(createPolyMesh.kPluginNodeName,
createPolyMesh.kPluginNodeId,
createPolyMesh.nodeCreator,
createPolyMesh.nodeInitializer)
except:
sys.stderr.write('Failed to register node: %s' % createPolyMesh.kPluginNodeName)
raise
def uninitializePlugin(obj):
plugin = omPx.MFnPlugin(obj)
try:
plugin.deregisterNode(createPolyMesh.kPluginNodeId)
except:
sys.stderr.write('Falid to deregister node: %s' % createPolyMesh.kPluginNodeName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment