C++ adding attributes in "Extra Attributes"

C++ adding attributes in "Extra Attributes"

MikeSSSS
Enthusiast Enthusiast
750 Views
1 Reply
Message 1 of 2

C++ adding attributes in "Extra Attributes"

MikeSSSS
Enthusiast
Enthusiast

Hello,

 

I have figured out how to add attributes using mel with addAttr and setAttr on a particular object, but I am struggling to do so with C++.

 

With C++, I want to ideally add an attribute to an object dynamically when my plugin starts, and then propogate those attributes to other objects.

 

 

The pieces I am playing around with are below--I just want to know if I am on the right track as far as doing this with the C++ api.

MFnNumericAttribute attr;
MGlobal::executeCommand("select MyParentObject;");

MObject newAttr = attr.create("testAttribute", "t", MFnNumericData::kDouble, 0.0);


	MDagPath node;
	MObject component;
	MSelectionList list;
	MFnDagNode nodeFn;

	MPxNode n;
	n.addAttribute(newAttr);

	// From our selection list
	MGlobal::getActiveSelectionList(list);
	for (unsigned int index = 0; index < list.length(); index++){
		list.getDagPath(index, node, component);
		nodeFn.setObject(node);
		nodeFn.addChild(newAttr);
		MStatus stat = nodeFn.addAttribute(newAttr);
		if (MS::kSuccess != stat) {
			MGlobal::displayInfo("Failed to add attribute");
		}
		
		
		MGlobal::displayInfo(nodeFn.name().asChar());
		MGlobal::displayInfo(" is selected");
	}

 

0 Likes
751 Views
1 Reply
Reply (1)
Message 2 of 2

MikeSSSS
Enthusiast
Enthusiast

Was able to 'sort of' find a solution on Maya 2020:

 

Made a static class, then was able to add a callback function.

s_callbackID = MNodeMessage::addAttributeChangedCallback(node, userCB, NULL, &stat);

 

It doesn't seem to register unless I add it twice however, which is likely going to cause problems elsewhere.

 

Anyone else have issues or solutions with this?

0 Likes