In this Blender 2.5 video tutorial, I will be giving a full introduction to scripting in Blender Python.
In the first half of the video I will give an overview of the basics of Python in general, such as variables, for-loops, if-statements and functions.
The second half will focus on Blender’s bpy-module and it’s API, as well as performing some basic tasks like changing a Material’s Shader-type.











What if haven’t programmed/scripted before? Can I watch this or is it useless for such a person?
You need to know some of basics I think. and don’t worry cause it’s not that hard!
you may want to learn a little python first. there are lots of learning courses over the web!
by the way, Patrick has provided some basics of python in the first part of the video, so there wouldnt be any thing to worry about
Thanks. I’ll try.
Man i love your videos.Keep them coming!
thank you, i learned a lot!
I would like to see more videos about scripting!
thx Patrick Boelens
you change your mind a lot haha, thanks for the tutorial… im not afraid to try out the bpy module anymore
Haha, yup! I generally go by what I have on my prepared ‘cheat sheet’, but sometimes I suddenly think of something while recording that I feel would be much better instead, hence my abrupt indecisiveness sometimes. =)
wll, you could edit the video and make it so that changing your mind didn’t happen… Just a tip.
i like the change in your tutorials. Writing code is very straight forward, but not always a straight forward process, seeing someone who knows their craft, change their mind and mess up is good to see. when i watch perfectly edited tutorials, it leaves out learning from other peoples mistakes. Please keep them coming Patrick and do not change a thing!!!!
Lovely!
Thank you for that. I found this very useful, and I now feel a little more confident in exploring more detailed Blender Python scripting. Also, your older tutorials are becoming more clear to me now.
I look forward to your next video!
One of the best introduction to the Blender API ! Thanks.
very interesting. Looks easyer that AS3.
Great!!, I was waiting for an introductory tutorial about Python on Blender.
Thanks!!.
I didn’t know about the Toggle System Console feature so that was a big help! Was kind of cumbersome to start Blender with cmd.exe
Thanks so much for this helpful video. ^^
Yeah! I love you dude!:D
Tuple
but pronounced as “tjoepl”
I did not find it in my dictionary, but in Wikipedia :
Etymology
The term originated as an abstraction of the sequence: single, double, triple, quadruple, quintuple, sextuple, septuple, octuple, …, n‑tuple, …, where the prefixes are taken from the Latin names of the numerals. The unique 0‑tuple is called the null tuple. A 1‑tuple is called a singleton, a 2‑tuple is called a pair and a 3‑tuple is a triple or triplet. The n can be any nonnegative integer. For example, a complex number can be represented as a 2‑tuple, a quaternion can be represented as a 4‑tuple, an octonion can be represented as an octuple, (many mathematicians write the abbreviation “8‑tuple”) and a sedenion can be represented as a 16‑tuple.
Although these uses treat ‑tuple as the suffix, the original suffix was ‑ple as in “triple” (three-fold) or “decuple” (ten‑fold). This originates from a medieval Latin suffix ‑plus (meaning “more”) related to Greek ‑πλοῦς, which replaced the classical and late antique ‑plex (meaning “folded”).[1]
[edit]
Ha, cool! Thanks for the info!
How do I get the print(“Yes, it’s me!!”) to work it says its a script problem something about a unneeded indentation?
Hey! You’re an xkcd fan!
Oh, absolutely! It’s definitely my favourite web comic (followed closely by the completely different/ unrelated “Romantically Apocalyptic”)!
For me, it’s my 3rd favorite. Ever heard of the Oatmeal?
I can’t get enough of the Oatmeal
Yup, I have, but for some reason I never started actively following it. =P
Um, how do you enable something like Subsurface Scattering with Python?
bpy.data.materials['Material'].subsurface_scattering.use = True
That is what I came up with after a few minutes of poking around. Then you’d have to figure out how to access the different settings. (Hovering over them would be easiest, that is how I figured out how to turn SSS on)
Juhu! I was waiting for this sooooooooooo long!:D
In the near future, I hope you will make a suite to this introduction. I know all the basics of python, but this kind of tutorial is just giving me envy to do a lot more with python in Blender. Please, Mr Patrick Boelens, can you make a second tutoriel and developping more examples to show creation of objects in the Blender’s scene, only via the use of bpy API ? Another question : are bpy scripts also used in animation ? If yes, can you show some actual examples ? Thanks so much for this kind of tutorials, very very usefull !
Hey Gigante,
Thanks for your feedback; it’s much appreciated! Are there any specific subjects you’d like to see covered?
As for your animation question, how exactly do you mean ‘used in animation’? Are you thinking tools like Bone-renaming or perhaps more along the lines of Motion-Trail?
Here is example of it used in animation but I have an issue with it. How do I make the program stop at each frame long enough to see the results change? I couldn’t figure out how to set a keyframe for a buttom property so I put it in the for loop.
@Gigante: Take a look at the bpy.ops.anim section of the api for anim stuff.
import bpy
import random
def playback():
frame = 0
endframe = 30
bpy.ops.anim.change_frame(frame = frame) # make sure we are at frame zero
for i in range(frame,endframe+1): # add one because range doesn’t include last num
print(frame) # for testing purposes to make sure the frame is changing.
print(bpy.data.lamps['Lamp'].energy) # make sure engergy is changing
bpy.data.lamps['Lamp'].energy = random.random()
bpy.ops.anim.change_frame(frame = frame)
frame = frame + 1
playback()
Sorry for the multiple replies but I figured out I could import time and use time.sleep(1) to pause it for a second. My main issue now is I’m trying to figure out how to update Blender’s 3D View to show the results. I think you went over that in one of the other tutorials – the one on building custom objects I think.
I forgot to mention the goal of the program was to try to make a flickering light by randomly adjusting the energy.
Hey Jeff,
I’m actually not sure if there even is a way to force an update of a view straight from Python. I always just move the mouse in the area I wish to see changed. I’ll prod around a bit later tonight. =)
I figured it out. While poking around for an update function I found out how to set a key for the energy button. To set a key for the lamps energy you do the following:
bpy.data.lamps['Lamp'].keyframe_insert(data_path = “energy”)
I figured out the data path by right clicking on the energy button and clicking “Copy Data Path” So I loop through the frames setting a key on each frame then it was just a matter of playing the animation. Hovering over the play button showed that was as simple as doing this:
bpy.ops.screen.animation_play()
Here is my final code. (It’s probably going to mess up the identation)
import bpy
import random
frame = 0
endframe = 30
bpy.ops.anim.change_frame(frame = frame) # make sure we are at frame zero
for i in range(frame,endframe+1): # add one because range doesn’t include last number
# For loop sets random energy keyframes for ‘Lamp’
bpy.data.lamps['Lamp'].energy = random.random()
bpy.data.lamps['Lamp'].keyframe_insert(data_path = “energy”)
bpy.ops.anim.change_frame(frame = frame)
frame = frame + 1
bpy.ops.anim.change_frame(frame = 0) # make sure we are at frame zero
bpy.ops.screen.animation_play()
I ended up not needing the time module after all. I learned a lot from doing this!
Another question: How do you set up a particle system with specific settings? I managed to automate adding the vertex groups and the particle system, but can’t change any settings.
bpy.data.particles['ParticleSettings'].count = 10
There is an example of setting the number of particles to 10. In the python console if you type that minutes the “count = 10″ part and hit auto-complete you will get a list of more settings.
Spot on Jeff! As always, your help in replying to these comment inquiries is greatly appreciated! =)
(I hope this isn’t a duplicate)
Thanks for the help!
No problem. It doesn’t take me long to figure it out and I learn something by helping out.
It is pretty easy to find out what you need from within Blender which I think is really cool. If you use the scripting layout provided with Blender there is a bar at the top that is an info editor type. When you do something in the 3D view it will show that command in python up there. It is really helpful – I used it recently to figure out how to delete text from an text object.
I’ve learned quite a bit from just poking around in the python console and auto-completing to see the options. I’ve done that enough that I am starting to get a better idea of where something might be that I would need. Between that, the info bar, and hovering over something in a menu I usually only need to check the API doc to clarify something or see what other options are available.
Happy blending and pythoning(?)!
Isn’t the templates menu in the Text Editor a good place to start ?
At the time of recording Blender 2.59 hadn’t been released yet, and 2.58 didn’t have it so prominently available. If it did, I probably would’ve mentioned it, but the intention of this tutorial was mainly to show the basic principles of (Blender)Python and to give a very basic introduction to it. =)
Hey guys!
I’m using Blender for nearly 3 years now, and last week i started with some Python programming – Your Tutorials are always a good point to start. It’s fun to see Professionals at work!
Just wanted to say THANK YOU & keep up the good work!
Thanks for the tutorial on scripting..
Since I have watched – I have learned how to add my plugin to blender and add
a utility to the menu and is pretty cool..
This is just what I needed to get started..
I am going to test out making a scripted for making fire and smoke in blender
to start..
Thanks again.. and please list or post more tutorials..
Excellent tutorial – while I’m not new to programming I’m new to python so I got up to speed quickly thanks to this.
Great intro! thanks for that.
But I have to stress that when programming or scripting in python it is important to adhere to the Style guide Python is build around. It will improve script readability and clarity. You can find it here (http://www.python.org/dev/peps/pep-0008/). Maybe you can put it in the movie description.
This is in response to your remark of enabling the line wrap function. It goes against the idea of keeping a coherent style as the spec states a document shouldn’t exceed 79 symbols per line.
Thanks again for this remarkable video.
Very nice tutorial Patrick. Here’s one tip I think a lot might not know.
You can hover a button or menu item and ctrl+c to copy it’s bpy string.
here’s a few examples
Render image button
bpy.ops.render.render()
vertices selection mode
bpy.ops.wm.context_set_value(data_path=”tool_settings.mesh_select_mode”, value=”(True, False, False)”)
add to group
bpy.ops.object.group_link()
It doesn’t work with entry boxes or drop boxes, but seems to work with everything else in blender. Should speed up a beginners coding substantially and be of benefit to more experienced coders like Patrick.
Awesome tip! That is a lot quicker than hovering over it to find out the bpy string and you don’t have to worry about accidentally mistyping it.
Very grateful for this – at long last peeking under the bonnet (or hood). Only one minor point, so trivial I almost feel bad about mentioning it. It’s not “import bee-pee-aye”, it’s “import bee-pee-why”. (I actually tend to say “import bee pie”…)
Thank you so much for the tutorial. I learned much more from you than most other people!
I have added this tutorial to the Blender Wiki : http://wiki.blender.org/index.php/Doc:2.6/Manual/Extensions/Python#Getting_Started_-_External_links
Is it possible to use a python 2.6 interpreter?
As a hobiest c# programmer, been toying with the idea of learning scripting as it may be useful.
I have a query with regards to scripting in the game engine: does it only use python or can other languages be used?
As far as I know, you can only script with python. It is as easy as any language to learn, specifically if you already know how to program. Don’t fear.
Python is a good language to learn beyond blender. Gimp uses it as well for instance.
HOW TO OPEN SYSTEM CONSOLE IN LINUX
Syntax for opening a system console in Linux (Bash) is as follows (from a terminal [command line]):
blender
The term should be replaced by the location of a .blend file and is an optional argument. If you include it, it will open the .blend file. If you don’t, it will open a fresh blender file. This should be the same for Mac OS as well, though I’m not sure cause I don’t use it.
Additionally, if the blender executable is _not_ in /usr/bin (or other similar location — this is a very common practice with blender), you may have to specify the location of the blender executable.
For instance if the executable is sitting on the desktop:
~/Desktop/blender
Where the tilde represents the user’s home directory and again is the location of a .blend file and is an optional argument.
Part of the two lines of syntax I used were parsed out (thus corrupting the message
) Therefore, I’ll repost.
HOW TO OPEN SYSTEM CONSOLE IN LINUX
Syntax for opening a system console in Linux (Bash) is as follows (from a terminal [command line]):
blender .blend_File_Location
The term .blend_File_Location should be replaced by the location of a .blend file and is an optional argument. If you include it, it will open the .blend file. If you don’t, it will open a fresh blender file. This should be the same for Mac OS as well, though I’m not sure cause I don’t use it.
Additionally, if the blender executable is _not_ in /usr/bin (or other similar location — this is a very common practice with blender), you may have to specify the location of the blender executable.
For instance if the executable is sitting on the desktop:
~/Desktop/blender .blend_File_Location
Where the tilde represents the user’s home directory and again .blend_File_Location is the location of a .blend file and is an optional argument.
This video tutorial is simply one of the best Python tutorial for Blender. Do you have full DVD discussing Python for Blender like this?
Thanks/
hi can anyone help me, i want to basically get a python script to do the same thing that an action actuator would ie play an animation when a keyboard event is pressed
Very Nice tutorial !!
You make a mistake at 25:00.
You write : for key in GSOC:
if(GSOC[key]) is “Camera Tracker”:
print(person)
But you have to print the key so : print(key)
dave