Removing Constraints w/ Python in the BGE

Hello,

I was looking for a way to remove a rigid body constraint from two objects in the BGE on the fly. Since the BGE is quite limited in what you can do through the UI, I had a look at some python documentation and found a reference to removeConstraint(Constraint_ID), but I can’t find any sample scripts that I can get to work. I’m very unfamiliar with python and have only run a few pre-written scripts before, so I’m not sure how to go about using this function.

Can someone show me, and maybe set up a simple demo blend file that calls it in a script?

Thank you for helping!

removeConstraint

[B]removeConstraint(constraint_ID)

Removes the constraint.

constraint_ID
Type: integer

[B]Note:

[B]Save the constraintID when the constraint is created.

Note:

[B]This removes the constraint between 2 game objects (point and edge constraints). It does not remove vehicle constraints.

Sample Code

[B]Part 1: Create a Point Constraint

[B]######### PointConstraint.py

[B]# import PhysicConstraints Module

import PhysicsConstraints

[B]# get object list

objList = GameLogic.getCurrentScene().objects

[B]# get object named Obj_1

obj1 = objList[“OBObj_1”]

[B]# get object named Obj_2

obj2 = objList[“OBObj_2”]

[B]# use point constraint type

constraintType = 1

[B]# get Obj_1 physics ID

obj1_ID = obj1.getPhysicsId()

[B]# get Obj_2 physics ID

obj2_ID = obj2.getPhysicsId()

[B]# Use bottom right front corner of obj1 for point position

pointPos_x = 1.0
pointPos_y = -1.0
pointPos_z = -1.0

[B]# create a point constraint

constraint = PhysicsConstraints.createConstraint( obj1_ID, obj2_ID,
constraintType,
pointPos_x, pointPos_y, pointPos_z)

[B]# get the constraint ID

constraint_ID = constraint.getConstraintId()

[B]# save the constraint ID as an Obj_1 variable

obj1[“constraint_ID”] = constraint_ID

[B]Part 2: Remove constraint script

[B]########### RemoveConstraint.py

[B]# import PhysicConstraints Module

import PhysicsConstraints

[B]# get object list

objList = GameLogic.getCurrentScene().objects

[B]# get object 1

obj1 = objList[“OBObj_1”]

[B]# get constraint ID that was saved as an Obj_1 variable

[B]# when the constraint was created

constraint_ID = obj1[“constraint_ID”]

[B]# remove constraint

PhysicsConstraints.removeConstraint(constraint_ID)[/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B]