0

Currently my code returns no errors, and generates a weird looking set of points, totally not a Mandelbulb. I've looked over the formulas multiple times and everything seems right, but I could definitely be overlooking something. Any ideas? Just to note, I'm quite inexperienced with Python (I work in Java a lot though so I get the main ideas). Here's my code:

import bpy
import numpy as np
import math

def mandelbulb(x, y, z, iterations):
    c = x + y*1j + z*1j
    z = c
    r = 0
    for i in range(iterations):
        r2 = x*x + y*y + z*z
        if r2 > 2:
            return math.sqrt(r2)
        theta = math.atan2(math.sqrt(x*x + y*y), z)
        phi = math.atan2(y, x)
        r = math.sqrt(abs(x*x + y*y + z*z))
        x = r*r*r*r * math.cos(4*theta) * math.cos(4*phi) + c.real
        y = r*r*r*r * math.cos(4*theta) * math.sin(4*phi) + c.imag
        z = r*r*r*r * math.sin(4*theta)
    return 0

def generate_mesh(size, iterations):
    vertices = []
    for x in np.linspace(-2, 2, size):
        for y in np.linspace(-2, 2, size):
            for z in np.linspace(-2, 2, size):
                value = mandelbulb(x, y, z, iterations)
                if value >= 2:
                    vertices.append((x, y, z))
    return vertices, []

def create_mesh_object(vertices, faces, name):
    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(vertices, [], faces)
    mesh.update()
    object = bpy.data.objects.new(name, mesh)
    bpy.context.collection.objects.link(object)

def execute(size, iterations):
    vertices, faces = generate_mesh(size, iterations)
    create_mesh_object(vertices, faces, "Mandelbulb")

class MandelbulbOperator(bpy.types.Operator):
    bl_idname = "object.mandelbulb_operator"
    bl_label = "Mandelbulb Operator"
    bl_options = {'REGISTER', 'UNDO'}

    size: bpy.props.IntProperty(
        name="Size",
        default=32,
        min=1,
        max=256,
        step=1
    )
    iterations: bpy.props.IntProperty(
        name="Iterations",
        default=64,
        min=1,
        max=512,
        step=1
    )

    def execute(self, context):
        execute(self.size, self.iterations)
        return {'FINISHED'}

    def draw(self, context):
        layout = self.layout
        layout.label(text="Create a 3D Mandelbulb")
        layout.prop(self, "size")
        layout.prop(self, "iterations")
        
def register():
    bpy.utils.register_class(MandelbulbOperator)

def unregister():
    bpy.utils.unregister_class(MandelbulbOperator)

if __name__ == "__main__":
    register()

I tried messing with values, such as size and iterations but nothing seemed to change the look of the result, and changing the iterations straight up did nothing. I've also tried using variations on the main Mandelbulb formula but to no avail. Any suggestions are welcome.

MinerDy
  • 1
  • 1

0 Answers0