# pyhon 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 dont 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 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,

• 1

##### 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 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():

def myOtherFunction():
#Do some other stuff

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

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

import myModule

myModule.myFunction()

myModule.myOtherFunction()

Cheers,

##### Share on other sites

You could have a script that launches RPC and then do whatever via RPC. Depending on what you're trying to do this might be overkill/cumbersome.

• 1

##### 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_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!

• 1

## Create an account

Register a new account