如何从Maya的上游节点获取数据? [英] How to get data from an upstream node in maya?
问题描述
我有一个Maya节点myNode
,它创建了一个shadeNode
,该inMesh
属性连接到shapeNode.outMesh
并具有属性distance
.
I have a maya node myNode
, which creates a shadeNode
, which inMesh
attribute is connected to shapeNode.outMesh
and has an attribute distance
.
myNode.outMesh -> shapeNode.inMesh
myNode.distance = 10
然后我有一个命令,该命令在shape节点上有效,但是需要distance参数,它是通过遍历inMesh
连接来完成的:
Then i have a command, which works on the shape node, but requires the distance argument, which it does by iterating over the inMesh
connections:
MPlugArray meshConnections;
MPlug inMeshPlug = depNodeFn.findPlug("inMesh");
inMeshPlug.connectedTo(meshConnections, true, false); // in connections
bool node_found = false;
for(uint i = 0; i < numConnections; i++) {
MPlug remotePlug = meshConnections[i];
myNode = remotePlug.node();
if(MFnDependencyNode(myNode ).typeName() == "myNode") {
node_found = true;
break;
}
}
MFnDependencyNode myDepNode(myNode);
MPlug distancePlug = myDepNode.findPlug("distance");
现在,当将另一个节点(另一个类型)应用于myShape
时,我遇到了问题,因为依赖关系图现在看起来像这样:
Now i get a problem, when applying another node (of another type) to myShape
, because the dependency graph now looks like this:
myNode.outMesh -> myOtherNode.inMesh
myOtherNode.outMesh -> shapeNode.inMesh
myNode.distance = 10
我试图删除对typeName() == "myNode"
的检查,因为我理解文档,例如当下一个节点为未知的MPlug返回Mstatus::kInvalidParameter
时,应该递归到父节点,但是我无法到达distance
无需执行进一步的图形遍历即可.
I tried to remove the check for typeName() == "myNode"
, because i understood the documentation like there should be recursion to the parent node, when the next node return Mstatus::kInvalidParameter
for the unknown MPlug, but i cannot reach the distance
plug without implementing further graph traversion.
即使在中间添加了一些其他节点的情况下,可靠地找到父节点属性的正确方法是什么?
What is the correct way to reliably find an attribute of a parent node, even when some other nodes were added in between?
命令本身应使用距离Plug连接到myNode
或某个递归获取值的插头.如果可能的话,我不想更改myOtherNode
以使用距离插头和对应的连接来转发数据.
The command itself should use the distance Plug to either connect to myNode
or to some plug which gets the value recursively. If possible i do not want to change myOtherNode
to have a distance plug and correspondig connections for forwarding the data.
推荐答案
我在答案(Python代码)中找到了答案如何获取图中的所有节点的问题.我在MPxCommand
中找到节点的代码现在看起来像这样:
I found the answer in an answer (python code) to the question how to get all nodes in the graph. My code to find the node in the MPxCommand
now looks like this:
MPlugArray meshConnections;
MPlug inMeshPlug = depNodeFn.findPlug("inMesh");
MItDependencyGraph depGraphIt(inMeshPlug, MFn::kInvalid, MItDependencyGraph::Direction::kUpstream);
bool offset_mesh_node_found = false;
while(!depGraphIt.isDone()) {
myNode = depGraphIt.currentItem();
if(MFnDependencyNode(myNode).typeName() == "myNode") {
offset_mesh_node_found = true;
break;
}
depGraphIt.next();
}
MItDependencyGraph
可以从对象或插头开始沿上游或下游方向遍历图形.在这里,我只是搜索myNode
的第一个实例,因为我认为用例中只会有一个.然后,它连接图中的距离MPlug
,当插入更多网格转换时,该距离仍然有效.
The MItDependencyGraph
can traverse the graph in upstream or downstream direction either starting from an object or a plug. Here i just search for the first instance of myNode
, as I assume there will only be one in my use case. It then connects the distance MPlug
in the graph, which still works when more mesh transforms are inserted.
MItDependencyGraph 对象允许过滤节点ID,但只有数字节点ID,而不是节点名称.当我在所有插件中分配了唯一的Maya ID 时,我可能会添加一个过滤器.
The MItDependencyGraph object allows to filter for node ids, but only the numeric node ids not node names. I probably add a filter later, when I have unique maya ids assigned in all plugins.
这篇关于如何从Maya的上游节点获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!