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");
}
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");
}
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?
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?
Can't find what you're looking for? Ask the community or share your knowledge.