Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Python Scripting in Blender

You're reading from  Python Scripting in Blender

Product type Book
Published in Jun 2023
Publisher Packt
ISBN-13 9781803234229
Pages 360 pages
Edition 1st Edition
Languages
Author (1):
Paolo Acampora Paolo Acampora
Profile icon Paolo Acampora

Table of Contents (19) Chapters

Preface 1. Part 1: Introduction to Python
2. Chapter 1: Python’s Integration with Blender 3. Chapter 2: Python Entities and API 4. Chapter 3: Creating Your Add-Ons 5. Chapter 4: Exploring Object Transformations 6. Chapter 5: Designing Graphical Interfaces 7. Part 2: Interactive Tools and Animation
8. Chapter 6: Structuring Our Code and Add-Ons 9. Chapter 7: The Animation System 10. Chapter 8: Animation Modifiers 11. Chapter 9: Animation Drivers 12. Chapter 10: Advanced and Modal Operators 13. Part 3: Delivering Output
14. Chapter 11: Object Modifiers 15. Chapter 12: Rendering and Shaders 16. Index 17. Other Books You May Enjoy Appendix

Animation Drivers

A driver is a function that controls the value of a property. It can take the value of other properties as input, creating a connection between two or more properties. For example, a driver might set the X location of an object based on the rotation of another object.

Drivers are similar to animations, with which they share the update system and f-curve data but are way more flexible and can be combined with Python to create custom setups.

They are an essential part of technical animation and are used for creating simple controls or complex mechanics. Drivers don’t have a specific purpose: they are designed to create custom behaviors. For that reason, they are ubiquitous in rigging and help connect properties, even between entities of different types, such as objects and shaders.

In this chapter, you will learn how to create and test your Python drivers easily, as well as how to script their creation. Besides helping with automating rig mechanics,...

Technical requirements

We will use Blender and Visual Studio Code in this chapter, but any IDE will do. The examples that were created for this chapter can be found at https://github.com/PacktPublishing/Python-Scripting-in-Blender/tree/main/ch9.

Creating drivers

The procedure for creating drivers is very similar to the one for creating animations. While the animation time is the only input of animation curves, drivers can depend on one or more of the following:

  • The result of Python expressions
  • Any property that can be animated
  • The transform channels of objects
  • The difference in rotations between objects
  • The distance between objects

When we create a driver, we must specify at least one input. In this section, we will learn how to set up a simple wheel by creating new drivers with the user interface.

Creating quick drivers via the right-click menu

There are a few shortcuts for creating drivers quickly.

Let’s take a look at an example to understand these shortcuts. Suppose that, to animate a wheel, we want an object’s Location Y to drive its Rotation X channel. We can set this up for Blender’s default cube:

  1. Open Blender or go back to the default scene via File...

Driving a cyclic motion

A pendulum is a weight suspended from a fixed point, free to swing back and forth. It has many real-life applications in time, gravity, and geographic measurements, while in 3D, an oscillating motion is used for displaying clock mechanisms, hanging props, and other cyclic motions. The trigonometric function sine is commonly used to simulate this kind of motion.

The sin function from the math module is the Python syntax for sine. We encountered sine in Chapter 7, where we used its inverse, arcsine, to orient objects using Python. Sine is a periodic wave function – it repeats itself at fixed intervals:

Figure 9.6: The sine function

Figure 9.6: The sine function

Playing the animation will make the cube jitter very fast. To slow it down, we can click Driven Property and give the formula a slower pace.

For instance, we can change it to sin(frame/10) and it will slow down tenfold. Now, the cube rocks back and forth gently. We can do even better and set...

Writing the pendulum add-on

Using what we have learned so far, we can write an add-on that sets up a pendulum for the active object.

We will start with the steps from Chapter 3, and create a .py file for our add-on.

Setting the environment

Let’s create a folder for Chapter 9 in our PythonScriptingBlender project. In the Blender Preferences area, set the ch9 folder as the Scripts Folder property and restart the application. We can create our new files and folder in our IDE (VS Code in this book) so that we can start editing:

  1. Select PythonScriptingBlender/ch9/addons in VS Code.
  2. Create a new file by clicking the New File icon.
  3. Name the new file pendulum.py.
  4. Open the file by double-clicking it.

We can now add the standard elements of most add-ons:

  • Add-on information
  • The Operator class
  • The menu function
  • Registration functions

Next, we’ll learn how to write this information.

Writing the information

As usual...

Summary

Drivers are powerful tools that sit at a crossroads between animation, rigging, and programming. On one hand, they can contain Python expressions and implement custom mechanics on their own, while on the other hand, the entire driver setup process can be automated via scripting.

The tool we wrote in this chapter is a small auto-rig that replicates the same mechanism, with editable parameters, on any Blender object.

The ability to combine drivers, constraints, and custom properties, as well as automate the whole procedure, is an essential part of 3D production as it allows non-technical users to carry on with technical tasks.

As a plus, by using Python, we converted a formula from physics into a working driver expression, a task that can sometimes be intimidating but can be carried out with observation and a little ingenuity.

This topic ends our tour of the animation system. In the next chapter, Chapter 10, we will learn how our operators can interact with the user...

Questions

  1. What color is used for driven properties in the interface?
  2. Can we set keyframes for purple properties?
  3. Can a metric property, such as Location, drive an angular property, such as Rotation?
  4. Can we change the ratio between the driving and driven properties?
  5. Can we type Python expressions when we set values in the interface?
  6. How do we tell Blender that the expressions we have typed should be a driver?
  7. How do we edit a driver property in the user interface? Is there only one way?
  8. Can we add custom properties to an object and use them to control other objects?
  9. In Python, can we create new drivers using the collection.new method, as we do with constraints? If yes, why do we use object.driver_add instead?
  10. Why is the targets attribute of driver variables a list? Which type of variable has more than one target?
lock icon The rest of the chapter is locked
You have been reading a chapter from
Python Scripting in Blender
Published in: Jun 2023 Publisher: Packt ISBN-13: 9781803234229
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}