How do you use IKA solver in python with respect to armature?

Hi guys,

I have a really tough question here. I’m trying to write some python code for animating a human hand touching and carrying other objects. I know that you can do this by putting the armature into Poser mode, then attaching a “follow path” constraint to the child bone, then attaching an IKA solver to the same child bone, but I just have no idea how to do this in python. I tried looking at Posebone’s documentation and there is an attribute called “constraints” but there is no documentation on how to use/modify this attribute.

Could someone please let me know if it is even possible to do what I want in python?

Thanks so much for reading.

Edit: I just accessed the “constraint” attribute of the bone that I was interested in, here is the code:


pose = armObj.getPose()
pose_bones = pose.bones
endBone = pose.bones['b2']

for x in endBone.constraints:
     print x

Here is the output:


[Constraint "Const", Type "Follow Path"]
[Constraint "Const.001", Type "IK Solver"]

This looks really promising, but I still have no idea how to add these two constraints in python. Furthermore, if I could add these two constraints in python, should I just modify the “loc” attribute of the relevant bone to create the different poses of touching a target?

-D

The way I set this up in the GUI,i.e not python, is to just use a copy location constraint and keyframe the influence slider. That should be a lot simpler to code. I’ll look around in the docs and the test scripts I’ve done and see what I can come up with.

…And why do you want to do this in Python anyway ? :smiley:

Mike

Thanks Mike! I just looked a bit more carefully at the docs and I found that it’s actually possible to append constraints into each Pose bone in python – although I still don’t quite get how the constraint works, I just appended “3” into the constraints of the bone that I’m interested in and I can get the IKA solver to work within the GUI, but still not from python.

So what I need to know now is how to get the rotation values of all the bones calculated by the IKA. I see that in the GUI, you can position all the bones using the IKA of the child bone and manually insert the ROT and LOC keys for each bone. I wonder how I can do this through python.

To answer your last question Mike, I’m trying to create a blender package for making animations using natural language, and I would like to be able to use instructions like “The man picks up the mug and throws it out of the window” to make the man’s hand touch the mug, then follow the mug as it moves towards the window. This is part of my PhD project, and I plan to release everything to the open source community once I finish it.

I’ve already written a drag and drop interface for creating virtual worlds using the BMR models and to be honest I’d like to release it if I could be sure of the copy right restrictions. Here is a screenshot of the main interface:

You click the top button on the left to add new objects – TV, people (with armature), animal, rooms, houses, roads, furniture, etc, all ready made (but without texture or colour).

Thanks a lot for reading.

-D

Wow ! That sounds very cool ! A tool like that would be great for people … like me … who just want to get the finished scene done and who don’t want to mess with keyframes, curves and the rest of the mess. I bought a copy of The Sims game, hoping I could do soemthing like that, but it’s too limited and not very customizable, though awhile ago someone on the sims forum was hackcing somehting together to create custom animations.

I thought I posted some example code for creating constraints in this forum, I’ll go looking for it. There’s also another Blender /Python related wiki somewhere …:)… I actually posted code there awhile ago … should be mentioned somewhere on this site … I’ll go look for that too.

**EDIT *
http://mediawiki.blender.org/index.php/Resources/Python_scripts


There is some other info on python/armatures scattered in this forum.

You could try joining the #blendercoders IRC channel, where the programmers hang out.

ideasman42 (Campbell Barton), one of the programmers, is one of, if not THE expert on python in Blender. You could try PM’n him.

You might also contact HarkyMan (Roland Hess)

Here’s his thread on Automated walking :
http://blenderartists.org/forum/showthread.php?t=63143

And his Blender People (crowd simulation) … with source code showing how to hook up Blender to a MySql database :

http://blenderartists.org/forum/showthread.php?t=76130

Still, for picking up an object, I think the simplest is the copy locatoin constraints.

Also, how much do you want to actually program ? … createing the bones … the animation ? … the constraints ? … all from scratch? … That’s possible, but more work than just having your scrpt respond to “Pick up the Ball”, by having it replay the action of the hand picking up the ball. …though that simplicity is probably not PHD material :smiley:

What course / school is this for if you don’t mind me asking ?

Mike

Thanks man!! That’s exactly what I’m trying to do. I think the current way of making animations is too hard for beginners such as myself who know very little about complex animation tools such as blender or 3D Max or computer graphics technology at all. That’s why I think using narrative natural language to encapsulate all the technical stuff can help people such like myself and other beginners who just want to create something cool.

Thanks so much for the references, I’m reading them right now.

I know, I’m just a bit afraid of pissing him off with my stupid questions.

Cool!

Um… that sounds like the way to go, I’m totally new to the constraints of Armature, so if you don’t mind, could you tell me how location constraints work with Armature and IKA?

Well, I’ve already written code (copied from Campbell’s code actually) to create Armature models from BVH files and loading the motions. You can see this in the picture I posted above. “Picking up the ball” is just an example, I plan to use this code to handle every action that involves an armature and something else. For example, if you want to animate “The man kicked the puppy”, I can essentially just make the man’s foot have a contact with the puppy". The coding part is actually not my PhD research, I have to do it to make my results convincing. The research part is to get the computer to automatically learn how to translate natural language to graphics commands.

PhD in computer science at the university of Melbourne.

Thanks so much for the help.

-D

This thread on how to create a biped-rig with python, the script creates IK arms and legs.
http://blenderartists.org/forum/showthread.php?t=81662
http://itu.dk/people/rusmus/blender/BSoD_rigs.py

…I’m looking at it now to see what the syntax is for the IK stuff

Mike

Hi Mike,

I’ve figured out how to do it … sort of !! The following code adds an IKA constraint to the bone :


endBone.constraints.append(Blender.Constraint.Type.IKSOLVER)

The following code makes the bone follow a given object (an objected denoted by the variable “cube” this time):


endBone.constraints[0][Blender.Constraint.Settings['TARGET']] = cube

And I just have to set the rotation thingy using the following code:


endBone.constraints[0][Blender.Constraint.Settings['ROTATE']] = True

Then I just have to use IPOs to move the target around. I think that’s it.

Thanks for all your help though!!

-D

LOL, if you’re smart enough to write a PHD thesis, animation tools will be a snap.

They are not very complex, it’s more that the process is very tedius and primitve (IMO).

E.g. A character picking up an object :

The process most animators use would be to use a stopwatch and time themselves doing the motion.

Say it takes ~ 2 seconds to move your arm from a “rest” postion to the object and bring your hand back. At 30pfs that means you want the animation to take ~60 frames.

So you’d keframe the arm at rest at the start, advance 30 frames, move the arm/hand to the object, keyframe it again, keyframe the location constraint to be “ON” at that point (you’d also keyframe it to be “OFF” at the beginning (frame 1)

For something more complex, like walking and talking / facial expressoins, it’s even MORE tedius to time everything out and re-construct the motion.

As for (Copy) Location constraints, they work the same whether it’s for a bone/armature or any other object.

Here’s an example from the “regression tests” that I modifed a bit :

http://www.4shared.com/file/9854729/5b042c1d/t_driven_hand.html

There is an armature with a hand picking up a ball and drpping it.

There is also another ball where a box moves in and “grabs” the ball.

Both work the same way.

Each ball has 3 Copy Location constraints. For the Red ball, the Target: in the constraint is the armature (“hand”).

The armature also has 3 bones named “tableBone”, “pickBone”, and “dropBone”. The “tableBone” and the “dropBone” are parented to nothing, while the “pickBone” is parented to the wrist (so it follows the motion of the hand)

The constraints have an Influence field and Key buttons so that the influece can be keyframed. You’ll generally want to keyframe the inlfunce at either zero (off) or one (on). I
You can see in the IPO windows, that the curve interpolation has also been set to “Constant”, so that the keyframes will abruptly change from 0-1 (off to on) and vice versa.

The yellow ball is an example not using armatures, but it’s basically the same thing, it uses Empty’s (nulls) for it’s targets.

So for python it’s a matter of changing the constraint.influence. I was playing with this awhile ago, I’ll dig up the code and post it

Mike

Thanks for the confidence man! Animation tools are fun to write, but sometimes they just take too long to get right, and they have almost no impact on the success of my thesis.

Interesting, this is exactly the same process that I described in the introduction chapter of my thesis, and the time-consuming and labour intensive factor is one of the major motivations for my own work.

I sort of got some of these covered with motion capture files. My argument is that there are so many mocap files lying around for complex armature animations, we can just get the computer to learn when to use them and when to use real time based commands.

Man, that’s one of the coolest animation I’ve seen!! How long did it take you to make it?

Um … I can sort of see how this works. Thanks so much man!!

-D

Ahh … duh … now I realize why you wanted to program the IK constraint … it makes the animation much more simple … for a “pickup example” anyway, as the hand will just move to / “find” the object. automatically :slight_smile:

The example file I included in the last post, assume that the actual motion of the arm is keyframed … or would have to be programmed controlled through python. It just shows how the object ->hand “pickup” is setup.

Hmm, you could keyframe the influence of a path constraint …, that might get tricky …:slight_smile:

Mike

As noted in the Readme file “Nozzy” created the original animation. I added the cube animation and just setup the screen windows to try to explain what is going on in the file.

Mike

I know! I always go for the easiest way. :o I just spent the last half an hour staring at your hand-grabbing-ball example, and I cannot even begin to imagine how you did it – especially that the deformation of the hand mesh — most of the fingers don’t even have a bone in them!! I doubt I’d be able to replicate something like this from scratch with python. :stuck_out_tongue: So yes … IK solver is much easier.

And using IK solver may also leave room for much more sophisiticated grabbing motion. So, instead of making the hand following the target, I could actually make each finger follow a different part of the target object using empties and that would make grabbing look more convincing. But I guess that will have to wait after I submit my thesis. :slight_smile:

The bones are there, I’ve just hidden them on “Bone Layer 8”, to reduce the clutter.

To move a bone to a bone-layer, select it in POSE mode then press “m” and click on the desired layer button. When the bone is selected, you can see which bone layer it is on in the Editing buttons / Selected Bones panel. Whichever of the 16 layer buttons is enabled is the layer the bone will be on.

Select the armature, change to POSE mode, then change to the Buttons/Editing
panel (F9). Under the Armature-Panel, Display Options SH-LMB the 8th button
under the Display Options Bone-Layer buttons to enable it. If you’re using
2.43rc2 or CVS, you can use the hotkey SH-M to display a floating bone-layer panel
in the 3d viewport.

Bone layers are great for de-cluttering / organizing armatures. You can
display a particular layer, then just select all “A” bones, and only the
displayed bones are selected, then you can transform, keyframe etc, just
those bones.

When you unhide the bones, you’ll see a bunch of green colored bones
which are the ones that are actually deforming the hand (named “t1, t2,
t3” …f1_1 …"). Thre are also five bones named thumb,index, middle,
ring, pinky. The “structural” bones (“t1” …) all have Action
constraints, (which is one of the more complex constraint types IMO :).
The Action constraint is very powerful though and once setup, make things
easier to animate.

The idea of an action constraint is that you can trigger a stored complex
action from the rotation of a “target” object. E.g. for the “t1” (base thumb)
bone, there are two action constraints, both using the “thumb” bone as the
target (driving object). If you look at the first constraint, you can see
the AC: field has the action name “dvn_thumb” in it and that the X Rot is
set as the triggering axis. So when the “thumb” bone is rotated on the X
axis (the triggering bones Normal axis, not the Global axis), then the “dvn_thumb”
action is played, causing the finger to curl. So just the “thumb” bone needs
to be rotated / keyframed instead of positioning all the other bones every
time.
The individul finger bones can also be rotated / animated in combination with /instead of using the the triggered action constraint.

Good disussioin here on actoin constraints :
http://blenderartists.org/forum/showthread.php?t=71440

Mike