How to Change the active viewport to given named views

Is there a way to control what view is showing in the viewport in the grasshopper python environment?

The reason I ask this question is I am trying to see if I can achieve a batch view snapshot capture. After some research in the forums, I found there are some previous discussions but the bar is too high and is not very user friendly.

However, I am not sure if the process in my mind is possible:

  1. A list of view name is given
  2. Find the rhino view object
  3. Make the view active
  4. Capture the view to a jpg/png file

I get stuck at step 2, cause I can only find some precedence that help getting rhino view object in the view layout. If I have 4 view layouts, I can only get views amount those layouts.

Hello - RhinoCommon has access to named views -

in Python:

import scriptcontext
nv = scriptcontext.doc.NamedViews

Is that what you need?

-Pascal

hey Pascal, that returns the string of view names, if I am correct. I am trying to get the Rhino view object actually.

Hi Hali - it gives you the view table - you can use it to set the view, e.g.

nv = sc.doc.NamedViews

sc.doc.Views.ActiveView.ActiveViewport.PushViewInfo(nv[0], False)

sc.doc.Views.Redraw()

-Pascal

That is exactly what I am looking for thank you so much!

A related question for me to learn. It seems in your script, nv is a list/arry. Correct me if I am wrong, when I print it to see its content, it returns its objects location in the RAM. Is there a way I can see what it actually contains?

Hi Hali - in EditPythonScript, put a break point on the next line - then you can see all kinds of stuff:

-Pascal

Thank you Pascal! I have moved to step 3 and have some new issues.

  1. ViewCaptureSettings give me an error saying it requires a RhinoPageView instead of a RhinoView. But the API doc says it takes both. Did I do anything wrong?

  2. In another script it works, but there will be a popup window, even though I have assigned a path. Is there a way to prevent that?

Also, I am trying to make similar thing with Snapshots, but it does not allow me to use the “for loop”
It seems Snapshots has more document info inside.

Hi Hali-

  1. If you only give two inputs, ViewCaptureSettings assumes you want a page view; a model view uses three parameters:
    image

I am not sure what you mean in 2…

-Pascal

Thank you Pascal. No worries about popup window in 2, I found another way to save the file and it works fine.

Do you mind telling me more info about my snapshot question above?

Like, is there a way I can get the snapshot object by its name?

Hi @hali,

Using RhinoCommon you can get the named of snapshots, but that’s it. To perform any snapshot behavior, you’ll need to script the Snapshot command using RhinoApp.RunScript.

For example:

# Restore the snapshot
script = "_-Snapshots _Restore \"" + snapshot + "\" _Enter _Enter"
Rhino.RhinoApp.RunScript(script, False)

– Dale

Thank you Dale. If I want to have some level of control to the saved properties with snapshot, like layer state, Objects Visibility/Position etc, is there a way I can find all of them with a given Snapshot name or do I need to go to individual Class to set them up?

I understand what you want to do. However, there no way of doing this, programatically, at this time.

https://mcneel.myjetbrains.com/youtrack/issue/RH-72513

– Dale

Got it, thank you Dale.

Hey Dale, a new question under this topic. With using RhinoApp.RunScript, it seems use a space (" “) to separate command. I run into a situation that, my Snapshot name has a space(” ") in it, and it messed up the command chain. Is there a way to deal with that situation?

never mind, I wasn’t fully copying your script and the \ in it fixed the problem.