2
$\begingroup$

I am trying to set up a scene using Python in BGE, there may be a simple way to do it. It is easier for me to explain the problem I have with a very simple Blender session:

Start with a new Blender session, delete the default cube. Add a plane as a floor, and attach an Always sensor to it, and connect the sensor to a Python controller running the following script (3 lines only)

import bpy

bpy.ops.mesh.primitive_cube_add(location=(0, 0, 5))
bpy.context.selected_objects[0].game.physics_type = 'RIGID_BODY'

When I press P to run in the Blender Game mode, I can't see the cube created. When I press Esc, I can see a new cube was created. When I press P again, I can see the original cube fall down and rest on the floor. When I press Esc again, I can see the original cube goes back to the original position, and another new cube was created at that same spot.

It is easy to modify the code so only one cube is created even if I run the script multiple times. I can also solve the problem easily as follows: Forget the sensor/controller, just run the script to setup the cube manually before BGE, and then play the game to see the cube falling onto the floor. But I don't like to have to run extra code to setup the scene.

What I have here is a very simple scene. My question is how to, in general, use Python to setup a scene at the start up of BGE?

Based on what I see, it seems that BGE execute the Python code at exit (when I press ESC to quit). How can I make sure the code is executed at the beginning (when I press P).

$\endgroup$
  • $\begingroup$ Had you done any research at all, you would already know this. $\endgroup$ – X-27 Apr 19 '15 at 17:42
  • $\begingroup$ As I said at the top of my post, it may be very simple to do it, But for some reasons, I don't know how to do it. I did some search and couldn't find what I want. $\endgroup$ – ShungChing Apr 19 '15 at 18:38
1
$\begingroup$

ShungChing!

This problem is due to the fact that you are using the wrong library. When you use Blender Game Engine, you have to use bge library instead of bpy. Try to do the next things: 1) Add an empty in the position you wan to add cube;

2) Add the cube to another layer;

3) To Empty object attach the always sensor --> python controller --> EditObject actuator(make the actuator's name is 'AddCube')

4) The script:

import bge
from bge import logic, types

cont = logic.getCurrentController()
act = cont.actuators["AddCube"]
act.instantAddObject()

Answer files

$\endgroup$
1
$\begingroup$

So inside the BGE, you can only use the bge module, but from the way I read your question, it sounds like you want to execute bpy code before entering the BGE.


My suggestion for this is to use a Makefile and to have the bpy as a separate script. You can then set up rules such as:

build_scene: setup_scene.py
    blender -b path/to/file.blend -P setup_scene.py

play_game: build_scene
    blenderplayer path/to/file.blend

You can now type "make play_game" and it will run setup_scene.py inside your blend file, and then open the game fullscreen. I have used a similar setup in a few similar situations in the past, where the blend can be generated easily. I suggest this solution because it clearly separates making the scene from playing the game. In BGE, these are disparate, and you cannot generate completely new objects in-game (but you can add them from inactive layers as per Max Titkov's answer).

Explanation of the makefile:

build_scene rule
The -b tells blender to run in the background without opening a GUI, and to close when done. The -P flag tells it to run the setup_scene.py file inside the blend before closing.

play_game rule
Runs the blend within blenderplayer. This rule depends on the build_scene rule, so you know that the scene will be setup first.


Another option is to have your bpy script start the game engine once it has set up the scene. This can be done using:

import bpy
bpy.ops.view3d.game_start()

However you will still need separate scripts for bge and bpy. So far as I know, there is no way around that.

$\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.