Forked from behreajj/bmesh_tut_18_convex_hull_fib_sphere.py
Created
September 8, 2022 14:53
-
-
Save sadernalwis/b860a941dcec3c812681887a79e428c8 to your computer and use it in GitHub Desktop.
Fibonacci Sphere Convex Hull
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bpy import data as D, context as C | |
import bmesh | |
import math | |
def fibonacci_points(count=32, radius=0.5): | |
""" | |
Distributes points on a sphere according to | |
the golden ratio, (1.0 + sqrt(5.0)) / 2.0. | |
""" | |
# Validate inputs. | |
v_count = max(3, count) | |
v_rad = max(0.000001, radius) | |
# Approximately 1.618033988749895 . | |
golden_ratio = (1.0 + 5.0 ** 0.5) * 0.5 | |
tau_gr = math.tau * golden_ratio | |
to_step = 2.0 / v_count | |
i_range = range(0, v_count) | |
vs = [] | |
for i in i_range: | |
azimuth = tau_gr * i | |
inclination = math.asin(1.0 - i * to_step) | |
rho_cos_phi = v_rad * math.cos(inclination) | |
# Convert spherical to Cartesian coordinates. | |
x = rho_cos_phi * math.cos(azimuth) | |
y = rho_cos_phi * math.sin(azimuth) | |
z = v_rad * -math.sin(inclination) | |
v = (x, y, z) | |
vs.append(v) | |
return vs | |
bm = bmesh.new() | |
count = 1024 | |
fib_points = fibonacci_points(count) | |
for point in fib_points: | |
bm.verts.new(point) | |
bmesh.ops.convex_hull(bm, input=bm.verts) | |
mesh_data = D.meshes.new("Sphere") | |
bm.to_mesh(mesh_data) | |
bm.free() | |
mesh_obj = D.objects.new(mesh_data.name, mesh_data) | |
C.collection.objects.link(mesh_obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment