6
$\begingroup$

I'm using the blender game engine, and I'd like to generate an object each time a key is pressed (i.e. press Space, and a rock will be loaded into a catapult)

My problem is that I am using this object within Morse, which currently doesn't support using hidden layers (everything seems to be put on the visible layer as soon as it is loaded)

Normally I would do something like this:

from bge import logic

scene = logic.getCurrentScene()
rock = scene.addObject("Rock", "Catapult")

Catapult is a mesh on one layer, and rock is a mesh on another. I would then use the logic bricks to make a keyboard actuator that calls this script. This works fine when I run it in Blender (currently using v2.6), but I get an error when running it with Morse:

ValueError: scene.addObject(object, other, time): KX_Scene (first argument): object must be in an inactive layer

Is there any way to generate objects without needing the inactive layer?

Alternatively, is there a way to programmatically move an object from an active layer to an inactive layer?

Thanks

$\endgroup$
1
  • 1
    $\begingroup$ A workaround to the 'object must come from inactive layers' problem is to use libload to dynamically load the object into the current Blender scene. Then you should be able to instantiate them. $\endgroup$
    – Mike Pan
    Commented Nov 13, 2013 at 7:24

1 Answer 1

2
$\begingroup$

Yes, but only via LibLoad (which likely isn't what your after).

You can use LibLoad/LibNew to dynamically load data, then instance that.

You can load data from another blend file or, you can load an existing mesh (but NOT an object).

See these functions:

$\endgroup$
2
  • $\begingroup$ Thanks! I created a new rock.blend file with the rock in a hidden layer, and then when I use LibLoad from the catapult.blend file, it stays hidden when I load it, and addObject will now work. The only issue I still have is whenever I press Space, it creates two objects, one when I press the key, and one when I release it. Any idea how to fix this? I've been playing around with the keyboard sensor parameters (True/False level triggering, frequency, level, tap, and invert) and I haven't been able to get it to work $\endgroup$
    – Brent
    Commented Nov 18, 2013 at 23:59
  • $\begingroup$ Solved it. Needed to add if cont.sensors["my_keyboard_sensor"].positive: to the python script $\endgroup$
    – Brent
    Commented Nov 19, 2013 at 18:55

You must log in to answer this question.

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