
First things first
Computers are very good but they cannot think by themselves, you have to tell them what to do on their language. And python is an wonderful high level open source language. Now you have Python and Blender. How these two work together?. Through Blender Python API, a collection of Python modules (libraries) that give access to part of the Blender's internal data and functions.
This is the first in a series of tutorials to be able to get started with Python scripting in Blender. You will go directly in "production" with a small and very useful script that rotates planes [tree cards for example] being perpendicular on camera in the first part, and continuing with the same script to make it work for animation [having the cards face the camera on every single frame], in second part.
Keep in mind that you need least these 2 links in order to fully understand how to write Python scripts in Blender:
http://docs.python.org/tut/tut.html Python tutorials
http://www.blender.org/documentation/242PythonDoc/index.html Blender Python API
You are not going to start from scratch because you have the script already thanks to Roland Hass, but you are going to extend it functionality.
What it does?
Open Blender and divide the screen in 2 vertical windows. On The right make it text and on left make it 3dViewport. In text window select the button who indicate 2 arrows and choose OPEN NEW and open ztrack.py file from Python folder. Erase the default cube and in front view add a plane. Go in top view and select the plane. In Text window RMB and choose Execute Script. You will notice that the plane now is perpendicular on camera. Shift+D copy the plane couple of time, select all the planes and run the script again. Now all the plane are perpendicular on camera. Make another copy of the camera and press Ctrl+NUMpad0 to change the view to Camera.001. Run the script again. The script is not gonna work for Camera.001. If you change in line 15 Camera with Camera.001 and run the script, this time will work.
Let's see what every line mean in this script.
Line 1 to 8 are comments and python ignore those lines.
Line 10 & 11 refer python modules with data who have to be imported into the script.
Line 13 define a list "obloop" with all selected objects. Somebody said that a list can be thought of as a series of boxes. In our case every box contain one selected object. If you have 3 planes, you have 3 boxes, box number 0 ,1 and 2 .
Line 15 assign a name to the object called "Camera"
Line 17 define a list "cameraloc" who contain x,y,z of Camera. So you have x = cameraloc[0], y = cameraloc[1] and z = cameraloc[2].
On line 19 For is a function who do a repetition for every object in the "obloop" list. In this function from line 19 to 29 you have 3 new function math.atan(dx/dy), getEuler and setEuler.
math.atan(dx/dy) is arctan(dx/dy)
getEuler get the rotation of the object in radians and setEuler rotate the object . What effectively happen between line 19 to 29 is getting x and y of every selected object, calculate the rotation to get the object perpendicular on camera (see below drawing) and rotate the object on z with the calculated value.
Improving the script
Is clear that this script works the best when you have only one camera. For other cameras you have to edit the script in order to use it. You are gonna change the script to work for every camera in the scene an to access the script from 3dwindows head menu Object>Script>ztrack.
I like to select all the card and the camera which i want the cards to "look at" and to apply the script. If I need another rendering from a second camera I will select again all the cards and the second camera and I run the script again and all this from 3dwindows directly. Selecting all the cards and last selecting the camera make the camera the active selected object and the first in the list of selected objected. The idea is to separate the camera from the list and to be able to work with the rest of the list.
Edit the script to become as below
First erase the line:
camera = Object.Get("Camera")
Edit the line 1 to 7. Those line instruct the blender to insert this script in its menu.
On line 22, now the list "obloop" will have a camera object too and this object is gonna be the first in list [will be the active object/the last selected].
On line 23 the variable "total" will get, with the function len() the numbers of selected objects.
The variable "camera" is gonna be assigned to the first object in the list, the active object.
From line 29 the loop is modified to get only the selected planes[1 to total] excepting the camera [first object in the "obloop" variable], to calculate the new Z angle and to set the planes to the calculated angle.
It is important to pay attention to the indentations all the lines have to start without any space and only those who are affected by the for loop have to be indented, other way you will got this error: SyntaxError: invalid syntax.
It is time to test the script now. Select the planes, last select the camera and run the script. If everything work go to File>SaveAs... from Text window header and save the file in /blender-2.43-windows/.blender/scripts/ as ztrack.py and make sure it have this name and extension. Change the Text window with Script window and go Scripts>UpdateMenus. Now you should be able to run the script from 3d window Objects>Script>ztrack.
LineDstudio.com is a web based studio who provide tools to enhance your interior and architectural design projects.
It's a affordable solution for architectural and interior designers who need the visualization of their projects.