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.

You must be logged in to upload images. Register

Discussion

59 Responses to “Introduction to Scripting with Python in Blender”
  1. Posts: 12

    What if haven’t programmed/scripted before? Can I watch this or is it useless for such a person?

    #
    1
    Aug 26, 2011 at 1:06 pm
    • Posts: 2

      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!

      #
      1.1
      Aug 26, 2011 at 1:38 pm
    • Posts: 2

      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 :)

      #
      1.2
      Aug 26, 2011 at 1:45 pm
      • Posts: 12

        Thanks. I’ll try.

        #
        1.2.1
        Aug 26, 2011 at 2:05 pm
  2. Posts: 5

    Man i love your videos.Keep them coming!

    #
    2
    Aug 26, 2011 at 1:06 pm
  3. MoritzH
    Posts: 1

    thank you, i learned a lot!
    I would like to see more videos about scripting!

    #
    3
    Aug 26, 2011 at 1:45 pm
  4. Posts: 136

    thx Patrick Boelens
    :D

    #
    4
    Aug 26, 2011 at 2:53 pm
  5. Marwin
    Posts: 7

    you change your mind a lot haha, thanks for the tutorial… im not afraid to try out the bpy module anymore :)

    #
    5
    Aug 26, 2011 at 3:42 pm
    • Posts: 225

      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. =)

      #
      5.1
      Aug 27, 2011 at 3:03 pm
      • FreeMind
        Posts: 45

        wll, you could edit the video and make it so that changing your mind didn’t happen… Just a tip.

        #
        5.1.1
        Aug 28, 2011 at 2:57 am
      • Posts: 1

        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!!!!

        #
        5.1.2
        Oct 19, 2011 at 4:52 pm
  6. Posts: 59

    Lovely! :D

    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! :)

    #
    6
    Aug 26, 2011 at 4:09 pm
  7. Posts: 11

    One of the best introduction to the Blender API ! Thanks.

    #
    7
    Aug 26, 2011 at 4:16 pm
  8. Posts: 20

    very interesting. Looks easyer that AS3.

    #
    8
    Aug 26, 2011 at 5:28 pm
  9. Posts: 43

    Great!!, I was waiting for an introductory tutorial about Python on Blender.
    Thanks!!.

    #
    9
    Aug 26, 2011 at 5:29 pm
  10. Posts: 256

    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

    #
    10
    Aug 26, 2011 at 6:12 pm
  11. Posts: 8

    Thanks so much for this helpful video. ^^

    #
    11
    Aug 26, 2011 at 10:17 pm
  12. Posts: 22

    Yeah! I love you dude!:D

    #
    12
    Aug 27, 2011 at 3:21 am
  13. Posts: 21

    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]

    #
    13
    Aug 27, 2011 at 6:16 am
    • Posts: 225

      Ha, cool! Thanks for the info!

      #
      13.1
      Aug 27, 2011 at 3:05 pm
      • Posts: 3

        How do I get the print(“Yes, it’s me!!”) to work it says its a script problem something about a unneeded indentation?

        #
        13.1.1
        Sep 16, 2011 at 6:30 am
  14. B M
    Posts: 15

    Hey! You’re an xkcd fan!

    #
    14
    Aug 27, 2011 at 7:58 am
    • Posts: 225

      Oh, absolutely! It’s definitely my favourite web comic (followed closely by the completely different/ unrelated “Romantically Apocalyptic”)!

      #
      14.1
      Aug 27, 2011 at 3:08 pm
      • B M
        Posts: 15

        For me, it’s my 3rd favorite. Ever heard of the Oatmeal?

        #
        14.1.1
        Aug 27, 2011 at 3:25 pm
      • Posts: 2965

        I can’t get enough of the Oatmeal :)

        #
        Aug 29, 2011 at 9:59 am
      • Posts: 225

        Yup, I have, but for some reason I never started actively following it. =P

        #
        14.1.2
        Aug 27, 2011 at 3:52 pm
  15. B M
    Posts: 15

    Um, how do you enable something like Subsurface Scattering with Python?

    #
    15
    Aug 27, 2011 at 9:37 am
    • Posts: 256

      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)

      #
      15.1
      Aug 27, 2011 at 10:09 am
  16. Posts: 22

    Juhu! I was waiting for this sooooooooooo long!:D

    #
    16
    Aug 27, 2011 at 11:40 am
  17. Posts: 11

    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 !

    #
    17
    Aug 27, 2011 at 2:33 pm
    • Posts: 225

      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?

      #
      17.1
      Aug 27, 2011 at 3:15 pm
      • Posts: 256

        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()

        #
        17.1.1
        Aug 28, 2011 at 11:11 am
      • Posts: 256

        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.

        #
        17.1.2
        Aug 28, 2011 at 12:07 pm
      • Posts: 225

        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. =)

        #
        17.1.3
        Aug 28, 2011 at 2:22 pm
      • Posts: 256

        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!

        #
        17.1.4
        Aug 28, 2011 at 11:36 pm
  18. B M
    Posts: 15

    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.

    #
    18
    Aug 27, 2011 at 3:24 pm
    • Posts: 256

      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.

      #
      18.1
      Aug 27, 2011 at 3:42 pm
      • Posts: 225

        Spot on Jeff! As always, your help in replying to these comment inquiries is greatly appreciated! =)

        #
        18.1.1
        Aug 27, 2011 at 3:53 pm
  19. B M
    Posts: 15

    (I hope this isn’t a duplicate)

    Thanks for the help!

    #
    19
    Aug 27, 2011 at 6:39 pm
    • Posts: 256

      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(?)!

      #
      19.1
      Aug 28, 2011 at 2:06 am
  20. Posts: 21

    Isn’t the templates menu in the Text Editor a good place to start ?

    #
    20
    Aug 28, 2011 at 3:13 pm
    • Posts: 225

      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. =)

      #
      20.1
      Aug 29, 2011 at 7:31 am
  21. Michael
    Posts: 1

    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!

    #
    21
    Aug 29, 2011 at 4:00 pm
  22. Posts: 42

    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..

    #
    22
    Aug 30, 2011 at 12:41 pm
  23. DigbyChickenCeasar
    Posts: 1

    Excellent tutorial – while I’m not new to programming I’m new to python so I got up to speed quickly thanks to this.

    #
    23
    Aug 30, 2011 at 1:04 pm
  24. Rio
    Posts: 1

    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.

    #
    24
    Sep 1, 2011 at 9:06 am
  25. Posts: 40

    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.

    #
    25
    Sep 2, 2011 at 9:54 pm
    • Posts: 256

      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.

      #
      25.1
      Sep 2, 2011 at 10:18 pm
  26. Posts: 1

    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”…)

    #
    26
    Sep 14, 2011 at 1:26 pm
  27. Datawraith
    Posts: 1

    Thank you so much for the tutorial. I learned much more from you than most other people!

    #
    27
    Sep 14, 2011 at 3:06 pm
  28. Jean Montambeault
    Posts: 1
    #
    28
    Jan 31, 2012 at 12:51 pm
  29. Posts: 8

    Is it possible to use a python 2.6 interpreter?

    #
    29
    Jan 31, 2012 at 6:20 pm
  30. Posts: 96

    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?

    #
    30
    Feb 3, 2012 at 9:15 am
    • Posts: 8

      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.

      #
      30.1
      Feb 3, 2012 at 1:01 pm
  31. Posts: 8

    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.

    #
    31
    Feb 4, 2012 at 6:40 pm
    • Posts: 8

      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.

      #
      31.1
      Feb 4, 2012 at 6:43 pm
  32. Posts: 5

    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/

    #
    32
    Feb 23, 2012 at 1:52 am
  33. help!
    Posts: 1

    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

    #
    33
    Aug 4, 2012 at 5:11 am
  34. Dave
    Posts: 1

    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

    #
    34
    Sep 12, 2012 at 12:30 pm

Leave a Comment

You must be logged in to post a comment.