Jump to content

Creating a new python script for automating camera DOF setup (SOLVED)


Farsheed Ashouri

Recommended Posts

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 by Farsheed Ashouri
Link to comment
Share on other sites

Nice and simple, SYmek!

Personally, I prefer to add some parameters & wire things up than write a script. :P 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

Link to comment
Share on other sites

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. B)

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. B)

cheers,

skk.

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

Nice and simple, SYmek!

Personally, I prefer to add some parameters & wire things up than write a script. :P 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.

Link to comment
Share on other sites

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. :D

Edited by Farsheed Ashouri
Link to comment
Share on other sites

OK, now the problem is it doesn't update the value. the script should set an exp​ression for focus value. How does python do this?

This is the exp​ression:

vlength(vtorigin("/obj/path_to_camera", "/obj/path_to_null"))

camera.parm('focus').setExpression(...)

Auto-completion in texport rocks! :)

cheers!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by SWANN
Link to comment
Share on other sites

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 by Farsheed Ashouri
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...