Scaling A Bone With Python To Simulate Breathing

Hi All,

I am trying to scale a bone in pose mode using a frame change script link.

I want my bone to scale up and then scale down as time changes. I am trying to simulate breathing.

I have this code:


import Blender
from Blender import Armature, Scene
from Blender.Mathutils import *
#from Blender.Scene import Render
#
scn = Scene.GetCurrent()
arms = Armature.Get()
for arm in arms.values():
 for bone in arm.bones.values():
  ctx = scn.getRenderingContext()
  curframe = ctx.currentFrame()
  print curframe
 
  if bone.hasChildren():
   print bone.children[0].name
   myScale = curframe/30
   r = (myScale * 10) + 1
   print "!" + str(myScale) +", " + str(r)
   bone.children[0].size = [r, r, r]
Blender.Redraw()

But there is no size attribute for the bone.

What is the property for scaling a bone?

I think you have to set either the ‘head’ or the ‘tail’ position. I can never remember which is which :wink:

I came up with a simple breathing script for the scriptlink.

Place this code in the scriptLink frame change event of the default cube.


import Blender as B
from Blender import Mathutils as Math
NAME = 'Cube'      # object to scale
START = 1        # initial frame
FRAMEINTERVAL = 30
DELTA_VALUE = 0.1
ob = B.Object.Get( NAME )
frame = B.Get('curframe')
if frame == START:
 ob.SizeX = 1.0
 ob.SizeZ = 1.0
 #Fetch our storage dictionary.
 myDict= B.Registry.GetKey('ap_Storage_Cube')
 if not myDict:  # our dictionary does NOT exist.  must create
  myDict = {}
  myDict['cube_scale_value'] = DELTA_VALUE
  print 'Creating new dictionary value!'
 else:
  #Dictionary already exists, just set the value.
  myDict['cube_scale_value'] = DELTA_VALUE
  print 'Resetting dictionary value!'
 #Return our dictionary back to the registry.
 B.Registry.SetKey('ap_Storage_Cube', myDict)
else:
 #Fetch our storage dictionary.
 myDict= B.Registry.GetKey('ap_Storage_Cube')
 
 #Fetch our value from the dictionay.
 v = myDict['cube_scale_value']
 if (frame % FRAMEINTERVAL) == float(frame/FRAMEINTERVAL):
  if v < 0:
   #Was going negative, now goes positive.
   myDict['cube_scale_value'] = DELTA_VALUE
  else:
   #Was going positive, now goes negative.
   myDict['cube_scale_value'] = -DELTA_VALUE
 #Update our object with the new value.
 ob.SizeX += v
 
 #Return our dictionary back to the registry.
 B.Registry.SetKey('ap_Storage_Cube', myDict)

Press ALT-A to observe the animation. This script will make the cube grow and shrink over time.

I’m still wondering how to adapt this to scale a bone instead of an object like the cube.