Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Find Dag Path from kMesh input

Find Dag Path from kMesh input

Anonymous
Not applicable
4,485 Views
9 Replies
Message 1 of 10

Find Dag Path from kMesh input

Anonymous
Not applicable

Hi all,

 

I'm writing a node that has an inMesh input created in the initialize function:

 

RayIntersectNode.inMesh = tAttr.create("inputMesh", "inMesh", om.MFnData.kMesh)

 

In my compute function I'm retrieving that data with:

 

inMeshVal = data.inputValue(RayIntersectNode.inMesh).asMesh()

 

I'm looking for a way to get the DagPath from that variable. I bellive .asMesh() returns an MObject. Thank you in advance. 

 

- Ben

0 Likes
Accepted solutions (1)
4,486 Views
9 Replies
Replies (9)
Message 2 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

You could find the use MPlug::source to get the source plug connected to your mesh.

 

import maya.api.OpenMaya as om

selList = om.MGlobal.getActiveSelectionList()
path = selList.getDagPath(0)
path.extendToShape()
print path.fullPathName()+".inMesh is from"
fnMesh = om.MFnMesh(path)
plug = fnMesh.findPlug("inMesh", True)
source = plug.source()
fnDagNode = om.MFnDagNode(source.node())

#We are not initializing MFnDagPath with a proper dagPath, so we'll need to get all possible dagPath because of features like instancing.
#It also could be not a dag object, please check if it is a dag object when you are using MFnDagNode.
pathArray = fnDagNode.getAllPaths()
print pathArray[0].fullPathName()

 

 

Yours,

Li

Message 3 of 10

Anonymous
Not applicable

Hi Cheng, thanks for the reply.

 

I'm confused a bit at the code you gave. Wouldn't om.MGlobal.getActiveSelectionList() require you to give it an MSelectionList() as an argument? I modified part of it but this isn't exactly what I'm going for. I'd like to get the dag path from the inMesh plug without needing the object to be selected.

 

I looked at the MPlug and MFnMesh docs and I'm not seeing a findPlug method. Am I looking in the right place?

 

 

import maya.OpenMaya as om

sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
path = om.MDagPath()
sel.getDagPath(i,path)
path.extendToShape()
print "inMesh is from: " + path.fullPathName()

 

0 Likes
Message 4 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

It is for demonstration only. As long as you could get a plug, it should be working. I am using Python API 2.0, it works more in a Python way instead of C++, so no MSelectionList required for MGlobal::getActiveSelectionList. If you look at inheritance diagram of MFnMesh, you could find  it inherits MFnDependencyNode, it has findPlug method.

 

 

Since you are creating a custom node, another way to do that is MPxNode::connectionMade.

 

I used it in a blog to create callbacks for my custom node. You could check it out if you are interested into that way.

 

 

 

Yours,

Li

0 Likes
Message 5 of 10

Anonymous
Not applicable

Hi Li, 

 

MFnMesh.findPlug would work, but the problem still exists that I can't get the dagPath from my inMeshVal attribute. 

 

I tried another solution after more searching, but it returns "kFailure: Object does not exist". The docs for the MDataBlock.asMesh() or .asMeshTransformed() says "Returns the data represented by this handle in the data block. The object returned by this call may be used directly with the mesh function set (MFnMesh) or any of the mesh iterators." So I'm not sure why it's not accepting it. 

 

dagPath = om.MDagPath()
fnMesh = om.MFnMesh(inMeshVal)
fnMesh.getPath(dagPath)

For context, I'm writing a simple ray intersection node.

 

dagError.PNG

0 Likes
Message 6 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

 

Please make sure it is a DAG node first. This error message usually indicates it is not a DAG node. If it won't be displayed in outliner with Display>Dag objects only enabled, it is not a DAG node and there won't be a dag path for it.

 

MDataHandle::asMeshTransform is still on the same node and it returns a mesh data with transform matrix applied. If you want source node's plug, you should call MPlug::source to get it.

 

Yours,

Li

0 Likes
Message 7 of 10

Anonymous
Not applicable

Hi Li,

 

I'm still unable to get the Dag Path from my inMesh input. The object I'm connecting to it (via the outMesh of the mesh's shape node) is a DAG object and shows up in the outliner when "Dag Objects Only" is enabled. 

 

So the problem persists that I'm not sure how to access the DagPath of the MObject returned by:

 

inMeshVal = data.inputValue(RayIntersectNode.inMesh).asMeshTransformed()

 

I tried this:

dagPath = om.MDagPath()
om.MDagPath.getAPathTo(inMeshVal, dagPath)
dagPath = dagPath.fullPathName()  

But it returned: (kInvalidParameter): Object is incompatible with this method //

 

Maybe I'm mistaken but I thought MDataHandle's asMeshTransformed() function returns an MObject. I'm still very new to the API so it's difficult for me to read the docs sometimes.

 

 

 

 

 

0 Likes
Message 8 of 10

Anonymous
Not applicable
Accepted solution

For posterity, it looks like you don't need to send an MFnMesh constructor an MDagPath for it to construct. I was able to pass it the inMeshVal I got from taking .asMesh from my input mesh data handle to the constructor and it worked fine. 

0 Likes
Message 9 of 10

Anonymous
Not applicable

Hi Ben,

 

Any chance you could share your code or elaborate on how you solved this? I'm trying the same things you mentioned and am having no success. Even someone in this thread says it's not possible to get a dag path directly from the mesh data returned from .asMesh() on a datahandle.
https://forum.highend3d.com/t/dagpath-from-a-mfndata-kmesh-attribute/40293

 

I'm trying out one of the other ideas from that thread, which seems to be about tracing the plug back to the input mesh and getting the DAG path from that instead.

Thanks!

0 Likes
Message 10 of 10

Anonymous
Not applicable

Looks like I just needed a couple minutes to get this done, I posted too soon! Here's the code I used inside compute() in order to get the DAG path of the input mesh on a scripted node.

    def compute(self, plug, dataBlock):
        if plug == self.attrIn_blendWeight:
            multiInputArrayHandle = dataBlock.inputArrayValue( self.attrIn_blendShapeMultiInput )

            fnDNode = om.MFnDependencyNode( self.thisMObject() )
            inputMeshPlug = fnDNode.findPlug( "inMesh", False )
            inputPlugArray = inputMeshPlug.connectedTo(True, False )
            inputNode = inputPlugArray[0].node()

            if inputNode.hasFn( om.MFn.kMesh ):
                fnDAGN = om.MFnDagNode( inputNode )
                meshDagPath = fnDAGN.getPath()
                print "The full DAG Path of the input mesh:", meshDagPath.fullPathName()
                inputMeshFn = om.MFnMesh(meshDagPath)

Now I can move on with my life!

0 Likes