2
$\begingroup$

I am using Blender for years and LOVE IT. For my university class do I need a tool that can do the following:

  • Get information (x1, x2, y1, y2, z1, z2 from every object) from a JAVA application) (works)

  • Start blender:
    Here is my first question. When I want to import the bpy (I also tried to import Blender and from Blender import *, it tells me that it does not exist. What do I miss?

  • Create objects I once made a script that can do this, no biggie, but is there a possibility to do this from a script started outside of blender?

  • After every object is placed and scaled etc, is there a way to tell the blender game engine to have a collision, use WASD and arrow-keys to move and mouse to look around, set the player to a special point.

  • Start the game engine so you can move through the 100% per script created forest of objects?

Besides that, is there a way to start blender minimized / hidden, so the user is going to see the "playable" engine and not blender etc?

$\endgroup$
1
  • 4
    $\begingroup$ You are asking multiple questions here; this site works best when there is only 1 question asked at a time, so it might be best to post these questions separately. $\endgroup$ Nov 13, 2014 at 11:09

1 Answer 1

6
$\begingroup$

There is a way to do what you want. I am not sure if this is optimal but it worked for me.

Starting Blender

You can not import bpy from outside of blender (see this related question). You can, however, start Blender and pass it the .blend file with your game and a python script to execute. From the command line start

<path-to-blender>/blender <path-to-blend-file>/my_game.blend --python <path-to-script>/my_script.py

Under Windows you have to use \ in the paths of course.

You can also execute this command from the follwing python script, if you want to create some data first:

import subprocess as sub

"""
create some data here and do other stuff like calling your Java application
"""
...

# execute Blender
command = r"<path-to-blender>/blender -b <path-to-blend-file>/my_game.blend --python <path-to-script>/my_script.py"

p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, shell=True)
output, errors = p.communicate()
if not output == "" :
    print("\nOutput:\n\n" + output)
if not errors == "" :
    print("\nErrors:\n\n" + errors)

You can find more informationon on subprocess here.

Creating Objects

I would create the objects in a script passed to Blender, as I described above. You can also pass command line arguments to this script if necessary. Any command line argument passed after -- in the command line is treated as an argument for the script. The command line woud change to

<path-to-blender>/blender <path-to-blend-file>/my_game.blend --python <path-to-script>/my_script.py -- myArg1 myArg2 myArg3 ...

Inside of Blender you can get the arguments as follows:

import sys
if (("--" in sys.argv) == False) :
    print("No arguments passed to the script.")
    return

argsStartPos = sys.argv.index("--")
arg1 = sys.argv[argsStartPos + 1]
arg2 = sys.argv[argsStartPos + 2]
arg3 = sys.argv[argsStartPos + 3]
...

# process command line arguments, create trees or what ever
...

After object creation: Start the game

I think the easiest way to achieve what you want is to set up the collision detection and player controls in the .blend file you passed to Blender. Then you just have to start the game engine's standalone player. Just add these lines to the python script that creates your scene:

import bpy

...
# create scene and stuff
...

bpy.context.scene.render.engine = 'BLENDER_GAME'
bpy.ops.wm.blenderplayer_start()

Hiding Blender

To hide the Blender window, start Blender in background mode. Just pass -b as command line argument. The full command should then be:

<path-to-blender>/blender -b <path-to-blend-file>/my_game.blend --python <path-to-script>/my_script.py -- myArg1 myArg2 myArg3 ...

Notice the -b before the .blend file.
The standalone player will still be started with a visible window.

I hope this helps :)

$\endgroup$
1
  • $\begingroup$ It helped me soooo much, thank you!! Now I only have to figure out, how to use the command in the CMD :D $\endgroup$
    – Spades
    Nov 14, 2014 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .