Jump to content
evilbear

How to run a python script in houdini?

Recommended Posts

Hi, All:

I wrote a python script can import camera data from a text file, but I can't figure out how to run my script in houdini, I was using python shell try to source my script, I just can not get it to work.

Could somebody teach me how to do it?

Here is a part of my script,

import os

import string

import hou

def importMayaCamera(fileName):

# check if file exists

if not os.path.isfile(fileName):

print 'file not found: %s' % fileName

return

# r means to read

fileHandle = open(fileName,'r')

data = fileHandle.readlines()

header = data[0]

cam = hou.node("/obj").createNode("cam", "camera1")

Thank you

Share this post


Link to post
Share on other sites

http://www.sidefx.co...2.1/hom/session

Do like that.

I fail to see the point of that script tho as it don't actually use any data from the file it reads?


import os
import string
import hou
def importMayaCamera(fileName):

# check if file exists

if not os.path.isfile(fileName):

print 'file not found: %s' % fileName
return

# r means to read

fileHandle = open(fileName,'r')
data = fileHandle.readlines()

header = data[0]

cam = hou.node("/obj").createNode("cam", "camera1")
[/CODE]

CODE tags are great for readability.

Share this post


Link to post
Share on other sites

you can use your code inside of shelf button code so it will execute from shelf, of course you will need to call importMayaCamera() function there after defining as well

or you can copy the python file into scanned scripts directory, then you can import it into houdini as module using

import filename (without extension)

it's advisable to do this for shelf tools as well since it is easier to manage one source module file than lots of shelf tools

but you will be to import it into python shell, python session simply anywhere in houdini to call your functions from that file

or don't copy it to scanned script directory and use

load_source() function from imp module to create module from full path to your file

not very flexible, but if you always know the path, it is an option

or you can use your code in session module of current hip file as Erik suggested, but that will narrow it's usage only from within that file

Share this post


Link to post
Share on other sites

http://www.sidefx.co...2.1/hom/session

Do like that.

I fail to see the point of that script tho as it don't actually use any data from the file it reads?


import os
import string
import hou
def importMayaCamera(fileName):

# check if file exists

if not os.path.isfile(fileName):

print 'file not found: %s' % fileName
return

# r means to read

fileHandle = open(fileName,'r')
data = fileHandle.readlines()

header = data[0]

cam = hou.node("/obj").createNode("cam", "camera1")
[/CODE]

CODE tags are great for readability.

Thank you Erik, I will try,

it's only part of my script, I didn't paste the whole script.

Share this post


Link to post
Share on other sites

you can use your code inside of shelf button code so it will execute from shelf, of course you will need to call importMayaCamera() function there after defining as well

or you can copy the python file into scanned scripts directory, then you can import it into houdini as module using

import filename (without extension)

it's advisable to do this for shelf tools as well since it is easier to manage one source module file than lots of shelf tools

but you will be to import it into python shell, python session simply anywhere in houdini to call your functions from that file

or don't copy it to scanned script directory and use

load_source() function from imp module to create module from full path to your file

not very flexible, but if you always know the path, it is an option

or you can use your code in session module of current hip file as Erik suggested, but that will narrow it's usage only from within that file

Thank you Tomas,

I did put my file in script scan dictionary, and run the import module, and it's failed and said can't find module hou.

could you explain more cleary on do it in shelf code?

Share this post


Link to post
Share on other sites

since it is python script, you need to put it to scripts/python directory, like $HOME/houdini12.1/scripts/python

other than that you probably did it correctly

so if your file is named cam.py then shelf tool will be


import cam
fileName = "C:\cam.txt"
cam.importMayaCamera(fileName)
[/CODE]

of course you wouldn't have hardcoded fileName, but it's just proto code

EDIT:oh, and while testing the code it is useful to add reload(<modulename>) after import, so it will update if you've changed the source file even without restarting houdini

so it would be like

[CODE]
import cam
reload(cam)
fileName = "C:\cam.txt"
cam.importMayaCamera(fileName)
[/CODE]

Edited by anim
  • Like 1

Share this post


Link to post
Share on other sites

It's working perfectly, thank you so much

since it is python script, you need to put it to scripts/python directory, like $HOME/houdini12.1/scripts/python

other than that you probably did it correctly

so if your file is named cam.py then shelf tool will be


import cam
fileName = "C:\cam.txt"
cam.importMayaCamera(fileName)
[/CODE]

of course you wouldn't have hardcoded fileName, but it's just proto code

EDIT:oh, and while testing the code it is useful to add reload(<modulename>) after import, so it will update if you've changed the source file even without restarting houdini

so it would be like

[CODE]
import cam
reload(cam)
fileName = "C:\cam.txt"
cam.importMayaCamera(fileName)
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×