How to access custom material attribute from a mesh?

How to access custom material attribute from a mesh?

Anonymous
Not applicable
883 Views
6 Replies
Message 1 of 7

How to access custom material attribute from a mesh?

Anonymous
Not applicable

Hi,

I am programming a ray tracing for Maya for learning purposes and I am creating custom material to use with the ray tracing. But when I try to access it with the connectedShaders() function of MFnMesh, I can only retrieve the outColor.

Do you have any idea how to access every attribute of a material node?

0 Likes
884 Views
6 Replies
Replies (6)
Message 2 of 7

olarn
Advocate
Advocate

I would get depend node MObject from mfnmesh's node(), slap a MFnDependencyNode on and then use getConnections(). Then check and record relevant plugs and nodes.

Another option is using MItDependencyGraph.

0 Likes
Message 3 of 7

Anonymous
Not applicable

Thank you for you answer

I just try your first solutions but the only connection that came out are "instObjGroups[0]" and "inMesh".

So I try the second option and was able to find my material "customMayaRendererLambert1" and "customMayaRendererLambert1SG" which are respectively the MPxNode containing all my data and the MPxSurfaceShadingNodeOverride if I would have to take a guess.

customMayaRendererLambert1 contains the following plug:

- message

- outColor

and customMayaRendererLambert1SG constains:

- message

- partition

- surfaceShader

- dagSetMembers[0]

0 Likes
Message 4 of 7

olarn
Advocate
Advocate

And what was instObjGroups[0] connected to? "customMayaRendererLambert1SG" is probably in one of the destination plugs.

If there's only 1 material involved, once you got the shading group node, grab the "surfaceShader" attribute using MFnDependencyNode and look for the source plug which should have been the material node. From there, use MFnDependencyNode again to grab any attribute from your custom material node.

0 Likes
Message 5 of 7

olarn
Advocate
Advocate

I found this snippet which is basically the procedure described above.

http://forums.cgsociety.org/t/accessing-a-meshs-material-in-maya-api/1393544/3

 

BTW from the first post I though some custom attribute were connected to mesh directly from material node. getConnectedShaders will give you the shading engine node.

0 Likes
Message 6 of 7

Anonymous
Not applicable

I did a mistake when I test it, the material is "customMayaRendererLambert1" and I can access my attribute, but if you unload and reload, it doesnt work anymore which make sense since you re removing a material in use by an object.

For now I use a lazy method, I query the connected shader then remove the SG from the name.

 

I dont know for "instObjGroups[0]", I dont really understand what it is and why there is "[0]". I should definitly investigate deeper.

Thanks for your help

0 Likes
Message 7 of 7

olarn
Advocate
Advocate

Its there for case of multi-material assignment. Shading engine have to look up target component with same groupId. And there can be many "instances" of those meshes..

Make some mesh instance by "special duplicate", assign multiple different mat to random faces for each and see for yourself.

0 Likes