Jump to content

Run script in Command Line tools with Python


kiryha

Recommended Posts

I have a render.cmd file with a list of hip files to render:

mread P:/PROJECTS/NSI/PROD/3D/scenes/RENDER/000/SHOT_010/RND_E000_S010_001.hiplc
render -f 1 2 -V /out/RENDER
mread P:/PROJECTS/NSI/PROD/3D/scenes/RENDER/000/SHOT_020/RND_E000_S020_001.hiplc
render -f 1 2 -V /out/RENDER


If I open hcmd.exe and run:

hscript render.cmd


I get all my shots rendered. 

The issue is that I can't get it working with Python, currently able only run Command Line Tools 

import subprocess
cmdTools = '"C:/Program Files/Side Effects Software/Houdini 17.0.459/bin/hcmd.exe"'
subprocess.call(cmdTools)


but have no idea how to execute <hscript render.cmd> later...

Link to comment
Share on other sites

If you want to render the files sequentially, you can do the same you did in that .cmd, but in python. Something like

hou.hipFile.load(file1)
rop = hou.node(path_to_rop)
rop.render(args)

hou.hipFile.load(file2)
rop = hou.node(path_to_rop)
rop.render(args)

...

If you want to to spawn new processes to render your files, you can call hrender using subprocess

subprocess.Popen([path_to_hython, path_to_hrender, hrender_args]) 

And, of course, you can write a specific custom python script to suit any other needs

 

 

  • Like 1
Link to comment
Share on other sites

For the first case, I need to import hou module in standalone Python. This is not working:

import sys
sys.path.append('C:/Program Files/Side Effects Software/Houdini 17.0.459/houdini/python2.7libs')
import hou

For the second, can you, please, provide examples of arguments (path_to_hython, path_to_hrender, hrender_args)?

Mine is not working...

import subprocess
cmdTools = '"C:/Program Files/Side Effects Software/Houdini 17.0.459/bin/hcmd.exe"'
batch = 'hscript P:/RENDER/render.cmd'
subprocess.call([cmdTools, batch])

 

Edited by kiryha
Link to comment
Share on other sites

Sure, here's a hip.

The code is all inside the pythonscript node. It's the following:

import os.path as op
import subprocess

hython_path = op.join(hou.expandString("$HFS"), "bin", "hython")
hrender_path = op.join(hou.expandString("$HFS"), "bin", "hrender.py")
hip_path = hou.hipFile.path()

subprocess.Popen([hython_path, hrender_path, "-e", "-d", "mantra1", hip_path])

As for importing hou, it's not very straight forward. Besides adding the path to the .py file to your current python path, you need to also add the $HFS/bin folder to your PATH variable in order to correctly import the custom houdini C libraries. As described here http://www.sidefx.com/docs/houdini/hom/commandline.html
  

 

subprocess_hython_odf.hip

  • Like 1
Link to comment
Share on other sites

If I load and render file via Python directly:

hou.hipFile.load(file1)
rop = hou.node(path_to_rop)
rop.render(args)

Does it use the same amount of memory as in the case with hscript? As far as I understand you need to load hip file anyway, but if you load it in Houdini it may take a lot of resources to draw the scene in viewport...
 

Link to comment
Share on other sites

  • 4 months later...

You can write a .py file to load a hipfile, set render frame ranges etc, and execute the ROP  ( pressButton() command ).

Save that .py file on disk, then just run:

hython /path/to/your/file.py

 

Hython is the non-gui python version of Houdini.  Just like how hbatch is the Hscript non-GUI version of Houdini.

Link to comment
Share on other sites

  • 2 years later...

To call an external command in a Python script, use any of the following methods:

  • subprocess.run() //Python 3.5+ only.

You can get the stdout, stderr, the "real" status code, better error handling, etc...

  • os.system()

 It passes the command and arguments to your system's shell.

  • subprocess.call() function
  • subprocess.Popen Class
  • os.popen() function

 

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