Jump to content
kiryha

Run Python script after Houdini starts

Recommended Posts

I am launching Houdini with subprocess:

HOUDINI = 'C:/Houdini 18.0.287/bin/houdinifx.exe'
subprocess.Popen(HOUDINI)

How can I import a Python and run a necessary function after Houdini is launched (import open_asset; open_asset.open())? 

In Maya I can send MEL command to be executed after Maya starts (and import script with this command):

subprocess.Popen('C:/Maya 2019/bin/maya.exe', '-command', "python('execfile(\"path_to_the_script\"')")

How can I do something like this in Houdini? The 123.py is not a solution, because I don`t need to run a script each time, only in certain cases, and the scripts are different each time.
I need this to open an asset file from Shotgun, for example.

Share this post


Link to post
Share on other sites

I know that with the command line tool you can pass this and it executes the script in houdini (at least I tried now with just a print and it prints to the houdini console) :

houdini D:\path\to\myfile.hip D:\path\to\mycript.py 

So maybe you can feed the subprocess with :

subprocess.Popen("C:\path\to\houdini.exe", ["D:\path\to\myfile.hip", "D:\path\to\myscript.py"])

 

Cheers,

  • Like 1

Share this post


Link to post
Share on other sites
subprocess.Popen([HOUDINI, ['C:/temp/script.py'], 'argument_one'])

That gives me what I need. The only thing I don't know how to run a function from script.py after import...

Share this post


Link to post
Share on other sites

I guess you can make 2 python files, one with your functions, and the other one where you source your module and execute the function you need. Like :

"""
This is my module file where I have defined my functions
myModule.py
"""

def __init__():
	pass
  
def myFunction():
	#Do your stuff
    
def myOtherFunction():
	#Do some other stuff
    

#########################

"""
This is my file called by Houdini during the opening process
setHoudini.py
"""

import myModule
reload(myModule)

myModule.myFunction()

myModule.myOtherFunction()

 

Cheers,

Share this post


Link to post
Share on other sites

I ran into the same problem, and was able to get this to work with the below code:

 

import subprocess
houdini_app = ('"C:\\Program Files\\Side Effects Software\\Houdini 18.0.566\\'
               'bin\\houdinifx.exe"')
houdini_load_file = 'C:\\houdini_filename.hip'
houdini_script_path = 'C:\\pythong_filename.py'
houdini_cmd = '{} {} {}'.format(houdini_app, houdini_load_file, houdini_script_path)
subprocess.Popen(
    houdini_cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    shell=True
)

Hope this helps someone else!

  • Like 1

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

×