This blog contains the scripts for my Blender 3D YouTube videos. They're published as follows:
Buy The Blender 3D Cookbook as a book!
Buy the Blender 3D Cookbook for your iPad!
Buy the Blender 3D Cookbook for your Kindle!
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Sunday, March 28, 2010
Python 3, Part 6 (Functions)
In this tutorial, we are going to see how to write and call functions in Python 3, as well as how to pass arguments, both as keyword and value, to a Python function. I will also introduce you to some of the neat features of the Blender 2.5 text editor, such as source code color coding, which help make coding Python scripts much easier. This video is based on the Python 3.1 function tutorial at: http://docs.python.org/release/3.0.1/tutorial/controlflow.html#more-on-defining-functions.
Wednesday, March 10, 2010
Python 3, Part 5 (List Methods)
In Part 4, we saw how to use slicing to work with parts of a string, list, or tuple, as well as how to insert, delete, and add to these types of variables. Python has a number of built in methods -- also called functions -- that can do these operations in a way that makes the code easier to read. Sometimes it's a bit hard to understand all those colons and square brackets, and it's easier to use words like append, remove, or insert, which are, indeed the names of methods associated with lists. We'll also see how these easy to understand methods can be used to build a simple stack (where the last element added is the first element retrieved) or queue (where the first element added is the first element retrieved). We'll also discover how to sort, count, and locate items in a list. Along the way, we'll learn about the autocomplete feature of Blender's Python console, a handy feature that documents available methods.
Python 3, Part 4 (Slicing)
In Part 3, we were introduced to lists, which lets us work with more than one variable at once. We also saw how to reference parts of the list, say the 3rd element in the list x as x[2] (since we start numbering from 0), using the [] operator, and how to use negative numbers to reference list elements from the end. In this tutorial, we will go into more detail about how to get parts of strings and lists, by use of slicing. Slicing also uses the square brackets, but in a new and different way.
Labels:
blender 2.5,
programming,
python 3,
scripts,
slicing,
tutorial
Monday, March 8, 2010
Python 3, Part 3 (Lists)
In Part 2, we saw different ways that string variables can be defined. In this tutorial, we will see how lists of variables can be defined and used. You're basically creating more than one variable at a time, in a structure called a list. Lists are a very important part of Python. If you want to be able both program in Python and to read Python code written by others, you need to know how lists work. Don't worry, you'll get the hang of it if you practice a bit.
Saturday, March 6, 2010
Python 3, Part 1 (Expressions)
The goal of this tutorial is to introduce Blender users, whether or not they are programmers, to Python 3. Blender 2.5 supports Python 3 scripts, which can let you program Blender behavioer very precisely. Of course, before you can create Blender scripts, you need to know Python 3. We will use the Blender 2.5 Scripting environment, which includes a Python console that supports most (but not all) of Python 3, as well as a fully featured text editor, with advanced features such as autocomplete and automatic indenting of code. I will point out where Blender's Python implementation differs from the standard Python 3 distribution. There are minor differences, none that should prevent you from writing fully featured scripts in Blender 2.5.
Labels:
blender,
console,
expressions,
programming,
python,
python 3,
scripting,
tutorial
Wednesday, March 3, 2010
2.50 Alpha 1 Python Part 4
Up to this point, we have been looking at the Blender 2.5 Python environment and to scripts to control windows, panels, and operators. We haven't done anything with actual Blender objects. It's time to see how to change and even create new Blender objects in Python.
Tuesday, March 2, 2010
2.50 Alpha 1 Python Part 3
In Part 2, we saw how windows are built from building blocks called Panels in Blender 2.5. You can lay out a panel and get data from the user. The next step is to actually do something with the data. For that, Blender 2.5 lets you define operators. Once you create an operator, it works anywhere within Blender. In this tutorial, we will create a simple operator, using the simple_operator.py template, one of a number of pre-designed templates that makes it easy for you to just add the functionality you need.
Tuesday, February 23, 2010
2.50 Alpha 1 Python Part 1
The purpose of this video is to give you an idea of the Python scripting environment in the new Blender 2.5, as well as to give you places to start if you either don't know Python at all, or are new to programming. Don't be afraid if you are not a programmer. Python is not a difficult language to learn. This tutorial is based on Blender Labrat (mfoxdogg) tour of the Python setup in Blender 2.5, and the Blender 2.5 Python API introduction at blender.org. I based this tutorial on Blender 2.5 Alpha 1, released in mid-February, 2010.
Monday, November 2, 2009
Blender Python 1: Setup
In addition to everything else, Blender is a programming environment, with Python its language of choice. Even if you don't plan on creating Python scripts in Blender, you should understand the Python environment. Python is how Blender talks to you. The purpose of this tutorial is to introduce you to Blender's Python environment. Understanding this environment should help you understand how Blender works as well. Of course, if you want to write Python scripts to make Blender behave exactly how you want, becoming familiar with the Blender Python environment is the first step. Even if you don't program, just understanding how to run a script will allow you to run Python programs, called scripts, written by others. For starters, go to the Blender Python Scripts Catalog.
Did you ever wonder what the purpose of the first window, and what the message:
Compiled with Python 2.6.2
Checking for installed Python... got it!
means? Does it have anything to do with big, poisonous snakes?
No. That first window is Blender's Python console. That message, which is what you should see as well, means that Blender found a good Python installation on your computer and that it could run Python programs. Blender also reports the Python version it found. You should, in general, go with the latest, most stable release of Python, which you can find at:
http://www.python.org
Python programs, incidentally, are called scripts and have the same meaning scripts have in the theatre. They're instructions for Blender. Blender uses Python scripts everywhere - to create objects, to move objects around in a scene, to animate objects, to add materials and textures, and so on.
Blender has two windows that were designed for interacting with Python. The Scripts window, the one with the snake icon, was designed to run Python scripts. The scripts are grouped into areas such as Mesh, Object, and UV. To run a script, select it from the menu. One commonly used script is to save the UV Face Layout to a file, for use with an image editor such as The Gimp or Photoshop. When you run the script, a screen with options for the UV image file displays. You can accept the defaults, or change them, and then press OK. The script will then run, asking you where you want to create the face layout file.
Blender has a built in Scripting setup, which works well if you want to create Python scripts in Blender. To access this window, go to the SR:5-Scripting view, which you can select from the dropdown menu. There are three windows: the 3D viewport, on the left; the Buttons Window, with the Scripts area highlighted, at the bottom; and the Text window at the right.
You have probably downloaded blend files with notes in the Text window. That's a great use for the text window. Another is to write Python scripts for Blender. Let's write our first Python script, a message to the Blender console announcing our presence. Type:
print "Ira was here."
in the Text window.
To run a script, you can either press Alt-P, or from the Text Menu, select Run Python Script. Press Alt-P. Seemingly, nothing happened. Blender is strangely silent if things go OK. Actually, the script run. To see the result, go to the Blender console, and the message "Ira was here." displays.
Blender will, however, tell you when you have made a programming error. Let's introduce an error. Text needs to be enclosed on each side by double quotes. Delete the last double quote. Press Alt-P. You get the following message:
Error: Python Script Error
Check console
Now that you have gotten this far, you know where to look. Click on the console window. You get the message:
SyntaxError: EOL while scanning string literal
File "Text" Line 1
and it shows you where it thought the error was. Actually, the error isn't quite where the message is pointing, but you have to give Blender a gold star for trying to be helpful.
Go back to the text window, add the double quote back, press alt-P, and check the console window. Everything is OK again.
Here are some formatting tips. Press the computer window icon. This makes the text window full screen. If your script is large, it's easier to have the window full screen so you won't have to scroll up and down as much. To return to the text window's original size, press the computer window icon (the screen is divided into 4 parts).
The next button displays line numbers along with the code. This can be very handy, especially considering that Blender's error messages give you the line number. Line numbers make it easier for you to get to a particular line.
The next window enables word wrap. Generally you don't want that button enabled when you are writing a script, although it is handy if you are writing notes or free form text in the window.
The "AB" button enables syntax highlighting. This is useful in coding to give you hints as to the proper syntax of a line of code.
The snake button enables Python text plugins. Text plugins can give the Text editor more power. You can find text plugins by going to the Text menu and selecting Text Plugins.
The Screen dropdown lets you change the font size. The code currently displays with a font size of 12. You can change it to 15 by selecting 15. Your choices depend on the fonts installed on your computer.
The Tab: selector lets you change the number of spaces that the editor inserts when you press the Tab key. Python syntax checks based on having the code properly tabbed. Making the indent larger helps the readability of the code as well. You can add a tab by placing the cursor at the beginning of the line and selecting Indent from the Format menu. You can also unindent.
Highlight the line "Ira was here". You can comment it out by selecting Comment from the Format menu. Commenting a line of code means that the code will not execute, i.e., run. To show this, press Alt-P to run the program. The message does not display on the console. You can uncomment by selecting Uncomment from the Format menu. Press Alt-P to run the program again. This time the message displays on the console.
You can create more than one script in a blend file. To do this, click the ADD NEW button. A new, blank text window, titled TEXT.001, displays. Enter a line of code
print "Message from Outer Space"
Press Alt-P and check the console. The message displays.
If you save the blend file (Control-W), all the text windows are saved. You can also save individual text windows by selecting the Save button from the Text window, or pressing Alt-S. Python files should be saved with an extension of .py.
I hope this gives you a good level of comfort with Blender's Python programming environment. I plan to do more tutorials on how to use Python in Blender. There is a lot to cover, much more than can be done in a 10 minute video. Happy Blending!
Subscribe to:
Posts (Atom)