Python Scripting in Blender - Paolo Acampora - E-Book

Python Scripting in Blender E-Book

Paolo Acampora

0,0
32,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 367

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Python Scripting in Blender

Extend the power of Blender using Python to create objects, animations, and effective add-ons

Paolo Acampora

BIRMINGHAM—MUMBAI

Python Scripting in Blender

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Rohit Rajkumar

Publishing Product Manager: Kaustubh Manglurkar

Senior Editor: Keagan Carneiro

Senior Content Development Editor: Debolina Acharyya

Technical Editor: Simran Ali

Copy Editor: Safis Editing

Project Coordinator: Sonam Pandey

Proofreader: Safis Editing

Indexer: Sejal Dsilva

Production Designer: Vijay Kamble

Marketing Coordinators: Nivedita Pandey, Namita Velgekar, and Anamika Singh

First published: June 2023

Production reference: 1120523

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80323-422-9

www.packtpub.com

To my daughters, Noè and Dedè, for all the games we play together, which are the best part of the whole thing.

– Paolo Acampora

Contributors

About the author

Paolo Acampora is a software developer at Binary Alchemy and a veteran technical director for animation, visual effects, and prototyping. He is a long-time Blender user and advocates for the widespread adoption of open source software and code literacy.

He works with studios to kickstart their computer graphics pipelines and shares his tools with the Blender community.

I want to thank my life companion, Dona, for her support and for tolerating my snoring. My parents and the whole bunch of rascals who make my family such a fun lot. My friends, who still invite me to parties, and everyone else I met along the way: a little piece of each one of us lies hidden in these pages.

About the reviewer

Dr. Edward Tate is an engineer who has used Blender for more than 15 years for both technical illustration and communication. He has extensively used Python for scientific work. He holds a bachelor of science in mechanical engineering (BSME) and a bachelor of science in electrical engineering (BSEE) from Kettering University, a master of science in electrical engineering (MSEE) from Stanford, and a doctor of philosophy (Ph.D.) from the University of Michigan. He is the author of more than 40 patents. He has worked on electric vehicles, scientific software, aerospace, and wireless power transfer.

Table of Contents

Preface

Part 1: Introduction to Python

1

Python’s Integration with Blender

Technical requirements

Installing Blender

Installing Blender on Windows

Installing multiple versions on macOS

The Scripting workspace – first steps with Python

The Python console

The Info Log

The Text Editor

External editor – Visual Studio Code

Loading our scripts folder

Keeping Blender’s text blocks in sync

Version control and backup

Initializing the repository

Making changes

Summary

Questions

2

Python Entities and API

Technical requirements

Useful features of Python

Options for developers

Python console’s utilities

Accessing Blender modules

The bpy module

API documentation

Accessing Blender data

Creating new objects

Removing elements

Understanding the user context

Summary

Questions

3

Creating Your Add-Ons

Technical requirements

Installing our add-ons in Blender

Add-on requirements

Installation

Uninstall

The scripts path

Creating our first add-on – object collector

Operators

Writing a basic operator

Loading operators in our add-on

Running our add-on

Refreshing the add-on list

Running from the Search Toolbar

Improving our code

Saving our edits automatically

Ignoring bytecode files (.pyc)

Fixing the operator logic

Reloading scripts

Avoiding re-assignment errors

Our final operator

Extending menus

Summary

Questions

4

Exploring Object Transformations

Technical requirements

Moving objects in space

Transforming objects

Transforming objects indirectly

Using Object Constraints

Using object hierarchies

Understanding the transform matrix

Accessing matrices

Storing object matrices

Copying matrices

Restoring transformations using the world matrix

Creating rest offsets with the parent inverse matrix

Writing the Elevator add-on

Setting the environment

Writing the first draft

Using input properties

Setting the height in the world matrix

Avoiding duplicate transformations

Adding the constraints switch

Avoiding duplicate constraints

Summary

Questions

5

Designing Graphical Interfaces

Technical requirements

Areas, regions, and panels

Creating a simple panel

Setting the environment

Drafting our panel add-on

Setting display attributes

Adding a panel to the Object Properties area

Using layouts in our panels

Arranging in columns and rows

Adding frames with box layouts

Using composite layouts

Providing color feedback

Checking whether an object has been selected

Checking whether an object is active

Drawing layouts in red or gray

Displaying buttons

Using the operator method

Setting the operator’s text and visibility

Overriding an operator’s settings

Setting operator properties

Adding buttons for our functions

Displaying the operator properties

Using different regions

Summary

Questions

Part 2: Interactive Tools and Animation

6

Structuring Our Code and Add-Ons

Technical requirements

Folders, packages, and add-ons

Creating a package folder and the init file

Writing the init file

Guidelines for separating modules

Writing the structured panel

Packing external images

Writing an icon library

Loading pictures from a folder

Unregistering icons

Getting the collection

Using relative imports

Adding a user interface

Writing the UI module

Importing the UI

Completing the Objects panel

Reloading cached modules

Reloading via importlib

Implementing a refresh module

Reloading the package modules

Using Developer Extras as a condition

Using add-on preferences

Creating preferences

Populating the preferences

Using add-on preferences in code

Adding operators

Writing the operators module

Registering operator classes

Refreshing operators on reload

Adding operator buttons

Packaging and installing add-ons

Cleaning up bytecode

Creating a .zip file using 7-Zip

Creating a .zip file using Windows File Manager

Creating a .zip file on Mac using Finder

Creating a .zip file using Gnome

Installing .zip add-ons

Summary

Questions

7

The Animation System

Technical requirements

Understanding the animation system

Timeline and Current Frame

Duration and Frame Rate

Current frame and preview range

Animation keyframes

Writing the Action to Range add-on

Setting the environment

Writing the Action to Range information

Writing the Action to Range operator

Writing the operator methods

Writing the menu function

Finishing the add-on

Enabling and running

Fixing context for other operators

Editing keyframes

Animation curves and the Graph Editor

Accessing animation data in Python

Adding keyframes in Python

Retrieving keyframes in Python

Writing the Vert Runner add-on

Setting the environment

Writing the Vert Runner information

Writing the Vert Runner operator

Writing the operator methods

Writing the menu and register functions

Creating cyclic animations

Adding rotations

Using Vert Runner

Summary

Questions

8

Animation Modifiers

Technical requirements

Using F-Curve Modifiers

Adding F-Curve Modifiers in the Graph Editor

Adding F-Curve Modifiers in Python

Writing the Shaker add-on

Setting up the environment

Writing the Shaker add-on info

Writing the Add Object Shake operator class

Writing the operator methods

Adding menu items

Finding the class names of context menus

Registering the Shaker add-on

Using the Shaker add-on

Summary

Questions

9

Animation Drivers

Technical requirements

Creating drivers

Creating quick drivers via the right-click menu

Setting up a wheel with the Drivers Editor

Creating driver expressions in properties

Driving a cyclic motion

Changing the rotation pivot via constraints

Controlling the period of the sin function

Implementing the pendulum equation

Controlling the amplitude

Writing the pendulum add-on

Setting the environment

Writing the information

Writing the Operator class

Writing the menu and registering the class

Summary

Questions

10

Advanced and Modal Operators

Technical requirements

Understanding the operator flow

Steps of execution

Writing the “PunchClock” add-on

Creating the add-on script

Using invoke to initialize properties

Ensuring default invoke in pop-up menus

Adding modal behavior

Adding the operator to the modal handlers

Writing the modal method

Styling the operator panel

Writing the draw method

Summary

Questions

Part 3: Delivering Output

11

Object Modifiers

Technical requirements

Understanding object modifiers

Adding modifiers

Subdividing an object

Changing the object’s shape using Cast

Adding modifiers in Python

Finding collection-type items

Using modifiers.new

Deformation objects

Using the Lattice modifier

Writing the Latte Express add-on

Setting the environment

Writing the Latte Express information

Writing the Latte Express operator

Finding the center of a model

Adding a Create Lattice menu item

Using the Latte Express add-on

Improving Latte Express options

Adding object subdivisions

Changing lattice resolution

Using armature deformers

Adding armature objects to the scene

Adding armature bones

Binding objects to armatures

Scripting a lattice armature

Adding an armature condition

Adding an armature to the scene

Creating edit bones

Assigning vertices to bones

Creating the Armature modifier

Adding custom bone shapes

Creating mesh objects in Python

Finalizing the setup

Summary

Questions

12

Rendering and Shaders

Technical requirements

Render and materials

Setting the Render Engine

The Shading workspace

Understanding object materials

Setting Material Properties

The Shader Editor

Understanding the Node Tree

Writing the Textament add-on

Using texture images

Setting up the environment

Writing an import operator

Connecting nodes

Adding a header button

Using Load Textures

Improving Load Textures

Arranging shader nodes

Mixing the Base Color

Summary

Questions

Appendix

Part 1: An Introduction to Python

Chapter 1, Python’s Integration with Blender

Chapter 2, Python Entities and API

Chapter 3, Creating Your Add-Ons

Chapter 4, Exploring Object Transformations

Chapter 5, Designing Graphical Interfaces

Part 2: Interactive Tools and Animation

Chapter 6, Structuring Our Code and Add-Ons

Chapter 7, The Animation System

Chapter 8, Animation Modifiers

Chapter 9, Animation Drivers

Chapter 10, Advanced and Modal Operators

Part 3: Delivering Output

Chapter 11, Object Modifiers

Chapter 12, Rendering and Shaders

Index

Other Books You May Enjoy

Part 1: Introduction to Python

In this part, we will familiarize you with Blender’s Python interface. This part will illustrate the fundamentals of scripting and provide an overall understanding of how to code tools for Blender.

This section comprises the following chapters:

Chapter 1, Python’s Integration with BlenderChapter 2, Python Entities and APIsChapter 3, Creating Your Add-OnsChapter 4, Exploring Object TransformationsChapter 5, Designing Graphical Interfaces

2

Python Entities and API

Blender expands Python by making the modules of its Application Programming Interface (API) available inside the app.

These modules provide wrappers that translate Blender’s internal data into Python objects. Comprehensive documentation and an API reference are available online and can be reached from inside the application. Plus, there are some extra features to help programmers in