Python extract color value from MObject in Node Plugin, NumericalAttribute value


#1

Hi everyone!

I am quite new to Maya and currently developing a node plugin using Python.

That node is a locator and I wanted to change node color using color chooser shown in customized attribute template editor.

Right now, I used ‘createColor’ function in MFnNumericAttribute() class to create this attribute, as shown below.

		
import maya.api.OpenMaya as om
numFn = om.MFnNumericAttribute() 		
Node.Color = numFn.createColor("nodeColor", 'noc')

So that there will be a color chooser in attribute editor, but I wanted to choose the color of node in run time.
I am drawing the node using MPxDrawOverride in ‘maya.api.OpenMayaRender’ and trying to get color value in runTime, my code is

		
plug_color = om.MPlug( objPath.node(), Node.Color ) 		
colorObj = plug_color.asMObject() 			
colorVal = om.MDataBlock.inputValue(colorObj)

I checked MDataBlock and it says inputValue can accept MPlug and MObject, but it always returns an error: descriptor ‘inputValue’ requires a ‘OpenMaya.MDataBlock’ object but received a ‘OpenMaya.MObject’.

Anyone has encountered this problems before? can you help me?

Thank you!


#2

Hey,

This line seems a bit suspect.

om.MDataBlock.inputValue(colorObj)

In which method are you trying to access the colour value?

Once you get the MObject using:

plug_color = om.MPlug( objPath.node(), Node.Color ) 		
colorObj = plug_color.asMObject()

You might be able to get the values using something like this:

dataFn = om.MFnNumericData( colorObj )

util1 = om.MScriptUtil(0.0)
p_val1 = util1.asFloatPtr()

util2 = om.MScriptUtil(0.0)
p_val2 = util2.asFloatPtr()

util3 = om.MScriptUtil(0.0)
p_val3 = util3.asFloatPtr()

dataFn.getData3Float(p_val1, p_val2, p_val3)

val1 = util1.getFloat(p_val1)
val2 = util2.getFloat(p_val2)
val3 = util3.getFloat(p_val3)

But if you are trying the grab the value in the compute method, surely you would just pull it directly from the data block. I’ve never created a custom locator, so I’m not sure if there are other methods that don’t have access to the data block.

Hope that helps!

Edit*

You might be able to skip using MScriptUtil, and get the values from the plug like so:

plug_color = om.MPlug( objPath.node(), Node.Color ) 
va11 = plug_color.child(0).asFloat()
va12 = plug_color.child(1).asFloat()
va13 = plug_color.child(2).asFloat()

#3

Hi mlefevre, thanks for your reply!

What I wanted is to retrieve rgb value from the ‘plug_color’ object, and I don’t know that I can only use datablock.inputValue() function in the function ‘compute’.

Now I am extracting the rgb values by using the code below:


plug_color = om.MPlug(objPath.node(), node.Color) 		
color_r = plug_color.child(0).asFloat() 		
color_g = plug_color.child(1).asFloat() 		
color_b = plug_color.child(2).asFloat()

As MFnNumericAttribute::createColor says ‘The names of child attributes will have “r”, “g” and “b” appended to both the full and brief names.’ and MPlug has a ‘child(index)’ method.


#4

Strange that I replied my solution yesterday but it doesn’t appear here.
I used ‘child’ to obtain values in MObject created by using createColor

 
plug_color = om.MPlug(objPath.node(), Node.Color) 		
color_r = plug_color.child(0).asFloat() 		
color_g = plug_color.child(1).asFloat() 		
color_b = plug_color.child(2).asFloat()