Game engine doesn't render

I have a simple script running as a controller attached to an “always” sensor. On the first pass through the script, I create two icospheres. These spheres are never visible in the game rendering window when they are created freshly in the script. If they exist at the time I type “p” to start the game, then I can see them in the game window. There seems to be a difference to the renderer between pre-drawn objects in the 3D window and objects created while the game engine is running. What is it?

How do I get the game engine to continuously render my game scene, making these spheres visible from a dynamically-changing camera location? I wonder if, after an object is created, it is necessary to tell the game engine to begin rendering it. The script is indeed being called, which I can see when I include “print()” calls.

Here is the script:

import bpy
from bpy import context
import bge

#layers = [False]*32
#layers[0] = True
if len(bpy.context.selectable_objects) <= 2:[“Camera”].location.x = 0

for x in range(0, NSPHERES):
    print("x:", x)
    bpy.ops.mesh.primitive_ico_sphere_add(location=(2*x, 0, 0))
    obj = bpy.context.object = True
    mods = obj.modifiers
    subsurf = obj.modifiers['Subsurf']
    subsurf.levels = 3
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subsurf")

def main():
camera =[“Camera”]
arrowOffset = 0.3
keyboard = bge.logic.keyboard
if bge.logic.KX_INPUT_ACTIVE ==[]:
camera.location.x += arrowOffset
if bge.logic.KX_INPUT_ACTIVE ==[]:
camera.location.x -= arrowOffset


Thanks in advance for anyone’s help on this,

bpy doesn’t work in game engine, try placing your spheres on the second layer and then using scene.addObject.

Thanks, Katot. I’ll try that.