How to check the object's physics type through python.

Hello, i’m making a python script, and in this script i need to apply something to all static objects in the scene, but how do i check to see if the object is static?

Any help appreciated
Many thanks in advance!


Edit: nevermind. Disregard this :slight_smile:[YourObject/Objects].game.physics_type

You can use the above. The value for a static type is ‘STATIC’.
Like: if[Objects].game.physics_type == ‘STATIC’

Online reference here:

Hope it helps :wink:

Don’t do this. It’s not available in the BGE runtime, and for a good reason.

Oh, in that case, sorry - disregard my post :slight_smile:

There’s a way to do this, but it’s not perfect.

Dynamic and Rigid objects have KX_GameObject.mass, but Static, Sensor and No Collision don’t. Static and Sensor have KX_GameObject.setCollisionMargin or KX_GameObject.getPhysicsId() not equal to zero, No Collision doesn’t. Unfortunately Radius and Anisotropic Friction cannot be accessed through bge, so as far as I know you couldn’t determine whether an object is Static or Sensor, by just looking at the KX_Scene.objects. However, if by chance all Static objects are visible and all Sensor object aren not, then you can. Also, if necessary rule out all Cameras and Lights with KX_Scene.cameras and KX_Scene.lights. But normally they would have No Collision anyway.

So this is the code:

def getStaticObjects(cont):
    staticObList = []
    for ob in cont.owner.scene.objects:
        if ob.visible and ob.getPhysicsId() and not ob.mass:

Edit: this function gets Dynamic/Rigid, Static/Sensor and No Collision:

def getPhysicsType(cont):
    rigid_dynamic_obList = []
    static_sensor_obList = []
    no_collision_obList = []
    for ob in cont.owner.scene.objects:
        if ob.getPhysicsId():
            if ob.mass:
    print('Rigid and Dynamic objects: ' + str(rigid_dynamic_obList))
    print('Static and Sensor objects: ' + str(static_sensor_obList))
    print('No Collision objects: ' + str(no_collision_obList))

I usually check the mass. Non physics types have a mass of zero.

Indeed this is more an empirical measurement, that might be wrong or change in future.

Raco’ solution seems to fit better as he checks he physicsId. I would not look for vsibility as this is irrelevant for physics calculation.

This wouldn’t be too hard to expose in the api. My only concern is that currently the enum for physics object type is stored in a struct and so one would need to change the code to have the enum in the constants list

It sure would be nice. I wish I could program and help refining.

Added a patch, but not sure if it will make it to trunk

That’s great agoose77! Hope it will make it. Could I also manually apply the patch to my existing Blender version? I’m sorry but this is new for me.

Wow, you guys are awesome, thank you, Raco, for the scripts.