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))


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?

  • $\begingroup$ 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. $\endgroup$ Commented Apr 17, 2020 at 23:09
  • $\begingroup$ 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 10*10*10 matrix and I am planning to create matrixes 100*1000*20 which seems almost impossible in run time terms. $\endgroup$
    – Fateh A.
    Commented Apr 18, 2020 at 1:01
  • $\begingroup$ 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 ? $\endgroup$
    – Gorgious
    Commented Apr 18, 2020 at 12:47
  • $\begingroup$ Yes, I am actually going to join them later in my code. $\endgroup$
    – Fateh A.
    Commented Apr 18, 2020 at 13:19


You must log in to answer this question.

Browse other questions tagged .