1
$\begingroup$

This is my first time using Python and I'm totally lost.
Is there a way to create an ellipsoid without using sinus and cosinus and using

x^2/a^2 + y^2/b^2 + z^2/c^2 = 1

instead?

I really need help and would be very thankful if someone could help me.

$\endgroup$
1
  • $\begingroup$ hi, do you have some example? or Can you give more details? $\endgroup$
    – yhoyo
    Commented May 16, 2017 at 21:28

2 Answers 2

2
$\begingroup$

I found the following code to create a ellipsoid by using sin and cos. But i need to define the ellipsoids surface by using the formular: z(x,y) = abs(p/w)*sqrt(w^2 - 4x^2 - 4y^2) while p is the projection in z-direction and p is the diameter from the ellipsoid.

This ist the code I found:

import bpy
import bmesh
from math import degrees, radians, sin, cos, tan
from mathutils import Vector


class CoordsPoints:
    @property
    def xyz(self):
        theta = self.theta
        phi = self.phi
        x = sin(theta) * sin(phi)
        y = cos(theta) * sin(phi)
        z = cos(phi)
        R = self.R
        return R * Vector((x,y,z))

    def __init__(self, R, theta, phi):
        self.R = R
        self.theta = theta
        self.phi = phi
        #self.xyz = self.point(theta, phi)

    def __repr__(self):
        return "Coords(%.4f, %.4f)" % (degrees(self.theta),
                                               degrees(self.phi))
# define the ellipsoid method.
def ellipsoid(a, b, c):
    def ellipsoid(v):
        x = a * (v.x)
        y = b * (v.y)
        z = c * (v.z)
        return Vector((x, y, z))
    return ellipsoid

# make the ellipsoid bmesh
bm = bmesh.new()

# TODO come up with a nicer way to do this.
rings = [[CoordsPoints(1, radians(theta), radians(phi)) 
                 for theta in range (0, 360, 2)]
                 for phi in range(0, 180, 2)]

h = ellipsoid(1.0, 1.0, 1.5)

verts_mesh = [bm.verts.new(h(p.xyz)) for p in rings[0]]
verts_mesh.append(verts_mesh[0])
for ring in range(1, len(rings)):

    verts_mesh_face = [bm.verts.new(h(p.xyz)) for p in rings[ring]]
    verts_mesh_face.append(verts_mesh_face[0])

    faces = [
        bm.faces.new((
            verts_mesh[i], verts_mesh_face[i],
            verts_mesh_face[i+1], verts_mesh[i+1]
        ))
        for i in range(len(verts_mesh) - 1)
    ]
    verts_mesh = verts_mesh_face

# create mesh link it to scene 
mesh = bpy.data.meshes.new("ellipsoid")
bm.to_mesh(mesh)
obj = bpy.data.objects.new("ellipsoid", mesh)
scene = bpy.context.scene
scene.objects.link(obj)
scene.objects.active = obj
obj.select = True
obj.location = scene.cursor_location
$\endgroup$
1
  • $\begingroup$ Are you help me how to هnsert this mesh (ellipsoid) with use three dimensions in menu? $\endgroup$
    – saded
    Commented Aug 16, 2017 at 19:40
0
$\begingroup$

Add a sphere, which is an ellipsoid where a == b == c, then scale it by (a, b, c)

Example with (a, b, c) = (2, 3, 4)

import bpy

context = bpy.context
scene = context.scene
# add a sphere
bpy.ops.mesh.primitive_uv_sphere_add()
sphere = scene.objects.active
# scale to (rx, ry, rz) of ellipsoid
sphere.scale = (2, 3, 4)
# apply scale
bpy.ops.object.transform_apply(scale=True)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .