Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenMaya getCompoundAttr Test
# -*- coding: utf-8 -*-
import maya.OpenMaya as om
import maya.OpenMayaMPx as omPx
kPluginNodeTypeName = "getCompoundAttr"
kPluginNodeId = om.MTypeId(0x87007)
class getCompoundAttr(omPx.MPxNode):
#アトリビュートオブジェクト
outputAttr = om.MObject()
compAttr = om.MObject()
strAttr = om.MObject()
numAttr = om.MObject()
#アトリビュート名
kOutputAttrName = "out"
kOutputAttrLongName = "output"
kCompAttrLongName = "comp"
kCompAttrName = "co"
kCompNumLong = "num"
kCompNum = "n"
def __init__(self):
omPx.MPxNode.__init__(self)
#計算メイン
def compute(self , plug , data):
#dataは、MDataBlockクラス
#MDataBlock.inputValue() outputValue() -> ret が MDataHandle を返す
#inputValue() は読み取り専用
#outputValue() は新しい値を書き込むことができる
if plug == getCompoundAttr.outputAttr:
arrayHandle = data.inputArrayValue(getCompoundAttr.compAttr)
count = arrayHandle.elementCount()
outNum = 0
#CompoundAttributeの配列を1つずつ処理する NodeName.compAttr[0].attr
# ↑ここの数字
for i in range(count):
#i個目のHandleにシークする
arrayHandle.jumpToElement(i)
handle = arrayHandle.inputValue().child(self.numAttr)
outNum += handle.asFloat()
#計算の結果を、outputに流す
dataHandle = data.outputValue(getCompoundAttr.outputAttr)
dataHandle.setDouble(outNum)
data.setClean( plug )
return om.MStatus.kSuccess
else:
return om.MStatus.kUnknownParameter
@classmethod
def nodeCreator(cls):
return omPx.asMPxPtr( cls() )
@classmethod
def nodeInitializer(cls):
nAttr = om.MFnNumericAttribute()
cls.outputAttr = nAttr.create(cls.kOutputAttrLongName,cls.kOutputAttrName,om.MFnNumericData.kDouble,0)
nAttr.setWritable(False)
nAttr.setStorable(False)
#String型
nAttrB = om.MFnTypedAttribute()
stringData = om.MFnStringData().create("def")
strAttr = nAttrB.create("stringAttr", "sa", om.MFnData.kString, stringData)
#compoundAttr用のアトリビュートを作成
nAttr = om.MFnNumericAttribute()
cls.numAttr = nAttr.create(cls.kCompNumLong,cls.kCompNum,om.MFnNumericData.kFloat)
#compoundAttribute作成
nAttr = om.MFnCompoundAttribute()
cls.compAttr = nAttr.create(cls.kCompAttrLongName,cls.kCompAttrName)
nAttr.setArray(True)
nAttr.addChild(cls.numAttr)
nAttr.addChild(strAttr)
cls.addAttribute(cls.compAttr)
#アトリビュートを追加
cls.addAttribute(cls.outputAttr)
cls.attributeAffects(cls.compAttr , cls.outputAttr)
#読み込み
def initializePlugin(obj):
plugin = omPx.MFnPlugin(
obj,
"Remiria",
"1.0","1.0",
)
try:
# ノード名,ID,クリエイタの関数名,ノード初期化関数名
plugin.registerNode(
kPluginNodeTypeName,
kPluginNodeId,
getCompoundAttr.nodeCreator,
getCompoundAttr.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
You can’t perform that action at this time.