Jump to content

Selecting nodes with python?


Tyfx567

Recommended Posts

I want to create a tool that will later have a callback script on a button that will select the transform node that is inside the tool so that I can say move points around without having to dive inside the tool to select the transform node. Is there any python script that actually selects nodes? I can't find anything. 

 

Thanks guys

Link to comment
Share on other sites

Ok so I  found out how to select: make a variable for the node path, then say exnode.setSelected(1, 1)

But now I can't figure how out how to use this in my callback script? Any ideas?

I eventually want to get to where I can say 

transformnode = hou.node('../uniquetransform')
transformnode.setSelected(1,1)

in my callbackscript since it would be inside a tool it has to be a relative reference

Any ideas?

 

I'm getting an error saying 

whenever I type 

hou.node('obj/box_object1/uniquetransform').setSelected(1,1)

in my callbackscript

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'setSelected'

 

UPDATE: So somehow it is correct when I actually changed it to the relative path so now it is 

hou.node("../uniquetransform").setSelected(1,1) 
Edited by Tyfx567
Link to comment
Share on other sites

If you are running the script as a button callback script and your function is located in your hdaModule this should work.

hou.phm().selectnode()

In the hdaModule you can then do this:

def selectnode():
    hou.node("{0}/uniquetransform".format(hou.pwd().path())).setSelected(1,1)

EDIT: Sorry I did not see that you already solved it with the relative path (however there is a dot too much, it should be "./uniquetransform"). I hope this is still helpful.

Edited by dennis.albus
Link to comment
Share on other sites

Oh thank you Michael and Dennis I didn't know those things. Ok I have been trying to get this to work all day today and am just confused a little about how to implement this. I am realizing that I must do a callback script for what I want to do:

I am building a tool that involves me wanting to show the user a template of an object when they select a certain menu item called templatedObject. It's a lot more involved than just that obviously. Anyway Here is essentially what I want to do:

Whenever I hit templated object in the menu selection. I want this callback script to run. 

viewer = hou.pwd().parm("Viewer")
myObject = hou.node("./null2")
if viewer.evalAsString() == "TemplatedObject":
    for i in hou.pwd().children():
	   i.setTemplateFlag(0)
myObject.setTemplateFlag(1)

so I am clearing all templated objects in the scene just in case there was one already made. Then assigning a template on one specific object. However I am thinking I will have to read this in as a file since I am doing a more complicated for loop in the callback which is really long and I keep getting an error in the callback. So that said I thought of reading in a file using this in the callback section: 

eval(hou.readFile("Path to file"))

To test to see if the file evaluates first before putting it in the callback section I tested it in the python shell reformatting the script to use not local but absolute paths to objects in the scene. But it doesn't work. It says the viewer = hou.node("") part is an invalid syntax.  Any ideas guys?

Edited by Tyfx567
Link to comment
Share on other sites

I would not use hou.readFile() or execfile() (or anything along those lines) for this. You should get used to using proper python modules.

Create you scriptfile (let's call it template.py) in your $HOME/houdinixx.x/python2.7libs folder (or any folder that is in your PYTHONPATH). Now you should be able to import it into the hdamodule of your digital asset or use it in the button callback directly.

import template; template.my_template()
The contents of template.py could be like this.

import hou

def my_template():
  viewer = hou.pwd().parm("Viewer")
  myObject = hou.node("./null2")
  if viewer.evalAsString() == "TemplatedObject":
    for i in hou.pwd().children():
	   i.setTemplateFlag(0)
  myObject.setTemplateFlag(1)
You might have to pass your current node as an argument as I seem to remember that an external module cannot access the current node via hou.pwd().

But I am not 100% sure about this.

Edited by dennis.albus
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...