TIPS SCRIPT CONTEXT IN GH PYTHON GHPYTHON的脚本本环境

来源:互联网 发布:最伟大的皇帝知乎 编辑:程序博客网 时间:2024/06/08 07:00

image

Contents

  1. Script Context
    1. Spaces to store objects info in GH / Rhino
    2. Object V.S. Guids
    3. How to change spaces to work
  2. Accessing to Rhino Document space
    1. Create / select objects directly from GH Python
    2. Object attributes
    3. Dealing with layer

Script Context

Spaces to store objects in GH Python

There are 3 spaces to store objects information:
- GH document
- Rhino document
- memory space on computer

In GH document and Rhino document, objects we can touch are not objects themselves but just interfaces. Namely, objects – actually they are not objects anymore – in these spaces are just IDs that indicates where the objects are stored in memory space on computer. GH Python can access these objects via rhinoscriptsyntax or ghpythonlibs. In these spaces object attributes, which are like color, layer, material, etc, is stored separately, because objects in these spaces cannot have object attributes.

As for the objects in memory space on computer are able to be dealt with Rhino.Geometry. Objects in memory have object attributes.

Objects V.S. Guids

Objects in GH document and Rhino document are called “Guid” and in memory space is called by objects type (Rhino.Geometry…).

Following image is the result displayed on Grasshopper with these 2 codes.

– Rhino.Geometry

import Rhino.Geometry as rga = rg.NurbsCurve.Create(False,3,x)print a

– rhinoscriptsyntax

import rhinoscriptsyntax as rsa = rs.AddCurve(x)print(a)

image

How to change spaces to work

We can easily change working space between grasshopper document and memory space in computer by changing script libraries. But to change working space into Rhino document, we need to import a function from Rhino. Scriptcontext is the function to change working space between GH document and Rhino document.

To import scriptcontext, you can just type a line at the beginning of your code:

import scriptcontext as sc

and to change space to store objects, you can change doc by making a change in “sc.doc”:

#GH doc to Rhino docsc.doc = Rhino.RhinoDoc.ActiveDoc#Rhino doc to GH docsc.doc = ghdoc

Accessing to Rhino Document space

Now you can access to the rhino document space using scriptcontext. You can easily bake object into Rhino, make some modification in objects in Rhino space, and also deal with layer structure.

Create objects in Rhino directly from GH Python

To add object into Rhino, you can just go to Rhino document with scriptcontext and create object with rhinosctiptsyntax.

import rhinoscriptsyntax as rsimport scriptcontext as scimport Rhino#go rhinosc.doc = Rhino.RhinoDoc.ActiveDocrs.AddCurve(x)#back to ghsc.doc = ghdoc

And to move objects from Grasshopper to Rhino, namely to bake objects, you need to format GH objects for Rhino document. And also, you can add some attributes to objects to bake object with certain specifications.

import rhinoscriptsyntax as rsimport scriptcontext as scimport Rhinosc.doc = ghdoc#get object id from grasshopperobj_id = x#convert grasshopper object into rhino objectdoc_obj = rs.coercerhinoobject(obj_id)#make attributes and geometry from rhino objectattributes = doc_obj.Attributesgeometry = doc_obj.Geometry#go rhino documentsc.doc = Rhino.RhinoDoc.ActiveDoc#bake object with attributesrhino_obj = sc.doc.Objects.Add(geometry, attributes)rs.ObjectColor(rhino_obj, y)#back to ghsc.doc = ghdoc

Dealing with layer

As you can add objects into Rhino, you can make layers from Grasshopper.

import rhinoscriptsyntax as rsimport Rhinoimport scriptcontext as scsc.doc = Rhino.RhinoDoc.ActiveDocrs.AddLayer(name="new layer", color=0, visible=True, locked=False, parent=None)sc.doc = ghdoc

But to bake object on certain layer, you need to find layer id on Rhino to identify the layer to bake on.

import rhinoscriptsyntax as rsimport Rhinoimport scriptcontext as sc#create layersc.doc = Rhino.RhinoDoc.ActiveDocrs.AddLayer(name="new layer", color=0, visible=True, locked=False, parent=None)#create rhino objectsc.doc = ghdocobj_id = xdoc_object = rs.coercerhinoobject(obj_id)attributes = doc_object.Attributesgeometry = doc_object.Geometry#select layer to bake and add to the attributessc.doc = Rhino.RhinoDoc.ActiveDoclayertable = sc.doc.Layerslayerindex = layertable.Find("new layer",True)attributes.LayerIndex = layerindex#bake objectrhino_obj = sc.doc.Objects.Add(geometry, attributes)sc.doc = ghdoc
原创粉丝点击