Time for drawing huge amount of objects.

I recently faced a efficiency bottleneck when drawing with python code. My project demands to draw thousands and may in the future millions of spheres and cubes in one scene. In the beginning the drawing takes just a blink of time, but as the number of objects getting bigger and bigger, the time requires to draw these object increases. From my perception the time increase is not linear.

I need someone to help me on resolving the bottleneck of adding objects.

For now in the beginning of the python script I use the following functions to draw spheres, tubes and cubes.


import bpyimport bmesh
import math
import mathutils


def makeMaterial(name, diffuse, specular, alpha):
	mat = bpy.data.materials.new(name)
	mat.diffuse_color = diffuse
	mat.diffuse_shader = 'LAMBERT'
	mat.diffuse_intensity = 1.0
	mat.specular_color = specular
	mat.specular_shader = 'COOKTORR'
	mat.specular_intensity = 0.5
	mat.alpha = alpha
	mat.ambient = 1
	return mat


def setMaterial(ob, mat):
	me = ob.data
	me.materials.append(mat)


w = 1
def MakePolyLine(objname, curvename, cList):
	curvedata = bpy.data.curves.new(name=curvename, type='CURVE')
	curvedata.dimensions = '3D'
	objectdata = bpy.data.objects.new(objname, curvedata)
	objectdata.location = (0,0,0) #object origin
	bpy.context.scene.objects.link(objectdata)
	polyline = curvedata.splines.new('NURBS')
	polyline.points.add(len(cList)-1)
	for num in range(len(cList)):
		polyline.points[num].co = (cList[num])+(w,)
	polyline.order_u = len(polyline.points)-1
	polyline.use_endpoint_u = True


def MakeCube(objname,cubename):
	bt = bmesh.new()
	bmesh.ops.create_cube(bt, size=1.0, matrix=mathutils.Matrix())
	me = bpy.data.meshes.new(cubename)
	bt.to_mesh(me)
	bt.free()
	obj = bpy.data.objects.new(objname,me)
	bpy.context.scene.objects.link(obj)
	bpy.context.scene.objects.active = obj
	obj.select = True



This seems unnecessary:

bt = bmesh.new()
bmesh.ops.create_cube(bt, size=1.0, matrix=mathutils.Matrix())
me = bpy.data.meshes.new(cubename)
bt.to_mesh(me)

The bmesh module is rather slow, you should use the standard API operator for that, then .copy() the object.
Make sure you don’t use operators in loops and do not call scene.update() much. See:

Q: Python performance with Blender operators

Thanks for the information.

Does that mean I have to implement the methodology for creating cube mesh myself?

I have just tried the standard API (I suppose I am going in a correct direction)…

From which I guess I should use:


bpy.ops.object.add('MESH')

but the python console give me this back


>>> bpy.ops.object.add('MESH', location=(0.0,0.0,0.0), rotation=(60,30,20))Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.70\scripts\modules\bpy\ops.py", line 186, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
TypeError: Calling operator "bpy.ops.object.add" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')

I think I got it!! Thanks!