Python Scripting in Blender

Book description

Learn how to use Python scripts in Blender 3.3 to automate tasks, optimize your workflow, think like a 3D programmer, and start creating your tools quickly Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Discover ready-to-go scripts that provide a clear solution to your problems
  • Find out how to automate repetitive tasks in an efficient way
  • Extend Blender’s actions and user interface with your code

Book Description

Blender, a powerful open source 3D software, can be extended and powered up using the Python programming language. This book teaches you how to automate laborious operations using scripts, and expand the set of available commands, graphic interfaces, tools, and event responses, which will enable you to add custom features to meet your needs and bring your creative ideas to life.

The book begins by covering essential Python concepts and showing you how to create a basic add-on. You’ll then gain a solid understanding of the entities that affect the look of Blender’s objects such as modifiers, constraints, and materials. As you advance, you’ll get to grips with the animation system in Blender and learn how to set up its behavior using Python. The examples, tools, patterns, and best practices present throughout the book will familiarize you with the Python API and build your knowledge base, along with enabling you to produce valuable code that empowers the users and is ready for publishing or production.

By the end of this book, you’ll be able to successfully design add-ons that integrate seamlessly with the software and its ecosystem.

What you will learn

  • Understand the principles of 3D and programming, and learn how they operate in Blender
  • Build engaging and navigation-friendly user interfaces that integrate with the native look and feel
  • Respect coding guidelines and deliver readable and compliant code without the loss of originality
  • Package your extensions into a complete add-on, ready for installation and distribution
  • Create interactive tools with a direct response to the user’s action
  • Code comfortably and safely using version control

Who this book is for

This book is for Blender users who want to expand their skills and learn scripting, technical directors looking to automate laborious tasks, and professionals and hobbyists who want to learn more about the Python architecture underlying the Blender interface. Prior experience with Blender is a prerequisite, along with a basic understanding of the Python syntax—however, the book does provide quick explanations to bridge potential gaps in your background knowledge.

Table of contents

  1. Python Scripting in Blender
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Introduction to Python
  7. Chapter 1: Python’s Integration with Blender
    1. Technical requirements
    2. Installing Blender
      1. Installing Blender on Windows
      2. Installing multiple versions on macOS
    3. The Scripting workspace – first steps with Python
      1. The Python console
      2. The Info Log
      3. The Text Editor
    4. External editor – Visual Studio Code
      1. Loading our scripts folder
      2. Keeping Blender’s text blocks in sync
    5. Version control and backup
      1. Initializing the repository
      2. Making changes
    6. Summary
    7. Questions
  8. Chapter 2: Python Entities and API
    1. Technical requirements
    2. Useful features of Python
      1. Options for developers
      2. Python console’s utilities
    3. Accessing Blender modules
      1. The bpy module
      2. API documentation
    4. Accessing Blender data
      1. Creating new objects
      2. Removing elements
    5. Understanding the user context
    6. Summary
    7. Questions
  9. Chapter 3: Creating Your Add-Ons
    1. Technical requirements
    2. Installing our add-ons in Blender
      1. Add-on requirements
      2. Installation
      3. Uninstall
      4. The scripts path
    3. Creating our first add-on – object collector
      1. Operators
      2. Writing a basic operator
      3. Loading operators in our add-on
    4. Running our add-on
      1. Refreshing the add-on list
      2. Running from the Search Toolbar
    5. Improving our code
      1. Saving our edits automatically
      2. Ignoring bytecode files (.pyc)
      3. Fixing the operator logic
      4. Reloading scripts
      5. Avoiding re-assignment errors
      6. Our final operator
      7. Extending menus
    6. Summary
    7. Questions
  10. Chapter 4: Exploring Object Transformations
    1. Technical requirements
    2. Moving objects in space
      1. Transforming objects
    3. Transforming objects indirectly
      1. Using Object Constraints
      2. Using object hierarchies
    4. Understanding the transform matrix
      1. Accessing matrices
      2. Storing object matrices
      3. Copying matrices
      4. Restoring transformations using the world matrix
      5. Creating rest offsets with the parent inverse matrix
    5. Writing the Elevator add-on
      1. Setting the environment
      2. Writing the first draft
      3. Using input properties
      4. Setting the height in the world matrix
      5. Avoiding duplicate transformations
      6. Adding the constraints switch
      7. Avoiding duplicate constraints
    6. Summary
    7. Questions
  11. Chapter 5: Designing Graphical Interfaces
    1. Technical requirements
    2. Areas, regions, and panels
    3. Creating a simple panel
      1. Setting the environment
      2. Drafting our panel add-on
      3. Setting display attributes
      4. Adding a panel to the Object Properties area
    4. Using layouts in our panels
      1. Arranging in columns and rows
      2. Adding frames with box layouts
      3. Using composite layouts
    5. Providing color feedback
      1. Checking whether an object has been selected
      2. Checking whether an object is active
      3. Drawing layouts in red or gray
    6. Displaying buttons
      1. Using the operator method
      2. Setting the operator’s text and visibility
      3. Overriding an operator’s settings
      4. Setting operator properties
      5. Adding buttons for our functions
      6. Displaying the operator properties
    7. Using different regions
    8. Summary
    9. Questions
  12. Part 2: Interactive Tools and Animation
  13. Chapter 6: Structuring Our Code and Add-Ons
    1. Technical requirements
    2. Folders, packages, and add-ons
      1. Creating a package folder and the init file
      2. Writing the init file
      3. Guidelines for separating modules
      4. Writing the structured panel
    3. Packing external images
      1. Writing an icon library
      2. Loading pictures from a folder
      3. Unregistering icons
      4. Getting the collection
      5. Using relative imports
    4. Adding a user interface
      1. Writing the UI module
      2. Importing the UI
      3. Completing the Objects panel
    5. Reloading cached modules
      1. Reloading via importlib
      2. Implementing a refresh module
      3. Reloading the package modules
      4. Using Developer Extras as a condition
    6. Using add-on preferences
      1. Creating preferences
      2. Populating the preferences
      3. Using add-on preferences in code
    7. Adding operators
      1. Writing the operators module
      2. Registering operator classes
      3. Refreshing operators on reload
      4. Adding operator buttons
    8. Packaging and installing add-ons
      1. Cleaning up bytecode
      2. Creating a .zip file using 7-Zip
      3. Creating a .zip file using Windows File Manager
      4. Creating a .zip file on Mac using Finder
      5. Creating a .zip file using Gnome
      6. Installing .zip add-ons
    9. Summary
    10. Questions
  14. Chapter 7: The Animation System
    1. Technical requirements
    2. Understanding the animation system
      1. Timeline and Current Frame
      2. Duration and Frame Rate
      3. Current frame and preview range
      4. Animation keyframes
    3. Writing the Action to Range add-on
      1. Setting the environment
      2. Writing the Action to Range information
      3. Writing the Action to Range operator
      4. Writing the operator methods
      5. Writing the menu function
      6. Finishing the add-on
      7. Enabling and running
      8. Fixing context for other operators
    4. Editing keyframes
      1. Animation curves and the Graph Editor
    5. Accessing animation data in Python
      1. Adding keyframes in Python
      2. Retrieving keyframes in Python
    6. Writing the Vert Runner add-on
      1. Setting the environment
      2. Writing the Vert Runner information
      3. Writing the Vert Runner operator
      4. Writing the operator methods
      5. Writing the menu and register functions
      6. Creating cyclic animations
      7. Adding rotations
      8. Using Vert Runner
    7. Summary
    8. Questions
  15. Chapter 8: Animation Modifiers
    1. Technical requirements
    2. Using F-Curve Modifiers
      1. Adding F-Curve Modifiers in the Graph Editor
      2. Adding F-Curve Modifiers in Python
    3. Writing the Shaker add-on
      1. Setting up the environment
      2. Writing the Shaker add-on info
      3. Writing the Add Object Shake operator class
      4. Writing the operator methods
      5. Adding menu items
      6. Finding the class names of context menus
      7. Registering the Shaker add-on
      8. Using the Shaker add-on
    4. Summary
    5. Questions
  16. Chapter 9: Animation Drivers
    1. Technical requirements
    2. Creating drivers
      1. Creating quick drivers via the right-click menu
      2. Setting up a wheel with the Drivers Editor
      3. Creating driver expressions in properties
    3. Driving a cyclic motion
      1. Changing the rotation pivot via constraints
      2. Controlling the period of the sin function
      3. Implementing the pendulum equation
      4. Controlling the amplitude
    4. Writing the pendulum add-on
      1. Setting the environment
      2. Writing the information
      3. Writing the Operator class
      4. Writing the menu and registering the class
    5. Summary
    6. Questions
  17. Chapter 10: Advanced and Modal Operators
    1. Technical requirements
    2. Understanding the operator flow
      1. Steps of execution
    3. Writing the “PunchClock” add-on
      1. Creating the add-on script
      2. Using invoke to initialize properties
      3. Ensuring default invoke in pop-up menus
    4. Adding modal behavior
      1. Adding the operator to the modal handlers
      2. Writing the modal method
    5. Styling the operator panel
      1. Writing the draw method
    6. Summary
    7. Questions
  18. Part 3: Delivering Output
  19. Chapter 11: Object Modifiers
    1. Technical requirements
    2. Understanding object modifiers
      1. Adding modifiers
      2. Subdividing an object
      3. Changing the object’s shape using Cast
    3. Adding modifiers in Python
      1. Finding collection-type items
      2. Using modifiers.new
    4. Deformation objects
      1. Using the Lattice modifier
    5. Writing the Latte Express add-on
      1. Setting the environment
      2. Writing the Latte Express information
      3. Writing the Latte Express operator
      4. Finding the center of a model
      5. Adding a Create Lattice menu item
      6. Using the Latte Express add-on
    6. Improving Latte Express options
      1. Adding object subdivisions
      2. Changing lattice resolution
    7. Using armature deformers
      1. Adding armature objects to the scene
      2. Adding armature bones
      3. Binding objects to armatures
    8. Scripting a lattice armature
      1. Adding an armature condition
      2. Adding an armature to the scene
      3. Creating edit bones
      4. Assigning vertices to bones
      5. Creating the Armature modifier
    9. Adding custom bone shapes
      1. Creating mesh objects in Python
      2. Finalizing the setup
    10. Summary
    11. Questions
  20. Chapter 12: Rendering and Shaders
    1. Technical requirements
    2. Render and materials
      1. Setting the Render Engine
      2. The Shading workspace
      3. Understanding object materials
      4. Setting Material Properties
    3. The Shader Editor
      1. Understanding the Node Tree
    4. Writing the Textament add-on
      1. Using texture images
      2. Setting up the environment
      3. Writing an import operator
      4. Connecting nodes
      5. Adding a header button
      6. Using Load Textures
    5. Improving Load Textures
      1. Arranging shader nodes
      2. Mixing the Base Color
    6. Summary
    7. Questions
  21. Appendix
    1. Part 1: An Introduction to Python
      1. Chapter 1, Python’s Integration with Blender
      2. Chapter 2, Python Entities and API
      3. Chapter 3, Creating Your Add-Ons
      4. Chapter 4, Exploring Object Transformations
      5. Chapter 5, Designing Graphical Interfaces
    2. Part 2: Interactive Tools and Animation
      1. Chapter 6, Structuring Our Code and Add-Ons
      2. Chapter 7, The Animation System
      3. Chapter 8, Animation Modifiers
      4. Chapter 9, Animation Drivers
      5. Chapter 10, Advanced and Modal Operators
    3. Part 3: Delivering Output
      1. Chapter 11, Object Modifiers
      2. Chapter 12, Rendering and Shaders
  22. Index
    1. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Download a free PDF copy of this book

Product information

  • Title: Python Scripting in Blender
  • Author(s): Paolo Acampora
  • Release date: June 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803234229