0
$\begingroup$

I'm using a script to generate and render a huge amount of scenes. The script is running just fine, but it seems to get slower and slower for each scene its rendering. (talking about the 5000th scene takes about twice as long as the first one) It feels like although I'm always deleting all the objects, something is stacking up...

for i in range(start, imcount+start):
 #delete all objects
 bpy.ops.object.select_all(action='SELECT')
 bpy.ops.object.delete(use_global=True)

 #backgroundcolor
 bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = (1, 1, 1, 1)

 #add floor
 bpy.ops.mesh.primitive_plane_add(size=10000, location=(0, 0, 0))

 #floor color
 r = random.uniform(0.5, 1)
 g = random.uniform(0.5, 1)
 b = random.uniform(0.5, 1)
 activeObject = bpy.context.active_object
 mat = bpy.data.materials.new(name="FloorMaterial")
 activeObject.data.materials.append(mat) 
 bpy.context.object.active_material.diffuse_color = (r, g, b, 1)

 #add light pointing on 0, 0, 0 from random direction
 a = random.uniform(0, 3.14/2)
 b = random.uniform(0, 3.14/2)
 r = -20*math.sin(a)
 x = -r*math.sin(b)
 y = r*math.cos(b)
 z = 20*math.cos(a) 
 bpy.ops.object.light_add(type='SUN', location=(x, y, z), rotation=(a, 0, b), scale=(1, 1, 1))
 bpy.context.object.data.energy = 10

 #add 5 shapes
 for j in range(5):
    
    #position
    x = random.randint(-3, 3)
    y = random.randint(-3, 3)
    z = random.randint(0, 6)
    
    #size
    s1 = random.uniform(1, 3)
    s2 = random.uniform(1, 3)
    
    #type
    type = random.randint(0, 3)        
    if type == 0: #cube
        bpy.ops.mesh.primitive_cube_add(size=s1, location=(x, y, z), scale=(1, 1, 1))
    elif type == 1: #cylinder
        bpy.ops.mesh.primitive_cylinder_add(radius=s1, depth=s2, location=(x, y, z), scale=(1, 1, 1))
    elif type == 2: #cone
        bpy.ops.mesh.primitive_cone_add(radius1=s1, radius2=s2, depth=2, location=(x, y, z), scale=(1, 1, 1))
    elif type == 3: #sphere
        bpy.ops.mesh.primitive_uv_sphere_add(radius=s1, location=(x, y, z), scale=(1, 1, 1))
    
    #color
    r = random.uniform(0, 0.3)
    g = random.uniform(0, 0.3)
    b = random.uniform(0, 0.3)
    activeObject = bpy.context.active_object
    mat = bpy.data.materials.new(name="Material"+str(j))
    activeObject.data.materials.append(mat) 
    bpy.context.object.active_material.diffuse_color = (r, g, b, 1)
  
 #add camera pointing on 0, 0, 3 from random direction
 a = random.uniform(1, 3.14/2)
 b = random.uniform(0, 3.14/2)
 r = -20*math.sin(a)
 x = -r*math.sin(b)
 y = r*math.cos(b)
 z = 20*math.cos(a)+3 
 bpy.ops.object.camera_add(location=(x, y, z), rotation=(a, 0, b), scale=(1, 1, 1))
 bpy.context.scene.camera = bpy.context.object
 mat1 =  bpy.context.scene.camera.matrix_world
  
 #render and save image 1
 inpath = os.path.join(impath, "img_{:010d}_in".format(i) + ".png")
 bpy.context.scene.render.filepath = inpath
 bpy.ops.render.render(write_still = True)

 #add camera2 pointing on 0, 0, 3 from random direction#
 adiff = random.uniform(-0.2, 0.2)
 bdiff = random.uniform(-1, 1)
 a = max(a + adiff, 0)
 b = b + bdiff
 r = -20*math.sin(a)
 x = -r*math.sin(b)
 y = r*math.cos(b)
 z = 20*math.cos(a)+3
 bpy.ops.object.camera_add(location=(x, y, z), rotation=(a, 0, b), scale=(1, 1, 1))
 bpy.context.scene.camera = bpy.context.object    
 mat2 =  bpy.context.scene.camera.matrix_world
  
 #render and save image 2
 outpath = os.path.join(impath, "img_{:010d}_out".format(i) + ".png")
 bpy.context.scene.render.filepath = outpath
 bpy.ops.render.render(write_still = True)
 
 #calculate tranformation matrix
 # mat = inv(mat1) * mat2
 invmat1 = np.linalg.inv(mat1)
 mat = np.dot(invmat1, mat2)
 textfile = os.path.join(textpath, "img_{:010d}".format(i) + ".txt")
 with open(textfile,'w+') as f:
    for line in mat:
        np.savetxt(f, line, fmt='%.2f')

I have no clue why this is happening... Any ideas?? Is there something wrong about the way I'm deleting the objects? Thank you!

schdief

$\endgroup$
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.