2010/06/15

CG Piano animation in Blender



Example on Youtube
Scripts: midi_piano.py & midi.py
Blender 2.49 file: midi_piano_test001.blend





Here is what I did.

Steps for the blender file:

  1. Create a new object for the white piano keys
  2. Edit the object until it looks like a piano key
  3. Duplicate the piano key object six times and align the objects
  4. Create a new object for the black piano keys
  5. Duplicate it four times and place the keys in the right position
  6. Edit the white keys so that the black & white keys don't overlap
  7. Make an armature with twelve bones and name the bones. Place the bones just behind the piano keys.
  8. Make the bones the parent of the corresponding piano key.
  9. Copy the armature and the piano keys to make a new octave


Steps for the scripts:
  1. Get midi.py (by Will Ware) for reading the midi file from:
    - http://code.google.com/p/meaws/source/browse/trunk/data/midi.py?spec=svn2&r=2
  2. Midi tempo change added (detect a SET_TEMPO event while reading) to midi.py for calculating the frame
  3. New script file midi_piano with the class Piano
    • The class uses an array that contains all the armatures for the octaves
    • This class changes the bone rotation for the piano keys on the calculated frames.
      midiBeat = float(midiTime) / float(ticksPerQuarterNote)
      midiBeatTime = float(self.tempo) / 1000000.00 * midiBeat

      frame = (midiBeatTime * self.fps) + 1
    • The pitch is used to determine the right armature and bone
  4. Create a MidiFile object for reading a file
  5. Create a Piano object
  6. Pass the register_note method results to the Piano object
  7. Select, open, read & close the file
Please let me know if you thought this was useful.

Update (2010-11-22):
I just found a very useful Blender script that can do all this and more.
You can find it at http://blenderartists.org/forum/showthread.php?t=195434 and http://blendit.xaa.pl/?p=middrv&l=eng. The creator of this script is working on a version for Blender 2.5.