Creating many group instances, performance drop.

Consider the following scenario:
You have your group consisting of a very simple mesh of around 100 triangles.

Now run the following script

import bpy
import os

for i in range(0, 10000):
    bpy.ops.object.group_instance_add(group='Group')
    #print(str(i))

This goes very quick with 10, 100 and relatively okay with 1000 instances. But the larger the amount of instances you want to create, the slower is the addition of new instances.
I assume this is because for each iteration the call has to go through every single object (group instance or group or whatever) in the scene and check its name to see if it matches the groups name. A quick look at the api didn’t let me find a solution where I can get the group directly as a variable and use it as a non-lookup-needed reference in a call like this.
Unfortunately the way it is now, runtime is horrible as I have to add objects/instances in the tens of thousands. I previously used actual copies where I could just use object references and copy over mesh data etc. which was somewhat fast (at least only a fraction of what it takes now) but eats a ton of RAM.
I’m hoping you guys can help me find a way to make the use of group instances feasible in this situation.

Thanks in advance
Cyba_Mephisto

bpy.ops is quite slow as it deals with scene updates and what not

i would add in empties manually and link them in like this


import bpy


newObjects = []
for i in range(0, 10):
    newEmpty = bpy.data.objects.new('EMPTY', None)
    newEmpty.dupli_type = "GROUP"
    newEmpty.dupli_group = bpy.data.groups['Group']
    newObjects.append(newEmpty)


for ob in newObjects:
    bpy.context.scene.objects.link(ob)

That does speed it up a lot, but in my config (2x15k instances) there is a huge dip somewhere.
With your test script I could get up to 10k and then the time per iteration seems to increase.

I do specifically append all objects in a global list and then in the very end link them to the scene. I don’t collect all instances for one group, then do the linking and then move on to the next group. I link all instances of all groups in the very end.

[ATTACH=CONFIG]459797[/ATTACH]
[ATTACH]459798[/ATTACH]

Somehow it is even worse in my use case which is a relatively elaborate script that is part of a bigger program using blender, but I made this unit test so you could see for yourself.


groupbench2.blend (471 KB)

Somehow it is even worse in my use case which is a relatively elaborate script that is part of a bigger program using blender, but I made this unit test so you could see for yourself.