TIPS SCRIPT CONTEXT IN GH PYTHON GHPYTHON的脚本本环境
来源:互联网 发布:最伟大的皇帝知乎 编辑:程序博客网 时间:2024/06/08 07:00
Contents
- Script Context
- Spaces to store objects info in GH / Rhino
- Object V.S. Guids
- How to change spaces to work
- Accessing to Rhino Document space
- Create / select objects directly from GH Python
- Object attributes
- 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)
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
- TIPS SCRIPT CONTEXT IN GH PYTHON GHPYTHON的脚本本环境
- debugging python script in ipython
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件 (本脚本是python的2.7.x环境下可用)
- gh公告华国锋的
- Batch script in windows(批处理脚本)
- Linker script in Linux(什么是链接脚本 )
- gh
- gh
- gh
- gh
- cocos2d-python 环境搭建 解决Couldn't find a setup script in ……
- Python Imaging Library: Script(脚本)
- Unity3D的脚本-script入门
- Unity3D的脚本-script入门
- Unity3D的脚本-script入门
- 常用的shell脚本tips
- use python in vim script of ~/.vimrc
- GH的热键,只要用gh都要拜读一下吧
- linux目录权限含义
- 循环
- C++ 类的静态成员详细讲解
- BadBoy的检查点和参数化
- #leetcode#220. Contains Duplicate III
- TIPS SCRIPT CONTEXT IN GH PYTHON GHPYTHON的脚本本环境
- 【每日一学】浮动IP
- 微信开发(Java)
- QtCreator控制台中文乱码解决方法
- 神经网络入门三
- 两个List拼接成一个List,怎么拼接?
- 2.•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
- 网页在手机上打开后禁止缩放的办法
- mac安装brew 软件包管理工具Homebrew