LoginSignup
3
3

More than 5 years have passed since last update.

Rhinoceros / Grasshopper / GHPythonで球状グリッドを作ってみる

Posted at

はじめに

指定した数に応じて、球状にグリッドを作り、そこに3Dオブジェクトを配列するスクリプトを書きました。その方法を書き残しておきます。

データ

[インプット]
name: obj, Data Access: Item Access, Type hint: GeometryBase, desc: 配列するオブジェクト
name: num, Data Access: Item Access, Type hint: int, desc: グリッドの数
name: rad, Data Access: Item Access, Type hint: float, desc: 球の半径
[アウトプット]
name: objs, desc: 球上に配列されたオブジェクト
name: pts, dec: 球状に配列されたポイント

import ghpythonlib.components as ghcomp
import Rhino
import math

objList = []
ptList = []
for i in range(num):
    #球状に配列する
    y = i * 2 / num - 1 + (1 / num)
    r = math.sqrt(1 - y * y)
    phi = i * math.pi * (3 - math.sqrt(5));
    x = math.cos(phi) * r;
    z = math.sin(phi) * r;

    #半径に応じてスケール
    x = x * rad;
    y = y * rad;
    z = z * rad;
    position = Rhino.Geometry.Point3d(x, y, z);
    ptList.append(position)

    #オブジェクトを配置
    clone = obj.Duplicate()
    center = clone.GetBoundingBox(True).Center
    dir = Rhino.Geometry.Vector3d(position) - Rhino.Geometry.Vector3d(center)
    dir.Unitize()
    clone = ghcomp.OrientDirection(clone,center,Rhino.Geometry.Vector3d(0,0,1),position,dir)[0]

    objList.append(clone)

objs = objList
pts = ptList

Screen Shot 2015-09-01 at 04.55.21.png
Screen Shot 2015-09-01 at 04.55.47.png

解説

import ghpythonlib.components as ghcomp
import Rhino
import math

objList = []
ptList = []
...

今回つかうライブラリをインポートして、空っぽのリストを2つ作っておく。
 

...

for i in range(num):
    #球状に配列する
    y = i * 2 / num - 1 + (1 / num)
    r = math.sqrt(1 - y * y)
    phi = i * math.pi * (3 - math.sqrt(5));
    x = math.cos(phi) * r;
    z = math.sin(phi) * r;
...

メインはこの部分で、ここで球状のグリッドのベースを作っています。
 

...
    #半径に応じてスケール
    x = x * rad;
    y = y * rad;
    z = z * rad;
    position = Rhino.Geometry.Point3d(x, y, z);
    ptList.append(position)

ベースを半径のサイズに応じてスケールしています。そのあと、ptListに作ったポイントを入れています。
 

...
    #オブジェクトを配置
    clone = obj.Duplicate()
    center = clone.GetBoundingBox(True).Center
    dir = Rhino.Geometry.Vector3d(position) - Rhino.Geometry.Vector3d(center)
    dir.Unitize()
    clone = ghcomp.OrientDirection(clone,center,Rhino.Geometry.Vector3d(0,0,1),position,dir)[0]

    objList.append(clone)
...

ベースのオブジェクトを複製して、先ほど作ったグリッドの点にGHコンポーネントのOrient Directionを使ってオリエントしています。最後にリストに複製したオブジェクトを追加する。
 

...
objs = objList
pts = ptList

最後にリストをアウトプットします。

ダウンロード

SphericalGrid.gh

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3