How to reference object.parent_bone in BGE?

In BPY it’s simply: obj.parent_bone

but that attribute doesn’t exist for a KX_GameObject.

Parenting objects to bones obviously works in the game engine but I am not clear how to get a reference the parent of a game object when that parent is a bone.

Any ideas?

You can do some fun stuff attaching stuff to bones, the API is just a little dull. BL_ArmatureObject.channels[bone_name or index] retrieves some data you can use.

I’d store the name of the bone from bpy through a script and then just fetch it at runtime.

Now if you want to change the bone parent I’m not sure you can. But there is the bone matrix somewhere so you can try applying that to object transform for some “parentless” parenting, though it’s unlikely to scale all too well.

this is relative simple and hacky way of doing it

import bge
cont = bge.logic.getCurrentController()

own = cont.owner

armatur = own.parent
bones_list = armatur.channels

#and here pick the bone you need from the list

one way to know what bone to pick is to store the name of the bone in a game property.

@Liebranca and @edderkop thank you both for your responses.

I am familiar with using something like:

boneObj = arm.channels[‘bone’].bone

to reference the bone directly but was curious to understand how the relationship between the bone and the child (e.g. an empty used to “grab” things) is represented in BGE.

For instance the childObj.parent returns the armature (and not the bone) in both BGE and BPY. As I mentioned: childObj.bone_parent returns the bone in BPY but there isn’t a clear parallel in BGE.

The REAL reason I want to this is because I want to reference the child empty of the hand bone in six different armatures. That empty object is going to have a different name in each case (empty.000, empty.001, etc…)

So instead of referencing the empty by name It would be great if I could use:


Unfortunately, bone.children only returns a list of bones, but sadly it excludes any objects which may also be parented by the bone.

So, I was hunting for a work around by understanding how the child recognizes its bone parent and – ideally – how the bone recognizes its non-bone children in BGE.

Use a property on the empty something like “hand_empty”
grab the root bone and:

for obj in rootbone.childrenRecursive:
    if "hand_empty" in obj:
        own['empty'] = obj

this is likely what you are looking for

the scripts is run on the collision shape in this example.

demo.blend (838.6 KB)

@Cotaks Interesting idea I’ll try that out.
@edderkop I haven’t made the leap to UPBGE 0.3 yet but maybe this will spur me on. How do you like it overall?

it works the same in BGE 2.79 i think, just a matter of recreate it in old BGE.

I guess I will try to install UPBGE 0.3 later to be sure but @edderkop just using the script in your demo:

armature.childrenRecursive DOES work in 2.79.

however, @Cotaks:


doesn’t work because BlArmatureBone objects don’t inherit from KX_GameObjects and therefore do not have the attribute childrenRecursive in blender 2.79 at least.

Thanks guys, this gives me plenty to think about.