10 December 2012 Gear Rotation Animation Python Script

COMMENTS

This script takes a pre-drawn group of gear outlines (generated using the CamBam gear generator), then animates them using a series of relationship rules within the script.

This script requires CamBam 0.9.8N or later.

This script demonstrates:

  • Importing a helper Python module.
  • Defining a Python class.
  • Animating objects by changing their transformation property.

Gear animation

Source

Download

This download also contains the example CamBam drawing required by this script.

gear-animate-source.zip (89 KB)

gear_animate.py

#
# gear_animate.py
#
# Open the file: gears.cb
# Then press F5 in this script.
#

import gears
from gears import *

# geardef parameters:
#
# 1 - ID of outer gear shape
# 2 - ID of center point (single point in a point list)
# 3 - number of teeth
# 4 - the tooth pitch size 
#
xgear = [ geardef(1,11,16,2),
          geardef(2,7,20,2),
          geardef(3,8,24,2),
          geardef(4,9,28,2),
          geardef(5,10,32,2),
          geardef(12,9,72,2),
          geardef(13,14,17.0,2) ]

# assume gear[0] is the driving gear, which has a relative speed of 1

# Links gears together.
# The gear number is the index in the xgear list defined above.
# for example, the following line links gear 0 and 1
# ie gear[0] drives gear[1]
xgear[1].link( xgear[0] )
xgear[3].link( xgear[0] )
xgear[4].link( xgear[0] )
xgear[2].link( xgear[3] )
# gear 5 and 3 are on the same axis, so give them the same speed
xgear[5].speed = xgear[3].speed
xgear[6].link( xgear[5] )


# radians per animation setp
th = Math.PI/180.0

# animation loop
for dd in range (1,360):

    # update each gear's rotation
    for xg in xgear:
        xg.rotate(th)
    
    view.RefreshView()
    app.Sleep(10)

for xg in xgear:
    xg.reset()

gears.py

#
# gears.py
#
# A class used to help with gear animations.
#

# imported python modules do not get default headers and imports
# so need to add all referenced assemblies here.
from CamBam.Geom import *

class geardef:
    def __init__(self,_id,_center,_teeth,_pitch):
        self.id = _id
        self.teeth = _teeth
        self.pitch = _pitch
        # d is the pitch diameter
        self.d = _teeth / _pitch
        self.ent = doc.FindPrimitive(_id)
        cp = doc.FindPrimitive(_center)
        self.c = cp.Points[0]
        self.speed=1.0

    # link this gear to another by adjusting it's
    # relative rotation speed based on pitch diameter ratio:
    def link(self,other):
        self.speed = -other.speed * other.d / self.d

    # rotate the gear about the center point by theta x speed radians
    def rotate(self,theta):
        self.ent.Transform.Translate(-self.c.X,-self.c.Y,0)
        self.ent.Transform.RotZ(theta*self.speed)
        self.ent.Transform.Translate(self.c)

    # reset the gear rotation
    def reset(self):
        self.ent.Transform = Matrix4x4F.Identity

Comments

<< >> 
name: Rate this article

url:
e-mail:
Name, url and e-mail are optional. E-mail addresses will NOT be displayed, but please provide one if you would like a reply.
If your comment or url is not displayed immediately, it may be awaiting approval.
The following BBCode formatting codes are supported : [b], [i], [u], [code], [quote], [list], [*]
Your comments...


Submit Comment