Jump to content

List out & select among nodes


younglegend

Recommended Posts

Hey Guys! Pretty new to python here. So I'm making a shelf tool to add items to selected mantra's 'forced objects' parameter. For now i know how to select nodes but wondering how to select among a list of nodes in '/out' in case the user has a bunch of mantra nodes.

All i want to do is if i select the nodes and click on this shelf tool, it should bring up a list of mantra nodes available in '/out' among which i can select, to which mantra nodes i can add the selected nodes to. Hope i can get some help with this. Cheers!

 

Link to comment
Share on other sites

Great, it works! I tried this below, which also works but yours allowed me to select more than one mantra. 

selectionrop=hou.ui.selectNode(relative_to_node=None,initial_node=None,node_type_filter=hou.nodeTypeFilter.Rop)
if selectionrop>0:    print "Mantra node selected:", selectionrop

 

Link to comment
Share on other sites

Now the second part of my problem is to get the name of the selected objects in to the 'forced object' parameter. Here is my full code. Sorry for the mess.

#Add selected nodes to mantra
#by Kishen PJ
#January 2017
  
  selection = hou.selectedNodes()
my_list = ""
if len(selection) < 1 :
    my_list = "nothing selected, please select at least one object"
else:
    a=1
    for sel in selection:
    my_list = my_list + "\n" + "selected object " + str(a) + ":" + sel.name()
    a=a+1        
hou.ui.displayMessage(my_list, buttons=('OK',))    

#for each object in selection
length = len(selection)
print "Number of nodes selected: " + str(length)

#for selecting mantra nodes
selectionrop=hou.ui.selectNode(relative_to_node=None,initial_node=None,node_type_filter=hou.nodeTypeFilter.Rop) # To select 
if selectionrop>0:    
  print "Mantra node selected:", selectionrop
  merop=hou.node(selectionrop) # Gives me the selected mantra node

#Set selected objects to mantra    
  merop.parm("force_objects").set(......?)

I can get the path of selected objects using 'my_list' or 'sel.name()'

But how do i get the names of the nodes like a list? like geo1 geo2 geo3 geo4.

Thanks again!

Edited by kishenpj
Link to comment
Share on other sites

I can get names using node's name() method.

>>> nodes = hou.selectedNodes()
>>> names = [n.name() for n in nodes]
>>> paths = [n.path() for n in nodes]
>>> names
['geo1', 'geo2']
>>> paths
['/obj/geo1', '/obj/geo2']

Not sure if I understand your problem well.

 

UP: oh, I see it! You need to set parameter to space-delimited list of strings. Use this method:

' '.join(names)

It will give you string like 'geo1 geo2'. Same goes for paths.

Edited by f1480187
  • Like 1
Link to comment
Share on other sites

4 minutes ago, f1480187 said:

I can get names using node's name() method.


>>> nodes = hou.selectedNodes()
>>> names = [n.name() for n in nodes]
>>> paths = [n.path() for n in nodes]
>>> names
['geo1', 'geo2']
>>> paths
['/obj/geo1', '/obj/geo2']

Not sure if I understand your problem well.

Its really close , instead of ['geo1', 'geo2'] i meant just geo1 geo2 like you enter in mantra.

Link to comment
Share on other sites

4 minutes ago, f1480187 said:

Yep, got it too. I updated the post couple of seconds before you answered it's initial version. Use 'sepstring'.join(list_of_strings) to merge something with sepstring.

Did not notice it! Awesome! Thanks man. I'll share the proper code once its done; it might be handy.

  • Like 1
Link to comment
Share on other sites

Sharing a proper code! It still selects only one mantra for now. Hope it'll be handy!

 

#Add selected nodes to mantra
#by Kishen PJ
#January 2017


#show selection tuple names
selection = hou.selectedNodes()
names = [n.name() for n in selection]
paths = [n.path() for n in selection]
my_list = ""
if len(selection) < 1 :
    my_list = "nothing selected, please select at least one object"
else:
    a=1
    for sel in selection:
        my_list = my_list + "\n" + " selected object " + str(a) + " : " + sel.name()
        a=a+1        
hou.ui.displayMessage(my_list, buttons=('Right',))    

#for each object in selection
length = len(selection)
print "Number of nodes selected: " + str(length)

#List Mantra
selectionrop=hou.ui.selectNode(relative_to_node=None,initial_node=None,node_type_filter=hou.nodeTypeFilter.Rop)
if selectionrop>1:    print "Mantra node selected:", selectionrop


#add to Mantra
if selectionrop:
   merop=hou.node(selectionrop)
   new_name=' '.join(names)
   merop.parm("matte_objects").set(str(new_name)) #CHANGE PARAMETER NAME AS REQUIRED
hou.ui.displayMessage("( "+ (new_name) + " )  - added to matte objects.", buttons=('*Claps*',))
 

 

Link to comment
Share on other sites

import hou

paths = [n.path() for n in hou.node('/out').allSubChildren()
         if n.type() == hou.nodeType('Driver/ifd')]
selected = hou.ui.selectFromTree(choices=paths)

mantras = [hou.node(path) for path in selected]
for m in mantras:
    m.parm('matte_objects').set(' '.join(n.name() for n in hou.selectedNodes()))

To select multiple operators, you still need to select from custom tree. It seems, hou.ui.selectNode() works only with single operators.

  • Like 1
Link to comment
Share on other sites

13 hours ago, f1480187 said:

import hou

paths = [n.path() for n in hou.node('/out').allSubChildren()
         if n.type() == hou.nodeType('Driver/ifd')]
selected = hou.ui.selectFromTree(choices=paths)

mantras = [hou.node(path) for path in selected]
for m in mantras:
    m.parm('matte_objects').set(' '.join(n.name() for n in hou.selectedNodes()))

To select multiple operators, you still need to select from custom tree. It seems, hou.ui.selectNode() works only with single operators.

Ya i guess so. Anyways i've updated the code. Now you can choose multiple mantra nodes; thanks to F1!

import hou

selection = hou.selectedNodes()
names = [n.name() for n in selection]
my_list = ""
if len(selection) < 1 :
    my_list = "nothing selected, please select at least one object"
else:
    a=1
    for sel in selection:
        my_list = my_list + "\n" + "selected object " + str(a) + ":" + sel.name()
        a=a+1        
hou.ui.displayMessage(my_list, buttons=('OK',))
length = len(selection)
print "Number of nodes selected: " + str(length)
paths = [n.path() for n in hou.node('/out').allSubChildren()
         if n.type() == hou.nodeType('Driver/ifd')]
selected = hou.ui.selectFromTree(choices=paths)

mantras = [hou.node(path) for path in selected]
for m in mantras:
    m.parm('matte_objects').set(' '.join(n.name() for n in hou.selectedNodes())) # CHANGE PARAMETER NAME AS REQURIED
    print ("Selected mantra nodes : ") + m.name()
    new_name=' '.join(names)
hou.ui.displayMessage("( "+ (new_name) + " )  - added to selected mantra(s) matte objects.", buttons=('*Claps*',))
    

 

Link to comment
Share on other sites

2 hours ago, Shinjipierre said:

It feels like what you actually needed is to use the bundle list feature, it stores a selection list in a variable that you can then call directly in your Mantra Rop.

Oh ya, that's a good alternative too. But personally i wanted a quick select and drop. It's handy for me when my nodes are really messy and scattered everywhere! Plus it was a good python exercise. :lol:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...