Running Solver for each loop iteration


I’m trying to collect a series of screenshots of a model with varying parameters using Rhino and Grasshopper. Essentially, the flow of the script I’m trying to create is:

for param in parameters
   set model to have param
   run solver on the model
   take screenshot of model

I am running into trouble with running the solver, as it runs only once even after running through the entire loop. I’ve also enclosed my script in the bottom

I’ve searched online and I found one video that does exactly what I want (it’s able to run solver at each loop iteration), but the tutorial is in an old version of Rhino (I think Rhino 6 or 7?) and no longer works for Rhino 8. Here is the video (timestamp 5:00) and the code snippet I am referring to:

I’m wondering what I need to change/do in order to acheive results similar to the video? Thanks (1.1 KB) (14.5 KB)

I ran the debugger and the model does seem to be re-rendered only when I step through the debugger. But I’d like to visibly see the model getting re-rendered with the click of a button, similar to what the video demonstrates. I tried adding time.sleep() around runSolver() but that doesn’t seem to help. Would appreciate any help. Thanks!

Try this:

#! python3

import os.path as op
import rhinoscriptsyntax as rs
import scriptcontext as sc

import System.Drawing as SD
import Rhino

# determine where to save the image files
path = op.dirname(__file__)

# grab active rhino view
view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView

# setup view capturer 
capture = Rhino.Display.ViewCapture()
capture.Width = view.ActiveViewport.Size.Width
capture.Height = view.ActiveViewport.Size.Height
capture.ScaleScreenItems = False
capture.DrawAxes = False
capture.DrawGrid = False
capture.DrawGridAxes = False
capture.TransparentBackground = False

# get access to running Grasshopper
grasshopper = Rhino.RhinoApp.GetPlugInObject('Grasshopper')

# set initial values
grasshopper.SetSliderValue('00094c37-36a2-4a83-9cfb-942b5f040a29', 10)

counter = 0
for i in range(-50, 91, 5):
    # update gh
    grasshopper.SetSliderValue('00094c37-36a2-4a83-9cfb-942b5f040a29', i)
    # capture and save viewport
    image: SD.Bitmap = capture.CaptureToBitmap(view)
    image.Save(op.join(path, f"image_{counter}.png"))
    counter += 1