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