前言
自学第四天。现在知道如果是一般的计算节点Node,是可以通过inputValue来获取自己的plug属性值。但一直有一个疑问就是如果自己开发的不是一般节点,而是命令行,ik节点等等这又怎么说?还有跨节点的属性值获取又要怎么解决。
今天乱翻之下找到了目前的一个方案。
现在大概能知道的情报就是,Maya的node有一串串插槽,大概就是对应MPlug类的管理。Mplug类有方法可以获取对应的属性内容。plug的引用内容,你需要指定数据类型,才能对内容进行读取和利用。
ps:小白一枚,有错误望指正。
列举某些情况下,找到Plug
MPlug文档指路
- 如果你开发的是一个节点,你想得到这个节点本身的plug
MObject thisNode = thisMObject();
MPlug thisPlug(thisNode, num);//num 是你声明的addAttribute的属性
- 你想找到拥有某个属性名对应的plug
MPlug findPlug ( const MObject & attr,
bool wantNetworkedPlug,
MStatus * ReturnStatus = NULL
) const->
const MFnDependencyNode & iNode,
MObject attr = iNode.attribute(i);
MFnAttribute mfnAttr(attr);
MPlug plug = iNode.findPlug(attr, true);
- 从获得的MDagPath-》MObject类型的node-》plug->
MObject locatorNode = objPath.node();
MPlug cvLocationsPlug(locatorNode, cvColor::cvLocations);
获得plug后,这个plug背后的引用属性可以分为非Array和Array,处理方式不同
如果属性真实类型为Numeric
可以直接使用这个方法,也可以用下面那个方法
MObject thisNode = thisMObject();
MPlug thisPlug(thisNode, num);
int numofjnts = thisPlug.asInt();
如果属性真实类型为其他
//
// Get the cvLocations attribute value
//
MPlug cvLocationsPlug(locatorNode, cvColor::cvLocations);
MObject cvLocationsObject;
stat = cvLocationsPlug.getValue(cvLocationsObject);
这个例子是插槽引用的属性类型是MObject->
测试
从int转成MString类型有些麻烦,干脆就这么测试
MObject thisNode = thisMObject();
MPlug thisPlug(thisNode, num);
int numofjnts = thisPlug.asInt();
MString s = "1";
for (int i = 0; i < numofjnts; i++)
{
s = s + "0";
MGlobal::displayInfo("nums");
}
MGlobal::displayInfo(s);
这个情况num=6,可以看到输出完全正确。
- 补一下int数据转MString
std::string t = std::to_string(joint_num);
const char* c = t.c_str();
MString s(c);
MGlobal::displayInfo("nodeFn.name() has got " + s + " joints in the chain.");
后记
关于如何取值,两个方法的说法有些不一样,目前使用上看不出什么差别
- 第一种方法
Retrieve the value in the current evaluation context of the attribute this plug references.
第二种方法
Retrieve the current value of the attribute this plug references.
有大佬能解释一下吗?