Python - remove constraint

Hello,

I was just wondering, if I could do the taking objects during game, so I wrote a little script “Constraint”, it includes point constraint and remove constraint, but I have a little problem about removing constraint - I can’t find out, how can I get constraint ID, it wrote me error to the console, it has problem with name ‘constraint’, “constraint_ID = constraint.getConstraintId()” won’t help, when I write “PhysicsConstraints.removeConstraint(constraint_ID)” console says “name ‘constraint’ is not defined”.
The full script is here:

import PhysicsConstraints

import PhysicsConstraints

GL = GameLogic
cont = GL.getCurrentController()
own = cont.getOwner()

objList = GameLogic.getCurrentScene().objects

Box = objList[“OBBox”]

Me = objList[“OBTakeEmpty”]

constraintType = 1

Box_ID = Box.getPhysicsId()

Me_ID = Me.getPhysicsId()

pointPos_x = 0.0
pointPos_y = 0.0
pointPos_z = 0.0

if own.take == 1:
constraint = PhysicsConstraints.createConstraint(Box_ID, Me_ID, constraintType, pointPos_x, pointPos_y, pointPos_z)

constraint_ID = constraint.getConstraintId()

if own.take == 0:
PhysicsConstraints.removeConstraint(constraint_ID)

hi Kapomafioso,

The reason that you’re getting errors is because you forgot to save the contrainst_ID. Right now, it’s a local variable that is deleted each time the script finishes running.

Change
constraint_ID = constraint.getConstraintId()
to
own.constraint_ID = constraint.getConstraintId()

and
PhysicsConstraints.removeConstraint(constraint_ID)
to
PhysicsConstraints.removeConstraint(own.constraint_ID)

BTW. Interesting mix of 2.48 and 2.49 blender python.

Clark

1 Like

I am wordering how do you use such a script? just curious

cthames: now, my script looks like this:

import PhysicsConstraints

GL = GameLogic
cont = GL.getCurrentController()
own = cont.getOwner()

objList = GameLogic.getCurrentScene().objects

Box = objList[“OBBox”]

Me = objList[“OBTakeEmpty”]

constraintType = 1

Box_ID = Box.getPhysicsId()

Me_ID = Me.getPhysicsId()

pointPos_x = 0.0
pointPos_y = 0.0
pointPos_z = 0.0

if own.take == 1:
constraint = PhysicsConstraints.createConstraint(Box_ID, Me_ID, constraintType, pointPos_x, pointPos_y, pointPos_z)

own.constraint_ID = constraint.getConstraintId()

if own.take == 0:
PhysicsConstraints.removeConstraint(own.constraint_ID)

But console still writes:
File Constraint, line 26, in <module>
NameError: name ‘constraint’ is not defined

Nikolay740: like I said, for taking objects to my hands…

BTW, how can I rewrite my script to every object in scene (universal script), now it works only for object ‘Box’, and one else question, when the box has constraint to object Me, which represents the camera and the whole person, it’s behaviour is strange, whenever I touch some other object (barrel, regal, chair), these objects fly around, the box has mass set up to 1.0, when property take is 1, (Edit object > dynamics > mass), but it behaves like it has mass 10000…what’s wrong with it? other objects are mass from 2.0 to 100.0 (chair is lightest, wooden regal is heaviest), despite the regal has mass 100, the box, when is in constraint with person (‘Me’) has mass 1, easily make the regal fly…

hi Kapomafioso,

Here’s a blend for the constraint.

Note that I changed the contact point from [0.0,0.0,0.0] to [1.0,1.0,1.0] just so you could see it swinging and know that the constraint is working.

As for making it universal, instead of hard coding the script to “Box”, code it to get the name from a string Property on the empty. In other words, get the name of the object you want to pick up from a near sensor, collision, mouseover or whatever. Save it to the string property on the empty and then have the constraint script get the name from the string property.

As for your other questions, I’d have to see an example blend that shows the problem.

Clark

Attachments

Point_Constraint.blend (156 KB)

I copied your script exactly as it is to my script, I have nothing different than you, but as soon as I apply your correct script to my object, it shouts that error on line 48 - PhysicsConstraints.removeConstraint(own.constraint_ID)
error: "AttributeError: ‘KX_GameObject’ object has no attribute ‘constraint_ID’ "
I have only property take on my object (like in your .blend), I have all settings same as yours, but it gives me this error…:frowning:

hi Kapomafioso,

The reason you are getting that error is because your code isn’t finished. You need to add code to check that there actually is a constraint id before you try to delete it. And you need to add the code to delete the constraint id after you remove the constraint.

Take a look at the blend I attached to see what I mean.

I also add the string property to make it universal. And used python for 2.49 instead of both 2.48 and 2.49

Clark

Attachments

Point_Constrain_B.blend (157 KB)

Oh, it works! Thanks, I can’t get used to new syntax :frowning: your previous script worked too

I have a small bug here…my box has strange behaviour, it has mass 1.0, but can easily discard three cubes, which has mass 50.0…I don’t know what to do now, it’s bit strange, I can imagine it like in half-life, when object hit another, it adjust, not throw another object from his way…
Go near textured Box and press ‘E’ to mount Cube, once again from unmount…I wanted to do it like in Half-Life, remove near sensor when unmounting, but it don’t work! I did this:
Near (AND) Keyboard (AND) Mouseover > property assign = 1
Keyboard (AND) Property (1) > Property assign = 0

But property don’t want to change to 1, it only activate the script for very small moment…now, I don’t know why…

Attachments

Constraint.blend (512 KB)

hi Kapomafioso,

I took a look at your blend and I just don’t have the time to try to figure it out. Good luck with it.

Clark

Thank you, I know where the problem is…the constraint is too strong, if I grab something to my hands in real, it don’t acts like glued in the game, yes…I hope you understand what I’m talking about, the Box is very forcefully constrainted to the empty, it needs some freedom in individual moving, he can’t absolutely follow the empty, is somewhere in settings of constraint (in script) considering some freedom of object? when I hit something the grabbed object won’t stay in same place and make other ojects fly, it will behave realistically (maybe it could be enough to mechanically limit the rotation of camera…)

I used this constraint script and track to logic bricks setting, so now the object behave like parent, it’s too staticall, it’s like part of person, which holds object…

Link or advice/explaination of solution to this problem would be enough for me…

THX

O solved this by removing TakeEmpty and adding a cube…I constrainted ( by normal constraint - F7) the cube to the person…added a property TakeEmpty and everything is working like before, but, I can set up the freedom of grabbed object by changing the mass of TakeEmpty cube…the higher value, less freedom…
This is the best solution I’ve done to this problem, if you have some ideas, write it here, I will be glad, thanks for all help, I appreciate that.