Creating a new viewport with python/rhinocommon

Hi,
Sorry about a noob question:

I’m trying to use rhinocommon with python to create a new viewport in rhino with the following method
Rhino.DocObjects.Tables.ViewTable.Add

the method requires the following inputs:

| title: The title of the new Rhino view.
| projection: A basic projection type.
| position: A position.
| floating: true if the view floats; false if it is docked.
| Returns: The newly constructed Rhino view; or null on error.
However when I try to use it python gives an error:

Add() takes exactly 5 arguments (4 given)
I’ve figured out that it wants me to give it also the “self” argument:

Add(self: ViewTable, title: str, projection: DefinedViewportProjection, position: Rectangle, floating: bool)
However I have no idea what to give as a first argument.

Here is the code if it helps:

import Rhino.DocObjects.Tables as tables
import Rhino
import System
tables.ViewTable.Add(“Testi”,Rhino.Display.DefinedViewportProjection.Perspective,
System.Drawing.Rectangle(0,0,100, 100),True)

Thanks from your help in advance!

-Matti Pirinen

Hi Matti,

This is by far a noob question

To work with the active document, use scriptcontext.
Try this:

import scriptcontext
scriptcontext.doc.Views.Add("Testi",Rhino.Display.DefinedViewportProjection.Perspective,System.Drawing.Rectangle(50,50,100, 100),True)

1 Like
import Rhino
import System

Rhino.RhinoDoc.ActiveDoc.Views.Add("Testi", Rhino.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(0,0,100, 100), True)

I can not find those topics, but both on grasshopper forum and in here, McNeel guys recommended using scriptcontext instead of Rhino.RhinoDoc.
So you should stick with Willem’s example.

Thank you for the answers!

Although it is weird that the method requires a argument for “self” since for my understanding it is only the argument that you use to refer to the method you are using… However I’m happy to continue with scriptcontext

I found a post with some further insight on scriptcontext:

http://discourse.mcneel.com/t/understanding-scriptcontext/7175

HTH
-Willem

Hi Matti

In my understanding …
the Add() method belongs to the ViewTable class, because it needs the table to add the viewport to.
If we don’t give it a ViewTable object, where should it put that viewport ? :wink:
Looks like you called the method as if it were a static method, but it is not.
It is an instance method that you can only call giving it an instance of the class as ‘self’.

The following works here:

import Rhino
import System
Rhino.RhinoDoc.ActiveDoc.Views.Add("Testi",Rhino.Display.DefinedViewportProjection.Perspective,
System.Drawing.Rectangle(0,0,100, 100),True)

BTW … it’s actually the same code both Willem and Djordje posted …
I don’t think scriptcontext makes any difference here ( tanking about a Rhino script, not in GH … ) … but I may be wrong obviously.

HTH Cheers

There is a very slight difference between RhinoDoc.ActiveDoc and scriptcontext.doc

  • When you are running Rhino python scripts on Windows, there is really no difference between the two
  • When you are running python scripts inside of Grasshopper, scriptcontext.doc is typically a “pseudo” doc that works well in the context of Grasshopper
  • When you are running Rhino python scripts on Mac, there are typically many docs and which one is “active” can be a little unclear. In this context scriptcontext.doc is always the doc that was active when the script started executing.

In general, it is typically best to use scriptcontext.doc instead of RhinoApp.ActiveDoc. This will make your scripts more “cross platform” capable.

3 Likes

Thanks a lot, Steve !
This is very valuable information

BTW … I don’t know if this information is already contained in the http://developer.rhino3d.com/ web site ( I’m not able to find it … )
If it is not there, I’d suggest to insert it.

Thanks

Thank you all to the answers on my question. Had already moved forward and actually came back to this post by accident. It’s nice to notice that people are willing to help the newbies!