Set physics type in bge using python?

I'm trying to set up a scene using a script that places a bunch of cubes in random locations throughout the screen and sets their physics type to rigid body. Ideally, I'd like to have one cube be an agent whose actions can be controlled via a different script. How do I set objects' physics type en masse using python in the game engine? I've tried

ActiveObj.game.physics_type = 'RIGID_BODY'
ActiveObj.game.use_collision_bounds = 1


as well as

for obj in bpy.data.objects:


but, obviously, neither works in the game engine. How do I get around this?

• As I was told, bpy won't work when you run the game as standalone so don't use it unless you are about to take it out in the end.That was just a notice. – Lev Jul 17 '15 at 21:51
• So how can I add objects via script in the game engine and set their physics type? Every method I've found uses bpy :/ – Archana Ram Jul 17 '15 at 21:59

It's easy to turn a rigid body static and back again. Start with a rigid body, and then you can suspend/restore the dynamics:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
own.suspendDynamics()
#OR
own.restoreDynamics()


A slightly more hacky way to turn a rigid body static is to set it's mass to infinite (ie no force will have an effect).

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
own.mass = float('inf')


To make it rigid body again, set it's mass back to a proper number

To make an object dynamic is hard as BGE doesn't have access to the inertia tensor/radius of gyration. Even setting the angular velocity cap to 0.0 doesn't work. I guess you'd have to, every frame, set it's angular velocity to zero and restore it's orientation.

(untested code):

import bge

own = bge.logic.getCurrentController().owner
if 'ori' not in own:
own['ori'] = own.worldOrientation.copy()
own.worldOrientation = own['ori']
own.worldAngularVelocity = [0.0, 0.0, 0.0]


Create a cube in a different layer and tick rigid in his physics property.

Then go in your active layer and place an empty where you want your cubes to spawn.

Add an "Edit Object" actuator to the empty.

Choose when to activate it and set the Cube in the different layer as the object to be added.

Then the newly added object will have rigid physics.

• Any way to do that without the gui? The project I'm working on relies on me being able to not use the gui whenever possible – Archana Ram Jul 17 '15 at 21:58
• I don't think so, but I haven't tried this to know. I just searched the API and did not find a command that would change rigid to static and so on. You can decide when to add the objects via python but you must have the actuator connected. Wait to read some other solutions too. I could have missed something. – Lev Jul 17 '15 at 22:15
• It's possible, though I don't recommend it this way... Spawn a headless blender, create the new object, save to a temp file, libload. Horrible, I know. – pqftgs Aug 17 '15 at 2:40