setParent(obj) Issues

Hello,

I am using setParent(obj) to parent one object to another (big surpise there :p) like so:


def attach(obj, obj2):
    obj.setPosition(obj2.getPosition())
    obj.setParent(obj2)

However when I do this, obj ends up parented to obj2, but it also ends up in completely the wrong place, similar to what sometimes happens when I unparent things in normal Blender, any help on fixing this?

Try to use world coordinates…

3x3 Matrix [[float]]
worldOrientation
The object’s world orientation.

                list [x, y, z]

worldPosition
The object’s world position.

http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_GameObject-class.html

P.S. They say also that setPosition is depricated, so you propably would avoid use it.

try this



def attach(obj, newParent):
    obj.worldPosition=newParent.worldPosition
    obj.setParent(newParent)

That’s right! :slight_smile:

Didn’t work 0_o

I made a cleanup on my code and made a tutorial here
http://blenderartists.org/forum/showthread.php?t=198894

I started learning Blender game engine 10 days ago (but I am a programmer already) so I don’t know if it’s compatible with Blender 2.49. I hope that helps.

I am already doing what you are doing in that example file, I’ve checked what is returned by worldPosition, and it is the place obj should end up at (i.e. the location of obj2) but instead it goes elsewhere, still no idea why.

you might need to unparent any other parents first:


def attach(obj, newParent):
    obj.removeParent()
    obj.worldPosition=newParent.worldPosition
    obj.setParent(newParent)

I’m not sure.

You might provide us a demo file with the problem.

Select the object and press ctrl-a. Maybe?