0
$\begingroup$

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))
$\endgroup$
  • $\begingroup$ Those are Blender module operators, not game engine ones. You can't run Blender operators inside game engine, nor create or alter Blender data during a game run. $\endgroup$ – Duarte Farrajota Ramos Aug 15 '17 at 3:06
  • $\begingroup$ well what good is that $\endgroup$ – Frederick Aug 15 '17 at 3:07
  • $\begingroup$ Simple way to paste scripts here. In blender text editor, select all, hit the tab, this makes whole script tab right one, copy and paste here. Shift tab, moves it back. $\endgroup$ – batFINGER Aug 15 '17 at 5:28
  • $\begingroup$ bge add objects ::: script run once $\endgroup$ – Strapicarus Aug 15 '17 at 5:36
2
$\begingroup$

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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.