0

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)
```
AvivSham
  • 185
  • 2
  • 13
  • 1
    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. – brockmann Sep 04 '19 at 12:10
  • Thank you for your response. Can you please guide me about this cache thing? and how to do it using script? – AvivSham Sep 04 '19 at 12:14
  • 1
    Search for "baking" the ______ (your simulation type) simulation. – Leander Sep 04 '19 at 15:17
  • @Leander Can you please have a quick look at my script (the last part) and be more specific about the "baking"? – AvivSham Sep 04 '19 at 15:47
  • https://blender.stackexchange.com/questions/26401/how-to-bake-rigid-body-physics-frame-in-blender-render – Leander Sep 04 '19 at 15:55
  • 1
    https://docs.blender.org/api/2.79/bpy.ops.rigidbody.html?highlight=bake%20rigid#bpy.ops.rigidbody.bake_to_keyframes – Leander Sep 04 '19 at 15:56
  • @Leander what is the line you run to do it? (bake it with python) – AvivSham Sep 04 '19 at 16:02
  • 1
    Sorry, I have *not* baked anything through python^^, but I think the last link looks like the right command. – Leander Sep 04 '19 at 16:04
  • @Leander Thank you so much it worked! maybe you will be able to help me with this question as well? – AvivSham Sep 04 '19 at 16:55
  • Glad it helped. If you found a solution, could you add a clear, concise and easy-to-understand answer to this question, please? – Leander Sep 04 '19 at 17:09

0 Answers0