5
$\begingroup$

I know that this has been asked before, but the answers that others have received did not help me much. When creating objects through Python, using bpy.ops.mesh.primitive_cube_add() there is an exponential slowdown when looping the creation of objects. My script that I am writing takes an image and creates a grid of cubes based on every pixel. It also assigns a material to each cube based on each pixel's color. When running it on a 32x32 image, it is very fast (about 1 second). When running it on a 100x100 image, the script gets slower and slower as it runs. I also have learned that using a low-level method to create my cubes I won't experience the slowdown. I changed my code to a low-level method, and the speeds are much faster, but I still experience the slowdown.

copy = ob.copy()
copy.data = ob.data.copy()
copy.data.materials.append(material)
copy.location.x = x - 1
copy.location.y = y - 1
copy.location.z = 1
bpy.context.scene.objects.link(copy)

Here is my current code for creating cubes. My full addon can be accessed here: https://gist.github.com/MrSprite/ff7fda1ec4bf6850d357

Is there a faster/better way of doing this? Because I would really like to be able to create 100,000+ cubes without waiting hours.

Thanks.

$\endgroup$
5
  • $\begingroup$ Do you need all 100k cubes as separate objects or would a single mesh in one object also work? $\endgroup$
    – stacker
    Commented Aug 8, 2014 at 6:19
  • $\begingroup$ @stacker Both. If I use it to create a rigid body simulation from an image, I want it seperate. If I use it to 3D print, I want it a single mesh. I have an option in my addon panel to choose between the two. $\endgroup$
    – doomslug
    Commented Aug 8, 2014 at 13:55
  • $\begingroup$ @CoDEmanX The Python performance with Blender operators solution still gives me slowdown if I create a larger amount of objects. Is this unavoidable? $\endgroup$
    – doomslug
    Commented Aug 13, 2014 at 16:34
  • $\begingroup$ Of course Blender slows down the more objects you have. But the time it takes should grow linearly with the number of objects, not quadratic. $\endgroup$
    – CodeManX
    Commented Aug 13, 2014 at 17:13
  • $\begingroup$ @CoDEmanX So my script will always get progressively slower as it runs, no matter what I do? $\endgroup$
    – doomslug
    Commented Aug 13, 2014 at 23:38

2 Answers 2

6
$\begingroup$

Some quick tests show that blender is designed to work with objects that have high vertex counts but not really with a large number of objects. Even with 3000 objects in a scene it takes a few seconds to select all objects, yet selecting the 33,000 vertices of those objects all joined together is almost instant.

I tested four ways to create multiple cubes.

  1. Create 1000 cube objects - 8 seconds
  2. Create a single mesh containing 1000 disconnected cube meshes - 0.5 seconds
  3. As 2 then separate by loose parts to get 1000 cube objects - 5 seconds
  4. Create a cube and a grid with 1000 verts, enable dupliverts and make real - 0.6 seconds

I am thinking your best approach would be to create a mesh that is a single plane with a vertex where you want each cube to be located, enable dupliverts, then if you want them separate for rigid body you can make the dupliverts real.

For info the script used to test these times is here.

$\endgroup$
4
  • $\begingroup$ I guess I wasn't very clear. When I run my script, it prints every % of the way done. Between each % increase, there is a slowdown. My computer is fine with even 10,000 cubes created. I wasn't speaking of slowdown in Blender. I will try that Dupliverts method though. $\endgroup$
    – doomslug
    Commented Aug 10, 2014 at 0:01
  • $\begingroup$ I am only looking at overall time not the time to create each object, as the object numbers increase it will take longer to add each object, so adding 100,000 takes longer than the time to add 1,000 multiplied by 100. The small overall times above just demonstrate that one way is much quicker than another. $\endgroup$
    – sambler
    Commented Aug 10, 2014 at 5:33
  • $\begingroup$ Do do you have enough RAM or is your system starting to cache things into disk? That would slow the system way down... $\endgroup$
    – user1853
    Commented Aug 10, 2014 at 6:48
  • $\begingroup$ I have 32 gigs of RAM. $\endgroup$
    – doomslug
    Commented Aug 10, 2014 at 13:51
1
$\begingroup$

Have you tried the DupliVerts method?

I'm not familiar with materials and textures, but you can, for example use the input image as a texture on the base cube and check the From Dupli in the Mapping Options (See here for an example, unfortunately in German, but not too complicated to follow without explanation).

$\endgroup$
1
  • $\begingroup$ Dupliverts won't work for my purposes. I have included better info in my question though. $\endgroup$
    – doomslug
    Commented Aug 10, 2014 at 3:52

Not the answer you're looking for? Browse other questions tagged .