Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
import maya.OpenMaya as om
import maya.OpenMayaMPx as omPx
kPluginNodeTypeName = "ADD_NUM"
kPluginNodeId = om.MTypeId(0x87007)
class ADD_NUM(omPx.MPxNode):
#アトリビュートオブジェクト
input1Attr = om.MObject()
input2Attr = om.MObject()
outputAttr = om.MObject()
#アトリビュート名
kOutputAttrName = "out"
kOutputAttrLongName = "output"
kInput1AttrName = "in1"
kInput1AttrLongName = "input1"
kInput2AttrName = "in2"
kInput2AttrLongName = "input2"
def __init__(self):
omPx.MPxNode.__init__(self)
#計算メイン
def compute(self,plug,data):
#dataは、MDataBlockクラス
#MDataBlock.inputValue() outputValue() -> ret が MDataHandle を返す
#inputValue() は読み取り専用
#outputValue() は新しい値を書き込むことができる
if plug == ADD_NUM.outputAttr:
#inputValue 取得
#asXXX で、dataHandleから値を取得して変数にセット
dataHandle = data.inputValue(ADD_NUM.input1Attr)
input1 = dataHandle.asDouble()
dataHandle = data.inputValue(ADD_NUM.input2Attr)
input2 = dataHandle.asDouble()
#計算
output = input1 + input2
#outputの定義
#計算の結果を、outputに流す
dataHandle = data.outputValue(ADD_NUM.outputAttr)
dataHandle.setDouble(output)
data.setClean( plug )
return om.MStatus.kSuccess
else:
return om.MStatus.kUnknownParameter
@classmethod
def nodeCreator(cls):
return omPx.asMPxPtr( cls() )
@classmethod
def nodeInitializer(cls):
#Attributeの作成
#NumericAttribute は、シンプルなアトリビュートの定義 create(longName,shortName,Type,Default)
nAttr = om.MFnNumericAttribute()
cls.input1Attr = nAttr.create(cls.kInput1AttrLongName,cls.kInput1AttrName,om.MFnNumericData.kDouble,0)
nAttr.setKeyable(True)
nAttr.setStorable(1)
nAttr = om.MFnNumericAttribute()
cls.input2Attr = nAttr.create(cls.kInput2AttrLongName,cls.kInput2AttrName,om.MFnNumericData.kDouble,0)
nAttr.setKeyable(True)
nAttr.setStorable(1)
nAttr = om.MFnNumericAttribute()
cls.outputAttr = nAttr.create(cls.kOutputAttrLongName,cls.kOutputAttrName,om.MFnNumericData.kDouble,0)
nAttr.setWritable(False)
nAttr.setStorable(False)
#アトリビュートを追加
cls.addAttribute(cls.input1Attr)
cls.addAttribute(cls.input2Attr)
cls.addAttribute(cls.outputAttr)
cls.attributeAffects(cls.input1Attr , cls.outputAttr)
cls.attributeAffects(cls.input2Attr , cls.outputAttr)
#読み込み
def initializePlugin(obj):
plugin = omPx.MFnPlugin(
obj,
"Remiria",
"1.0","1.0",
)
try:
# ノード名,ID,クリエイタの関数名,ノード初期化関数名
plugin.registerNode(
kPluginNodeTypeName,
kPluginNodeId,
ADD_NUM.nodeCreator,
ADD_NUM.nodeInitializer
)
except:
raise Exception(
"Failed tor register node: %s"%
kPluginNodeTypeName
)
def uninitializePlugin(obj):
plugin = omPx.MFnPlugin(obj)
try:
plugin.deregisterNode(kPluginNodeId)
except:
raise Exception(
'Failed to unregister node: %s' %
kPluginNameTypeName
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment