Farsheed Ashouri Posted August 28, 2009 Share Posted August 28, 2009 (edited) Hi folks. I asked a question about distance calculation and some nice guys answered me very accurate. Now I want to automate that process. this is what I want my python script does for me: when I select a camera and press a button, It should make a null object that my camera aims to it and calculates the distance between them and put the value to focus distance. it seems very simple and I am totally familiar with python but I need some start help. How can I get the selected object and put it in a variable in hou? How can I make a simple GUI ( Like I had in maya)? Do I need something like wxpython or houdini has it's own gui? Thanks in advanced. Edited August 28, 2009 by Farsheed Ashouri Quote Link to comment Share on other sites More sharing options...
symek Posted August 28, 2009 Share Posted August 28, 2009 (edited) I prepared this example some time ago. No scripting involved. Check Sampling Tab of Camera object. Such ad hoc setups are common in Houdini and usually don't require any scripting (brr... ) cheers, skk. focusOnObject.hip.zip Edited August 28, 2009 by SYmek 1 Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 28, 2009 Author Share Posted August 28, 2009 Well, The example is nice but don't you think it is better to do it with one click rather than crating a set of chops and adding parameters? Second, I am learning hou and I need to know how can I do it using scripting. Quote Link to comment Share on other sites More sharing options...
stevenong Posted August 28, 2009 Share Posted August 28, 2009 Nice and simple, SYmek! Personally, I prefer to add some parameters & wire things up than write a script. Plus, if it's something really useful, you can always package the camera up as a Houdini Digital Asset (HDA), call it Camera with DOF, for example, and you can even distribute it to the company/community. Of course, like you said, you can create a button (or shelf tool) to do the same thing and share it with everyone too. There's always more than one way to do something in Houdini. I would prefer a shelf tool which operates on the selected camera & do what you described. No need for fancy GUIs. Have you gone through the Python Cookbook in the online Help? Cheers! steven Quote Link to comment Share on other sites More sharing options...
symek Posted August 28, 2009 Share Posted August 28, 2009 (edited) Well, The example is nice but don't you think it is better to do it with one click rather than crating a set of chops and adding parameters? Well, assuming all you need to do is RMB on that camera and select "Create New Digital Asset", to create new type of camera with focus puller built in, no, I actually think it's better then scripting. Second, I am learning hou and I need to know how can I do it using scripting. Well, in competition of "dirty programming" this would look like this: def computeFocus(): for node in hou.selectedNodes(): if node.type().name() == "cam": camera = node else: target = node cT = camera.worldTransform().extractTranslates() tT = target.worldTransform().extractTranslates() try: camera.parm("focus").set(cT.distanceTo(tT)) except: print "Did you actually select any camera?" If you paste it into python text port, or place under shelf button and select two objects (which one of is camera and second is an object to compute focus for), you can call computeFocus() and hopefully it will be set correctly (but it won't follow interactively objects of course). Ok, from that point lot's of possibilities. cheers, skk. Edited August 28, 2009 by SYmek 1 Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 28, 2009 Author Share Posted August 28, 2009 Nice and simple, SYmek! Personally, I prefer to add some parameters & wire things up than write a script. Plus, if it's something really useful, you can always package the camera up as a Houdini Digital Asset (HDA), call it Camera with DOF, for example, and you can even distribute it to the company/community. Of course, like you said, you can create a button (or shelf tool) to do the same thing and share it with everyone too. There's always more than one way to do something in Houdini. I would prefer a shelf tool which operates on the selected camera & do what you described. No need for fancy GUIs. Have you gone through the Python Cookbook in the online Help? Cheers! steven Ya, I totally agree with you but I started this thread in scripting section because I want to learn the houdini way of scripting. I am more scripting fan than parameters and nodes fan. I know your are pros and you choose best ways .I know python. I wrote many tools with python API for maya and I just want to comfortable with it inside houdini. Wow, SYmek thanks. I'll test it and report my results. Appreciate all of you. Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 28, 2009 Author Share Posted August 28, 2009 (edited) Ok. I tested your script and added few lines to it. It works good if you select a target and a camera now: def computeFocus(): if hou.selectedNodes(): for node in hou.selectedNodes(): if node.type().name() == "cam": camera = node else: target = node cT = camera.worldTransform().extractTranslates() tT = target.worldTransform().extractTranslates() try: camera.parm("focus").set(cT.distanceTo(tT)) except: print "Did you actually select any camera?" else: print "Please select a camera and a target" I'll continue posting if I found something new. Edited August 28, 2009 by Farsheed Ashouri Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 28, 2009 Author Share Posted August 28, 2009 OK, now the problem is it doesn't update the value. the script should set an expression for focus value. How does python do this? This is the expression: vlength(vtorigin("/obj/path_to_camera", "/obj/path_to_null")) Quote Link to comment Share on other sites More sharing options...
symek Posted August 28, 2009 Share Posted August 28, 2009 OK, now the problem is it doesn't update the value. the script should set an expression for focus value. How does python do this? This is the expression: vlength(vtorigin("/obj/path_to_camera", "/obj/path_to_null")) camera.parm('focus').setExpression(...) Auto-completion in texport rocks! cheers! Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 31, 2009 Author Share Posted August 31, 2009 OK, Here is the first full working (but initial) version of it. Thank you all for helping me doing this. def computeFocus(): if hou.selectedNodes(): for node in hou.selectedNodes(): # print node.path() if node.type().name() == "cam": camera = node else: target = node cT = camera.worldTransform().extractTranslates() tT = target.worldTransform().extractTranslates() # try: camera.parm("focus").set(cT.distanceTo(tT)) theExp='vlength(vtorigin("%s", "%s"))' try: camera.parm("focus").setExpression(theExp % (camera.path(),target.path())) print "Done." except: print "Did you actually select any camera?" else: print "Please select a camera and a target." computeFocus() And here is the .py file: dof.py.tar.gz Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 31, 2009 Author Share Posted August 31, 2009 Now what if I want to create that Null object automatically? I mean what is the hou function for creating a object/node or a null object (or a light)? Thanks. Quote Link to comment Share on other sites More sharing options...
Guest Swann Posted August 31, 2009 Share Posted August 31, 2009 (edited) Now what if I want to create that Null object automatically? I mean what is the hou function for creating a object/node or a null object (or a light)? Thanks. This is for standard light: light = hou.node("/obj").createNode("hlight", "light", run_init_scripts = True) If you use name light instead of hlight as a type of node it will create lightTemplate. for null it just a matter of changing names. null = hou.node("/obj").createNode("null", run_init_scripts = True) Edited August 31, 2009 by SWANN Quote Link to comment Share on other sites More sharing options...
Farsheed Ashouri Posted August 31, 2009 Author Share Posted August 31, 2009 (edited) OK. So with your help I completed this script: Now you just need to select your camera and fireup the script. #!/usr/bin/env hython _version = 0.11 ''' Thanks to everyone who have helped. ''' import hou def computeFocus(): if hou.selectedNodes(): for node in hou.selectedNodes(): # print node.path() if node.type().name() == "cam": camera = node # else: target = node target = hou.node("/obj").createNode("null","DOF_Controler") theExp='vlength(vtorigin("%s", "%s"))' try: camera.parm("focus").setExpression(theExp % (camera.path(),target.path())) # cT = camera.worldTransform().extractTranslates() # tT = target.worldTransform().extractTranslates() # try: camera.parm("focus").set(cT.distanceTo(tT)) print "Done." except: print "Did you actually select any camera?" else: print "Please select a camera and a target." #if __name__ == "__main__": computeFocus() and the python file: dof.v.0.1.py.tar.gz Edited August 31, 2009 by Farsheed Ashouri 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.