Creating objects slows over time

I am in the process of creating a little addon for myself and I am stuck with making it run faster. I have read that using bpy.ops.mesh.primitive_cube_add slows it down exponentially because it has to update more cubes every time I create one.

So, I changed my script to make copies of a cube. It works much faster for small amounts of cubes, but when I up the number of cubes to about 10,000, I am still seeing an exponential increase in time between updates. (My script prints the % done, and it has gotten slower and slower as it goes through.)

Is there a faster way than running this code to create an object?


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)

With a bpy.context.scene.update() a bit later of course.

My full script can be accessed on GitHub Gist: https://gist.github.com/MrSprite/ff7fda1ec4bf6850d357

Addon Info: My addon is made to convert an image (usually a sprite from an old video game) to a grid of cubes that match the number of pixels of the image with coordinating materials so I can either 3D print them or destroy them with rigid body physics.

If you would like to test it to see what I mean by slow down, run my script (see below) to create the panel in the toolbar. Then open the attached image in Blender. In my Pixel Maker panel, type “test.png” Then click the big button. The console will pop up and show the % completion. I notice that it is much faster than creating a new cube each time, but for even larger images, it slows down even more.

Attachments