Maya: Trouble with MFnCompoundAttr of MFnMessageAttr python

Hi All,

I am using the python api (v1) deriving from an MPxNode for a fairly simple plugin.

At the moment I am having trouble figuring out how to retrieve the name/object itself for incoming connections to an MFnMessage Attribute. These are also within an array of MFnCompoundAttributes…

eg.

CompoundArray
- Compound Attribute
- MessageAttribute
- MessageAttribute

In this case, my messageattributes will be connected to a bunch of shadingEngines.message,
At the moment Im just trying to query what that shadingEngine connection is, and afterwards what elements are in the set.

Where im at is below.

class acMaterialSwitch(OpenMayaMPx.MPxNode):

class variables

SwitchAttr = OpenMaya.MObject()
sgData = OpenMaya.MObject()
proxyData = OpenMaya.MObject()
renderData = OpenMaya.MObject()
DummyOut = OpenMaya.MObject()

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

def compute(self,plug,dataBlock):

if plug == acMaterialSwitch.SwitchAttr or plug == acMaterialSwitch.DummyOut:
  switchVarHandle = OpenMaya.MDataHandle()
  switchVarHandle = dataBlock.inputValue(acMaterialSwitch.SwitchAttr)
  switchHandle = switchVarHandle.asShort()
    
    #Array of CompoundAttrs to Iterate Over
  sgDataArray = dataBlock.inputArrayValue (acMaterialSwitch.sgData)
  noChildren = sgDataArray.elementCount()
  
  if noChildren < 1:
      dataBlock.setClean(plug)
      return
  
  for i in range(noChildren):
    sgDataArray.jumpToElement(i)
    
    #Compound Attr, should be a pair of MsgAttribute
    sgPairDataHandle = sgDataArray.inputValue()
    
    #Handles for each attr in the compound  
    proxyDataHandle = OpenMaya.MDataHandle(sgPairDataHandle.child(acMaterialSwitch.proxyData))
    renderDataHandle = OpenMaya.MDataHandle(sgPairDataHandle.child(acMaterialSwitch.renderData))
    
    print proxyDataHandle   
    print renderDataHandle     
dataBlock.setClean(plug)

def nodeInitializer():
enumAttr= OpenMaya.MFnEnumAttribute()
dummyEnum = OpenMaya.MFnEnumAttribute()
gAttr = OpenMaya.MFnMessageAttribute()
cAttr = OpenMaya.MFnCompoundAttribute()

acMaterialSwitch.proxyData = gAttr.create(“proxyData”, “proxyData”)
acMaterialSwitch.renderData = gAttr.create(“renderData”, “renderData”)
gAttr.setReadable(1)
gAttr.setWritable(1)
gAttr.setConnectable(1)
gAttr.setHidden(0)
ProxyAttr = gAttr.create(“PROXY”, “PROXY”)
acMaterialSwitch.sgData = cAttr.create(“sgData”, “sgData”)
cAttr.setArray(1)
cAttr.setHidden(0)
cAttr.setUsesArrayDataBuilder(1)
cAttr.addChild(acMaterialSwitch.proxyData)
cAttr.addChild(acMaterialSwitch.renderData)

Grps = [“PROXY”, “RENDER”]

acMaterialSwitch.SwitchAttr = enumAttr.create(“Switch”, “Switch”, 0)
acMaterialSwitch.DummyOut = dummyEnum.create(“Dummy”, “Dummy”, 0)

for g in Grps:
attrF = enumAttr.addField(g, Grps.index(g))
attrD = dummyEnum.addField(g, Grps.index(g))

acMaterialSwitch.addAttribute(acMaterialSwitch.SwitchAttr)
acMaterialSwitch.addAttribute(acMaterialSwitch.DummyOut)

acMaterialSwitch.addAttribute(acMaterialSwitch.sgData)

enumAttr.setReadable(1)
enumAttr.setStorable(1)
enumAttr.setHidden(0)

#dummyEnum.setHidden(1)

acMaterialSwitch.attributeAffects(acMaterialSwitch.SwitchAttr, acMaterialSwitch.DummyOut)
acMaterialSwitch.attributeAffects(acMaterialSwitch.sgData, acMaterialSwitch.DummyOut)

Any assistance would be great, as i am feeling a little lost in this at the moment and havent been able to retrieve many ( if any ) practical examples of MFnMessage being used.

Hi All,

I got what I needed done in the end using the below code… However, as you can see i reverted back to non-api for most of it as I was having so much difficulty translating it over. If anyone can help, I would still really appreciate some pointers in converting it as a learning experience.


def compute(self,plug,dataBlock):
		if plug == acMaterialSwitch.SwitchAttr or plug == acMaterialSwitch.version:
			switchVarHandle = OpenMaya.MDataHandle()
			switchVarHandle = dataBlock.inputValue(acMaterialSwitch.SwitchAttr)
			switchHandle = switchVarHandle.asShort()
        
			thisNode = acMaterialSwitch.thisMObject(self)
			thisDN = OpenMaya.MFnDependencyNode(thisNode)
			thisObj = pm.PyNode(thisDN.name())
			
                        #Iterate over SG connections in proxy Attrs
			for conn in [x for x in thisObj.listConnections(p = True, connections=True, t="shadingEngine") if 'proxyData' in str(x[0])]:
				proxyAttr = conn[0] 
                                #proxySG
				proxyConn = conn[1].replace('.message', '')

				renderAttr = proxyAttr.replace("proxyData", "renderData")
                                #renderSG
				renderConn = pm.getAttr(renderAttr)               

                                #renderSG is Connected and is type SG
				if renderConn != None and pm.nodeType(renderConn) == 'shadingEngine':
					if switchHandle == 0:       
                                                #switch from render to proxy SGs
						switchSG(renderConn, proxyConn)
					else:
                                                #switch from proxy to render SGs
						switchSG(proxyConn, renderConn)
		
		dataBlock.setClean(plug)

def switchSG(fromSG, toSG):
        
	if len(pm.sets(fromSG, query=True)) > 0:
                #transfer elements 
		pm.sets(toSG, fe=pm.sets(fromSG, q=True))