Jump to content

Run Python script when Houdini starts


Nerox

Recommended Posts

Hi Guy's!

I wonder if there is a way to execute a python script when Houdini starts. Applications for this might be for instance: Show a project selection GUI.

In this case I would like to run the sys.path.append(path to script folder), since it seems that home/user/houdin.X.Y/scripts isn't recognized as a script dir

Hope this makes sense :-)

Link to comment
Share on other sites

Hi Guy's!

I wonder if there is a way to execute a python script when Houdini starts. Applications for this might be for instance: Show a project selection GUI.

In this case I would like to run the sys.path.append(path to script folder), since it seems that home/user/houdin.X.Y/scripts isn't recognized as a script dir

Hope this makes sense :-)

In your houdini path ie ../houdini/scripts/ there are two different .cmd files. 123.cmd and 456.cmd. The 123.cmd gets run when you open up a new houdini session and 456.cmd gets run when you open up a new scene in an existing session. Im sure Im missing some other technical details to what these scripts do so someone correct me if Im wrong :blink:

You can modify these to included your own additional pipeline scripts. The .cmd files are in hscript but you can create a python version by calling it the same name ie 123.py/456.py and save it in the same directory. Have your python version source the .cmd file so that all the defaults get run when the python version gets called.

import hou
hou.hscript("source 456.cmd")

then after that you can add your sys.path.append(path to script folder)

Link to comment
Share on other sites

In your houdini path ie ../houdini/scripts/ there are two different .cmd files. 123.cmd and 456.cmd. The 123.cmd gets run when you open up a new houdini session and 456.cmd gets run when you open up a new scene in an existing session. Im sure Im missing some other technical details to what these scripts do so someone correct me if Im wrong :blink:

You can modify these to included your own additional pipeline scripts. The .cmd files are in hscript but you can create a python version by calling it the same name ie 123.py/456.py and save it in the same directory. Have your python version source the .cmd file so that all the defaults get run when the python version gets called.

import hou
hou.hscript("source 456.cmd")

then after that you can add your sys.path.append(path to script folder)

Awesome :-)

The downside of this is that you have to create (or copy) this file every time you install a new build. I'm trying to make my life as easy as possible, obviously :P, by placing al the scripts in the home/username/houdinix.y/scripts. But I guess I can't get around this one ('eh?' = one of the souvenirs I got from Toronto ;-)

Link to comment
Share on other sites

I also noticed that from 123.py, although sys.path.append(path to script folder) works like a charm, any UI type of commands (like hou.ui.displayMessage('hello')) don't seem to have any effect. I also tried to import an external script/module and launch a function from there but this also doesn't seem to have effect. This leads to the question where should I call my 'open project window' which opens when Houdini launches?

Edit:

I see! I can also store 123.py in $HOME/houdinix.y, that's nice :-). And now the popup window works as well, though it show up at the splash screen, but at least it show up :-).

Edited by Nerox
Link to comment
Share on other sites

In this case I would like to run the sys.path.append(path to script folder), since it seems that home/user/houdin.X.Y/scripts isn't recognized as a script dir

I assume it is a python script that you want to put in /home/user/houdini.X.Y/scripts that Houdini isn't picking up? Try putting it in /home/user/houdini.X.Y/scripts/python. If that doesn't work, try /home/user/houdini.X.Y/python2.xlibs (if you are using python 2.x). I think since Houdini 11 the preferred location for python scripts is the python2.xlibs directory.

Link to comment
Share on other sites

I assume it is a python script that you want to put in /home/user/houdini.X.Y/scripts that Houdini isn't picking up? Try putting it in /home/user/houdini.X.Y/scripts/python. If that doesn't work, try /home/user/houdini.X.Y/python2.xlibs (if you are using python 2.x). I think since Houdini 11 the preferred location for python scripts is the python2.xlibs directory.

I actually tried that first but that doesn't seem to work. the 123.py file like described above and than saved in /home/user/houdini.X.Y/scripts works perfect

Link to comment
Share on other sites

  • 1 month later...

Hey,

in that relm i have another question. Is it possible in that very python script to add python code to the hou.session() part of things?

Basicaly i want to be a able to store a .py file in a directory that contains certain functions specific to the file and then when this file gets opened to append a function to that.

This is based on the idea in the python master class about the "bling server" to have a function inside that hip file that once that file is called in hython can be assumed to be there. For example and init function that checks for assets and a render function that is then called on a farm box... am i confusing?!

Thanks

jo

Link to comment
Share on other sites

Hey,

in that relm i have another question. Is it possible in that very python script to add python code to the hou.session() part of things?

Basicaly i want to be a able to store a .py file in a directory that contains certain functions specific to the file and then when this file gets opened to append a function to that.

This is based on the idea in the python master class about the "bling server" to have a function inside that hip file that once that file is called in hython can be assumed to be there. For example and init function that checks for assets and a render function that is then called on a farm box... am i confusing?!

Thanks

jo

You mean like importing a module?

Link to comment
Share on other sites

You mean like importing a module?

like a custom bit of python..

def awesome():

pint 'wohooo'

and have that loaded into hou.session() to be used somwhere else...

i think i found the answer there is a hou.session function forgot the name that allows you to add function to the module

i post uppon completion of this idea ;)

Link to comment
Share on other sites

like a custom bit of python..

def awesome():

pint 'wohooo'

and have that loaded into hou.session() to be used somwhere else...

i think i found the answer there is a hou.session function forgot the name that allows you to add function to the module

i post uppon completion of this idea ;)

Correct me if I'm wrong, but what about creating your own module like re and os, basically a collection of one or more functions stored in a file which you can load in when ever you like. If your module is called 'myModule' and a typical function is called 'functionX()' then you can place 'import myModule' in the 123.py script and then you can call in houdini: 'myModule.functionX()'. If you would like to import an specific fucntion and don't want to type 'myModule' all the time, you can also say: 'from myModule import functionX.

Hope this helps

Link to comment
Share on other sites

  • 5 years later...

Thanks Alex!

I was able to get everything working!  I ended up creating a 123.py module and putting it inside a custom scripts location and setting the HOUDINI_SCRIPT_PATH.

For example:

import os
HOUDINI_SCRIPT_PATH = 'custom/path/to/houdini/scripts' + os.pathsep + '&'

I put all of my code inside of the 123.py module and only put the execution bits into a function.  This function was then executed using hdefereval. Example code below.  It searches for a pypanel file at a specified PYPAN_PATH.  If it doesn't find it, then it creates the file, installs it, and creates it on startup of Houdini:

"""
123.py @ houdini.scripts

Some code taken from Paul Winex's hqt: https://github.com/paulwinex/hqt/blob/master/hqt_example.py
"""
import os
import sys
import tempfile

import hou
import hdefereval

# Path to python_panels dir (must exist already)
PYPAN_PATH = os.path.join(
	'path/to/module', 
	'resource', 'houdini', 'python_panels', 'mb_MMV_pypanel.pypanel'
).replace('\\', '/')

def createPanelFile():
	xml = '''<?xml version="1.0" encoding="UTF-8"?>
<pythonPanelDocument>
  <!-- This file contains definitions of Python interfaces and the
 interfaces menu.  It should not be hand-edited when it is being
 used by the application.  Note, that two definitions of the
 same interface or of the interfaces menu are not allowed
 in a single file. -->
  <interface name="test" label="test_label" icon="MISC_python" help_url="">
		<script><![CDATA[import sys
from PySide import QtGui

path = "path/to/your/UI/source/"
if not path in sys.path:
		sys.path.append(path)

import your_module
reload(your_module.MainWindow)

def createInterface():
		widget = your_module.MainWindow.Build()
		return widget
]]></script>
		<help><![CDATA[]]></help>
  </interface>
</pythonPanelDocument>
	'''
	pypanel_file = open(PYPAN_PATH, "w")
	pypanel_file.writelines(xml)
	pypanel_file.close()
	print pypanel_file
	return pypanel_file.name
		
get_pypanel = os.path.isfile(PYPAN_PATH)

if get_pypanel == True:
	panFile = PYPAN_PATH
else:
	print 'create path'
	panFile = createPanelFile()

hou.pypanel.installFile(panFile)
pypan = hou.pypanel.interfacesInFile(panFile)[0]

def installedInterfaces():
   res = []
   menu = hou.pypanel.menuInterfaces()
   for i in  menu:
	try:
		hou.pypanel.setMenuInterfaces((i,))
		res.append(i)
	except:
		pass
   return res


menu = installedInterfaces()
menu.append(pypan.name())
menu = [x for x in menu if not x == '__separator__']
new = []
for m in menu:
	if not m in new:
		new.append(m)


def make_panel():
	hou.pypanel.setMenuInterfaces(tuple(new))
	pane =  max(0,len(hou.ui.curDesktop().panes())-1)

	python_panel = hou.ui.curDesktop().panes()[pane].createTab(hou.paneTabType.PythonPanel)
	python_panel.setIsCurrentTab()
	python_panel.showToolbar(0)
	python_panel.setActiveInterface(pypan)

hdefereval.executeDeferred(lambda: make_panel())

The hdefereval was only necessary because I wanted to make changes to Houdini's UI (like create a custom panel).  Check out Paul Winex's MSE and hqt to learn more about automating UI creation in Houdini.

Edited by borbs727
  • Like 1
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...