younglegend Posted January 24, 2017 Share Posted January 24, 2017 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! Quote Link to comment Share on other sites More sharing options...
f1480187 Posted January 24, 2017 Share Posted January 24, 2017 (edited) Try this: import hou choices = [n.path() for n in hou.node('/out').allSubChildren() if n.type() == hou.nodeType('Driver/ifd')] selected = hou.ui.selectFromTree(choices) nodes = [hou.node(p) for p in selected] Edited January 24, 2017 by f1480187 Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 24, 2017 Author Share Posted January 24, 2017 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 Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 24, 2017 Author Share Posted January 24, 2017 (edited) 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 January 24, 2017 by kishenpj Quote Link to comment Share on other sites More sharing options...
f1480187 Posted January 24, 2017 Share Posted January 24, 2017 (edited) 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 January 24, 2017 by f1480187 1 Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 24, 2017 Author Share Posted January 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
f1480187 Posted January 24, 2017 Share Posted January 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 24, 2017 Author Share Posted January 24, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 24, 2017 Author Share Posted January 24, 2017 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*',)) Quote Link to comment Share on other sites More sharing options...
f1480187 Posted January 25, 2017 Share Posted January 25, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 25, 2017 Author Share Posted January 25, 2017 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*',)) Quote Link to comment Share on other sites More sharing options...
Shinjipierre Posted January 25, 2017 Share Posted January 25, 2017 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. Quote Link to comment Share on other sites More sharing options...
younglegend Posted January 25, 2017 Author Share Posted January 25, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.