For loop takes up to a second per loop

Hi guys,

I have created my first Blender Python script the last couple of days which went far more smoothly than expected considering that I am no coder. The only problem I ran into is performance. (1 second per iteration) As soon as I call Blender functions, loops become very slow. In particular I am instancing two very simple groups in the 3D viewport and distribute them in a grid. One of the groups is scaled. (I basically create a custom 2D amplitude grid.)

Here is the part of the script that slows it down (This function is called by the for loop.):


def createInstance(scalefactor, linewidth, loc):
	bpy.ops.object.group_instance_add(group='box', location=loc, layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
	bpy.ops.transform.resize(value=(scalefactor, scalefactor, scalefactor))

	bpy.ops.object.group_instance_add(group='box_inner', location=loc, layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
	bpy.ops.transform.resize(value=(scalefactor, scalefactor, scalefactor))

	linewidth = round(0.5 + (linewidth*linewidth)*0.5, 1)	
	bpy.ops.transform.resize(value=(linewidth, 1, linewidth))
	return

If I comment the “bpy.ops.object.group_instance_add” calls, it becomes blazingly fast. Interestingly the round() function causes it to significantly slow down even more by an estimated factor of 2-3. That’s the case even tho the rounding function itself is not slow as long as the group_instance_add stuff is deactivated.

Help would be greatly appreciated!

Sebastian

I’m not familiar with what these operators are supposed to do. In general, things on modern computers only slow down when they are working on a lot more data than you think they are. The operators operate on a Context. Is it possible that you have lots of items that are being grouped each time you run this call?

I cannot think of a case where the ‘round’ function would cause the loop to slow down. With out more info it seems the most likely issues are:

  • The “group_instance_add” doing more work than expected. Does this link in object from other .blend files?
  • The ‘resize’ doing more work. How many items are in your groups and how many groups do you have? If this number can grow to the millions then resizing might start to see some slowdowns.

Other clues to what is going on are if each loop in the call takes the same amount of time or if the loop slows down as it is run more often. i.e. Does each loop really take 1 second or did you just do 60 loops and it took 1 minute total? It is possible that the first loop just takes a few milliseconds and the last loop ends up taking 40+ seconds.

suggest use python time module to find out which functions are slow.

suspect its recalculating all object relationships when you add to the scene,

for this code you can avoid using bpy.ops completely, add object, link to scene, set dupligroup can all be done without operators and will cut down scene update time.

Thx, to both of you! I think it was caused by the bpy.ops. Gotta investigate a bit more, but this helped me a great deal!