List of links in component

Hello,

i am looking for a python code to get a list of ALL nodes in any depth of a component.
I’ve searched through the forum and the help-file, tried many approaches but had no success.
Is there such a command/code or do i have to loop every node with the .Children-Command? Because of the unknown depth of nodes this loop will be quite difficult!?

Greetings
Andreas

1 Like

vcHelpers.getGivenComponentsNodes() should do the trick.
Alternatively, you can recursively go through all node children and add them to a list.

Hey Este,

thank you for your quick answer.
I tried this command but when i use it i get a list of numerous nodes. It seems that this command returns all nodes related to this component in any depth of the layout - see picture below.
I would like to have a list of all nodes inside the given component in any depth. Like when i select a robot for example i want to get a list of the axes: A1, A2, A3, A4 etc… - see picture below

I am currently working on a small code where you get all attached components to selected components. For the rootnode everything works fine but i also wanted to get the components attached to the component nodes such as grippers etc.

My current code with the missing “Node-Loop”:

from vcCommand import *
from vcHelpers.Application import*
from vcHelpers.Selection import*

app = getApplication()
selectionManager = app.SelectionManager

def OnStart():
attached =
selection = selectionManager.getSelection(VC_SELECTION_COMPONENT)

#check if minimum 1 component is selected
if not selection == :
#loop for each component in selection
for comp in selection:
#loop for nodes in comp

  #---------------------
  #INSERT NODE-LOOP HERE
  #---------------------
  
  #loop for each attached component 
  for child in comp.ComponentChildren:
    if not child in selection:
      attached.append(child)
#when no component is attached to selection
if len(attached) == 0:
  print("No component(s) attached to selection")
  #option to deselect selection when no comp attached to selection
  #attached = []
  #selectionManager.setSelection(attached)
else:
  selectionManager.setSelection(attached)

else:
print(“No component(s) selected”)

Can you please help me with this?

Greetings
Andreas

I didn’t understand what the question was about, but here is an example how to do the same with a loop.

comp = getComponent()
visited = []
nodes = [comp]
while nodes:
  node = nodes.pop(0)
  visited.append(node)
  nodes.extend(node.Children)
for node in visited:
  print node.Name

Hey Este,

thanks again for your help, but your code did the same as the command vcHelpers.getGivenComponentsNodes() - so a list of ALL components/nodes in any depth.

Nevertheless i found the solution in the code of a wizard.
There was a line wich i totally don’t understand but it does the trick, so… :smiley:

loop_nodes.extend( [x for x in node.Children if x.Type == 32768] )

Maybe someone can explain what is happening here?
In the wizard they commented, that vcNode does not work and it seems that the number 32768 is another way to look for vcNodes?

Here is my full code:

from vcCommand import *
from vcHelpers.Application import*
from vcHelpers.Selection import*


app = getApplication()
selectionManager = app.SelectionManager

 
def OnStart():
  attached = []
  selection = selectionManager.getSelection(VC_SELECTION_COMPONENT)
  
  #check if minimum 1 component is selected
  if not selection == []:
    #loop for each component in selection
    for comp in selection:
      #loop for all nodes in comp
      loop_nodes = [comp]
      nodes = []
      while loop_nodes:
        node = loop_nodes.pop(0)
        loop_nodes.extend( [x for x in node.Children if x.Type == 32768] )
        for child in node.ComponentChildren:
          if not child in selection:
            attached.append(child)
    #when no component is attached to selection
    if len(attached) == 0:
      print("No component(s) attached to selection")
      #option to deselect selection when no comp attached to selection
      #attached = []
      #selectionManager.setSelection(attached)
    else:
      selectionManager.setSelection(attached)
  else:
    print("No component(s) selected")

Hi,

that line itself is mostly just python’s list comprehension. The reason to use extend over append here is because we want to concatenate lists together into one like here:

the [x for x …] can be thought like this:

my_list = []
loop_nodes = [contents]
for x in node.Children:
    if x.Type == 32768:
        my_list.append(x)
loop_nodes.extend(my_list)
return loop_nodes

You can also go to 5.1.3 section in the python docs

About the other problem, I don’t actually know, but maybe understanding some parts of this code will help you go forward.

br,
Lefa

1 Like

VC_NODE == 32768

from vcScript import *
from vcHelpers.Selection import *

comp = getComponent()
nodes = getGivenComponentsNodes(comp) 
print [node.Name for node in nodes if node.Type == VC_COMPONENT] # List components only
print [node.Name for node in nodes] # List nodes
1 Like