Viewport Screenshot

   15843   17   3
User Avatar
Member
19 posts
Joined: 12月 2015
Offline
Is it possible to save what is currently in the active viewport using Python? Like flipbook for just one frame. I cannot find a way to do this and would really appreciate help.
User Avatar
Member
538 posts
Joined: 12月 2006
Offline
Create shelf tool with script:

from time import gmtime, strftime

# GET VIEWPORT CAMERA PATH
cur_desktop = hou.ui.curDesktop()
desktop = cur_desktop.name()
panetab = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).name()
persp = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).curViewport().name()
camera_path = desktop + “.” + panetab + “.” + “world” “.” + persp

# BUILD DEFAULT FILE NAME FROM CURRENT TIME
default_filename = strftime(“screenshot_%d_%b_%Y_%H_%M_%S.jpg”, gmtime())

# SELECT FILE
filename = hou.ui.selectFile( title='Select Screenshot File', default_value=default_filename, file_type=hou.fileType.Image )

# WRITE TO FILE
if filename is not None:
frame = hou.frame()
hou.hscript( “viewwrite -f %d %d %s ‘%s’” % (frame, frame, camera_path, filename) )
https://gumroad.com/alexeyvanzhula [gumroad.com]
User Avatar
Member
19 posts
Joined: 12月 2015
Offline
Thanks a million vux! That's exactly what I was looking for!
User Avatar
Member
140 posts
Joined: 6月 2016
Offline
That is cool trick to know.. thanks for sharing Vux!
Mohan Pugaz
movfx
https://www.instagram.com/movfx/ [www.instagram.com]
https://www.youtube.com/channel/@_movfx
User Avatar
スタッフ
3456 posts
Joined: 7月 2005
Offline
you can look through the python code that builds the Pose Library as well.
Michael Goldfarb | www.odforce.net
Training Lead
SideFX
www.sidefx.com
User Avatar
スタッフ
635 posts
Joined: 7月 2005
Offline
elveatles
Is it possible to save what is currently in the active viewport using Python? Like flipbook for just one frame. I cannot find a way to do this and would really appreciate help.

There is also something called an “OpenGL ROP” in Houdini. I don't think it's quite what you want in this particular case, but it may serve you well in other instances when you just need a quick render of your geometry.

Cristin
User Avatar
Member
140 posts
Joined: 6月 2016
Offline
vux
Create shelf tool with script:

from time import gmtime, strftime

# GET VIEWPORT CAMERA PATH
cur_desktop = hou.ui.curDesktop()
desktop = cur_desktop.name()
panetab = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).name()
persp = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer).curViewport().name()
camera_path = desktop + “.” + panetab + “.” + “world” “.” + persp

# BUILD DEFAULT FILE NAME FROM CURRENT TIME
default_filename = strftime(“screenshot_%d_%b_%Y_%H_%M_%S.jpg”, gmtime())

# SELECT FILE
filename = hou.ui.selectFile( title='Select Screenshot File', default_value=default_filename, file_type=hou.fileType.Image )

# WRITE TO FILE
if filename is not None:
frame = hou.frame()
hou.hscript( “viewwrite -f %d %d %s ‘%s’” % (frame, frame, camera_path, filename) )






For all of you who try to use VUX's script.

I copy pasted this script and it doesnt work at first.
Because there is something wrong with the double quotes,
I just deleted the quotes and retyped them that works perfectly

Thanks a lot to VUX for this script its really useful for me
Mohan Pugaz
movfx
https://www.instagram.com/movfx/ [www.instagram.com]
https://www.youtube.com/channel/@_movfx
User Avatar
スタッフ
5182 posts
Joined: 7月 2005
Offline
Note that H16.0 added HOM support for flipbooking directly, so that you could do the entire script in HOM now without the hscript(“viewwrite”) command.

scene = cur_desktop.paneTabOfType(hou.paneTabType.SceneViewer)
flip_options = scene.flipbookSettings().stash()
flip_options.frameRange( (frame, frame) )
flip_options.output(filename)
scene.flipbook(scene.curViewport(), flip_options)
User Avatar
Member
140 posts
Joined: 6月 2016
Offline
Really cool to know , thanks for the useful info and code
Mohan Pugaz
movfx
https://www.instagram.com/movfx/ [www.instagram.com]
https://www.youtube.com/channel/@_movfx
User Avatar
Member
4593 posts
Joined: 2月 2012
Offline
Can we now both launch the flipbook window and also save the result on disk in H16? Before it wasn't possible. For example auto save flipbook on disk while also showing it interactively flipbooking using mplay?
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]

youtube.com/@pragmaticvfx | patreon.com/animatrix | pragmaticvfx.gumroad.com
User Avatar
スタッフ
5182 posts
Joined: 7月 2005
Offline
You could write a HOM script to do that, interleaving writing to disk and ip for each frame, but otherwise no.
User Avatar
Member
4593 posts
Joined: 2月 2012
Offline
Thanks Mark that's what I wanted to do. I was told before it couldn't be done.
ID #72062

I thought once I launch flipbook, I can't control mplay anymore. Any idea how would this look like in pseudo code?
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]

youtube.com/@pragmaticvfx | patreon.com/animatrix | pragmaticvfx.gumroad.com
User Avatar
Member
8 posts
Joined: 9月 2017
Offline
animatrix3d
Thanks Mark that's what I wanted to do. I was told before it couldn't be done.
ID #72062

I thought once I launch flipbook, I can't control mplay anymore. Any idea how would this look like in pseudo code?

I also want to do this, do you have any ideas or code can solve this problems
User Avatar
Member
900 posts
Joined: 2月 2016
Offline
Hello, I have to bump this old discussion.

I tried the code, which works super fine, but it popups the save dialog.
I need it to automatically write the viewport.jpg to disk, as fast as possible, as my trying to do a batch export of the crude geometry in the viewport. No need fancy stuff like AA, or shadows. Headlight only.

Ideally it should save the viewport_$F.jpg just after houdini cooks the frame. (so no save sequence)

I had a look at the hou.FlipbookSettings class [www.sidefx.com] , but couldn't find any method to automatically skip the save dialog, and write the file to disk.

thanks for any advice!
User Avatar
Member
900 posts
Joined: 2月 2016
Offline
Should I go this way maybe?
https://pypi.org/project/pyscreenshot/ [pypi.org]
External module to take screenshots and save them to disk.



*Edit*
I guess I should use the openGL render node.
Edited by Andr - 2018年11月13日 16:21:33
User Avatar
Member
7900 posts
Joined: 9月 2011
Offline
There's no save dialog when flipbooking directly to a path. I use a qt gui that initiates flipbooks through python, and this is not an issue. Perhaps you are not passing a flipbooksettings object?
User Avatar
Member
900 posts
Joined: 2月 2016
Offline
oh thanks, now I get it.
I was still using hou.ui.selectFile() from the previous code.

cheers
User Avatar
Member
380 posts
Joined: 7月 2005
Offline
This was a tad annoying for me in H19.0.455 as the MPlay window would launch by default whenever I invoked the render -- until I discovered that outputToMplay() is True by default.

import traceback
from os.path import dirname,basename,splitext

def getSceneViewer():
    """
    return an instance of a visible viewport. 
    There may be many, some could be closed, any visible are current
    """
    panetabs = hou.ui.paneTabs()
    panetabs = [x for x in panetabs if x.type() == hou.paneTabType.SceneViewer]
    panetabs = sorted(panetabs, key=lambda x: x.isCurrentTab())
    if panetabs:
        return panetabs[-1]
    else:
        print("No SceneViewers detected.")
        return None
    
   
def viewwrite():
    """
    Get the current sceneviewer (may be more than one or hidden)
    and screengrab the perspective viewport to a file in the 
    publish location to be picked up with the publish. 
    
    Note that .png output will render pooly, so use .jpg   
    """
    try:
        node = hou.node(".")
        path = node.parm("outputpath").eval()       
        file,ext = splitext(basename(path))
        filename = f'{dirname(path)}/{file}.jpg'
        frame = hou.frame()
    
        desktop = hou.ui.curDesktop()
        sceneview = getSceneViewer()
        viewport = sceneview.curViewport()
    
        # this will open an mplay window to show the result
        fbs = sceneview.flipbookSettings().stash()
        fbs.frameRange( (frame, frame) )
        fbs.output(filename)
        fbs.outputToMPlay(False)
        sceneview.flipbook(viewport, fbs)
            
    except Exception as e:
        print(f'ERROR: Failed to Capture Viewport\n{traceback.format_exc(2)}')
        
  • Quick Links