[python] access connected nodes through attributes?


#1

I create a polygon mesh. I try to access the radius of the sphere in the DG node connected to the Shape node.

I get the shape node when i create it. I thought i could access the other node through:

shapeNode + “.inMesh.radius” or something like that. I just want the node connected to that attribute. Why doesn’t it work?

I’ve found listConnections of the shape node, which lists that history node of the sphere and radius, but how do i get there directly ? is there any way?

In case i use listConnections, how do i know which one is connected to inMesh?

Thanks


#2

What language and what interface/kit?
One of the most object oriented ways to do it through the API is initializing a DG iterator on your shape node and looking for whatever node type (a generator in your case) you’re after.
Or if you know for sure the name of a parameter you can simply narrow in on that, get the MPlug, and then with connectedTo() you can get all other plugs that one is connected to.


#3

THanks!

It’s on the title: Python. Python for Maya.

I add some information: i tried this:

cil = cmds.polyCylinder()
shape = cmds.listRelatives(cil)[0]
polyCil = cmds.listConnections( shape )[1]
–> returns “polyCylinder1”, which is the history node with subdivisions and radius info which is deleted after cleaning the history.

cmds.nodeType( polyCil ) —> returns “polyCylinder”. Is that a node type?. i cannot access any information there…


#4

Ops, hadn’t noticed the thread title plug.

This might be more extensive than you’d want, but it’s also handling the selection and it’s unrolled to be clear, you can make it more compact or more versatile (e.g. make getPolySphere generic by passing the type of node you want to look for instead of having kPolySphere hard baked in there).

A safe way to find Node types is to test what functionalities they have with hasFn().

Haven’t checked this, it’s a hapzarded conversion of some C++ stuff I had open elsewhere, but I’m confident enough it’ll run or be pretty close to, and it should give you an idea of what to do and what to look for in the API.
Run it with an object selected (the transform, not its shape, as I extend to the shape from the transform in there).


   from maya import OpenMaya as OM
   
   # get the polysphere node connected to the dag path
   #  if there is one, otherwise return false
   def getPolySphere(dagPath):
   	itrDG = OM.MItDependencyGraph(dagPath.node(),
   								  OM.MItDependencyGraph.kUpstream,
   								  OM.MItDependencyGraph.kPlugLevel)
   
   	itrDG.reset()
   	while not itrDG.isDone():
   		currentItem = itrDG.currentItem()
   		if currentItem.hasFn(OM.MFn.kPolySphere):
   			return currentItem
   			
   		itrDG.next()
   	return False
   
   
   # get the first shape under the first object in selection
   def getFirstShapeInSel():
   	sel = OM.MSelectionList()
   	OM.MGlobal.getActiveSelectionList(sel)
   	dagPath = OM.MDagPath();
   	sel.getDagPath(0, dagPath)
   	dagPath.extendToShape()
   	return dagPath
   
   dagPath = getFirstShapeInSel()
   
   polySphere = getPolySphere(dagPath)
   
   if polySphere:
   	# functionalities attached to a dependency node
   	#  can be invoked from this
   	nodeFunctor = OM.MFnDependencyNode(polySphere)
   
   	attrib = nodeFunctor.attribute("radius")
   
   	# the plug is the actual functional port
   	#  that gives you access to the attribute
   	plug = OM.MPlug( polySphere, attrib)
   	
   	# get the plug's value as a double
   	print plug.asDouble()
   	
   

#5

I don’t belive there is not an easier way to just enter another node from the one we have through its connection…


#6

You can condense what I wrote down to a lot less, and you can look at pyMel for some very convenient wrappers that let you get API objects wrapped from some concise shortcuts.

None of that teaches you much about how Maya works or how to get yourself out of anything remotely more complex, and I’m in the habit of handing out fishing poles, not fish :stuck_out_tongue:


#7

Thanks a lot for your help.

The reason i’m not searching in the api or pymel is because I need to do it just in python for maya. If i don’t find a solution i’ll be travelling through the nodes… but i know which node i want to access to.

Isn’t there any way to know WHICH node is connected to a particular attribute?? (inmesh?).
I have “listConnections” -> it returns the node.
but how do i check if it is connected to my node? using isConnected? should i know which type of connection?.

I find this really unusual…


#8

Could you post this example in C++ please?


#9

Perhaps I’m misunderstanding, it seems you just want to query the radius from the polySphere node?

Couldn’t you just do:


import maya.cmds as cmds

shape = cmds.listRelatives(cmds.ls(sl=1)[0],s=1)
cons = cmds.listConnections("{}.inMesh".format(shape[0]),d=0)
radius = cmds.getAttr("{}.radius".format(cons[0]))

If you’ve deleted history, you could query the height of the bounding box (divided by 2) to get the radius?
Unless I totally missing the point, in which case, I apologize.


#10

In such cases pymel is really convenient:

import pymel.core as pm

shape = pm.ls(sl=True)[0]
n = shape.inMesh.inputs()[0].node()
n.radius.set(123)