Sandbox:Piffpoof9-3

Introduction
This tutorial will animate a key in a simple fashion. This tutorial continues on from tutorial YYY where we used the Part workbench to create a key to fit the lock housing.



Problem Background
This tutorial will take the work from the previous 2 tutorials, and use it to create a simple animation. The emphasis here is to create a simple animation to demonstrate the techniques, and to provide example code that can be taken and adapted for more complex situations.

Preparation
FreeCAD has numerous facilities and features. To make our work easier in this tutorial, we need to make sure some of them are visible. From the menu bar at the top of the screen select the option. This will produce the Combo View which is a browser with two panes, one above the other. The upper pane has two tabs Model and Tasks - select Model. The lower pane also has two tabs View and Data - select Data.

As you will be working with Python code running within FreeCAD, also enable and. The Python Console will show all the Python statements as they execute while the Report View will list any error messages.

Preparation for Animation
This tutorial is a slightly hybrid one, in that it supplies code which depends on the basis of two modeling sessions. As consequence there is one more step must be taken before the animation code can be used. Animation in FreeCAD is achieved by manipulating the X,Y,Z coordinates as well as the values for rotation. Any object may be repositioned and rotated. The key that was generated in the previous tutorial is a number of discrete objects. We will use the Fuse to make a single object from the different component objects. Dealing with one object is much simpler when animating.


 * 1) Open the work you saved at the end of the previous tutorial.
 * 2) In the Combo View single-click the key shaft in the Combo View to select it
 * 3) Holding down, single-click on the following objects to select them:
 * 4) * key teeth
 * 5) * key handle part 1
 * 6) * key handle part 2
 * 7) Click on [[Image:Part_Fuse.svg|24px]] Fuse to fuse the 4 objects into one object. It is this resultant object that we will now animate. Notice that the resultant is another object called something like 'Fusion'. This is very important as the Python code used in this tutorial will only work with n object called Fusion. The objects that were used to form the result are encapsulated within the resulting object. Click to the left of 'Fusion" to view the objects used to form it.

Code for Animation
The following Python code will take our newly crated object Fusion and animate it. The code is:

Putting It All Together

 * 1) make sure the output from the previous tutorials is loaded
 * 2) make sure the object Fusion exists - this is the only object that the Python code will animate
 * 3) copy the Python code and paste into the Python Console

Further Information
For continuing to learn about using Python within FreeCAD, the FreeCAD Wiki has other articles on:
 * a tutorial on Python programming within FreeCAD - Introduction to Python
 * 3 tutorials on using PySide (which is the user interface used by Python within FreeCAD) - Introduction to PySide
 * a tutorial on scripting with Python in FreeCAD - Python scripting in FreeCAD
 * an advanced tutorial on scripting with Python in FreeCAD - Python scripting with the FreeCAD data models
 * user contributed Python code, known as Macros in FreeCAD - Macro Recipes
 * a tutorial on installing Python code as a Macro with FreeCAD - How To Install Macros
 * a tutorial on installing Macro code onto toolbars - How To Install Macros on Toolbars