0

I am creating a program in blender that makes cubes based on the values of an array. For example, when one of the values is one it creates a cube at a specific point and then it goes on to print another cube until you have "printed" a numpy array. Here is my code:

import bpy
import numpy as np

def print_array(a, size=1):
    count = 0
    x = 0
    for i in a:
        y = 0
        for j in i:
            z = 0
            for k in j:
                if k == 1:
                    bpy.ops.mesh.primitive_cube_add(size=size, location=(x, y, z))
                    count += 1
                z += size
            y += size
        x += size
    return count

a = np.ones((3, 3, 3))

print_array(a)

While this does work for smaller numpy arrays blender crashes when I try to "print" larger arrays. Is there a less tasking way to do this without making the arrays smaller?

Fateh A.
  • 155
  • 7
  • How many cubes are you planning on creating? Do they all have to be individual objects? If they don't have to be individual objects, this can be solved easily with (among others) instancing/duplis, bmesh.ops.create_cube, or perhaps mesh.from_pydata. Each method will have pros and cons that can be weighed against your specific objective. I'm not experienced enough to comment on blender crashing as a function of object numbers. – Jeremy Hilgar Apr 17 '20 at 23:09
  • It seems that blender doesn't actually crash but takes an extremely long time to compute. It seems to take a long time to create a 101010 matrix and I am planning to create matrixes 100100020 which seems almost impossible in run time terms. – Fateh A. Apr 18 '20 at 01:01
  • See this answer for a faster method of creating arrays. Note that bpy.ops are highly inefficient, they are generally meant to be called in the UI and not hundred of thousands of times in an operation. Also in python I recommend using for i in range(start, end, step): instead of what you did. If you don't want your computer to stall you must not separate all the objects (ie comment out the last line). Can you achieve your goal with one single object but a multitude of meshes ? – Gorgious Apr 18 '20 at 12:47
  • Yes, I am actually going to join them later in my code. – Fateh A. Apr 18 '20 at 13:19

0 Answers0