PyCarrara, a free Python scripting plugin for Carrara
© 2010 Frederic Rible

 

def Modifier(obj, t):

  for i in range(3):

     piston = obj + ("/Piston %d" % (i+1))

     s = c3d.object_get_offset(piston);

     s.z = 21.5 + 4*c3d_midi.Interpolate(appli.pistons[i], t)

     c3d.object_set_offset(piston, s)

 

Here is step by step what is done by the Python code :

1. When “Modifier()” is called, the “obj” variable contains ”/Trumpet” because the modifier is setup for the trumpet group.

2. A loop is started for the 3 valves of the trumpet.

3. The line “piston=...” loads the “piston” variable with the full path name of each valve.

4. The “c3d.object_get_offset()” returns the position of the valve in the variable “s”.

5. The “z” component of “s” is set to a value computed by  “c3d_midi.Interpolate()” function (this function reads the MIDI file and returns the velocity of each valve).

6. “c3d.object_set_offset()” is called to move the valve to the new position (only “z” is modified for vertical animation).

The MIDI file parser is based on some Python code I have found on the Net. I have added some features such as automatic note mapping to musician fingers: this is required because the MIDI scores describes only notes and not fingers usage ! Of course, fingers mapping depends on the instrument: trumpet is not the same as piano. The fingers animation is smoothed with a filtering function containing an overshot effect for more realistic motions.