はじめに
指定した数に応じて、球状にグリッドを作り、そこに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
解説
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
最後にリストをアウトプットします。