How can I read a float array attribute with Mel or Python?

How can I read a float array attribute with Mel or Python?

morinnicolas8944
Advocate Advocate
721 Views
6 Replies
Message 1 of 7

How can I read a float array attribute with Mel or Python?

morinnicolas8944
Advocate
Advocate

I created a node with a float array attribute. Its initialize function is as follows:

MStatus MyTestNode::initialize()
{
	MStatus status;
	MFnTypedAttribute tAttr;
	MFnFloatArrayData fnData;

	MObject oData = fnData.create(&status);
	CHECK_MSTATUS_AND_RETURN_IT(status);

	aPositions = tAttr.create("positions", "pos", MFnData::kFloatArray, oData, &status);
	CHECK_MSTATUS_AND_RETURN_IT(status);

	CHECK_MSTATUS_AND_RETURN_IT(addAttribute(aPositions));
	return MS::kSuccess;
}

 

However, whenever I try to set to values and read from it using a script, I always read back a single value of 0.0.

But if I add a float array attribute to the node using a script, I am then able to access that new attribute.

Here is an example Python script:

import maya.cmds as mc

mc.createNode('MyTestNode', n='MyTestNode1')
mc.addAttr(ln='fArray', sn='fa', dt='floatArray')
mc.setAttr("MyTestNode1.positions", (0.1, 0.25, 0.5, 0.75), type="floatArray")
mc.setAttr("MyTestNode1.fArray", (0.1, 0.25, 0.5, 0.75), type="floatArray")
print('positions : {0}'.format(mc.getAttr("MyTestNode1.positions")))
print('fArray : {0}'.format(mc.getAttr("MyTestNode1.fArray")))
print("")

print('positions type : {0}'.format(mc.getAttr("MyTestNode1.positions", type=True)))
print('fArray type : {0}'.format(mc.getAttr("MyTestNode1.fArray", type=True)))

Reading from positions returns 0.0

Reading from fArray returns [0.10000000149011612, 0.25, 0.5, 0.75]

 

The types are also different. positions returns a type of "typed" while fArray returns a type of "floatArray".

 

So I am wondering what is different between the attribute created with the C++ API and the one created with maya.cmds.

And how can I use the C++ API to create a float array attribute that can be accessed with scripts, or alternatively, how can I access the C++ float array attribute using scripts?

 

Note: My goal is to use a single float array attribute and not an array of float attributes (attribute created with setArray(true)).

 

Thanks!

0 Likes
Accepted solutions (1)
722 Views
6 Replies
Replies (6)
Message 2 of 7

brentmc
Autodesk
Autodesk

Hi,

1) What happens if you call setArray(true) on the attribute before adding it in C++?

2) Did you try saving out a .ma file to see what the difference is between the attribute created in C++ vs scripting?

Brent McPherson
Principle Software Developer
0 Likes
Message 3 of 7

morinnicolas8944
Advocate
Advocate

Thank you for your help.

 


1) What happens if you call setArray(true) on the attribute before adding it in C++?

When I do this I get an array of (floatArray) attributes. I also need to index into the array to add values with

mc.setAttr("MyTestNode1.positions[0]", (0.1, 0.25, 0.5, 0.75), type="floatArray")

Not adding the [0] returns an error.

 

2) Did you try saving out a .ma file to see what the difference is between the attribute created in C++ vs scripting?


Good idea. After running the script in my previous post, the node in the .ma file only contains "0" as the value for positions.

On the other hand fArray contains the values I set in the script.

The section of the .ma file for the node is this:

createNode MyTestNode -n "MyTestNode1";
	rename -uid "C1A2621C-427B-495A-7C67-64854965B319";
	addAttr -ci true -sn "fa" -ln "fArray" -dt "floatArray";
	setAttr ".pos" 0;
	setAttr ".fa" -type "floatArray" 4 0.1 0.25 0.5 0.75 ;

Just out of curiosity, I tried setting the value of positions using MEL instead of Python using the command saved in the .ma file for fArray, but it didn't make any difference.

setAttr "MyTestNode1.positions" -type "floatArray" 4 0.1 0.25 0.5 0.75 ;

 

0 Likes
Message 4 of 7

brentmc
Autodesk
Autodesk

Hi,

What version of Maya are you using. I did a similar test here and it works as expected!?
The only difference is I named the attribute input/in since I adapted a plugin node I had lying around to test.

positions : [0.10000000149011612, 0.25, 0.5, 0.75]
fArray : [0.10000000149011612, 0.25, 0.5, 0.75]

positions type : floatArray
fArray type : floatArray

 

Brent McPherson
Principle Software Developer
0 Likes
Message 5 of 7

morinnicolas8944
Advocate
Advocate

Thanks!

 

I was using Maya 2024, but I just tried with Maya 2026 and had the same problem.

Maybe I can make a clean project with only that node and send it to you because it seems like I am doing something different (and wrong) in the code for the node.

0 Likes
Message 6 of 7

morinnicolas8944
Advocate
Advocate
Accepted solution

Indeed the problem was on my side.

I was doing some manipulations in the attribute changed callback to avoid having identical values in the array, but treating the attribute as an attribute array. That was messing up the attribute when setting its value. After disabling that callback I was able to write to and read the attribute as an array (in both Maya 2024 and 2026).

 

Thanks for your help and sorry for my mistake!

0 Likes
Message 7 of 7

brentmc
Autodesk
Autodesk

No problem. Glad to hear you got to the bottom of the issue! 🙂

Brent McPherson
Principle Software Developer
0 Likes