Unformatted text preview:

Blender first steps with Python Thanks to Diamond Editions for authorizing online publication of this article first published in Linux Magazine N 73 Saraja Olivier olivier saraja linuxgraphic org English translation Ed Montgomery You have discovered thanks to Yves Bailly in GNU Linux Magazine N 68 article online at http www kafka fr net the possibilities offered by the coupling of Blender and Python in the framework of developing graphics So we are going to leave for the moment the scene description language of POV ray largely explored in previous issues in order to discover how the Blender Python interpreter allows us to approach the same level of power and versatility as the standalone version of Python But first of all as in all programs it is necessary to define objectives In the framework of learning python we are going to simply try to reproduce the opening default scene of Blender So we are going to learn to create a cube assign it a material then add a lamp and a camera Each of these objects will be in the exact same positions and orientations in particular the camera and the lamp as those of the default scene Figure 1 An easy to obtain objective Yes we will see how 1 A well configured python A large number of functions that I will describe are embedded in the python API of Blender The result is that with the actual scripts it is less often necessary to have a complete installation of python on the harddrive perhaps not even a separate python installation at all There are sometimes certain modules offered by Python that you will need such as the math module for example which we will marginally need today 1 1 Setting the Pythonpath Most distributions actually provide a version of python probably already installed and all that is required is to confirm that it is installed Then all you need to do is give your system the path which points to python In order to determine the path s there is a simple procedure to follow Open a console or term program and type the command python and then confirm this by pressing the Enter key Python 2 3 4 1 Feb 7 2005 15 50 45 GCC 3 3 4 pre 3 3 5 20040809 on linux2 Type help copyright credits or license for more information You have just entered the console mode for python Then type import sys followed by print sys path and you will obtain something like usr lib python23 zip usr lib python2 3 home olivier usr lib python2 3 plat linux2 usr lib python2 3 lib tk usr lib python2 3 lib dynload usr lib python2 3 site packages Now open the file bashrc from the root of your home directory home moi At the bottom of this file add the following lines export PYTHONPATH usr lib python23 zip usr lib python2 3 usr lib python2 3 plat linux2 usr lib python2 3 lib tk usr lib python2 3 libdynload usr lib python2 3 site packages Note the above separators no apostrophies and paths separated by colons this is the exact content dictated by the command print sys path 1 2 Specifying the default path for scripts Start Blender and click on the icon at the bottom of the 3D view indicating the window type for the viewport Choose User Preferences Figure 2 choose User Preferences in order to set the paths for your scripts The window changes appearance and this makes it possible to personalize the behaviour of Blender in several different categories View Controls Edit Methods Language Font Themes Auto Save System OpenGL and finally the category which interests us File Paths Click on this last button in order to discover all the default paths of Blender The last line second column sets the directory reserved for Python Now you only need to click on the small folder to open the mini navigator for files and directly select the folder where you will keep your python scripts Figure 3 and voila The python scripts path has been set Now all that remains to do is to return to the regular 3D view and pressing on CTRL U in order to save the user s preferences Thus the next time you launch Blender and each time you open a new file it will be placed in this folder 2 Preliminaries and basic advice I strongly advise that you start Blender from a terminal or console in order to see the messages attached to the console by the python interpreter in the course of executing your scripts In this way if your code contains syntax errors or unknown commands it will be easier for you to understand the problem and to debug it Once Blender has started separate the 3D view into two parts with a right click on the horizontal separation line between the 3D view and the command window the cursor will change shape Then choose Split Area and confirm with a left click Figure 4 preparing your work space In the right hand viewport open the text editor window SHIFT F11 and create a new file by using the combination ALT N or use the menu under the window File New Activate the attachment of line numbers by clicking on the icon essential this will prove to be very useful if not For the first few lines we are going to go quite easy First we will define the skeleton of our script 01 import Blender 02 from Blender import Nmesh Scene Object 03 Blender Redraw For the moment it appears that this program does absolutely nothing However you may execute the script by pressing the combination of ALT P while taking care that the mouse pointer is in the text editor window 3 Creation of a cube So what is the reason for this If python would permit us to perform some marvels something as simple as a command cube x1 y1 z1 x2 y2 z2 as is the custom with POV ray this would be wonderful In effect we do not work here with primitives but with some meshes And the creation of a mesh follows a coherent but demanding protocol We are going to illustrate with a simple case abundantly commented 3 1 Creation of a face or surface To begin we start simply Let s suppose that we want to construct a plane as shown in Figure 5 It consists of 4 points or vertices which joined together form a face It is then easy to build the coordinates of the 4 vertices For convenience we are going to number them from 1 to 4 or actually from 0 to 3 In reality computer scientists have some strange habits and they begin numbering from 0 rather than 1 but that just requires some practice to which one becomes rapidly accustomed vertex vertex vertex vertex 0 1 2 3 1 1 0 1 1 0 1 1 0 1 1 0 Now we are easily able to define our surface as that which relies on the points 0 to 3 face 1 0 1 2 3 Figure …


View Full Document

IUB CSCI-A 202 - Blender- first steps with Python

Documents in this Course
Load more
Download Blender- first steps with Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Blender- first steps with Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Blender- first steps with Python and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?