How to expose an armature bone for parenting?

Well, time has come for my game to really have a character creation; for that I need to be able to do something like the below:

self.player_head = armature.bone('head)

so I can use KX_GAMEOBJECT set parent to self.player_head(at least I think it is how it would work) for a given newly loaded character part object.

I would be grateful for any help on achieving the above, and if it is not possible in BGE, for info on what are the workarounds.

Well, I looked in the BGE docs and nothing pops to my mind regarding the above logic. I do think however I can parent a empthy or something to each armature bones, and then parent the new object to that empthy. Anyone votes for a better logic?

Just looked through the api and I don’t think that dynamically assigning bone parents is supported at runtime in bge. I’d really like to know if this is possible and if it’s not then how hard would it be to implement in the source code?

Bone parenting is supported in bge, but it seems that the defining of bone parents is only supported in Blender editor outside the game.

If an armature with bone parented objects is spawned from a hidden layer using addObject then the bone parented objects appear and behave as expected (just as any child of a spawned object is automatically spawned with it’s parent object) so bge must be capable of dynamically creating the bone parented objects. It does seem strange that bone parenting is not supprted in the api. Perhaps there is a technical issue that prevents this, but the addObject creation of bone parented objects leads me to believe that this is merely an unsupported feature rather than an unsupportable feature.

This will definately work. I do something similar in my ragdolls addon. Any object that you bone parent can be used to position any other object.

This even works after you have added the armature to the scene dynamically using addObject. All objects that are bone parented to the armature will be automatically added to the scene and will be bone parented to the armature so they will properly move with animations as expected.

You can even use the linearVelocity and angularVelocity of the bone parented objects to add movement to any other rigid body objects that you want to spawn at their locations. I use this in my addon to take account for animated movement at the moment that the ragdoll is activated.

Double post, but I’m a little tipsy and think my ramblings might be better understood if I break them up…

Edit: If you want to automatically create hit boxes for all bones in your armature (these can be used like the emptys that you suggested) then my addon lets you do it in a single button press. Link to the addon thread is in my signature. Just ignore everything else and hit the “create hit boxes” button. Feel free to edit the addon to just create emptys instead of hit boxes.

Modifying Sourcecode is always great, though a functionable Workaround might be to parent some Objects or even Empties to the Bones in the Editor and then inside the Game using ReplaceMesh. Well, even though I have not tried that, it sounds as if it should work fine.

EDIT:
Damn. I somehow hadn’t recognized this had already been mentioned.

by 2:57 I was able to copy the position of the bones as if it was to parent, but the position became wrong when the object armnature (not bone) rotate
the best solution I found was to put the empty pre-parented

Have you checked the newer builds? The BGE has had a number of animation enhancements as well as more control via python through the merge of the Pepper branch, I’m not sure of how extensive the docs. are for the new features though.

I just wanted to add that unless you plan on having collision-enabled parts for your objects, you might just be able to spawn the parts and parent them to the armature. Give the objects the same vertex group that you need them to deform with (i.e. helmet has a ‘Head’ vertex group with all of its vertices assigned to that group), and it should work. Haven’t tried it exactly, though.

Thanks for the tip FunkyWyrm, I will take a look at your thread.

About having already assigned vertex groups, I must say it didn’t worked SolarLune, I already had tried it.

EDIT: findings in the post below

I tried to parent newly created objects to an object parented to a bone, the new object does rotate together with the object if all the armature moves(even with it assigned to a single bone), but it does not do action bone rotation or any of that…besides I can still accept a few duplicated optimized armatures for animated objects, but I’m having to use them even for not animated objects(ex: player hair, which just has to follow the head bone), that is a lot of unnecessary armature duplicates to take.

and I am starting to get tired…

I will check if I can get each bone position, rotation and scale, if yes I may still have a chance.

EDIT:

I maybe saw something I can use in the BGE API docs, not sure thought:

pag 1328 - class bge.types.BL_ArmatureChannel(PyObjectPlus)

EDIT2:

Yes, I think maybe that is it.

armature_game_object.channels[bone.name].desired_check

There is thought a lot of stuff in BL_ArmatureChannel, but I just need the ones necessary to check the action affected bone position, rotation and scale; then if needed conversion math(if not better), and from that rotate the objects I want which do not require complex animation(like the character head, which needs only to follow 1 single bone), no workaround objects needed.

EDIT3:
I think what I need is here:

self.head_mat = self.player.channels[8].channel_matrix

doing the below I can get which bone is which number:

		for bone in range(len(self.player.channels)):
			print(bone, self.player.channels[bone])

But it is a 4x4 matrix, I have no idea what its contents mean and how to use it so I can:
self.player_head.localOrientation = [bone_rotX, bone_rotY, bone_rotZ]

EDIT4:
I was experimenting with .pose_tail, it gave nice results

Also I really think I should submit a bug report about this.