34
Rhino &Grasshopper 入门手册 刘中远 2017/11/1

Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

Rhino &Grasshopper 入门手册 刘中远 2017/11/1

Page 2: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

•Grasshopper学习目标:

• 1.掌握gh操作与底层的数据结构、电池/程序的执行优先级。使用gh出视频动画。

• 2.C#路线:初步掌握C#的语法(参考C++),参考文档,在gh内利用C#调用任意Rhino或gh函数。Python路线:习惯使用python的同学也可以使用python。

• 3.使用gh内置的C#、python电池调用NativeC++DLL。

• 4.了解托管与非托管代码。最后可以利用gh调用任意以前自己写好的库或其它开源算法。

第二章Grasshopper

Page 3: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

• 2.1 什么是Grasshopper? • For designers who are exploring new shapes using generative algorithms,

Grasshopper® is a graphical algorithm editor tightly integrated with Rhino’s 3-D modeling tools. Unlike RhinoScript, Grasshopper requires no knowledge of programming or scripting, but still allows designers to build form generators from the simple to the awe-inspiring.

• 官网http://www.grasshopper3d.com/

类似节点编程软件: Cycling 74 Max (互动音乐制作平台),unreal的blueprint(游戏开发平台),houdini (特效制作软件),3dmax与maya的材质编辑器,Revit中Dynamo(建筑信息化模型平台)等。

第二章Grasshopper

Page 4: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

Grasshopper出图案例 四面体剖分着色显示(gh演示)

Page 5: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系
Page 6: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系
Page 7: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系
Page 8: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

Grasshopper优化案例 Karamba插件(有限元优化)

Page 9: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

GH面板大体上分3个区域, 文件夹选项 默认电池拖拽区域 gh存放、操作电池的gh画布,详见gh1.jpg

2.2GH面板介绍

Page 10: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

Always:(始终) RMB+Drag = Pan left/right/up/down 上下左右移动窗口 RMB+Ctrl+Drag = Zoom in/out 缩放窗口 Scroll Wheel = Zoom in/out (注:缩放中心与鼠标位置有关,借此可快速远距离移动视窗及电池。) MMB+Click = Wheel menu 中键菜单 Spacebar = Wheel menu 中键菜单 LMB+Alt+Drag = Split objects (aka 'The Moses Tool')在当前位置横向增加画布面积 ------------------------------------------------------------------- While over an empty canvas spot: (当鼠标在空白画布上) LMB+Drag = selection rectangle 矩形选择 LMB+Shift+Drag = addition selection rectangle 矩形选择加选 LMB+Ctrl+Drag = removal selection rectangle 矩形选择减选 RMB+Click = Canvas menu 画布菜单(功能同中键菜单)

2.3 gh电池操作

Page 11: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

While over an object:(当在一个电池上) LMB+Drag = Drag selected objects 拖动选择的电池 LMB+Shift+Drag = Drag selected objects along 45-degree guides 垂直移动电池 LMB+Drag+Alt = Copy all dragging objects 复制拖动物体(注:先拖动再按Alt) LMB+Click = Either select one object or do nothing if object is already selected 选择 LMB+Shift+Click = Add an object to the selection 加选 LMB+Ctrl+Click = Remove an object from the selection 减选 LMB+Ctrl+Shift+Click = Toggle the selection state 切换选择状态 LMB+Ctrl+Alt = Info mode 显示电池在菜单的位置 LMB+Ctrl+Shift+Alt = Save info mode as image 保存位置信息图片 RMB+Click = Object context menu 电池详细菜单

2.3 gh电池操作

Page 12: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

While over an input/output grip: (当鼠标在一个输入/输出端上) LMB+Drag = Create a new wire 创建一根新的线,替换原来的所有连线 LMB+Shift+Drag = Create a new wire without erasing old wires链接一根新的线,不影响已经链接上的 LMB+Ctrl+Drag = Erase an existing wire by tracing over it擦掉已经链接的线 LMB+Ctrl+Shift+Drag = Move all existing wires to some other grip 把所有链接的线移到其它电池上,不会影响其它电池已有的连线 LMB+Drag+RMB = Create a new wire without exiting the wire tool连续创建新的链接状态 替换 LMB+Drag+RMB+Shift = Add a wire without exiting the wire tool连续创建新的链接状态 增加 LMB+Drag+RMB+Control = Remove a wire without exiting the wire tool连续创建新的链接状态 删除 While over a component icon on the toolpanels:(当电池在工具面板里) LMB+Shift+Click = Create a component aggregate 一次抓取多个电池

2.3 gh电池操作

Page 13: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

GH电池相当于一个函数,函数输入-计算-输出的过程是实时的 左侧是输入端,右侧是输出端,不使用插件的情况下,输出后的结果不可以再连回输入。 因此不支持类似for(){}或者do{}while();的循环计算,但可以通过数据结构处理完成类似需求。

2.4 gh运行原理

Page 14: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系 比如使用.NET Reflector 9.0对其反编译的结果(下一页): 其中Grasshopper.Kernel.GH_Component类是所有电池的父类,它定义了所有电池的接口(输入输出的类型、个数)、gh在画布上的显示的名称以及图标等 推荐免费的反编译软件:https://github.com/0xd4d/dnSpy ,中文版详见课程资料

2.5 电池反编译

Page 15: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

所有具体电池的实现都在一些特殊的dll里,这些dll改了名字叫.gha .gha本身也是dll,比如找到我们刚才创建NURBS曲线的例子中使用的电池所在的gha(dll) C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper (b45a29b1-4343-4035-989e-044e8580d9cf)\0.9.76.0\Components

2.5 电池反编译

Page 16: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

对Curve.gha进行反编译,找到Component_NurbsCurve类, 这个类就是这个Nurbs Curve电池,反编译可以看见内部的函数实现

2.5 电池反编译

Page 17: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

注意其函数实现中有这样一句话: NurbsCurve data = NurbsCurve.Create(false, degree, list); 这个 NurbsCurve.Create();就是Rhino自身的函数,gh只是调用了它,并加了一些接口与类型自动转换的函数,并打包成了一个电池, 其中,false就是是否闭合,degree就是曲线的阶数,list就是控制点的List 而这个函数的实现在Rhinocommon中可以查到:

2.6 函数调用关系

Page 18: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

NurbsCurve.Create(){} 包括一些例子

2.6 函数调用关系

Page 19: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

NurbsCurve.Create()的实现可以在Github上查到 https://github.com/mcneel/rhinocommon 官网链接 http://developer.rhino3d.com/guides/rhinocommon/what-is-rhinocommon/

2.6 函数调用关系

Page 20: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

NurbsCurve.Create()的实现可以在Github上查到 https://github.com/mcneel/rhinocommon 的第137行 官网链接 http://developer.rhino3d.com/guides/rhinocommon/what-is-rhinocommon/

2.6 函数调用关系

Page 21: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

NurbsCurve.Create()

2.6 函数调用关系

Page 22: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

Create()函数内调用了一个UnsafeNativeMethods.cs的类,而在这个类内部,引入了一些外部的C/C++函数 ON_NurbsCurve_CreateClampedUniformNurbs https://github.com/mcneel/rhinocommon/blob/master/dotnet/UnsafeNativeMethods.cs

2.6 函数调用关系

Page 23: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

https://github.com/mcneel/rhinocommon/blob/master/c/on_nurbscurve.cpp

2.6 函数调用关系

CreateClampedUniformNurbs

Page 24: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

crv->CreateClampedUniformNurbs 这个在Opennurbs5 商业版中可以查看 https://www.rhino3d.com/download/opennurbs/5.0/release

2.6 函数调用关系

Page 25: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

crv->CreateClampedUniformNurbs

2.6 函数调用关系

Page 26: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

crv->Create

2.6 函数调用关系

Page 27: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.7 C#电池编程与引用 • 用vs打开MinimalSurfaceComponentExample • 可以看见在gh内使用Plankon与mathnet矩阵库实现的极

小曲面案例 • 注意引用的库与自定义的gha文件的存放位置,存放位置

查看gh菜单说明.jpg

Page 28: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.8 C#电池Pinvoke调用NativeC++

• 1.查看工程案例Pinvoketest2 练习c#与C++间传递结构体指针。

• 2.查看工程MiniMeshdll参考对Mesh的转换与传递。C++内生开辟的内存空间可在dll中做一个释放内存的函数接口,gh电池运行完之前调用进行释放,可避免内存泄漏。

• 提示:通过Pinvoke调用c++dll,可以把任意外部C++库在rhino内使用,亲测可用:Eigen、MKL、OpenMesh、Libigl、CGAL、Qt等,注意调用时要配好特点依赖的dll到Rhino环境变量中,让Rhino能找到,懒的话可以粗暴的扔进system32内。。。

Page 29: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.9.1 IronPython2.7.8.0

• 1:IronPython2.7.8.0(gh自带) • 特点:rhino官方维护,支持rhino、grasshopper object与gh数据结构。

不支持外部python库,如tensorflow、numpy等。帮助: • https://developer.rhino3d.com/guides/rhinopython/ • https://developer.rhino3d.com/guides/rhinopython/your-first-

python-script-in-grasshopper/

Page 30: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.9.1 IronPython2.7.8.0 • 虽然IronPython不支持外部库,但其可用大部分rhino、gh函数,并且支持gh的数据

结构,且响应迅速,可以使用python通信机制与外部python、C++程序交互(甚至利用TCP通信与其他电脑传输数据)。

• 下图为IronPython与另一台服务器交互数据,在服务器上进行强化学习训练拟合曲线

Page 31: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.9.2 Gh_Cpython • 下载地址: https://github.com/MahmoudAbdelRahman/GH_CPython • 特点:本质上使用的是外部Python解释器,电池本身只是一个txt编辑器+数据传输接

口,会把内部代码存成.py文件并调用配置好的外部python.exe运行,并把接口传入的数据送给外部python.exe,并且接收回返回数据。所以很难使用rhino对象,需要把rhino对象转成pythonlist([])等传入处理,并且相应速度较慢。但可以使用任意外部python库,如tensorflow、numpy,与任意pytho版本,2.7、2.8、3.6、3.7等。详细看github.com源代码。实际上速度慢,需要跑完全程,不建议使用。

• 下图直接在gh中使用Gh_Cpython调用tensorflow与numpy训练神经网络拟合点云。

Page 32: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

2.10 cmd.exe • 可以使用C#电池直接在rhino中调用cmd.exe,运行任何exe!代码如下:

• private void RunScript(string exefullpath, string argv1, string argv2, ref object A) • { • string commond, output; • commond = exefullpath + " " + argv1 + " " + argv1 + " " + argv2; • RunCmd.run(commond, out output); • A = output; • }

• // <Custom additional code> • public class RunCmd • { • public RunCmd() • {

• } • private static string CmdPath = @"C:\Windows\System32\cmd.exe";

• /// <summary> • /// 执行cmd命令 • /// 多命令请使用批处理命令连接符: • /// <![CDATA[ • /// &:同时执行两个命令 • /// |:将上一个命令的输出,作为下一个命令的输入 • /// &&:当&&前的命令成功时,才执行&&后的命令 • /// ||:当||前的命令失败时,才执行||后的命令]]> • /// 其他请百度 • /// </summary> • /// <param name="cmd"></param> • /// <param name="output"></param> • public static void run(string cmd, out string output) • { • cmd = cmd.Trim().TrimEnd('&') + " &exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态 • using (Process p = new Process()) • { • p.StartInfo.FileName = CmdPath; • p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 • p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 • p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 • p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 • p.StartInfo.CreateNoWindow = true; //不显示程序窗口 • p.Start();//启动程序

• //向cmd窗口写入命令 • p.StandardInput.WriteLine(cmd); • p.StandardInput.AutoFlush = true; • p.StandardInput.WriteLine("exit"); • //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死 • //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令

• //获取cmd窗口的输出信息

• output = p.StandardOutput.ReadToEnd();

• //p.WaitForExit();//等待程序执行完退出进程 • p.Close(); • } • } • } • // </Custom additional code> • }

Page 33: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

3.1 查看gh关联模型案例、动画案例等练习

• 打开2.gh文件 • 尝试理解电池逻辑并复制出一样的电池实现,之后看set

综合.gh

» » set综合.gh

Page 34: Rhino &Grasshopper 入门手册 - USTCstaff.ustc.edu.cn/~tongwh/CG_2019/slides/Grasshopper...对grasshopper.dll进行反编译,可以看到所有电池对Rhino命令的调用关系

练习:基于C++的Grasshopper插件制作

• 1. 要求: • 使用vs的gh模板制作自己的电池插件,并调用C++dll,