# Why is my script to add large batches of objects so slow? v2.90

I have to create about 30,000 spheres and I notice that even adding a 1000 is already a lot of work for Blender. I've cut down (hopefully?) on work and memory usage by creating just 1 mesh and 1 material for the batch, but I'd like blender to remain responsive during the batch job. Is there a way to update the UI, or an all together more recommended way of adding such a large number of objects? I will need to individually animate them.

# Creates a collection to store all the objects in.
collection = create_collection(scaffold, name)
mat = bpy.data.materials.new(name=name)
mat.diffuse_color = (1, 0, 0, 1)
# Creates an icosphere mesh
for cell in cells:
cell.object = obj = bpy.data.objects.new(name=f"{name} #{cell.id}", object_data=mesh)
obj.location = cell.position
obj.data.materials.append(mat)


Even just being able to add the spheres one by one in a very slow fashion would be OK, right now Blender is unresponsive.

New contributor
Robin De Schepper is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• object is not a keyword, it's a builtin type ;) Even if overwritten you can still access it with import builtins; builtins.object. But you're right, it's bad form of me to let this code dangle around on SE for others to trip over, I'll edit it. – Robin De Schepper Sep 16 at 14:21

This is due to the material.

As all your objects share the same data, adding the material will add many material slots to the same data. This is visible in the material property of the object: give it a try with n = few objects.

You'll see, for each of them (as same data) n material slots added:

So, remove the line:

object.data.materials.append(mat)


If you want this mat, add it previously to the ico mesh data or add it once after the loop:

mesh.materials.append(mat)


30k objects should be around 1mn (depending on your configuration).

• Thanks! If possible could you explain a bit more about these slots in your answer? A link to a resource that explains them is fine too – Robin De Schepper Sep 16 at 9:43
• @RobinDeSchepper, ok... even if don't know what is unclear? – lemon Sep 16 at 9:44
• My understanding right now is that I did not take something called "material slots" in mind and that that led to grinding Blender to a halt. What are they and how is it avoided? – Robin De Schepper Sep 16 at 9:46
• @RobinDeSchepper, does the edit answer your question? – lemon Sep 16 at 9:51
• It seems you wanted to edit the answer... maybe i've rejected it but that was not intentional (as was editing it simultaneously). – lemon Sep 16 at 9:52