0
$\begingroup$

I'm trying to create a really simple scene in which a sphere collides with a cube. When I render the scene using (Ctrl + F12) UI the output video is as expected but when I use python script to do so bpy.ops.render.render(animation = True) the sphere is "eaten" by the cube instead of colliding with it. I also played the scene in the UI before using the script for rendering and it worked fine.

Any ideas about what can cause this?

BR, Aviv

The script:

import bpy 
import numpy as np


for o in bpy.data.objects:
    if o.type == 'MESH':
        o.select_set(True)
    else:
        o.select_set(False)

bpy.ops.object.delete()

plane_scale = 8
locations = 2*(plane_scale - 1)*np.random.rand(4) - (plane_scale - 1)

bpy.ops.object.delete(use_global=False, confirm=False)
bpy.ops.mesh.primitive_plane_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.scale[0] = 8
bpy.context.object.scale[1] = 8
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.location[0] = locations[0]
bpy.context.object.location[1] = locations[1]
bpy.context.object.location[2] = 1
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide(number_cuts=4, quadcorner='INNERVERT')
bpy.ops.mesh.bevel(offset=0.137534, offset_pct=0, vertex_only=False)
bpy.ops.object.editmode_toggle()

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.location[1] = locations[2]
bpy.context.object.location[0] = locations[3]
bpy.context.object.location[2] = 1

bpy.data.objects['Camera'].rotation_euler[2] = 0.8254
bpy.data.objects['Camera'].rotation_euler[0] = 0.6799
bpy.data.objects['Camera'].location[0] = 9.69
bpy.data.objects['Camera'].location[1] = -10.85
bpy.data.objects['Camera'].location[2] = 12.3883
bpy.data.cameras[0].lens = 18


passive_list = [item.name for item in bpy.data.objects if (('plane' in item.name.lower()) & (item.type == 'MESH'))]
active_list = [item.name for item in bpy.data.objects if (('plane' not in item.name.lower()) & (item.type == 'MESH'))] 


bpy.ops.object.select_all(action='DESELECT')

for obj in passive_list:
    bpy.data.objects[obj].select_set(True)
    bpy.ops.rigidbody.objects_add(type='PASSIVE')
    bpy.data.objects[obj].rigid_body.use_margin = True
    bpy.data.objects[obj].select_set(False)

for obj in active_list:
    bpy.data.objects[obj].select_set(True)
    bpy.ops.rigidbody.objects_add(type='ACTIVE')
    if obj == 'Sphere':
        bpy.data.objects[obj].rigid_body.enabled = False
    else:
        bpy.data.objects[obj].rigid_body.enabled = True
    bpy.data.objects[obj].rigid_body.use_margin = True
    bpy.data.objects[obj].select_set(False)


bpy.data.objects['Sphere'].rigid_body.kinematic = True
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=0)
bpy.data.objects['Sphere'].location[0] = locations[0]
bpy.data.objects['Sphere'].location[1] = locations[1]
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=40)

bpy.data.scenes[0].frame_start = 0
bpy.data.scenes[0].frame_end = 50

try:
    bpy.data.scenes[0].render.filepath = f'/home/aviv/Desktop/{i}.avi'
except:
    bpy.data.scenes[0].render.filepath = f'/home/aviv/Desktop/'

bpy.data.scenes[0].render.image_settings.file_format = 'AVI_JPEG'

bpy.data.scenes[0].render.engine = 'CYCLES'
bpy.data.scenes[0].cycles.device = 'GPU'
bpy.data.scenes[0].render.use_render_cache = True
bpy.ops.render.render(animation = True)
```
$\endgroup$
10
  • 1
    $\begingroup$ Is your blend file containing any kind of simulation data? If so, try to cache it before rendering. ProTip: Never render to video, render single frames and compile them to a video afterwards. $\endgroup$
    – brockmann
    Sep 4 '19 at 12:10
  • $\begingroup$ Thank you for your response. Can you please guide me about this cache thing? and how to do it using script? $\endgroup$
    – AvivSham
    Sep 4 '19 at 12:14
  • 1
    $\begingroup$ Search for "baking" the ______ (your simulation type) simulation. $\endgroup$
    – Leander
    Sep 4 '19 at 15:17
  • 1
    $\begingroup$ docs.blender.org/api/2.79/… $\endgroup$
    – Leander
    Sep 4 '19 at 15:56
  • 1
    $\begingroup$ Sorry, I have not baked anything through python^^, but I think the last link looks like the right command. $\endgroup$
    – Leander
    Sep 4 '19 at 16:04

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.