0

I couldn't find anything online and well... when I bring up my code, which makes a simple sphere in BGE blenders game engine it doesn't run til after I close the game and that's where it creates the mesh. Is there something you're supposed to put into the code for blender to run it outside of the viewport?

here's my code, it runs and does what I want it to... only after I've closed BGE which noone else seems to have run into.

import bpy
for obj in bpy.context.selected_objects:

    bpy.ops.mesh.primitive_uv_sphere_add(location=origin)
    bpy.ops.transform.translate(value=(ObjX,ObjY,ObjZ))
    setMaterial(bpy.context.object, blue)
    bpy.ops.object.shade_smooth()

if __name__ == "__main__":
    run((0,0,0))
batFINGER
  • 84,216
  • 10
  • 108
  • 233
Frederick
  • 159
  • 12

1 Answers1

2

here's my code, it runs and does what I want it to... only after I've closed BGE which noone else seems to have run into.

Your code is Blender code rather than BGE code.

You basically tell Blender what to do. But you want running a BGE game session.

BGE behavior is setup via Logic Bricks (Logic Editor). You can create a custom Controller brick. It refers to a text block (script mode/module mode) or an external python file (module mode).

The code should not refer to Blender (do not import bpy). When you distribute your game Blender will not be available and running such code will result in errors.

More information can be found at Blenderartists.org. E.g. The BGE Guide to Python Coding

Just to let you know: The BGE does not support mesh creation. All objects and meshes must be loaded either at startup or via Python code while running the game. You can make copies of loaded objects as long as they are inactive (reside on an hidden layer).

Monster
  • 8,178
  • 1
  • 11
  • 27