Python performance without Blender operators After reading some posts here, i try to avoid using operators in order to avoid useless scene.update slowdown.

Unfortunatly i still have a slowdown when creating multiple objects in blender, here is the 'test code' showing performance hit (seems to be linear and not exponential).

import bpy
from datetime import datetime

iter = 0
startTime = datetime.now()

def duplicate_object(scene, name, copyOb):
    global startTime
    global index

    mesh = bpy.data.meshes.new("tmp")
    ob_new = bpy.data.objects.new(name, mesh)

    ob_new.data = copyOb.data.copy()
    ob_new.data.name = name
    bpy.data.meshes.remove(mesh)
    scene.objects.link(ob_new)

    print(iter, datetime.now()-startTime)
    iter+=1
    startTime = datetime.now()

    return ob_new

for i in range(0,5000):
    duplicate_object(bpy.context.scene, "toto", bpy.data.objects['Cube'])


Not sure why script slowdown with object number, it doesn't seem to check for every object in scene to create a new one.. Is it only blender having bad time handling more and more objects (even before any scene.update()?)

Any help welcome ! :)
I'm not sure, but I think it's mainly Blender having to deal with more and more meshes. In your case an extra slowdown is introduced by first creating a "tmp" mesh.

Here you have an improved version of your code, which is a little bit faster, but still slows down after duplicating more objects.

import bpy
from datetime import datetime


def duplicate_object(scene, name, copyOb, iter):
    startTime = datetime.now()

    mesh = copyOb.data.copy()
    mesh.name = name
    ob_new = bpy.data.objects.new(name, mesh)

    scene.objects.link(ob_new)

    print(iter, datetime.now() - startTime)

    return ob_new

for i in range(5000):
    duplicate_object(bpy.context.scene, "toto", bpy.data.objects['Cube'], i)