Automatic snapshots in different Rhino viewpoints. Python script

Hi everyone,

I am struggling to create a python script within GH to make snapshots automatically from Rhino baked geometries to .png files.

I found this amazing script made by @AndersDeleuran which solves this problem. However, in the case I have, geometries are placed in different Rhino views and my lack of scripting knowledge makes me impossible to edit the code.

The input I have is a bunch of curves placed in a row within an individual rectangle which represents the area to print in a png. What I would like to do is to create an individual png file of each rectangle.

What I thought is to create a script, which inputs would be these rectangles, capable of selecting each rectangle, make an individual zoom selected, and use Ander’s script to print the screenshot in a png file with a defined name.

Would anyone help me with this topic?

I really appreciate it!

Regards!

2 Likes

This might get you started with that part:

2 Likes

Hi @AndersDeleuran ,

That script was very helpful, thank you. I am still struggling with the coding since something is wrong but I am not capable of finding the mistake:

import Rhino as rc
import ghpythonlib as gh
import os

def zoomSelected(geometryBases):
    
    """ Zoom to geometryBases ala Grasshopper Zoom function, inflate to zoom out """
    
    bb = rc.Geometry.BoundingBox.Empty
    for gb in geometryBases:
        bb.Union(gb.GetBoundingBox(False))
    bb.Inflate(0)
    avp = rc.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport
    result = avp.ZoomBoundingBox(bb)
    
    return result


def captureViewportToFile(width,height,directory,fileName):
    
    """ Captures the Rhino viewport to an image file (png). """
    
    # Check that the directory exist
    if not os.path.exists(directory):
        print "Folder does not exist"
        
    else:
        # Concatenate the directory, file and extension strings
        filePath = directory + "\\" + fileName + ".png"
        filePath = filePath.replace("\\","\\\\")
        
        # Setup the capture settings
        wh = " _Width=" + str(width) + " _Height=" + str(height)
        sga = " _Scale=1 _DrawGrid=No _DrawWorldAxes=No"
        abg = " _DrawCPlaneAxes=No _TransparentBackground=No "
        capSettings =  wh + sga + abg
        
        # Capture the image to file
        if rc.RhinoApp.RunScript("-ViewCaptureToFile " + filePath + capSettings + "_Enter",echo=False):
            
            # Print message
            print "Captured: " + os.path.normpath(filePath)

Vector_length = gh.components.ListLength(Geometry)

if Calculate:
    for i in range(Vector_length):
        zoomSelected(Geometry[i])
        captureViewportToFile(Width,Height,Directory,FileName[i])

The script worked with one geometry but not with a list of them. The error says TypeErrorException: iteration over non.sequence of type Guid.

Does anyone know about this error?

The input here are two rectangles

Thanks in advance

I made some changes and it finally worked!

I cannot still understand why the previous script was not working. If someone can detect the mistake I really appreciate to let me know it.

Here is the solution:

import Rhino as rc
import ghpythonlib as gh
import os

vector_length=gh.components.ListLength(Geometria)

if Toggle:
    for i in range(vector_length):
        bb = rc.Geometry.BoundingBox.Empty
        gb = Geometria[i]
        solido = gb.GetBoundingBox(False)
        avp = rc.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport
        Result= avp.ZoomBoundingBox(solido)
        
        if not os.path.exists(Directory):
            print "Folder does not exist"
        else:
            filePath = Directory + "\\" + FileName[i] + ".png"
            filePath = filePath.replace("\\","\\\\")
            wh = " _Width=" + str(Width) + " _Height=" + str(Height)
            sga = " _Scale=1 _DrawGrid=No _DrawWorldAxes=No"
            abg = " _DrawCPlaneAxes=No _TransparentBackground=No "
            capSettings =  wh + sga + abg
            if rc.RhinoApp.RunScript("-ViewCaptureToFile " + filePath + capSettings + "_Enter",echo=False):
                print "Captured: " + os.path.normpath(filePath)

Thank you @AndersDeleuran for your help. That script helped me a lot!

By the way, is there a command to crop the capture or to print just a selected rectangle area?

Regards!

2 Likes