Tuesday, 30 November 2010

Quick Walk Cycle and Python to Mirror a Cycle



I started out aiming for John Travolta's Saturday Night Fever walk to staying alive but I quickly ended up with far too many keys and too snappy and exaggerated an action for a cycle.  I realised I needed to change the rig a bit to get the movements I needed so I stripped it right down and worked up this basic walk cycle just to test the changes work.

I also wanted to work with only half a cycle and be able to have a script mirror it for me.  I coded this quick python timesaver.  Basically just select all the bones you have animation on and enter the start and end frames into the script.  Eg. You have a stride on frames 1 to 21 and you want the mirrored stride to be placed on frames 21-41 so you enter start_frame=1 end_frame=21.  Be warned the script will purge any modifiers you have on fcurves (its lazy at the moment) and replace them with a simple cycle modifier so your pair of strides (on 1-41 in the example) continue looping forever.

import bpy

start_frame=1
end_frame=21


def set_frame(sf):
    bpy.context.scene.frame_set(frame=sf)
    bpy.context.active_object.update(scene=bpy.context.scene)
    bpy.context.scene.update()


for each_fcurve in bpy.context.active_object.animation_data.action.fcurves:
    for each_modifier in each_fcurve.modifiers:
        each_fcurve.modifiers.remove(each_modifier)

each_frame=start_frame
while each_frame<=end_frame:
    set_frame(sf=each_frame)
    bpy.ops.pose.copy()
    set_frame(sf=each_frame+(end_frame-start_frame))
    bpy.ops.pose.paste(flipped=True)
    print('copying '+str(each_frame)+' to '+str(bpy.context.scene.frame_current))
    bpy.ops.anim.keyframe_insert_menu(type=-4, confirm_success=False, always_prompt=False)
    if each_frame==end_frame:
        break
    set_frame(sf=each_frame)
    bpy.ops.screen.keyframe_jump(next=True)
    each_frame=bpy.context.scene.frame_current

#kill fcurve mods and add a cycle mod
for each_fcurve in bpy.context.active_object.animation_data.action.fcurves:
    each_fcurve.modifiers.new(type='CYCLES')       

Saturday, 27 November 2010

Au Soleil at the London Underground Film Festival (LUFF)

I'm really happy to have heard from the London Underground Film Festival that my short 'Au Soleil' will be screening at the Horse Hospital next Saturday the 4th December, as part of their programme Around the Compass Rose: Human Geographies on Film.  'Au Soleil' was nominated for the technical achievement award at CAM*ERA earlier in the year.  It seems like ages since I finished work on this film, and though with hindsight some of the technical work isn't as strong as I would have liked, I hope it goes some way to proving that as a medium animation has more to offer than just tired/banal overworked childrens' entertainment.  Tickets available here £5 or £3 concessions, showing with 'Wild West', 'Faith Hope and Greenland', and 'Those Who Live Off the Dead'.

In completely unrelated news, Campbell Barton did a terrific job last week of bringing blender's rigging tools slightly further out of the stone age by fixing the infamous bone roll bug (which has been in blender for nearly as long as I can remember).  It seems that all of the devs have been completely oblivious to it, and no riggers have ever got round to reporting it as it was fixed just hours after I reported it!  Not only that, but Campbell has also added a few more options to the menu giving a choice of which axis to align to (+Y for  a leg IK chain for example).  Commit notes are here for anyone interested: 1 2 3.

Friday, 26 November 2010

Rigging the Sidewinder Desert Snake


The desert snake basically has a continuous sine wave running throughout its body, apart from the tail and the head which are partially free - the head tends to stay fairly level but still inherits some of the rotation from the body wheras the tail flips loose.  For the continuous sine wave, rather than using a wave modifier on a curve and relying on keying the speed (clumsy at best - I'd rather key the offset but then with speed set to 0 the wave disappears and further adjusting wave width during the animation isn't possible, unless you choose to scale the curve to change the wave width but then you get into more problems!) I drew a simple sine wave with a path, arrayed it many times, curve deformed it into a circle then applied the circular curve deform and set the path to be cyclic (on U).  I then converted my circular wavy path to polygons and extruded it out (with shift-Z to constrain scale to X and Y only).  I built a simple FK armature each of whose bones began and ended on the wavy surface, running round with a slight circular bend.  Below the start and end of each bone I put an empty which was shrinkwrapped with Z project to the wavy surface.  Each bone then has a track to constraint aimed at the empty nearest its tail.

I then use a second of chains of bones to convert this wavy (but curved) motion into a wave in a wave in a leaning plane (roughly the XZ plane rotated 45 degrees about Y) by copying only their local X rotation and making sure the bones in my second chain have their Z axes pointing towards the vector (0,1,1).  Then a third chain of bones tracks to the tail of its double in the second chain with maintain Z up set.  A fourth chain is used to add a user specified rotation in the world Z to each bone (for example a curve in the snake).  This has to be done in a separate chain containing copies of the third chain's bones rotations as rotating any bone in the third chain disrupts all of its children's track to constraints!)

A final few bones are used to add non-automated animation to the head and tail.  The width and height of the waves can be controlled by scaling (the parent of) the large rotating wavy disk in the world X or Z axis (there are some empties in between with maintain volume constraints to move the chain of empties which the first chain tracks to into the right position), and the rate of progress of the wave down the snake's body is controlled by rotating the wavy disk.

Now down to animating, at long last.


Wednesday, 24 November 2010

Birds and Fish



Quick Crow Flight Cycle


Might give it another go with a simpler setup when I get a moment.  Its still a struggle to get the wings completely folded up.  Rerigged the shark with a much simpler setup to get the simple sine wave propagating nicely, he's still a little dead for now though!



Updated - thanks to Frankie Swan for some great tips.

Monday, 22 November 2010

Bone Roll in Blender

It's well known that setting bone roll to Z-axis up in blender often doesn't work well, if at all.  If you find yourself having to set bone roll manually using Ctrl+R then this script might help you out.  Tab into bone edit mode, select the bones you want to roll, copy and paste the below script to a text editor, set your target roll vector as new_z_axis_vector in the script below (at the moment its set for z up) and hit run script.

import bpy,mathutils,math

new_z_axis_vector=mathutils.Vector((0,0,1))

for each_bone in bpy.context.selected_bones:
    new_x_axis=each_bone.y_axis.copy().cross(new_z_axis_vector)
    if new_x_axis.copy().angle(each_bone.x_axis)>90:
        new_x_axis=(-1)*new_x_axis
    previous_z_angle=each_bone.z_axis.angle(new_z_axis_vector)
    roll_adjust=each_bone.x_axis.angle(new_x_axis)
    each_bone.roll+=roll_adjust
    if each_bone.x_axis.angle(new_x_axis)>0.01:
        each_bone.roll-=2*roll_adjust


Of course the correct rigging practice is to make sure the bone roll of all the bones in a chain is such that the z axes vectors of all the bones are in the same plane so you might want to set new_z_axis_vector to something other than (0,0,1) or world z, for example a leg IK chain might need a value of (0,1,0) to make all the bone z-axes point forward.

Saturday, 20 November 2010

Getting Renderman Shader Editing on Windows

With renderman exporters for blender 2.5 on the horizon I thought I'd get a head start by learning the renderman shading system.  Sadly on windows its not quite as easy as it should be get a shader editor going.  The original Shaderman runs fine but its a little dated, and I'd previously tried getting shaderman.next to run to no avail.  Instead I tried Sler which like shaderman.next is written in python.  Some of the links to sler's dependencies are  broken so I tried searching around the internet and installed the dependencies as listed on sler's download page. Sadly, according to this blog post on superjazz it seems that many of the recent compiles of gtk are incompatible with recent compiles of python.  If you're getting lots of these errors you've probably hit the same problem I did:

ImportError: DLL load failed: The specified module could not be found.

To get round this you need to install an older version of python.  On top of that you need GTK with glade (which for some reason doesn't always seem to be included) - if you're getting errors about a missing libglade-2.0-0.dll then you've only got plain GTK without the glade.  The exact files which worked for me are:

Sler-v0.2beta
python-2.5.4
gtk-dev-2.12.9-win32
pygtk-2.12.1-3.win32-py2.5
pygobject-2.14.2-2.win32-py2.5
pycairo-1.4.12-2.win32-py2.5
aqsis-setup-1.6.0 - for preview rendering

Just unzip Sler to a clean folder then inside the bin subfolder create a new batch file with something like the following to set your path and start sler:

set PATH=C:\Program Files\GTK2-Runtime\bin;C:\Python25;C:\Program Files\GTK2-Runtime\lib;C:\Program Files\GTK2-Runtime\;C:\Program Files\Aqsis;C:\Program Files\Aqsis\bin
@set GTK2R_PREFIX=G:\Program Files\GTK2-Runtime\bin
@echo set PATH=%GTK2R_PREFIX%;%%PATH%%
@set PATH=%GTK2R_PREFIX%;%PATH%
python runsler.py
pause

Best of luck, let me know if you have any problems.


Thursday, 18 November 2010

Disco Guy - Character for the mood change walk exercise

Disco Guy: Rig and Wire

Some of the texture painting's not perfect but in the end I was pretty happy with how the rig turned out.  I have IK/FK switching on arms and legs, free/fixed hip choice, stretchy IK body with volume preservation and a nice B-bone torso, as well as clavicle deformers to keep the chest shape.  On the hands I have palm fan controls and finger spread/fist controls as well as individual finger curls.  Not too bad for half a days rigging and weighting.

Shape keys yet to come, only the eye controls are done for the face so far.  Not sure if I'll bother with custom bones as I know the rig inside out and spent a while sorting out layers.  Sadly the 32-bit PC in the studio can't cope with deep shadow buffers and hair without running out of RAM very quickly.


Saturday, 13 November 2010

Boundaries for Python Boids and Using the NLA for Animation Layers



Thanks to Campbell's bug fix for the shrinkwrap constraint, my python coded boids (not blender's built in types) can now stay constrained to a surface, albeit very slowly.  The execution of my python code is pretty rapid, considering the number of adjacencies which need to be taken into account, and I have some tidy ups planned which should make it even faster.  The downside is that I need the code to know the visual location (not actual location) of each boid after shrinkwrapping, and then apply this to the actual location.  Logically if I did object.location=object.matrix_world.copy().translation_part() you would expect this to update the visual location.  However it seems that (somewhat sensibly) blender doesn't always update constraints after every line of python which could make the visual location of an object differ from its actual location.  Trying to tell blender to update the world matrix to reflect changes in the object's location seems to be the main problem, especially when the object has keys (so just setting the current frame won't work).  The best option seems to be to key the location (and oddly enough using object.keyframe_insert('location') doesn't update the matrix, but ops.anim.keyframe_insert(....) does, but is much much slower) then read back the matrix.  This unfortunately is extremely slow, but does seem to work.

UPDATE: documented a workaround inspired by some of Bassam's suggestions.

Some python work which on the other hand has been more successful is a workaround to use NLA strips as animation layers (in the same way they would work in maya).  To use the script key the base animation in and then snowflake the action in the NLA window.  Then add a new NLA strip on a higher layer and set the mode to additive (to do this you'll probably have to create an action with a garbage key which you can later delete, link the action to the object then snowflake it).  Set your new top layer NLA strip to add mode then tab into it (tweak mode as its now called).  Start posing and whenever you want to insert a key use this script http://www.pasteall.org/16791/python instead of the 'I' key.  This makes sure that the new keys are relative to the layer below.  In good news aligorith confirmed that this behaviour will soon be built into blender and you won't need my script for much longer!

Friday, 12 November 2010

Shark Swim Cycle



Tried to get layered animation working in blender but the NLA didn't seem to work the way I wanted it to (like animation layers in maya) so I gave up for the morning and tried to animate a swim cycle.  That didn't go too well either - I'd build the flippers on the model far too low down and they looked unnatural in what should have been their rest pose by their side.  My spline IK rig which was meant to make animating the sine wave easier ended up making it harder as I had far too few bones controlling the spline to get a smooth enough wave.  Added to that the tail could have done with a few more bones to smooth out the motion.  The looping destroys the effect of the fake caustics and volumetrics somewhat, but never mind, I hate default grey (or 'Material.001' as its otherwise known).

Fear and Loathing Fish Animation



The brief was to animate a fish as if it had the character of a celebrity/famous person etc.  I picked Johnny Depp as Hunter S. Thompson in Fear and Loathing in Las Vegas.  I'm not sure if the neuroses just comes across as bad animation?  Took from 10am yesterday to 6pm to animate this short clip.  Still not 100% happy with the rig.  Lets hope the shark goes better!

Wednesday, 10 November 2010

Painting the Fear and Loathing Fish

slowly going insane...

Spent the last 3 hours painting seamsless textures and bone weights (this ones an FK only rig... I'm running out of time).  At least the fish can now deform into shapes like this, and blow bubbles thanks to instanced metaballs on a particle system emitting from his cigarette!  He has a nice irradescent glow which changes colour as he turns round courtesy of blender's underused 'magic' texture mapped to the reflection channel.  Animation starts tomorrow!

Tuesday, 9 November 2010

Fish Modelling

Hunter S Thompson Herring
4 hours modelling

Great White Shark
3 hours modelling
(incl 1 hour of teeth vertex pushing!)
2 hours texture painting
(incl 1 hour of messing around with Photoshop 3D)
2 hours rigging and weighting

Some half cooked models I built in a rush for this weeks anim exercise (fishes with character).  The shark's got a nice spline IK rig (a spline for the back and for each of the big flippers), but thanks to blender's outgrown deps graph, it has more than twice the number of bones it would need in maya.  At least the modelling and texture painting in blender kicks the socks off maya.


Saturday, 6 November 2010

Python Boids in 3D



I'd only tested my code out in 2D up till now, even though I'd been coding all the vector maths in a way I hoped would work in 3D.  I ran a simulation above and to my pleasant surprise it worked perfectly first time.  Sadly trying to shrinkwrap/constrain the boids to a surface (a mesh marking the limits of where they are allowed to wander) I ran into a new bug in blender.  Its in the tracker, fingers crossed that by the next time I have a moment to work on the code all will be resolved!

Escape Route Searching in Python Boids






Yey! after some more coding this morning I got escape route searching working.  In this example the boids just give up when they collide as there are no controllers (or boid brains) yet to tell them to do anything other than that.

Friday, 5 November 2010

Boids in Python



Coding boids in blender's python API might seem like a lot of unnecessary work, but when the built in simulation system doesn't offer quite the options you need there really isn't much choice!  At least there's the hope that the python prototyping I'm doing might inspire a competent coder to leap to the mantle and take on the hard work.  Boids generally work in 3D space without stable collisions and don't have the ability to be constrained to a surface/mesh (ie a mesh offset from the ground plane which represents the translational limits of say their torso/root bone).  On the tube project we'll soon be starting work on a scene which requires a large scale crowd simulation.  Over the last 4 days I've put down the foundations of an almost-stable collisions system in python with some basic flock awareness.  More importantly I've implemented some important optimisations which make slow python less painful than it otherwise would be.  Black specks on a grey background might look pretty boring, but this is a big milestone (almost) successfully reached in implementing my half typed up design docs!

Mood Change Walk

First attempt at a mood change walk with the Ludwig Rig (and some special FK/IK adaptations) in blender 2.5.  Background flies are boids with a vortex field.



Talking of boids I've been working on some python code for boid like crowd simulations, the difference being they're slightly less flock like, constrained to a 'ground' geometry and they can't collide or pass through each other.

Thursday, 4 November 2010

Week 4 Line Test and Maya

Week 4 - pushing and pulling and last week's bouncy ball with a mesh deform to vertex color based shader I coded in MEL.




code...

int $numberOfVertices[]=`polyEvaluate -vertex pSphere1`;
string $neighbouredges[];
string $neighbourverts[];
float $edge_start_original[3];
float $edge_end_original[3];
float $edge_start_deformed[3];
float $edge_end_deformed[3];
float $diff_original[3];
float $diff_deformed[3];
float $total_length_original;
float $total_length_deformed;
float $stretch_amount[];
int $edges[];
int $i=0;
for($i=0;$i<$numberOfVertices[0];$i++)
 {
 //find neighbour edges
 $neighbouredges=`polyInfo -ve pSphereShape1.vtx[$i]`;
 string $buffer[];
 int $num_tokens = `tokenize $neighbouredges[0] $buffer`;
 $num_tokens-=2;
 for ($k=0;$k<$num_tokens;$k++)
  {
  $edges[$k]=(int)$buffer[$k+2];
  }
 //now convert the edges to lengths

 $total_length_original=0;
 $total_length_deformed=0;
 for ($l=0;$l<$num_tokens;$l++)
  {
  $neighbourverts=`polyInfo -ev pSphereShape1.e[$edges[$l]]`;
  tokenize $neighbourverts[0] $buffer;
  int $verts[2];
  $verts[0]=(int)$buffer[2];
  $verts[1]=(int)$buffer[3];

  //get edge lengths
  $edge_start_original=`xform -ws -q -t pSphereShape1Orig.vtx[$verts[0]]`;
  $edge_end_original=`xform -ws -q -t pSphereShape1Orig.vtx[$verts[1]]`;
  $edge_start_deformed=`xform -ws -q -t pSphereShape1.vtx[$verts[0]]`;
  $edge_end_deformed=`xform -ws -q -t pSphereShape1.vtx[$verts[1]]`;
  $diff_original[0]=$edge_end_original[0]-$edge_start_original[0];
  $diff_original[1]=$edge_end_original[1]-$edge_start_original[1];
  $diff_original[2]=$edge_end_original[2]-$edge_start_original[2];
  $diff_deformed[0]=$edge_end_deformed[0]-$edge_start_deformed[0];
  $diff_deformed[1]=$edge_end_deformed[1]-$edge_start_deformed[1];
  $diff_deformed[2]=$edge_end_deformed[2]-$edge_start_deformed[2];
  $total_length_original+=sqrt(pow($diff_original[0],2)+pow($diff_original[1],2)+pow($diff_original[2],2));
  $total_length_deformed+=sqrt(pow($diff_deformed[0],2)+pow($diff_deformed[1],2)+pow($diff_deformed[2],2));
  }
 $stretch_amount[$i]=($total_length_deformed/$total_length_original);
 }

//find max and min
float $max_stretch=-1;
float $min_stretch=-1;
for($i=0;$i<$numberOfVertices[0];$i++)
 {
 if($max_stretch==-1 && $min_stretch==-1)
  {
  $max_stretch=$stretch_amount[$i];
  $min_stretch=$stretch_amount[$i];
  }
 if($max_stretch<$stretch_amount[$i])$max_stretch=$stretch_amount[$i];
 if($min_stretch>$stretch_amount[$i])$min_stretch=$stretch_amount[$i];
 }

for($i=0;$i<$numberOfVertices[0];$i++)
 {
 if($min_stretch!=$max_stretch)$stretch_amount[$i]=($stretch_amount[$i]-$min_stretch)*(1/($max_stretch-$min_stretch));
 else $stretch_amount[$i]=0;
 polyColorPerVertex -colorR $stretch_amount[$i] pSphereShape1.vtx[$i];
 polyColorPerVertex -colorG $stretch_amount[$i] pSphereShape1.vtx[$i];
 polyColorPerVertex -colorB $stretch_amount[$i] pSphereShape1.vtx[$i];
 }