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

NSPHERES = 2
#layers = [False]*32
#layers[0] = True
#print(len(bpy.context.selectable_objects))
#sys.stdout.flush()
if len(bpy.context.selectable_objects) <= 2:
bpy.data.objects[“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
    obj.data.use_auto_smooth = True
    mods = obj.modifiers
    bpy.ops.object.modifier_add(type='SUBSURF')
    subsurf = obj.modifiers['Subsurf']
    subsurf.levels = 3
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subsurf")
    bpy.ops.object.shade_smooth()
    bge.render.enableVisibility(True)

def main():
camera = bpy.data.objects[“Camera”]
arrowOffset = 0.3
keyboard = bge.logic.keyboard
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
camera.location.x += arrowOffset
print(camera.location.x)
sys.stdout.flush()
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
camera.location.x -= arrowOffset
print(camera.location.x)
sys.stdout.flush()

main()

Thanks in advance for anyone’s help on this,
lbredehoft

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.