Python-based planetary gears

| | Comments (0)



So, that's some pretty intense rolling right there. The concept is pretty simple, just use a custom constraint made in python to correctly map the rotations to the planet gears. Here's how it works.

Skills you will need:
-Basic knowledge of blender interface
-some coding experience
-fundamental trigonometry knowledge
-ability to find stuff

Start by modeling the gears themselves. Try to use nice, easy numbers of teeth. Mine used 16 for the planets, 32 for the sun, and 64 for the outer gear.(Remember that teeth count increases linearly with radius.)
model.png















Next, open up a text window and find the constraint script template. You will need at least 2.46 for this to work.
window.png















Now, implement the following changes:

Change NUM_TARGETS to 2 instead of 1.
Add:   

    # Get the rotation for inner and outer gears.
     
    sunrot = targetmatrices[0].toEuler()
    outrot = targetmatrices[1].toEuler()

    # Get the position of the gear itself.
     
    localrot = obrot.z
         
    offsetx = (0 - obloc.x)
    offsety = (0 - obloc.y)
     
    globalrot = 0
     
    offset = math.sqrt((offsetx * offsetx) + (offsety * offsety))
     
    # Apply sun movement to temp values.
     
    globalrot += sunrot.z
     
    localrot += (1 - (sunrot.z * 3))
     
    # Apply outer movement to temp values.
     
    globalrot += outrot.z
     
    localrot += (outrot.z * 3)
     
    # Apply temp values to owner.
     
    obloc.y = (offset * math.sin((globalrot/180) * math.pi))
    obloc.x = (offset * math.cos((globalrot/180) * math.pi))
     
    obrot.z = localrot


After the "Do stuff here" comment. This will work for the first gear only, you will need to change the trig function maths for each individual gear. My actual file has four different pyconstraints.
Add two circles and a plane. Delete the faces on the circles and plane. Make the circles children of the plane, and lock their location, scale, and x and y rotation. Give the outer and sun gears transform constraints to rotate them the right number of times to line them up with the planets. You'll need to experiment with this one.
Finally, add a script constraint to each gear and select the correct script for the gear. Set all "spaces" to local, enter the circle which is controlling the sun as the first target, and the circle for the outer gear as the second target.

That's all! If you didn't get anything, feel free to comment or lay down a post at the blenderartists thread.

Leave a comment

Pages