1
$\begingroup$

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
mesh = _create_ico_mesh(scaffold, name, radius)
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)
    collection.objects.link(obj)

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.
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Robin De Schepper Sep 16 at 14:21
2
$\begingroup$

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:

enter image description here

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Robin De Schepper Sep 16 at 9:43
  • 1
    $\begingroup$ @RobinDeSchepper, ok... even if don't know what is unclear? $\endgroup$ – lemon Sep 16 at 9:44
  • $\begingroup$ 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? $\endgroup$ – Robin De Schepper Sep 16 at 9:46
  • $\begingroup$ @RobinDeSchepper, does the edit answer your question? $\endgroup$ – lemon Sep 16 at 9:51
  • $\begingroup$ It seems you wanted to edit the answer... maybe i've rejected it but that was not intentional (as was editing it simultaneously). $\endgroup$ – lemon Sep 16 at 9:52

Your Answer

Robin De Schepper is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.