# programmatically start the game engine?

I have this piece of code that's meant to start the game engine (and it does so if I just run this on the default screen)...I have a script that generates an environment with planes, spheres, etc. and appended this piece of code on the bottom but I'm getting a poll() context error...I'm in 3d view by default and the below thing should make it run in the game engine...any way to fix this so it actually works?

bpy.context.scene.render.engine = 'BLENDER_GAME'
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
override = bpy.context.copy()
override['area'] = area
bpy.ops.view3d.game_start()
break

• can you post the error message that is in the console? – David Sep 17 '15 at 2:47

My guess is that you are in edit mode. The bge will only start in object mode.

Adding this line bpy.ops.object.mode_set(mode='OBJECT') to your script will alleviate that issue.

import bpy

bpy.context.scene.render.engine = 'BLENDER_GAME'

for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
bpy.ops.object.mode_set(mode='OBJECT')
override = bpy.context.copy()
override['area'] = area
bpy.ops.view3d.game_start()
break

• hm, I'm still getting a poll() context error... full error message: RuntimeError: Operator bpy.ops.view3d.game_start.poll() failed, context is incorrect – Archana Ram Sep 18 '15 at 5:58