I am creating an object from vertices and faces. I have written two functions that can convert from spherical coordinates to cartesian and another one for the other way around. I am generating the tetrahedron so that if a bounding sphere touches the vertices the radius is 1. The problem is that the even though the radius is equal to 1 for all four vertices the tetrahedron shape isn't right but then looks correct when the top vertex radius is 3/4. Weird. I am running this with Blender 2.9. What do you suppose I did wrong? Or is there something weird about Blender (which I doubt).
import bpy
import numpy as np
def sphere_to_cart(r,theta, phi):
x = r*np.sin(np.radians(theta))*np.cos(np.radians(phi))
y = r*np.sin(np.radians(theta))*np.sin(np.radians(phi))
z = r*np.cos(np.radians(theta))
return x, y, z
v_tetra = []
# x,y,z = sphere_to_cart(1,0,0)
# v_tetra.append([round(x,7),round(y,7),round(z,7)])
v_tetra.append([0,0,0.75])
x,y,z = sphere_to_cart(1,120,0)
v_tetra.append([round(x,7),round(y,7),round(z,7)])
x,y,z = sphere_to_cart(1,120,120)
v_tetra.append([round(x,7),round(y,7),round(z,7)])
x,y,z = sphere_to_cart(1,120,240)
v_tetra.append([round(x,7),round(y,7),round(z,7)])
f_tetra = [[0,1,2],[0,2,3],[0,3,1],[1,2,3]]
name = "Tetrahedron"
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
col = bpy.data.collections.get("Objects")
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj
mesh.from_pydata(v_tetra, [], f_tetra)
so = bpy.context.active_object
verts = so.data.vertices
print("Vertex info:")
for v in verts:
print(v.co)
To illustrate I have checked the math:
print(v_tetra)
r,theta,phi = cart_to_sphere(0.000000001,0,1)
print(r,theta,phi)
r,theta,phi = cart_to_sphere(0.866,0,-0.5)
print(r,theta,phi)
r,theta,phi = cart_to_sphere(-0.433,0.75,-0.5)
print(r,theta,phi)
r,theta,phi = cart_to_sphere(-0.433,-0.75,-0.5)
print(r,theta,phi)
Which give the result:
[[0.0, 0.0, 1.0], [0.866, 0.0, -0.5], [-0.433, 0.75, -0.5], [-0.433, -0.75, -0.5]]
1.0 1e-09 0.0
0.9999779997579946 -1.0471848490249271 0.0
0.9999944999848749 -1.047194375741009 -1.0472102531819656
0.9999944999848749 -1.047194375741009 1.0472102531819656