In this Blender 2.5 video tutorial, we will be taking a look at how to use sound to drive an object’s scale.

We will be building a ‘field’ of cubes, each of which will pulse along to certain frequencies of a song.

What is covered in this tutorial:
- Baking sound to F-curves.
- Programatically creating an N-by-M matrix (field/ grid)
- Setting a keyframe and baking F-curves from Python
- Finding out which frequencies to use using Audacity

Note: In recent SVN versions the API has been updated. If you’re using one of these later versions please be sure to replace “bpy.ops.object.scale_apply()” with “bpy.ops.object.transform_apply(scale=True)

You must be logged in to upload images. Register

Discussion

291 Responses to “Building an Audio Visualizer in Python”
  1. Posts: 66

    You got the author wrong again :P

    This sounds like an awesome tutorial, though.

    #
    1
    Jun 2, 2011 at 5:35 pm
    • Posts: 1761

      Ah man! Fixed again :(

      -Jonathan

      #
      1.1
      Jun 2, 2011 at 5:48 pm
    • Feg940
      Posts: 1

      Hey i have this problem
      (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-4:truncated\UXXXXXXXX escape when i write de file path. Help PLZ!!

      #
      1.2
      Sep 24, 2011 at 10:29 am
      • Mira
        Posts: 1

        Is there a possibility to multiply/scale the F-Curves?

        #
        1.2.1
        Oct 12, 2011 at 11:36 am
      • aphexx
        Posts: 1

        i got the same error..plz help m8 :)

        #
        1.2.2
        Feb 16, 2012 at 8:29 am
      • Posts: 1

        I think you have to use / instead of \ for the path

        #
        1.2.3
        Feb 20, 2012 at 7:43 pm
      • Posts: 43

        Stefan is correct, I encountered the same problem because I am running windows the / in the hierarchie appears as a \ and blender only reads /.

        #
        1.2.4
        Apr 13, 2012 at 9:02 am
  2. Posts: 14

    Awesome tutorial. I have been wanting to do some python in blender and didn’t really know where to start. This was a great way for me to get into it :D . Keep up the great work!

    #
    2
    Jun 2, 2011 at 6:15 pm
    • Posts: 223

      Awesome; welcome aboard the Python train! =D

      #
      2.1
      Jun 3, 2011 at 6:55 am
  3. Posts: 3

    haha nice,

    I wanted to do something like this a while ago, now I have it :)

    Thank you!

    #
    3
    Jun 2, 2011 at 6:42 pm
  4. Posts: 14

    awesome I’ve always wanted to do this, but since the video isn’t working could we have a link to it?

    #
    4
    Jun 2, 2011 at 9:31 pm
  5. Posts: 17

    Its saying that its having a problem with this line… bpy.context.active_object.animation_data.action.fcurves(0).lock = true

    Any ideas?

    #
    5
    Jun 2, 2011 at 10:31 pm
    • bendicott
      Posts: 3

      same problem, but i wrote [] instead of ()

      bpy.context.active_object.animation_data.action.fcurves[0].lock = true

      #
      5.1
      Jun 3, 2011 at 1:26 am
      • Posts: 12

        Have you tried using “True” with a capital T?

        #
        5.1.1
        Jun 3, 2011 at 5:14 am
    • Posts: 17

      Yes i did try the caps True and it still didnt work.

      #
      5.2
      Jun 3, 2011 at 4:24 pm
  6. Posts: 22

    when I run the script with exactly what you have written down, i only get 4 cubes in a row, no colums. I don’t know what i am doing wrong. there is no error message either

    #
    6
    Jun 2, 2011 at 11:14 pm
    • Posts: 223

      That’s odd. It’s sort of hard to find the cause without seeing the script though. Could you upload it to pastebin.com or pasteall.org?
      My first thought would be that you forgot a “c += 1″ or “r += 1″.

      #
      6.1
      Jun 3, 2011 at 6:45 am
    • Posts: 1

      I seem to be having the same problem. My script looks completly identical and I’m only using the blender 2.60.3
      Here is my script so far.
      ——————————————————-

      import bpy

      rows = 5
      columns = 5

      r = 0
      c = 0

      for i in range(0, rows*columns):
      if c == columns:
      r += 1
      c = 0

      bpy.ops.mesh.primitive_cube_add(location = (r*2, c*2, 0))

      c += 1

      ———————————————

      #
      6.2
      Feb 18, 2012 at 4:57 am
  7. Posts: 164

    Can I make a job out of doing tutorials all day long??! There’s never enough time!!! :D

    Jeremy Deighan

    #
    7
    Jun 3, 2011 at 12:18 am
  8. Posts: 7

    Thanks man!!! I was looking how to do this in blender, i find a tutorial for cinema 4d, but i love blender…so thanks a lot

    #
    8
    Jun 3, 2011 at 1:04 am
  9. Posts: 26

    i´ve got a problem with the last line of the script (spiral(rows, columns)
    it´s says python script fail

    #
    9
    Jun 3, 2011 at 1:59 am
    • Posts: 223

      Could you tell me the exact error message please? If it’s an indentation problem, you can just go to “Format > Convert Whitespace > To Spaces”. Also, from what you’ve written here, the parenthesis at the start is redundant.
      spiral(rows, columns) should work.

      #
      9.1
      Jun 3, 2011 at 6:48 am
      • Posts: 26

        it says Error: Python script fail, look in the console for now

        #
        9.1.1
        Jun 3, 2011 at 7:30 am
      • Posts: 223

        Could you paste what it says in the console? It should give you a line number and a description of the error.

        #
        9.1.2
        Jun 3, 2011 at 7:32 am
      • Posts: 26

        there is nothing else in the console, just the usual stuff.:(

        #
        9.1.3
        Jun 3, 2011 at 7:40 am
      • Posts: 223

        That’s really weird. Would you mind uploading your script to pasteall.org or pastebin.com so I can take a look at it?

        #
        9.1.4
        Jun 3, 2011 at 8:10 am
      • Posts: 26
        #
        9.1.5
        Jun 3, 2011 at 8:28 am
      • Posts: 15

        You forgot an underscore at line 31:
        bpy.context.area.type = ‘GRAPH EDITOR’
        (it is missing between GRAPH and EDITOR)
        then it should work. At least it did on my machine.

        #
        9.1.6
        Jun 3, 2011 at 9:35 am
      • Posts: 223

        Thanks for jumping in Dudeplayer; you’re absolutely right. =)

        #
        9.1.7
        Jun 3, 2011 at 10:08 am
      • Posts: 26

        Thanks for the help^^

        #
        9.1.8
        Jun 3, 2011 at 10:19 am
      • Posts: 15

        np
        two tips would be:
        -to indent multiple lines just select all and then hit tab
        -you could have added bpy.context.area.type = ‘TEXT_EDITOR’ at the end so you dont have to jump back to the text editor manualy
        but great tutorial, I was looking for something like this.
        cheers, and happy blending,

        dueplayer

        #
        9.1.9
        Jun 3, 2011 at 10:20 am
  10. DingTo
    Posts: 3

    This is a very cool tutorial. Thanks a lot for it! Will try it out!!

    #
    10
    Jun 3, 2011 at 3:27 am
    • WTomi
      Posts: 2

      Haha DingTo,
      just asked you a couple days ago, how to start with Python and there is already a tutorial coming here at BlenderCookies :-)
      That’s great.

      Tomi

      #
      10.1
      Jun 5, 2011 at 7:12 am
  11. Posts: 12

    This is really nice! I’d like to see more videos on Python, too.
    tx

    #
    11
    Jun 3, 2011 at 5:15 am
    • Posts: 12

      Btw, how did you create that cool lightning for the picture?

      #
      11.1
      Jun 3, 2011 at 5:16 am
      • Posts: 223

        I gave the cubes a slightly glossy, raytraced transparent material with a bit of an emit value, turned on Indirect Lighting and placed a simple Point Light at the center of the whole thing. =)

        #
        11.1.1
        Jun 3, 2011 at 6:52 am
  12. Enrique
    Posts: 3

    Great tutotial!!!

    #
    12
    Jun 3, 2011 at 6:01 am
  13. Martin Lindelöf
    Posts: 30

    Patrick Booleans :)

    #
    13
    Jun 3, 2011 at 7:34 am
    • Posts: 223

      I better be a True one then. ;) =P

      #
      13.1
      Jun 3, 2011 at 10:01 am
  14. timmy
    Posts: 5

    THANKS !!

    #
    14
    Jun 3, 2011 at 7:40 am
  15. Posts: 15

    Patrick,
    thanks a lot for the tut, I do work with sound for living and I think that this note could help in the visualization.
    The range hearing frecuencies that the human ear can hear is from 20 Hz to 20 KHz, the thing is, that the representation of this scale is logarithmical usually, no lineal. Remember when you look at a equalizer, that you can find in the left most side of the scale 20 Hz and in the right side 20 KHz, but in the middle there’s no 10 K, but there’s always 1 KHz. I can’t understand why Audacity developers, that in fact are doing a great job, didn’t have this in mind when designing the audio spectrum display.
    As I started that this’ll help with the viz it’s because all the cubes will interact smoothly, and not lots of them very flat and some very high.
    Sorry I can’t help with the script, as I told you I’m a sound designer for living, and a enthusiastic hobby man of blender.

    #
    15
    Jun 3, 2011 at 8:13 am
    • Posts: 223

      Ah, I see what you mean! My knowledge of audio is pretty much non-existent so I didn’t know that; thanks a bunch! I’ll be sure to keep this in mind. I believe Audacity does have an option for displaying the spectrum logarithmically though. =)

      #
      15.1
      Jun 3, 2011 at 10:05 am
    • Christopher Baker
      Posts: 3

      For anybody interested, I have modified the script to use a logarithmic breakdown.

      Firstly, you need to:

      import math

      Then define a few extra variables at the top, before the loop:

      low = 20
      high = 20000

      start = low
      c = low
      k = math.log(high/low)/(rows*cols)

      Modify low and high to be the low and high frequencies. DO NOT set low to 0. it must be 1 or greater.

      Then, in the loop, use this code:

      end = c * math.exp(k * i)
      bpy.ops.graph.sound_bake(filepath=song, low=start, high=end)
      start = end

      If you are using the spiral code, you also need to make a small change. The spiral loops over more spaces that it actually uses (it loops over max(X,Y)**2 which would be bigger than X*Y.

      Right before the loop, put i = 0 Then replace the variable i in the for loop with dumb. Then right inside the do stuff section, put i+=1 This will increase i only when it is being used, not on the ones that it is skipping as it did before. If your rows and cols are the same, you can skip this step, but keep it in mind if you change it later.

      You can see a render using this equation at http://www.youtube.com/watch?v=EbxH19J5NV4

      #
      15.2
      Jun 30, 2011 at 6:43 pm
      • Alan
        Posts: 1

        @ Christopher I get a problem with the last line spiral(rows, columns) when I run it.
        Also wasn’t sure what exactly the “loop” is, would that be the:
        _________________
        def spiral(X, Y):
        x = y = 0
        dx = 0
        dy = -1
        for i in range(max(X, Y)**2):
        if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
        __________________
        As a result i wasn't sure where to place several of the pieces.
        New to python and would appreciate a proof-read of my script.

        http://www.pasteall.org/25316/python

        #
        15.2.1
        Oct 1, 2011 at 9:46 pm
  16. ni-ko-okin
    Posts: 3

    @patrick at 32:30 you’re indenting the whole block manually. you can do it much faster by selecting the block and then press tab.

    niko
    .

    #
    16
    Jun 3, 2011 at 8:52 am
    • Posts: 223

      Ah, you’re right; thanks! I tend to work a lot in Unitron lately, which is one of the few editors that doesn’t support this little trick. Must’ve had my mind at that at the time or something. o.o

      #
      16.1
      Jun 3, 2011 at 10:07 am
  17. Posts: 4

    Nice tutorial but, unfortunately, don’t work on my pc :(

    The error is:
    Microsoft Visual C++ Runtime Library
    Runtime Error!
    This application has requested the Runtime to terminate it in an unusual way.

    then blender quit…
    Any idea to fix it???

    Thanks ;)

    P.S. My s.o. is Windows XP SP3

    #
    17
    Jun 3, 2011 at 9:42 am
    • Posts: 1761

      You probably need to install the Microsoft Visual C++ Redistributable 2008 package. You can download it here: http://www.microsoft.com/downloads/en/details.aspx?familyid=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en

      -Jonathan

      #
      17.1
      Jun 3, 2011 at 9:45 am
      • Posts: 4

        Hi Jonathan :)

        thanks for the tip, I tried but didn’t work!
        (I think blender uses his own version of C++ library in the installation dir!)

        But after some experiments I found the pitfall!

        Blender crash if in the ‘bpy.ops.graph.sound_bake’ filepath there is an
        element (dir or file) that start with a number!!!

        In my case I had:
        “E:\3D\_Blender2 take me to the bonuslevel because i need an extralife.mp3″

        this crash for the ’3D’ folder *and* the ’02′ at the start of the song title!

        I hope this will help someone!

        Ciao ;)

        #
        17.1.1
        Jun 3, 2011 at 5:05 pm
      • bendicott
        Posts: 3

        thanx, i had this problem. My song started with 2….

        #
        17.1.2
        Jun 4, 2011 at 2:03 am
      • wischmob
        Posts: 1

        Got the same probleme. Thanks;)

        #
        17.1.3
        Jun 7, 2011 at 5:36 am
  18. Matthew
    Posts: 16

    OMG this is so geeky i just soiled my pants with binary, excuse me while i go change them!

    Great tutorial!

    #
    18
    Jun 3, 2011 at 10:46 am
    • Posts: 223

      Be sure to wash out all the dirty Bits later! ;) [/geekjoke]

      Glad you liked the tutorial! =)

      #
      18.1
      Jun 3, 2011 at 10:56 am
  19. Chaosbadger
    Posts: 2

    When I click run script nothing happens

    #
    19
    Jun 3, 2011 at 10:54 am
    • Posts: 223

      Hey Chaosbadger,

      Did you make sure to call the spiral-function using spiral(rows, columns)?

      #
      19.1
      Jun 3, 2011 at 12:10 pm
      • Chaosbadger
        Posts: 2

        Yep

        #
        19.1.1
        Jun 3, 2011 at 12:16 pm
      • Posts: 223

        In that case, could you please upload the script to pasteall.org or pastebin.com so I could take a look at it?

        #
        19.1.2
        Jun 3, 2011 at 2:43 pm
      • Chaosbadger
        Posts: 6
        #
        19.1.3
        Jun 4, 2011 at 6:00 am
      • Chaosbadger
        Posts: 6

        http://pastebin.com/p7qwdfcr
        I am using blender 2.57

        #
        19.1.4
        Jun 4, 2011 at 7:05 am
  20. DingTo
    Posts: 3

    Hey Jonathan, just a note: The Python code does not work in latest SVN versions.
    bpy.ops.object.scale_apply()
    has been replaced with
    bpy.ops.object.transform_apply(scale=True)

    DingTo

    #
    20
    Jun 3, 2011 at 10:54 am
    • Posts: 223

      Gaah, another API change?! x.x Thanks for the heads up Thomas!

      #
      20.1
      Jun 3, 2011 at 10:58 am
      • DingTo
        Posts: 3

        You’re welcome! Great tutorial, the first one I bought here on this site! I love Audio! :)

        #
        20.1.1
        Jun 3, 2011 at 11:02 am
  21. --------
    Posts: 50

    thats what I was waiting for I’m saying this even I havent seen the video yet

    #
    21
    Jun 3, 2011 at 2:00 pm
  22. CMITZ
    Posts: 14

    Hey,
    I was trying my first python scripting ever… and actually all of it seems pretty obvious.

    But I’ve got a small problem here:
    I use this music to bake on.
    http://www.newgrounds.com/audio/listen/418830

    It gives this error: Codec can’t decode bytes in position 2-4: truncated \UXXXXXXXX escape

    Anybody got an idea?

    +Casper

    #
    22
    Jun 3, 2011 at 3:19 pm
    • CMITZ
      Posts: 14

      seems to not work with Pornophonique’s music either, so the error is not file-related….
      damn, I thought I was doing things right.
      anyway, here’s my script so far:
      http://www.pasteall.org/22159/python

      #
      22.1
      Jun 3, 2011 at 3:33 pm
    • CMITZ
      Posts: 14

      uuh, It might look like I’m just spamming here right now, but that’s not the case :P

      I now have this, which gives as only error “Python script fail, look in the console for now…”
      http://www.pasteall.org/22162/python
      Ideas?

      #
      22.2
      Jun 3, 2011 at 4:01 pm
      • Posts: 223

        Hey CMITZ,

        Glad to see you’re taking an interest in Python! There were a couple of issues with your script, all of which I’ll explain below. However, please note that a more descriptive and meaningful error-message appears in the console accompanying blender. That said, here were the 3 issues:

        When you were locking the x and y fcurves, you were using parenthesis () instead of square brackets []. You were also setting them to ‘true’, instead of ‘True’.
        Lastly, you made a typo twice, typing ‘curxor_location’ instead of ‘cursor_location’.

        I also had to change back transform_apply to scale_apply in my case, but this is fully dependant on which version of Blender you’re using.

        Hope this was helpful and let me know if you need any further assistance!

        -Patrick

        #
        22.2.1
        Jun 3, 2011 at 4:38 pm
      • CMITZ
        Posts: 14

        ow, ok, that’s kind of stupid of me :P
        I’ll be more careful next time ;)

        and indeed I had to change transform_apply(scale=True) back to scale_apply()

        But now my blender just keeps crashing
        using official release of Blender 2.57b :)

        #
        22.2.2
        Jun 3, 2011 at 5:27 pm
      • Posts: 223

        Hmm, perhaps you’re trying to create too many cubes at once? I haven’t done any real testing on this, but the script seems to be pretty heavy on the resources. Perhaps you could try it with a 1-by-1 or 2-by-2 grid to see if this is the problem?

        #
        22.2.3
        Jun 4, 2011 at 6:36 am
      • CMITZ
        Posts: 14

        Hey Patrick,

        Even on a 1-by-1 grid it crashes…
        And I know for sure that my computer ain’t thát bad….
        could you look into it? (don’t forget to change “bpy.ops.transform_apply(scale=True)” to “bpy.ops.scale_apply()”) =)

        if it doesn’t work I think I don’t mind again…

        One last thing:
        I looked trough the comments, and I must say that your support on this topic is marvellous, or any word in english that explains: “very good” ;)

        #
        22.2.4
        Jun 5, 2011 at 10:32 am
      • CMITZ
        Posts: 14
        #
        22.2.5
        Jun 5, 2011 at 10:33 am
      • Posts: 223

        I feel sort of bad for saying this after your praise on my support (thanks for that!), but I’m afraid I have no idea why it’s not working. =/ I’ve tried your script on both the official release of 2.57b and a newly downloaded version of r37026 and they both seemed to work flawlessly.
        One last thing I could think of would be to try a different audio file, but other than that, I’m clueless. =/ Perhaps you could try asking around on IRC/ BlenderArtists or trying the script on a different machine if you have access to one?
        Let me know if you figured it out; I’m really curious myself as well now. =P

        #
        22.2.6
        Jun 5, 2011 at 11:27 am
      • Posts: 2

        Hi CMITZ,

        you may have an increased “end frame” (since most songs last longer than 250 frames). I got kind of the same problem. After hitting “Run Script” Blender kept crashing. Changing “end frame” back to 250 fixed it at least for me.
        After baking is done, you can adjust the end frame value back again.

        greetz,

        marschl

        #
        22.2.7
        Jun 6, 2011 at 2:14 am
      • CMITZ
        Posts: 14

        I would have loved you if you were right :)
        For 1 moment I actually thought it would help, because indeed I did set the end frame to something at 8000 (end of song), but then I remembered I even tried the script with the official build with the factory settings. (so: end frame 250)

        Though with a little hope I just tried it again but no luck =)

        Anyway, thanks for your reaction Marschl!

        #
        22.2.8
        Jun 6, 2011 at 12:42 pm
  23. swv
    Posts: 2

    Great tutorial, my first to Python. I have one question, how add color to animation in the Python?

    #
    23
    Jun 3, 2011 at 3:42 pm
    • Posts: 223

      You can just insert a keyframe for any specific channel (ie: diffuse_color) and bake the curve to that. =)

      #
      23.1
      Jun 3, 2011 at 4:39 pm
      • Ethan Jurman
        Posts: 8

        how would the python code look for having the emit value of a new material baked to sound, instead of scaling (or with scaling)?

        #
        23.1.1
        Jun 4, 2011 at 7:33 am
      • Posts: 223

        Hey Ethan,

        I’ve uploaded the script to bake it to the emit channel here: http://www.pasteall.org/22189/python

        Hope this helped! =)

        #
        23.1.2
        Jun 4, 2011 at 9:13 am
      • Ethan Jurman
        Posts: 8

        after setting the material name to “Material” the console gave me this error…

        Traceback (most recent call last):
        File “/home/ethan/Desktop/ethans/soundvisulizer.blend/Text.001″, line 42, in
        File “/home/ethan/Desktop/ethans/soundvisulizer.blend/Text.001″, line 26, in spiral
        KeyError: ‘bpy_prop_collection[key]: key “Material” not found’

        #
        23.1.3
        Jun 4, 2011 at 9:56 am
      • Ethan Jurman
        Posts: 8

        oh wait… I forgot to make a material…
        though it still has an error

        File “/home/ethan/Desktop/ethans/soundvisulizer.blend/Text.001″, line 35, in spiral
        AttributeError: ‘NoneType’ object has no attribute ‘action’

        and will this be able to create a new material with a every cube… so the emit matches the freq?

        #
        23.1.4
        Jun 4, 2011 at 10:01 am
      • Posts: 223

        Hmm, that’s weird. I’ve been playing with this for the past 20 minutes and I can’t figure out what’s causing that error. Creating a new material and using that seemed to fix it. If the problem persists I’d advice you to try asking the people over at BlenderArtists.
        And no, I forgot to take multiple materials into account. You can easily create these using ‘bpy.ops.material.new()’ and then setting it this latest material by using ‘bpy.context.active_object.active_material = bpy.data.materials[len(bpy.data.materials)-1]‘

        #
        23.1.5
        Jun 4, 2011 at 10:57 am
      • JRJurman
        Posts: 9

        I’ve been fiddling around with it, trying to get a z scale and and emit baked to the sound, however I’m having trouble getting the emit to lock it’s animation… Also, the emit seems too small to be noticeable, akin to the z-scale problem which was fixed by making the z axis 5x bigger.

        NB: my code has a couple of prints to show progress and increments the frequency by a formula Ethan came up with… I don’t quite get it completely, lol.

        http://www.pasteall.org/22202/python

        #
        23.1.6
        Jun 4, 2011 at 3:20 pm
  24. Makers_F
    Posts: 3

    Really nice.
    But as soon as i saw it, i thought how to do this in realtime. Scaling the cubes is a no problem, but accessing the audio data is a bit bigger one.. Audspace doesn’t help in this case, but for sure blender has the functions to access the data. Do you know if them are exposed in python?

    #
    24
    Jun 3, 2011 at 4:11 pm
    • Posts: 223

      Hi Makers_F,

      What exactly would you like to use/ access in real-time? Perhaps a solution would be to bake the sound to an fcurve and access the information from there or use that curve to drive other properties?

      #
      24.1
      Jun 3, 2011 at 4:41 pm
      • Makers_F
        Posts: 3

        i mean a small bge “game”, where you select the song you want to play, and the “game” plays it and shows at screen cool effects (life what the big majority of audio/video players already do). You can’t bake anything since you don’t know what song will be used. For playing the audio audspace works without problems, but i can’t find a way to access in realtime at the frequencies of the song in a given moment, something like song.getFreq(time, min freq, max freq), and use this to modify the scale of the cubes(i’m not an audio expert, but for sure blender go through all the song and get a float number from some data in a given moment of the song, and use it to create the F-curve) :)

        #
        24.1.1
        Jun 4, 2011 at 6:24 am
      • Posts: 223

        Ah, I see what you mean. I actually haven’t looked into Audaspace a lot yet, so I’m afraid I’m not sure what’s possible there or what is still in development. =/

        #
        24.1.2
        Jun 4, 2011 at 6:40 am
  25. Posts: 17

    Im getting an error: python script fail, look in console for now…

    this is my code http://www.pasteall.org/22164/python

    any ideas?

    #
    25
    Jun 3, 2011 at 4:34 pm
    • Posts: 223

      Hey Jkoocs,

      You seem to be using parenthesis () rather than square brackets [] to access the fcurve index. Just change those around and you should be good to go! =)

      #
      25.1
      Jun 3, 2011 at 4:44 pm
      • Posts: 17

        Changed it and I’m still getting the same error. I’m not sure if it means anything but it is highlighting the 24th line of code now. Last night it was highlighting the 27th though?

        #
        25.1.1
        Jun 3, 2011 at 4:52 pm
      • Posts: 223

        You’re probably using a later version on blender. We’ve added a note at the top of this page to accommodate for that API change. Just replace bpy.ops.object.scale_apply() with bpy.ops.object.transform_apply(scale=True)

        #
        25.1.2
        Jun 4, 2011 at 6:43 am
    • Phil
      Posts: 5

      I kept getting the same error and it would highlight the last line. I was using a build from graphicall.org which had luxblend with pylux built in but when I installed the default blender from blender.org it worked fine.

      #
      25.2
      Jun 4, 2011 at 6:29 am
      • Posts: 17

        This was the problem i figured out after a couple of tries.

        #
        25.2.1
        Jun 5, 2011 at 3:59 pm
  26. Javier
    Posts: 1

    I have a problem in line 32, can someone help me? It says that there’s a syntax error. Link: http://www.pasteall.org/22165/python

    Note: I’m new on this, so I don’t understand everything at all.

    Anyway, great tutorial

    Thanks!

    #
    26
    Jun 3, 2011 at 6:06 pm
    • Posts: 17

      Hey Javier,

      After the “step = 20000/ (rows*columns)” you have to make a new line for the rest of that.

      #
      26.1
      Jun 3, 2011 at 9:15 pm
  27. kpvw
    Posts: 2

    I’ve written all my code, but when I press alt+P (or the run script button) it just doesn’t do anything

    http://www.pasteall.org/22166/python

    #
    27
    Jun 3, 2011 at 6:23 pm
    • Posts: 223

      Hi Kpvw,

      That’s a mean little mistake you’ve made there. =P All you need to do is remove the indentation at the final line (spiral(rows, columns)). Right now the script things this statement is still part of the spiral function.

      #
      27.1
      Jun 4, 2011 at 6:49 am
      • kpvw
        Posts: 2

        I thought I fixed that at some point… Thanks, now it works!

        #
        27.1.1
        Jun 4, 2011 at 9:49 am
  28. Mc226
    Posts: 3

    Patrick, these vidtuts of yours have me thinking I might be able to learn Python (at least, for Blending) – being a VERY right-brained, visually-based person, that’s a fairly big deal. I didn’t think it could be done. So thank you.

    I think a little Marley would have the cubes jumpin’……

    Now I have to go sort through all my screen caps and figure out what happened :)

    #
    28
    Jun 3, 2011 at 6:51 pm
    • Posts: 223

      That’s great to hear; congratulations! =)

      #
      28.1
      Jun 4, 2011 at 6:50 am
      • Mc226
        Posts: 3

        Heh. We’ll see how it goes.

        I have a related but slightly off-topic question, and please remember this is from a coding-ignorant person;

        Would it be possible to map the keyboard to use only the punctuation that Python (or any other language, I suppose)recognizes as valid, healthy, and correct? I’m guessing this would be a system-level thing, and smarter folks than me have probably thought of it before….but shouldn’t it be possible? Has it been done?

        Just a thought. I’m prone to typos sometimes, in direct relation to caffeine intake. Anyway….

        #
        28.1.1
        Jun 7, 2011 at 2:58 pm
  29. unknown
    Posts: 6

    Mine says:

    SyntaxError: (unicode error)’unicodeescape’ codec can’t decode bytes in position 2-4: truncated
    here is the script

    http://www.pasteall.org/22168/python

    #
    29
    Jun 3, 2011 at 7:22 pm
    • Posts: 223

      Unfortunately I haven’t been able to reproduce this error message, but it seems to be because it’s trying to use the backslashes as escape characters. Could you try placing an ‘r’ before your filepath and see if that works?
      (bpy.ops.graph.sound_bake(filepath=r”\Users\SIDY\Downloads\Halo 3 OST – [Ending] Roll Call.mp3″, low=i*step, high=i*step + step))

      Also, you seem to have forgotten ‘menu’ when inserting a keyframe. ( bpy.ops.anim.keyframe_insert_(type=’Scaling’) should be bpy.ops.anim.keyframe_insert_menu(type=’Scaling’))

      #
      29.1
      Jun 4, 2011 at 7:01 am
      • unknown
        Posts: 6

        it works now but the last line of code has and error:
        spiral(rows, columns)
        the script
        http://www.pasteall.org/22195/python

        #
        29.1.1
        Jun 4, 2011 at 12:04 pm
      • Posts: 223

        How weird, I can’t seem to be able to reproduce the error on my end. Is the console telling you anything useful? =/

        #
        29.1.2
        Jun 4, 2011 at 2:21 pm
  30. Posts: 6

    I like the tut but I keep having a script error with line 16 and I’m not sure why?

    #
    30
    Jun 3, 2011 at 7:33 pm
    • Posts: 223

      Hey Matt,

      It’s hard to tell without actually seeing the script. Would you mind uploading what you have to pasteall.org or pastebin.com so I can take a look at it?

      #
      30.1
      Jun 4, 2011 at 7:03 am
      • Posts: 6

        Here is what I have so far

        http://pastebin.com/3DJwUvUZ

        Thanks for the help!

        #
        30.1.1
        Jun 4, 2011 at 12:02 pm
      • Posts: 223

        There were two little errors I could find in your script.
        First, the level of indentation plays an important part in Python. You need to match the level so that the indentation of line 11 matches that of line 16 (i.e.: Inset the if-statement by one tab).
        Second, you seem to be using parenthesis () where square brackets [] are required (lines 27, 28, 38).

        And that should do the trick! =)

        #
        30.1.2
        Jun 4, 2011 at 1:59 pm
      • Posts: 6

        Thanks so much!!!

        #
        30.1.3
        Jun 4, 2011 at 3:33 pm
  31. Posts: 174

    Great tutorial. I never knew about the sound to f-curve option in Blender so even if I don’t use it with python it was good to learn about.

    #
    31
    Jun 4, 2011 at 12:23 am
  32. Shine
    Posts: 3

    Just great !! I like it
    Thanks !!

    #
    32
    Jun 4, 2011 at 4:58 am
  33. Chaosbadger
    Posts: 6
    #
    33
    Jun 4, 2011 at 5:58 am
    • Posts: 223

      Hi Chaosbadger,

      Your script should be fine when you change ‘X = Y = 0′ on line 11 to ‘x = y = 0′ (non-capitals).
      Also, on line 30 you’re setting ‘step’ by saying ‘step = 5000/ (row*column)’. This should be ‘step = 5000/ (rows*columns)’ (plural, like you defined it at the top of your script).
      For my version of Blender I also had to reset bpy.ops.object.transform_apply(scale=True) to bpy.ops.object.scale_apply, but this depends on which version your currently using.

      Good luck! =)

      #
      33.1
      Jun 4, 2011 at 9:32 am
      • Chaosbadger
        Posts: 6

        That fixed the script, but I then get a Microsoft Visual C++ Runtime Library error

        #
        33.1.1
        Jun 5, 2011 at 9:30 am
      • Chaosbadger
        Posts: 6

        I have the latest update for the C++ but I still get the error

        #
        33.1.2
        Jun 5, 2011 at 2:57 pm
      • Posts: 223

        I remember someone earlier in the comments mentioning this and they seem to have fixed it by not having a number in the file path. I’m not sure why, but perhaps that helps?

        #
        33.1.3
        Jun 6, 2011 at 2:17 pm
      • Chaosbadger
        Posts: 6

        Yep, that helped, thanks

        #
        33.1.4
        Jun 7, 2011 at 6:39 am
  34. Posts: 11

    Wow! Thanks. Great tutorial!!!

    #
    34
    Jun 4, 2011 at 7:05 am
  35. Jay
    Posts: 7

    Hi. I’m really stuck quite early on, just can’t get the script to do anything when I run it – any ideas?

    #
    35
    Jun 4, 2011 at 8:39 am
    • Posts: 223

      It’s hard to say without seeing your script. Please feel free to upload what you have so far to pastebin.com or pasteall.org and I’ll take a look at it. =)

      #
      35.1
      Jun 4, 2011 at 9:33 am
      • Jay
        Posts: 7

        Okay, well I have now cheated and copied Chaosbadgers’ paste file, but changed your corrections and the audio file location, but any attempts at running my original script at any point didn’t produce any result at all. I now get an error on line 41 using Chaosbagers paste file.
        This is the first time I have attempted using Python, the Text Editor and the Console windows, so beginner to this? – yes. Do I have to set up somehow before the first stage in your tutorial?
        I paste my code and a screen-shot:
        http://www.pasteall.org/22193
        http://www.pasteall.org/pic/13265

        The error message at the top of the 3D window says to look in the console window but nothing appears?
        Any help is gratefully received, I’ve been trying to do a sound to light for a long time, thanks.
        Blender 2.57.0
        r36339M

        #
        35.1.1
        Jun 4, 2011 at 10:01 am
      • Posts: 223

        Hi Jay,

        When Blender prompts you to look at the Console, it means the external one, not the one inside Blender. To open this on OSX, just go to Blender.app > Show package contents > context > MacOS > Blender. This will launch Blender and the external console.
        As for the script, reverting line 23 (bpy.ops.object.transform_apply(scaling=True) back to bpy.ops.object.scale_apply() made it work. =)

        #
        35.1.2
        Jun 4, 2011 at 5:06 pm
  36. Posts: 10

    Terrific tutorial Patrick, You just light my fire! The bakery only keeps getting better.

    #
    36
    Jun 4, 2011 at 9:29 am
  37. Posts: 10

    a couple of questions. How did you get the materials, lighting and background of the tutorial’s main image? Are there BC tutorials on them?

    Could you please give us the link on the spiral function you used? Thanks! And once again, great tutorial.

    #
    37
    Jun 4, 2011 at 9:55 am
    • Posts: 223

      As for the materials, I gave the cubes a slightly glossy, raytraced transparent material with a bit of an emit value, turned on Indirect Lighting and placed a simple Point Light at the center of the whole thing (or see comment 11.1 ;) ).
      I’m not sure on the background, Wes Burke did that part. =)

      The spiral algorithm can be found on http://stackoverflow.com/questions/398299/looping-in-a-spiral

      #
      37.1
      Jun 4, 2011 at 10:43 am
  38. JRJurman
    Posts: 9

    For those of you who want to track the progress (since baking can take quite a while) it’s defiantly worth it to add a couple of print statements:

    print(i)
    print(“Baking Sound”)

    I have added a couple print statement so I can track the progress, but I can only view the console by opening the blender application in blender.app/Contents/MacOS

    Is there a better way to open the console on at least Mac OS X?

    #
    38
    Jun 4, 2011 at 12:51 pm
    • Posts: 223

      Hey, good call! I’m afraid there isn’t a more graceful way to open the console on OSX though. Windows users can access it from the Help menu at the top.

      #
      38.1
      Jun 4, 2011 at 1:53 pm
  39. Dhia eldeen
    Posts: 3

    Hello, can this bring back the .blend to the audio file?
    ps: didn’t watch the tut yet.

    #
    39
    Jun 4, 2011 at 9:05 pm
    • Posts: 223

      Hi Dhia,

      I’m afraid I don’t understand what you’re asking. Could you rephrase your question please?

      #
      39.1
      Jun 5, 2011 at 6:54 am
  40. Posts: 2

    Its says i got this line wrong:
    bpy.ops.mesh.primitive_cube_add(location = (r*2, c*2, 0))

    #
    40
    Jun 5, 2011 at 6:58 am
    • Stephen Johnson
      Posts: 7

      so do i!

      #
      40.1
      Jun 5, 2011 at 11:35 pm
    • Posts: 223

      That’s really odd.. Did you make sure to import bpy at the top of your script?

      #
      40.2
      Jun 6, 2011 at 1:55 pm
  41. Jay
    Posts: 7

    Thanks Patrick, the earlier advice you gave me worked perfectly, and I played around with all last night adding cool material effects. All good so far. After reopening the blend file today, the previously built script ran fine, but re-building the script after making minor changes now doesn’t work. I’ve deleted all of my text after being unable to find an error, and pasted the original script that I showed you on pasteall, and made the change to line 23 as you said- which worked last time. Now it doesn’t. The only differences in the blend file are environmental and lighting, etc.
    Anyway, I paste my text again. Please help – it was so much fun when it worked! The only difference I think is the 3 x 3 grid, to save baking time when trying new changes.

    http://www.pasteall.org/22215/python

    Also getting these errors in console:

    Traceback (most recent call last):
    File “/sound1.blend/Text.001″, line 41, in
    File “/sound1.blend/Text.001″, line 32, in spiral
    File “/Applications/blender-2.57b-OSX_10.5_ppc/blender.app/Contents/MacOS/2.57/scripts/modules/bpy/ops.py”, line 179, in __call__
    ret = op_call(self.idname_py(), None, kw)
    RuntimeError: Operator bpy.ops.graph.sound_bake.poll() failed, context is incorrect

    PS – the maths you are using to generate the frequency ranges isn’t very representative for music – for a start, the first increment of 800 is way too high, it should be more like 32. A simple change to line 30 – step = (20000/ (rows*columns)) / (rows*columns) should be better suited for a 5 x 5 grid, but is the syntax correct – I’m going for 20000/25 = 800/25 = 3. If the syntax is right, it gives a better range all the way to the higher frequencies, although this is still a rough way of doing it. If you can help with my problem, I’ll try to come up with a better way of getting the frequency ranges to give in return, I should be able to, I’m a composer and sound engineer!

    Thanks in advance.

    #
    41
    Jun 5, 2011 at 9:56 am
    • Jay
      Posts: 7

      should read – I’m going for 20000/25 = 800/25 = 32

      #
      41.1
      Jun 5, 2011 at 9:59 am
      • Jay
        Posts: 7

        Don’t know why I didn’t try this before, but I’ve just copied and pasted the script only into a new blend file, and it works fine. What could be in the other blend file that causes a problem when running the script? Guess I’ll just have to setup my project again.
        I’ve used step = (20000/ (rows*columns)) / (rows*columns) on a 5 x 5 grid, and it seems to work a little better, although for both these ranges and the original ranges, there still seems something wrong. The bass frequencies are being displayed as far louder than the higher frequencies – is this something in the spiral that |I’m missing, or is it more to do with how blender interprets the audio. Could the threshold values for the bake process be responsible?

        #
        41.1.1
        Jun 5, 2011 at 10:32 am
      • Posts: 223

        Hey Jay,

        Weird about the script failing in another .blend. o.ô This almost makes me think it’s a blender bug, as nothing in the script depends on anything else in the scene. Glad to hear you’re back on track though! =)
        As for the frequencies, someone mentioned in a previous comment that a logarithmic function would work better. My math-knowledge is very rusty, so I’m afraid I won’t be of much help with that. However, after doing some simple plotting (no actual testing yet) f = 2.2^(i/2) and f = i^3.1 seemed to produce a nice graph for a 5×5 grid.
        I’m also not sure how bass frequencies affect the visualization, as my knowledge of audio isn’t just rusty; it’s pretty much non-existent. =/

        Please keep us posted if you find a nice way of dividing the frequencies! =)

        #
        41.1.2
        Jun 5, 2011 at 11:16 am
  42. swv
    Posts: 2

    Thanks again for this tutorial. My first video in blender: http://youtu.be/AW_xhxCW07A . Thanks again :)

    #
    42
    Jun 5, 2011 at 11:15 am
    • Posts: 223

      Hey, it looks awesome; great job! =D

      #
      42.1
      Jun 5, 2011 at 11:34 am
  43. Jay
    Posts: 7

    Thanks, I’ll test that one, and number crunch it to see what frequencies come out, although if I’ve understood you right, doesn’t the first frequency still come out at 800? The first should probably be in the area of 0 – 32 or so. The overall range would ideally be 20 Hz – 20 kHz, with the 20 kHz range always being reached in the upper limit of the last range regardless of grid size. I may not be able to produce elegant maths to produce these results, but here are example frequencies from a 31 band graphic equaliser:
    20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3100, 4000, 5000, 6300, 8000, 10000, 12000, 16000, 20000
    I’ve also realised that my earlier maths was way out, so I’m still looking for an elegant method. Is there a way to enter these values as a string of constants instead, then you could just pick the frequency ranges for each cube?
    Log scales are used in audio for both the frequencies and the levels reached, so that may also explain why this method seems to lead to bass heavy results. Is there a way to make the baked fcurve response logarithmic, so that the scaling isn’t linear, that would definitely help.

    #
    43
    Jun 5, 2011 at 11:58 am
  44. Jay
    Posts: 7

    One simple formula for calculating the frequencies would be to start with 20, and multiply by 1.34, so that the first range would be 20 – 26.8, then multiply the upper range by 1.26 to get the next lower value, ie the second range would be 26.8 – 35.912, and so the way up to the 25th cube: 16766 – 22467, thus covering the full audible spectrum with good separation in a 5 x 5 grid. Thing is, I have no idea how to implement this in python. Any ideas?

    #
    44
    Jun 5, 2011 at 2:45 pm
    • Posts: 223

      If you just want to use multiplications you could use an extra variable to store the initial/ old value in and just use *. So for example:

      oldVal = 20
      low = oldVal #frequency 1
      high = oldVal * 1.26 #frequency 2
      oldVal = high #So this high is the next low

      As for using fixed values, you could do something like this:

      frequencies = (0, 32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000) #Though this is basically just constantly doubling it.

      #Then in the loop:
      low = frequencies[i]
      high = frequencies[i+1] #Just make sure to add one more value to the array than the amount of cubes

      #
      44.1
      Jun 6, 2011 at 2:15 pm
      • Dave Gun
        Posts: 1

        I used a LOG scale and broke it into even parts from 20hz to 20Khz. LOG10(20)= 1.3, and LOG10(20000) = 4.3. I divided this by 25 and got an increment of 0.12. I then used this to find the frequencies. 10^1.3, 10^1.24,… I used what you posted in my script:

        freq = (20, 26, 35, 46, 60, 80, 105, 138, 182, 240, 316, 416, 550, 724, 955, 1259, 1660, 2188, 2884, 3801, 5012, 6607, 8710, 11482, 15136, 20000)

        and

        low = freq[i]
        high = [i+1]

        I didn’t add the spiral code, so it starts in the first row with the lows to the highs in the last row. It turned out good, you can see the lows through the highs ripple through the bars.

        Thanks everyone, great tutorial!!

        Dave

        #
        44.1.1
        Jun 8, 2011 at 12:23 am
      • Posts: 223

        Cool, it seems to work perfectly! Thanks for sharing! =)

        #
        44.1.2
        Jun 8, 2011 at 2:28 pm
  45. Posts: 20

    I am having trouble, when I run the script is says error- python script fail look in console for now and it highlights

    spiral(rows, columns)

    then in the console it says

    AttributeError: Calling operator “bpy.ops.object.scale_apply” error, could not be found

    I will post my blend on pasteall

    http://www.pasteall.org/blend/6864

    the script link is

    http://www.pasteall.org/22227/python

    -supersecrethackername
    and thanks

    #
    45
    Jun 5, 2011 at 10:38 pm
    • Posts: 223

      Hi,

      Would you happen to be using a newer version of blender? If so, please be sure to follow the note at the top of this page and replace bpy.ops.object.scale_apply() by bpy.ops.object.transform_apply(scale=True) =)

      #
      45.1
      Jun 6, 2011 at 2:00 pm
      • Posts: 20

        for some reason it still has that error, i changed that and i am using a build off http://graphicall.org/ it is a luxblend 2.57.1 r36543, i tried it in a older version to that i still have but still wont work, and the error seems to be coming from

        spiral(rows, columns)

        #
        45.1.1
        Jun 6, 2011 at 4:59 pm
    • Posts: 20

      for some reason it still has that error, i changed that and i am using a build off http://graphicall.org/ it is a luxblend 2.57.1 r36543, i tried it in a older version to that i still have but still wont work, and the error seems to be coming from

      spiral(rows, columns)

      thanks for your patience

      #
      45.2
      Jun 6, 2011 at 5:01 pm
      • Posts: 223

        Hmm, I’m very sorry, but I can’t seem to be able to reproduce the error on my end; it works fine here. =/ Perhaps you could try asking this question on BlenderArtists.org or #blendercoders on IRC?

        #
        45.2.1
        Jun 6, 2011 at 6:01 pm
  46. Stephen Johnson
    Posts: 7

    bpy.ops.mesh.primitive_cube_add(location = (r*2, c*2, 0))

    Blender says this line is an error! Dont know what i did wrong!
    Please help!

    #
    46
    Jun 5, 2011 at 11:33 pm
    • Posts: 223

      Hi Stephen, did you make sure to type ‘import bpy’ at the top of your script?

      #
      46.1
      Jun 6, 2011 at 1:57 pm
  47. Posts: 2

    When I run the script I get a problem locating the song…

    bpy.ops.graph.sound_bake(filepath=”C:\Users\Jostein\Documents\Blender\Songs2 take me to the bonuslevel because i need an extralife”, low=i*step, high=i*step + step)

    … why do I get backwards slash instead of normal (/)?. Is this the problem? Any suggestions?

    #
    47
    Jun 6, 2011 at 11:15 am
    • Posts: 2

      bpy.ops.graph.sound_bake(filepath=”C:\Users\Jostein\Documents\Blender\Songs2 take me to the bonuslevel because i need an extralife”, low=i*step, high=i*step + step)

      #
      47.1
      Jun 6, 2011 at 11:17 am
      • Posts: 223

        Hi Jostein,

        It seems as if you’ve forgotten the extension (.mp3) and possible a slash somewhere. I’m guessing it should be more like: filepath=”C:\Users\Jostein\Documents\Blender\Songs2 – take me to the bonuslevel because i need an extralife.mp3”

        Please let me know if you need any further help on this.

        #
        47.1.1
        Jun 7, 2011 at 5:27 pm
      • CMITZ
        Posts: 14

        yeah, I know that problem.
        Even though my script doesn’t work on my pc, I know that you’d better use \\Song.mp3 in the same directory as your saved blendfile

        #
        47.1.2
        Jun 12, 2011 at 3:27 pm
  48. Posts: 2

    first of all great tutorial! its my first ever at blendercookie and ill be comming back for sure. i found you from blender guru =)

    anyways this is my first try at some scripting, and im already seeing the possibilities of it, but im running into a problem..

    when i run the scrip iv been getting many errors, most of which ive been able to fix just by going through it with a fine tooth comb, but this one has me stumped.

    the error message that pops up is as follows:

    File “F:\Username’s files\blender\Sound waves.blend\Text”, line 33
    Syntax Error:(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-4: truncated\UXXXXXXXX escape
    location::-1
    Python script fail, look in the console for now…

    i did see another person with a similar issue, but i think mine had alittl more detail, wondering if that would help? ill also put my full scrip up for you to check out.

    again i just want to say that your support here is awesome, its crazy how many comments you have looked at and helped with =) weather you can help me or not thanks very much for trying!

    http://www.pasteall.org/22247

    #
    48
    Jun 6, 2011 at 7:45 pm
    • Posts: 223

      Haha, I had no idea my work was in Andrew Focused Critique; totally forgot about that! Thanks for the heads up! =D

      As for your error, it’s a tough one for me to solve as I can’t replicate it on my machine, but I think placing an ‘r’ before the filepath should fix it. (Without it, it’s not a Raw String and for some reason, Blender only notices this on some machines, using the ‘/’ as an escape character.)

      So codewise, could you try replacing
      bpy.ops.graph.sound_bake(filepath=”C:\Users\Brent Reel\Downloads\Eiffel 65 – Move Your Body”, low=i*step, high=i*step + step)
      by
      bpy.ops.graph.sound_bake(filepath=r”C:\Users\Brent Reel\Downloads\Eiffel 65 – Move Your Body”, low=i*step, high=i*step + step)
      ?

      Please let me know if it worked! =)

      PS: Thanks for the compliment on the support, I try my best!

      #
      48.1
      Jun 7, 2011 at 5:41 pm
  49. Posts: 8

    It says that the python script fail check in the console.

    Any ideas?

    -Xiong

    #
    49
    Jun 6, 2011 at 7:51 pm
    • Posts: 223

      Hi Xiong,

      A good start would be to check Blender’s console (the external one) to see a more elaborate description and line number of the error. If you’re still stuck after that, please feel free to paste the exact error here and upload your script to patebin.com or pasteall.org so I can take a look at it. =)

      #
      49.1
      Jun 7, 2011 at 5:43 pm
  50. Posts: 8

    Hey Patrick,
    I don’t know if anyone else had a problem with this, but when i run my script it works but it puts all the cubes at the cursor and not in a 5×5 grid. I checked my script against yours in the video but couldn’t figure it out. Here’s a link to the code: http://www.pasteall.org/22251/python

    Thanks,

    -Peter

    #
    50
    Jun 6, 2011 at 10:25 pm
    • Posts: 223

      Hey Peter,

      Seems you’ve forgotten some indentation from line 37 onwards. That should do the trick! =)

      #
      50.1
      Jun 7, 2011 at 5:51 pm
  51. Kris K
    Posts: 8

    Can anybody help me please?
    http://www.pasteall.org/22264/python
    It keeps saying that line 38 is an error. – spiral(rows , columns)
    I tried some of the methods above but it didn’t work.

    #
    51
    Jun 7, 2011 at 10:12 am
    • Posts: 17

      Hey Kris,

      Having a look at your code, (note that I haven’t run your code myself) it looks like you might need to change the ‘apply_scale()’ call for the updated API. Look at the NOTE above the tutorial video.

      Just a comment for using the API in general, I found that in the case of errors, if you look in the Console, it gives you a better idea about what the error is.

      #
      51.1
      Jun 7, 2011 at 12:38 pm
    • Posts: 223

      Hey Kris,

      John’s right: In most cases the Console will print out a lot of useful information. In your case, the “…cursor_location.Z” on line 15 needs to be changed to “….cursor_location.z”, without capitalization.
      Also, while your final line works, I always like to use quotation marks for enums, just for consistency’s sake. (so bpy.context.area.type = ‘TEXT_EDITOR’, rather than bpy.context.area.type = (TEXT_EDITOR))

      Hope this helped!

      #
      51.2
      Jun 7, 2011 at 5:20 pm
      • Kris K
        Posts: 8

        Thank you both of you.

        #
        51.2.1
        Jun 12, 2011 at 11:10 am
      • Kris K
        Posts: 8

        Using what both of you have said, the scene crashed. The sound file was only 150 Kb and it is .mp3. Have any ideas?

        #
        51.2.2
        Jun 12, 2011 at 11:19 am
      • Kris K
        Posts: 8

        Oh by the way, in case this information helps (just keep figuring ways to solve), I am using Intel Core i5 Quad Core with 2500 MB RAM free when Running, CPU usage was 4-7% when running.

        #
        51.2.3
        Jun 12, 2011 at 11:28 am
  52. Posts: 17

    Here is my attempt.

    http://www.youtube.com/watch?v=zyDriamOWzU

    All of the BlenderCookie tutorials are great but I especially enjoy your ones Patrick. Please keep them coming. Learning how to control things using the API is incredible. You’ve got me learning Python now :)

    #
    52
    Jun 7, 2011 at 12:32 pm
    • Posts: 223

      Great work on the visualizer John, love the nice zig-zag pattern! =) And thanks for the compliment; I’m glad you’re liking my tutorials! Cool to hear you’re getting into Python!

      #
      52.1
      Jun 7, 2011 at 5:23 pm
  53. Alex U
    Posts: 3

    hey patrick, i am new to python and in the tutorial when you first start typing the script, i ran it and it looks nothing like yours and ive copied your script exactly how it looks. Right now its showing a row of regular cubes. I cant seem to fix this issue, help?

    #
    53
    Jun 7, 2011 at 7:23 pm
    • Alex U
      Posts: 3

      oh and heres my code, im not that far in yet, but if it matters, i am using blender 2.57.

      http://www.pasteall.org/22276/python

      #
      53.1
      Jun 7, 2011 at 8:02 pm
      • Posts: 223

        Hey Alex!

        Python is a language that’s sensitive to the level of indentation. In your script, you’ve placed the indentation of line 16 (bpy.ops.mesh.primitive_cube_add(location = (r, c, 0))) so that python thinks it’s part of your if-statement. So now, it’s only adding a cube when it reaches the end of a row. To fix this, just remove one level of indentation (tab) from line 16 so that it matches that of lines 11 and 20. =)

        #
        53.1.1
        Jun 7, 2011 at 8:28 pm
  54. BlenderEi
    Posts: 15

    Hey you are so great, Patrick!!!

    Thanks a hundred, no a thousand times!
    Keep your awesome tutorials coming please!

    #
    54
    Jun 8, 2011 at 7:57 am
  55. Posts: 87

    Hi Patrick, firstly awesome tutorial thanks for sharing these tips.

    I have a minor problem there is is an script error on line 32 which is my address to the sound file.

    this is the error notice:
    Error

    File”C:\Documents and Settings\SL1200\Desktop\Blender_2011\AudioVisualizer.blend\Text”, line 32

    SyntaxError:(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 55-57:truncated\Uxxxxxxxx escape

    location::=1

    here is line 32
    bpy.ops.graph.sound_bake(filepath=”C:\Documents and Settings\SL1200\My Documents\Downloads\Unicorn Kid – Dream Catcher.mp3″, low=i*step, high=i*step + step)
    I’m using blender on win XP

    any help would be great.

    Sl1200

    #
    55
    Jun 8, 2011 at 9:04 am
    • Posts: 87

      Additional:
      Is it possible to define the grid further by skipping/removing cubes.
      The reason I ask is I would like to spell out the name of the Artist who’s music I am using.

      SL1200

      #
      55.1
      Jun 8, 2011 at 9:26 am
      • Posts: 17

        I’m sure there is a better way to do it using a 2D logical array or something but this is my newbie way.

        Manually insert a single cube.
        Add 2 array modifiers, the first in the X direction, the 2nd in the Y.
        Make the grid big enough that you can select the desired pattern from it.
        Apply both array modifiers.
        Select the desired pattern. Hit ‘ctrl+i’ to select the inverse and delete.
        Now re-select all the cubes in your pattern.

        Go to Patrick’s script and take out the lines that add new primitives. Then replace the for loop with

        ##
        ilist=[]
        for i in range(len(bpy.context.selected_objects)):
        ilist.append(bpy.context.selected_objects[i])

        for i in range(len(ilist)):
        bpy.context.scene.objects.active = ilist[i]
        ##

        The first loop creates a list of selected objects. The second loops over the list of objects making each subseqent one “active” (with the body of Patrick’s original script going within this loop). This SHOULD work although the selection order of the cubes will depend on their name which is dependent on how you initially set up the cubes. Anyway, hope this helps and doesn’t hinder. :)

        #
        55.1.1
        Jun 8, 2011 at 12:31 pm
      • Posts: 223

        Hey SL1200,

        About your error: I’m not sure why, but sometimes Blender likes to think of the slash ‘/’ as an escape character. To fix this, all you need to do is parse it as a raw string by placing an ‘r’ in front of the filepath. So in your case:
        bpy.ops.graph.sound_bake(filepath=r”C:\Documents and Settings\SL1200\My Documents\Downloads\Unicorn Kid – Dream Catcher.mp3″, low=i*step, high=i*step + step)

        As for the pattern, I think John’s solution is a very elegant one as it allows you to first work out the pattern visually, rather than having to run the script, tweak, run it again, etc. This would also the remove the limitation of pure grid placement, allowing different patterns.

        If you want to do it all programmatically, I’d suggest looking into arrays and building a grid dynamically from that. This would probably lead to an array similiar to this:

        placementArray = (
        1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
        0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
        0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0,
        0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
        0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0,
        )
        #15×5 matrix spelling TEST, where 1 represents a cube and 0 represent empty space.

        The advantage of this approach would be that you could add more variables easily (ie: 0 = nothing, 1 = blue cube, 2 = red cube, 3 = green sphere).

        Hope this helped and thanks again John for jumping in! =)

        #
        55.1.2
        Jun 8, 2011 at 12:51 pm
    • Robert
      Posts: 1

      So, I actually just had this error for very different code and looked it up online, finding yours as the only example of it anywhere. I’ll assume that you, like myself, are new to Python. The way I was able to fix the problem, with the help of a colleague, was to use double slashes in the file path. Thus it will read:\n bpy.ops.graph.sound_bake(filepath=”C:\\\\Documents and Settings\\\\SL1200\\\\My Documents\\\\Downloads\\\\Unicorn Kid – Dream Catcher.mp3″, low=i*step, high=i*step + step)\nAt the very least, this is how I fixed the problems, and it’s worth trying. If it doesn’t work I’m sure a real expert will respond soon enough.\n\nGood luck,\nRob

      #
      55.2
      Jun 8, 2011 at 11:26 am
    • Flynnstone
      Posts: 5

      You can also double (or escape) the slashes to fix the line 32 error (it worked for me anyway):

      filepath=”C:\\Documents and Settings\\SL1200\\My Documents\\Downloads\\Unicorn Kid – Dream Catcher.mp3″,

      Although the r is a quicker solution :)

      #
      55.3
      Jun 8, 2011 at 2:43 pm
      • Posts: 223

        Oh, good call! =)

        #
        55.3.1
        Jun 8, 2011 at 4:31 pm
  56. unknown
    Posts: 6

    Here is mine

    #
    56
    Jun 8, 2011 at 3:26 pm
  57. Posts: 87

    Patrick & John thanks for the reply Guy’s, great ideas for me to work on.
    I got the demo working now thanks to that pesky r in front of the address, going to try the array work next.
    It seems that 1 cube is always higher than the rest mine is at start of my cube array but your demo pic above shows it in the center, any ideas?

    Some advice if you will please:

    My OOP knowledge is novice at best, I have dabbled mainly in Flash AS 2.0 for application development and briefly in VB, however the flow of the python language seems different, or is it just me?
    It appears there is no declaration of variables or any strict datatyping, or is it optional?
    Also what dictated the end of the for loop and the if conditional? is it the hashes? as nothing seems to close it off.

    Its probably that I’m just used to them looking being open and closed with braces.

    Any info on these areas would be great.

    Once again many thanks for your help and hopefully letting me pick your brains further.

    SL1200

    #
    57
    Jun 8, 2011 at 3:43 pm
    • Posts: 223

      RE the high cube placement: It’s because I placed them in a spiral shape, where I’m guessing you used the first method described. In the comments on page 1 there’ve also been suggestions on how to even out the bars’ height more if you’re interested.

      Python is indeed a very loose language when it comes to datatyping (and strictness in general).
      Python is also one of the few languages (the only other one I know off being Boo) that uses indentation to mark scopes. So an if-statement is ended by ‘jumping back’ a level. A quick example:

      def someFunction():
      someVar = 42 #level 1 indentation – function scope

      if someCondition == false and someVar == 42:
      executeSomeOtherFunction() #level 2 indentation – if-scope
      someVar = 1337

      someOtherVar = “Python rox my sox!” #Back to level 1 – outside of if-cond.

      def newFunction(): #No indentation – new function

      Of course if this were to be, say, all in a class, then the class declaration would be level 0, function defining level 1, etc.

      Hope this cleared some things up! =)

      #
      57.1
      Jun 8, 2011 at 4:45 pm
    • Posts: 17

      Hey, I have a mostly ‘monkey see, monkey do’ relationship with OOP and Python at present. I am coming from a mostly procedural (Fortran 90/95) background so my little Python experience has been using it in a procedural style.

      With regards your questions, I know just about enough (with assistance from a Python book next to me) to hopefully answer you. Python is dynamically typed, so variables are assigned a type when initialised. For example,

      test1 = 1 # will make ‘test1′ an integer
      test2 = 1.0 # will make ‘test2′ a float
      test3 = ‘blahblah’ # will make ‘test3′ a string

      In Python, the ‘code blocks’ for loops and conditional statements are identified by indentation rather than being enclosed in curly brackets. The start of a loop (i.e. the for or if statement) is finished by using a colon. The text you wish to be included in eg. a ‘for loop’ should all be indented by the same amount of whitespace. The general recommendation seems to be using 4 spaces. Oh and for each nested block, there needs to be further indentation for that block. I used periods to show the spaces in the example below.

      n=10
      for i in range(n):
      ….# this is a comment
      ….print(i)
      ….if i==5:
      ……..#this is another comment
      ……..print(‘ahhhh’)

      I hope I didn’t end up confusing you more and will go back to letting Mr Boelens do the answering. :)

      #
      57.2
      Jun 8, 2011 at 4:53 pm
      • Posts: 223

        Perfect explanation! Thanks a bunch for helping me answer these questions by the way, it’s much appreciated! =)

        #
        57.2.1
        Jun 8, 2011 at 5:43 pm
      • Hunter
        Posts: 1

        I have an error in the last line (line 42)
        and I doun´t know why.

        #
        57.2.2
        Jun 10, 2011 at 7:00 am
  58. Jake
    Posts: 5

    ok so i have no idea whats wrong i keep getting an error at line 32. can you give it a look.

    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range (0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location =(r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    ####
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 5
    bpy.ops.object.scale_apply()

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves(0).lock = True
    bpy.context.active_object.animation_data.action.fcurves(1).lock = True

    bpy.context.area.type = ‘GRAPH EDITOR’

    step = 20000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath=*C:\Users\Jake\Desktop\Rapture StopBreatheBump.com.mp3* low-i*step. high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves(2).lock = True

    c += 1

    #
    58
    Jun 8, 2011 at 11:19 pm
    • Posts: 87

      Hi jake looks like you have same prob I did, looking at your code u seem to have an astrisk where the double quotes should be in the filepath.

      bpy.ops.graph.sound_bake(filepath=*C:\Users\Jake\Desktop\Rapture StopBreatheBump.com.mp3* low-i*step. high=i*step + step)

      Also as Patrick advised me if you use windows add an r after the equals to parse it as a raw string.

      try:
      bpy.ops.graph.sound_bake(filepath=r”C:\Users\Jake\Desktop\Rapture StopBreatheBump.com.mp3″, low-i*step. high=i*step + step)

      I’m new at Python and struggling but hope this helps.

      #
      58.1
      Jun 9, 2011 at 12:44 pm
      • Jake
        Posts: 5

        Thanks a ton, i am not really much of a programmer. Thanks for the help

        #
        58.1.1
        Jun 9, 2011 at 11:34 pm
      • Jake
        Posts: 5

        one more thing. i added the code you posted and it gave me an error at line 40. so my code currently looks like this:

        import bpy

        rows = 5
        columns = 5

        r = 0
        c = 0

        for i in range (0, rows*columns):
        if c == columns:
        r += 1
        c = 0

        bpy.ops.mesh.primitive_cube_add(location =(r, c, 0))
        bpy.context.scene.cursor_location = bpy.context.active_object.location
        bpy.context.scene.cursor_location.z -= 1
        bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
        ####
        bpy.context.active_object.scale.x = 0.5
        bpy.context.active_object.scale.y = 0.5
        bpy.context.active_object.scale.z = 5
        bpy.ops.object.scale_apply()

        bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
        bpy.context.active_object.animation_data.action.fcurves(0).lock = True
        bpy.context.active_object.animation_data.action.fcurves(1).lock = True

        bpy.context.area.type = ‘GRAPH EDITOR’

        step = 20000/ (rows*columns)
        bpy.ops.graph.sound_bake(filepath=r”C:\Users\Jake\Desktop\Rapture StopBreatheBump.com.mp3″, low-i*step. high=i*step + step)

        bpy.context.active_object.animation_data.action.fcurves(2).lock = True

        c += 1

        #
        58.1.2
        Jun 9, 2011 at 11:45 pm
      • Jake
        Posts: 5

        i think one big problem is the file path, im not totally sure if i am putting it in right. i changed the song to this:

        C:\Users\Jake\Desktop2 Fall (Remixed By M83 VS Big Bla 1.m4a

        i would be a HUGE help if i could just have the code for the spiral one with this file path in place.

        #
        58.1.3
        Jun 10, 2011 at 12:14 am
      • Posts: 223

        Hi Jake,

        I can’t seem to be able to paste this code in the Blender Text Editor for some reason. The indentation formatting has also disappeared because of the way our comment system handles multiple spaces. Could you please upload the script to pastebin.com or graphicall.org?

        If you just want the working code you might want to consider buying this lesson’s source files (which would be greatly appreciated! ;) ).

        #
        58.1.4
        Jun 10, 2011 at 1:22 pm
      • Jake
        Posts: 5

        I think i will go and buy it, i didn’t even realize i could i am new to this site. :D thanks :D

        #
        58.1.5
        Jun 10, 2011 at 10:08 pm
  59. Nino
    Posts: 10

    hi Patrick Boelens!
    I’m having a Phyton script fail in line 45 (spiral(rows, columns)) when I check in the console it says “spiral not defined”.

    could you please help?

    here is my code:
    http://www.pasteall.org/22303/python

    /Nino

    #
    59
    Jun 9, 2011 at 4:28 am
    • Posts: 223

      Hey Nino!

      The script seems to work fine here. Are you sure you got the file path right? =)

      #
      59.1
      Jun 10, 2011 at 1:24 pm
  60. Posts: 87

    @ John M
    Many thanks for advice, have tried what you suggested and it works great but I am running into other problems, lol it never ends :)
    I have some scaling issues, scaling on all axis and towards the 3d cursor, have looked into this for few hours and found that only the last cube created has the scale locks applied all the rest have x and y open (editability of keyframes for this channel (in graph editor)).

    http://www.pasteall.org/22292/python
    the above code has the scaling issues I describe, only 1 cube seems to be working as it should.

    comparing your indentation example the code in my script above looks wrong as the second for loops is not indented correctly so that it is nested inside the first loop.

    However the code below looks correct if I have the correct understanding of your indentation example. but this code just hangs and chews memory.
    http://www.pasteall.org/22291/python

    My OOP knowledge is screaming to use an if statement inside the 2nd for loop but as I’m new to python I’m unsure, but I’m going to get stuck in today and see what I can come up with.

    @ Patrick
    As I ran into a few problems while watching the tutorial I never got to end so didn’t see that you had already explained it , I will also look at the post regarding leveling out the height of the cubes evenly.

    Regarding the Python language thank you both for giving explanations, hopefully I’m starting to get how this syntax is structured.
    I think its time to have a crash course in the python syntax :)

    Once again Guy’s thanks for your much needed insight and help.

    #
    60
    Jun 9, 2011 at 7:34 am
  61. Posts: 87

    Hey guy’s is there a python script editor you would recommend as I’m having lots of problems doing the simplest of tasks with this language.

    all I want to do is some simple variable value checks using print, i.e before and after
    myNum =0
    if myNum ‘==’ 0:
    myNum +1
    print (myNum)
    Am I missing an option some where? when I press run script none of the print statements work, nothing is passed to command console, so in order to check my code I have to put the entire code into the command console.

    #
    61
    Jun 9, 2011 at 10:55 am
    • Posts: 87

      sorry for double post submitted by accident:
      Hey guy’s is there a python script editor you would recommend as I’m having lots of problems doing the simplest of tasks with this language.

      all I want to do is some simple variable value checks using print, i.e before and after
      myNum =0
      if myNum ‘==’ 0:
      myNum +=1
      print (myNum)
      Am I missing an option some where? when I press run script none of the print statements work, nothing is passed to command console, so in order to check debug my code I have to put the entire code into the command console.
      hopefully you can suggest a script editor as using what’s inside blender or the python IDE(as there are none of the bpy classes) is driving me insane, what I want to do is so simple!

      At the end of my tether.
      SL1200

      #
      61.1
      Jun 9, 2011 at 11:06 am
      • Posts: 17

        Hello again Mr 1200,

        I assume you are on Windows. By default the Blender Console is hidden. Go to

        Help > Toggle System Console

        This will bring up a ‘command prompt’ type window and all the output of print statements, more detailed errors etc will appear in this window. If you use Linux, you need to launch blender from inside a terminal. If you are on a Mac, then you are on your own as I have no idea :)

        I should have a working solution to the code you mentioned. I just need to get a free couple of minutes to check things.

        #
        61.1.1
        Jun 9, 2011 at 12:10 pm
      • Posts: 17

        Ok this is what I managed to do. I think that this is what you were trying to achieve or at least something similar. After doing as described before with the arrays, make sure you go into edit mode, select all, press P and choose ‘by loose parts’ to make sure each cube is a separate object. Other than that, I just ran this code

        http://www.pasteall.org/22311/python

        and got this result

        http://www.pasteall.org/pic/13469

        #
        61.1.2
        Jun 9, 2011 at 1:03 pm
  62. Posts: 87

    Hi John, I will use this in future, nice tip.
    Have gone back to original script to get a fresh look at things, now I’m kicking myself at how I could have overlooked how simple it really is:
    all I changed was this

    rows = 32
    columns = 6

    after running script I just delete cubes from the grid to leave the letters I need.

    it now works I have: Rob P spelt out in blocks and all the letters are driven by the fcurves.
    There is a very noticeable degree of “falloff” I think would describe it best
    it slopes down towards the P but it will do for now. :)

    if you still are able to have a tinker with that code I wouldn’t mind seeing what you work out to see where I was going wrong.

    Many thanks
    SL1200

    #
    62
    Jun 9, 2011 at 1:17 pm
  63. Posts: 87

    Thanks for the help John, tried your script:

    bpy.ops.object.transform_apply could not be found

    so swapped it back to

    bpy.ops.object.scale_apply()

    When I run the script I get a Visual C++ runtime error and blender quits :)
    I’ll assume its relevant to the build of blender I’m using, as your using newer functions.
    My current build is 2.57.1 r36339 will get the latest build tomorrow and have another look.
    Thanks for the code comments they help me understand what’s being used & where its going and why.

    Once again many thanks for your time looking into this.

    SL1200

    #
    63
    Jun 9, 2011 at 9:11 pm
  64. Frederik
    Posts: 4

    Very helpful, thanks a lot.

    #
    64
    Jun 10, 2011 at 5:10 am
  65. Posts: 17

    This was a good learning experience for me and I think I am starting to understand things a little better now. I used the advice in comment 15 and made one last go of it by sampling the log(spectrum). My final script is at

    http://www.pasteall.org/22328/python

    and I uploaded the blend file also which contains a baked song

    http://www.pasteall.org/blend/6920

    I hope this helps somebody to understand things a bit better. Thanks again to Patrick, without who I wouldn’t know this was even possible :)

    #
    65
    Jun 10, 2011 at 7:47 am
    • Posts: 87

      Hi john just looked over your code and its very impressive!
      I managed to follow & understand nearly all of it – math is not my strong point :(
      In this single script you have answered lots of questions I had like setting up and applying materials and how to align another object onto the current one – the hat cube :)
      Thank you so much for sharing this with the community :)
      And a big thanks to Patrick for A: making this tutorial and B:getting me started on Python :)

      SL1200

      Everyone I meet is my superior, and being that, they can teach me something new.

      #
      65.1
      Jun 11, 2011 at 9:10 am
      • Posts: 17

        I’m grateful to have the chance to give back. It’s not that often that I find myself being the one able to give help with this kind of thing.

        Although to be fair, I’m not exactly giving back much. Between Patrick’s tutorial and using the ‘code snippets’ example of how to assign materials, there isn’t much original in there.

        I do like my little ‘hat cubes’ though :) I was trying to get them to pulse similar to an equaliser where the red bar gets pushed up by the green bar but falls slower than the green bar shrinks. It’s definitely possible. I’m just not familiar enough with contraints to do it. It’s on my to-do list so if you figure out a way, please share :)

        #
        65.1.1
        Jun 11, 2011 at 9:45 am
  66. GazRecord
    Posts: 2

    Every time i click Run Script, Blender crashes and windows says it has to close, can you help?

    Here is my code http://www.pasteall.org/22340/python

    Thanks.

    #
    66
    Jun 10, 2011 at 7:11 pm
    • Posts: 17

      I ran your code and it worked fine for me (I tried with Blender 2.57.0 r36332). Assuming your file
      “C:\Users\Gaz Record\Desktop\Blitz” is a .mp3 or whatever then it should work. I tried using a song on my disk both with and without the file extension and it worked both ways. The baking sound to curve does take some time though, during which Blender tends to become unresponsive. It could be up to a few minutes depending on your machine. When you click “Run Script”, just leave Blender to go about it’s business and it will come back to life with the curves baked. You could just keep clicking on the Blender window but it will cause Windows to decide that the program is not responding and will ask you to close the program.

      #
      66.1
      Jun 11, 2011 at 3:38 am
      • GazRecord
        Posts: 2

        Hi John,

        I added the .mp3 to the end of my filepath and it worked first time, guess i should have tried that earlier on. Thanks very much, and thanks for all the great tutorials you guys have on this site, as a new user of blender it really helps.

        Gaz

        #
        66.1.1
        Jun 12, 2011 at 5:58 pm
  67. Posts: 13

    Something is just not working for me, i get only one cube though i did all like in tutorial so far but looks like columns and rows are not working…
    im using blender 2.57.1 for witch version of blender is this tutorial?

    #
    67
    Jun 11, 2011 at 7:46 am
  68. Posts: 13

    Ok, sorry for posting again…
    now i have different issue
    this is my python script http://www.pasteall.org/22345/python

    and when i run it i get only one row with 4 cubes
    this happens in blender 2.57.1 for windows version

    i really like this one and i will very much like to master it
    so please help…

    #
    68
    Jun 11, 2011 at 8:24 am
    • Posts: 17

      Hi Nemanja,

      Looking at your script, you have indented line 16 too much. The scope of code blocks in Python is evaluated based on the indentation level of the lines. This being the case, Python sees your line 16 as being part of the ‘if statement’ so it only adds a cube everytime the condition “c == columns” is met. If you unindent line 16 by one level, so it starts in the same column as line 11 and 20, then you will see it work correctly.

      As it is, the cubes will be overlapping as a cube is 2 blender units wide and you are inserting one every 1 blender unit. That is why Patrick suggests you scale the cubes in the X and Y directions by half so that each cube has width of 1 blender unit.

      John

      #
      68.1
      Jun 11, 2011 at 9:36 am
  69. Posts: 13

    10Q very much, im noob in python still much to learn
    again thnx this fix my problem

    #
    69
    Jun 11, 2011 at 11:52 am
  70. Denis
    Posts: 5

    So here is my result:
    http://www.youtube.com/user/DDDGamer?feature=mhee#p/u/0/a75MQnQHt0Y
    Thanks BlenderCookie!!!

    #
    70
    Jun 12, 2011 at 11:41 pm
  71. Xbody
    Posts: 1

    Fantastic tutorial.
    Really amazing effect. Thank you!

    #
    71
    Jun 13, 2011 at 2:00 am
  72. Posts: 13

    Hey guys im stuck with my filepath…i dont know what am i doing wrong and how far back should i go in file path this is my python http://www.pasteall.org/22390/python

    im new to python so i dont really understand it enough to see where im making a mistake….
    can anyone help me with this please?

    #
    72
    Jun 13, 2011 at 6:59 pm
    • Posts: 17

      I’m just guessing but maybe you spelt your name wrong :P

      Also the ‘low’ and ‘high’ values should not be set to the same value. You want them to specify a frequency range that will be used for each object. So for each step, if the frequencey increment you want to use it 800Hz then you want

      step = 800
      low = i * step
      high = (i + 1) * step

      i.e. the value for cube 1
      low = 1 * 800 = 800
      high = (1 + 1) * 800 = 1600

      then cube 2
      low = 2 * 800 = 1600
      high = (2 + 1) * 800 = 2400

      [although Python indices start at zero so

      for i in range(10):
      ....print(i)

      will give you values

      0
      1
      2
      3
      4
      5
      6
      7
      8
      9

      but you get the idea]

      This way for each value of i in your loop, the upper limit (high) of the frequency will be one step size (800Hz here) larger than the lower limit (low).

      #
      72.1
      Jun 14, 2011 at 3:38 am
  73. Posts: 13

    i know i spell my name wrong :) figure it out as soon as i post it but i change that and i have same problem, can you tell me how far back in computer file path tree should i go? in tutorial i see only copy paste folder name that shows in blender path…is that enough or should i go like Computer/d:/…….

    #
    73
    Jun 14, 2011 at 6:02 am
    • Posts: 223

      Hey Nemanja,

      It depends on the OS. On OSX the filepath generally starts with /Users/rest/of/path. On Windows I believe the path starts with C:/rest/of/path. I’m not in a position to check right now, but I’ll try out your script when I get home. =)

      #
      73.1
      Jun 14, 2011 at 7:16 am
      • Posts: 87

        Hi Patrick, sorry to post here, but could do with pointing in the right direction.
        Where is the best place for me to get answers, sorry make that lots of answers to the many questions I have about Blender’s BPY API.
        I don’t want to be posting all my questions here.

        I have scoured the official blender bpy api documents but I’m finding things don’t work like the documents say they do.

        Any advice would be great!

        #
        73.1.1
        Jun 14, 2011 at 10:00 am
      • Posts: 223

        Hey SL1200,

        No worries about asking questions; programming can be quite tricky to get into and especially with the constantly changing API not everything’s as straightforward as you might expect it to be.
        The best places to get help on BlenderPython are the BlenderArtist forums on blenderartists.org and the #blendercoders IRC channel on irc.freenode.net. =)

        #
        73.1.2
        Jun 14, 2011 at 6:13 pm
  74. Posts: 13

    thnx, i have windows and ubuntu so im trying in both of them to make it work but so far i haven’t

    #
    74
    Jun 14, 2011 at 9:50 am
    • Posts: 223

      Hi Nemanja,

      John was spot-on with his feedback, though there’s one more thing. The reason your script isn’t working is because you forgot to set the area type to Graph Editor, returning false on the operator’s poll().
      To fix it, just do the following before calling the the operator:
      bpy.context.area.type = ‘GRAPH_EDITOR’

      And that should do the trick! =)

      #
      74.1
      Jun 14, 2011 at 6:10 pm
  75. Posts: 59

    Thank you very much Patrick!

    You could use this to animate bones, eg: jaw movement – speach, singing, etc. Also, for dance moves, if you have the beat (just the kick drum) as an audio file, you could use it to animate basic dancing.

    I don’t know if this way would cut some corners and make some things simpler, but I think it’s possible.

    The possibilities are endless! ^-^

    I think I might need to learn more Python =P

    #
    75
    Jun 14, 2011 at 6:57 pm
  76. Alex
    Posts: 3

    http://www.pasteall.org/22424/python

    i am pretty sure i got your code right, but everytime i try to run the code, it crashes my blender and puts up a windows C++ visualizer error and i cant seem to fix it. I have done alot of research about why this is happening but i cant find the answer.

    #
    76
    Jun 15, 2011 at 11:00 am
  77. Posts: 2

    I’ve been wanting to do audio visualizations in Blender for a while now, so I thank you very much for this tutorial. I’m trying to modify the script so that each cube gets a material assigned to it in the same loop that creates it. I figured out how to create a material slot (bpy.ops.object.material_slot_add()) and how to create a material (bpy.data.materials.new(‘blue’)), but I can’t figure out how to link the material to the material slot. I figured I’d ask since more people might want to know how to do this. FYI – my ultimate goal is to have the color and emit values change to the music as well.

    #
    77
    Jun 15, 2011 at 4:02 pm
    • Posts: 2

      It seems Patrick Boelens already answered my question here: http://www.pasteall.org/22189/python The line of code I need to use is: bpy.context.active_object.active_material = bpy.data.materials['blue']

      Does this make bpy.ops.object.material_slot_add() pointless?

      #
      77.1
      Jun 15, 2011 at 4:36 pm
  78. NollieFlipX
    Posts: 3

    Hello! First off thanks you Patrick for this awesome tutorial.

    I’ve found a small bug on the script, some may be having it so i’ll late a note for them:

    For those who are having problems with this lines: ” bpy.context.active_object.animation_data.action.fcurves[0].lock = true ”

    all you need to do is change it to: ” bpy.context.active_object.animation_data.action.fcurves[0].lock = True ”

    basically just make the “T” on the word “true” a “captal T”, the word will turn blue, =p cheers.

    #
    78
    Jun 15, 2011 at 10:00 pm
    • NollieFlipX
      Posts: 3

      Sorry I was writing the comment while watching the audio XD

      #
      78.1
      Jun 15, 2011 at 10:44 pm
  79. rare
    Posts: 2

    I’ve only watched the tutorial, but after amending with the spiral function, I would have thought this line:
    [code]
    step = 20000 / (rows*columns)
    [/code]
    would want to become:
    [code]
    step = 20000 / (X*Y)
    [/code]

    #
    79
    Jun 16, 2011 at 8:20 am
  80. Danny
    Posts: 2

    hi i am having a problem blender keeps crashing when i go to bake to script? has anyone got any advice?

    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range(0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location = (r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    ####
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 7
    bpy.ops.object.scale_apply()

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True

    bpy.context.area.type = ‘GRAPH_EDITOR’

    step = 18000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath= “C:\Documents and Settings\Dan\My Documents\My Music\Daft Punk- Tron Legacy Original Soundtrack3 – The Son Of Flynn.mp3″, low=i*step,high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves[2].lock = True

    c += 1

    here is the script i use, it says there arent any errors but it crashes?

    #
    80
    Jun 16, 2011 at 1:03 pm
  81. eMugen
    Posts: 3

    I’ve a problem with the code i think. When i run the script in the step of minute 29 of the video

    I get this error:
    http://i51.tinypic.com/2vnlz6p.png

    The code:
    http://pastebin.com/ehYnee4v

    #
    81
    Jun 17, 2011 at 1:46 pm
    • eMugen
      Posts: 3

      Ok i solved this error but now i get one in this line:

      bpy.context.active_object.animation_data.action.fcurves[0].lock=true

      The error only says ‘The script fail, look in the console for now’

      #
      81.1
      Jun 17, 2011 at 1:53 pm
      • eMugen
        Posts: 3

        Solved again. It’s bpy.context.active_object.animation_data.action.fcurves[0].lock = True like the video says later

        #
        81.1.1
        Jun 17, 2011 at 5:07 pm
  82. Great tutorial was a bit scared of doing it at first but it was fun! Looking at a screen for an hour or so wondering where i had gone wrong in the script then fixing it et voila! superb. to all the people who struggle with it persevere its well worth it. mr Boelens any chance of a screen grab of the material? appreciatively thankful.

    #
    82
    Jun 20, 2011 at 11:43 am
  83. Posts: 1

    How did you rendered the Picture on top of the page?
    It looks really awesome!
    Is it made with blender?

    #
    83
    Jun 20, 2011 at 2:16 pm
  84. illincrux
    Posts: 2

    just wanted to point out that pressing ctrl+Right Arrow will step through different layouts including one that’s most similar to the one in the tutorial. By the way, thanks for sharing this with all of us! THESE are the kind of tutorials that everyone could us more of…

    #
    84
    Jun 20, 2011 at 2:59 pm
  85. illincrux
    Posts: 2

    Forgot to mention that you can save the layouts (including the one in the tutorial) that you step through (via ctrl+Right Arrow) by going into into preferences and pressing the Save As Default button. =]

    #
    85
    Jun 20, 2011 at 3:28 pm
  86. Olivier F
    Posts: 1

    I write everything, i think that you have wrote but it’s give me this error:

    Traceback (most recent call last):
    File “C:\PROGRA~2\BLENDE~1\Blender\2.57\scripts\modules\console_python.py”, line 151, in execute
    line = line_object.body
    UnicodeDecodeError: ‘utf8′ codec can’t decode byte 0xe9 in position 43: invalid continuation byte

    That’s what i wrote:
    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range(0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location = (r ,c ,0 ))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -=1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    ####
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 6
    bpy.ops.object.scale_apply()

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True

    bpy.context.area.type =’GRAPH_EDITOR’

    step = 20000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath=”\Users\Félix\Desktop4-the_killers-somebody_told_me-esc.mp3″, low=i*step , high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves[2].lock = True

    c += 1

    #
    86
    Jun 20, 2011 at 10:21 pm
  87. anna
    Posts: 1

    I have problems with the script – my script runs, but I´ll only get 4 Cubes. What´s the matter here????
    This is my script:

    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range(0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location = (r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -=1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    ####
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 5
    bpy.ops.object.scale_apply()

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True

    bpy.context.area.type = ‘GRAPH_EDITOR’

    step = 20000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath=r”C:\Users\Anna\Documents\Blender-Dateien\Musik\Kalimba.mp3″, low=i*step, high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves[2].lock = True

    c += 1

    #
    87
    Jun 21, 2011 at 7:09 am
  88. Jason
    Posts: 1
    #
    88
    Jun 22, 2011 at 3:21 pm
  89. Charles Cortes
    Posts: 1

    Hey Patrick and others…I’m really NOT the programmer type and I am basically banging my head against the desk. I got my script to work all except for this line\n\nbpy.ops.graph.sound_bake(filepath=r”C:\\Documents and Settings\\Owner\\Desktop\\Blender\\northern_lights_revised.mp3″, low=i*step, high=i*step + step)\n\nIt just says python script fail.\nCould it be because I failed to put the step in the same script line? Also how would one do that? \nfor example mine is like this could this be the problem?\n\n32: step = 20000/ (rows*columns)\n33: bpy.ops.graph.sound_bake(filepath=r”C:\\Documents and Settings\\Owner\\Desktop\\Blender\\northern_lights_revised.mp3″, low=i*step, high=i*step + step)\n\nPlease somebody help, thankyou =)

    #
    89
    Jun 24, 2011 at 8:27 pm
  90. Rainer
    Posts: 1

    I searched over one week for the problem why the script crashed the windows version of blender. After some debugging i found out that the path to the mp3 file needs to be written with escaping sequences for the backslash.\nA path like “c:\\test.mp3″ would internally end up as “c:est.mp3″ because the character after the \\ is treated as a special char. To solve this every \\ hast to be escaped and written as \\\\.\nSo the correct path should look like “c:\\\\test.mp3″.

    #
    90
    Jun 25, 2011 at 4:55 pm
  91. Lanceros
    Posts: 2

    My script crashes Blender, any idea why? here it is thanks in advance!

    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range(0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location = (r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    #####
    bpy.context.active_object.scale. x = 0.5
    bpy.context.active_object.scale. y = 0.5
    bpy.context.active_object.scale. z = 7
    bpy.ops.object.transform_apply(scale=True)

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True

    bpy.context.area.type = ‘GRAPH_EDITOR’

    step = 20000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath=”C:\Documents and Settings\Gurtz\My Documents\My Music\Pink Floyd\Dark side of the Moon”, low=i*step, high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves[2].lock = True

    c += 1

    #
    91
    Jun 26, 2011 at 3:18 pm
  92. Lanceros
    Posts: 2

    Bah, i’ve figured it out, didn’t have the actually MP3 File name which happens to be Time.mp3 at the end of my path

    #
    92
    Jun 26, 2011 at 3:29 pm
  93. kutu
    Posts: 2

    how to remove sound_baked curve without remove cube?

    #
    93
    Jun 27, 2011 at 5:44 am
  94. Saltima
    Posts: 1

    hi,

    im a little late for the party but i just wanted to say great tutorial and you may want to note that audio frequencies are not linear which is why you are getting so much action from the lower frequency bars.

    this is how i did the loop to get a exponential spread of the frequencies

    while a < 18000:
    r+=1
    c=0
    scale = 1.2

    ….. content of loop

    a = a * scale

    #
    94
    Jun 27, 2011 at 10:17 pm
  95. Posts: 59

    I’m a little late to the party too. xD

    I’m having difficulties executing the script – the much loved ‘Runtime error’. Then Blender closes.

    My script looks fine, but perhaps I’m missing something:

    import bpy

    rows = 5
    columns = 5

    r = 0
    c = 0

    for i in range(0, rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add(location = (r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
    ####
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 5
    bpy.ops.object.transform_apply(scale=True)

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock=True
    bpy.context.active_object.animation_data.action.fcurves[1].lock=True

    bpy.context.area.type = ‘GRAPH_EDITOR’

    step = 20000/ (rows*columns)
    bpy.ops.graph.sound_bake(filepath=r”C:\Users\Sledge\Desktop\Exported Music\The Jig.mp3)”, low=i*step, high=i*step + step)

    bpy.context.active_object.animation_data.action.fcurves[2].lock=True

    c += 1

    I’m running Blender 2.58.0 r37702 on Windows Vista.

    I’d very much appreciate any help. I’m new to Python, and of course scripting in Blender.

    I’d love to play around with this! :)

    #
    95
    Jun 30, 2011 at 7:02 am
  96. Christopher Baker
    Posts: 3

    I’ve always loved creating audio visualizations, usually in Flash. It never occurred to me to do it in Blender. The first thing I noticed was that it was linear, not logarithmic. Mine is logarithmic though =] (and I added instructions to the comment that the sound engineer made about this)

    http://www.youtube.com/watch?v=EbxH19J5NV4

    #
    96
    Jun 30, 2011 at 6:45 pm
  97. melissa
    Posts: 2

    Patrick (or anyone for that matter),

    how did you apply that great luminescent blue to the squares? is this also done programmatically?

    #
    97
    Jul 1, 2011 at 3:53 am
  98. kutu
    Posts: 2

    my version with dinamically change color of cubes
    http://youtu.be/c1I0XjSaVmg?hd=1

    #
    98
    Jul 1, 2011 at 5:51 am
    • Posts: 3

      hy man, looks kool, i am new in blender and im trying to run the script but i cant get it. your video have another shape, can you share your script to understand a little more about this? thanks man ;)

      #
      98.1
      Jan 3, 2012 at 11:17 am
    • Posts: 43

      I agree, could you please upload a copy of your script so that it may be analysed and I can further my study of python. This would be much appreciated. thank-you.

      #
      98.2
      Apr 20, 2012 at 8:32 pm
  99. Posts: 2

    Hi,
    first of all i would like to thank you for another great tutorial. It was very easy to follow and it all worked fine for me. But when I render it, there is no sound, and I don’t know what do I have to do for it to be there.

    #
    99
    Jul 4, 2011 at 5:23 am
  100. Posts: 8

    With the new API changes in line 24 change it from what Patrick does to:
    bpy.ops.object.transform_apply(scale = True)
    and it should work with the scaling. :D

    #
    100
    Sep 3, 2011 at 4:49 pm
  101. Chris
    Posts: 7

    I have a Toshiba Laptop that has crashed a few times (which might add to my problem) and whenever I run the script, Blender crashes, I’ve downloaded optimized builds and it still doesn’t work. Is there any way for this to be fixed?

    #
    101
    Sep 3, 2011 at 5:40 pm
  102. Phil Unrue
    Posts: 1

    Everytime i run the script, it shuts down my blender and i even deleted blender to download the new update.

    heres my script…

    http://pastebin.com/HKwGcLMF

    #
    102
    Sep 12, 2011 at 5:17 pm
  103. ivan2340
    Posts: 1

    can someone tell me if that would work in Blenders GameEngine?? Thx in advance

    #
    103
    Dec 8, 2011 at 2:50 pm
    • superseeed
      Posts: 1

      as ivan2340
      can you do that in realtime ?
      game engine script ?

      #
      103.1
      Dec 11, 2011 at 2:55 pm
  104. Posts: 1

    Hello! I’ve loved your video, it’s very helpful and easy to understand even for a newbie in python programming like me ;)
    Now I’m working in a project and the first step is to make blender “hear what I hear”, I mean make the same thing as you but without baking the sound, with a real time audio. Is this possible without using the game engine?
    By the way, happy new year! :D

    #
    104
    Dec 30, 2011 at 2:54 pm
    • Colonel
      Posts: 2

      I second this question. Is there a way to make it live ? Can someone point us some ressources ?
      And thanks for this great turial, by the way :-)

      #
      104.1
      Apr 19, 2012 at 8:12 am
  105. Posts: 3

    hello everybody, im trying to run this script but i get an error, this is code is the same of the most of you.

    import bpy

    rows = 6
    columns = 6

    r = 0
    c = 0

    def spiral(X, Y):
    x = y = 0
    dx = 0
    dy = -1
    for i in range(max(X, Y)**2):
    if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
    bpy.ops.mesh.primitive_cube_add (location = (x, y, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')
    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 5
    bpy.ops.object.scale_apply()
    bpy.ops.anim.keyframe_insert_menu(type = 'Scaling')
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True
    bpy.context.area.type = 'GRAPH_EDITOR'
    step = 22000/ (rows*columns)
    bpy.ops.graph.sound_bake (filepath ="C:\\Users\Mateo Contreras\Desktop\animacion audio blender\song.mp3", low=i*step, high=i*step+step)
    bpy.context.active_object.animation_data.action.fcurves[2].lock = True
    c += 1
    if x == y or (x 0 and x == 1-y):
    dx, dy = -dy, dx
    x, y = x+dx, y+dy

    spiral(rows, columns) >>>>>>>>>> HERES IS THE ERROR

    i dont know what is the error, thanks for the help guys ;)

    sorry for my sintax, my english is not good.

    #
    105
    Jan 3, 2012 at 11:03 am
    • Posts: 3

      I am using blender 2.61

      #
      105.1
      Jan 3, 2012 at 11:58 am
      • burnoutbbs
        Posts: 1

        Hi Luis,

        the problem is most likely that bpy.ops.object.scale_apply() does not exist anymore and the scale is directly applied by calling bpy.context.active_object.scale.. so just delete that line and you should be fine :)

        cheers

        #
        105.1.1
        Jan 10, 2012 at 8:03 am
      • Stewart
        Posts: 1

        Hey, Luis, I got the same problem. I corrected all the code for new changes, like the bpy.ops.object.transform_apply(scale=True), but I still got the same result.
        I too, am using 2.61, and it consistently lights up the last row: spiral(rows, columns)

        If you manage to find a solution, that would be wonderful.

        #
        105.1.2
        Feb 5, 2012 at 9:24 pm
      • anpaw76
        Posts: 8

        Hi guys,

        I found out in Blender 2.61 tha tthe line should looks like :
        bpy.ops.object.transform_apply(scale = True)

        And it works for me just fine.

        #
        105.1.3
        Feb 22, 2012 at 6:11 am
    • anpaw76
      Posts: 8

      @Injeklition – you need to make sure that the spiral(rows, columns) will be at the same position as the initial definition : def spiral (X, Y):
      like

      def spiral (X, Y):
      smth
      smth
      smth2
      smth2
      spiral(rows, columns)

      Also, your bpy.ops.graph.sound_bake has wrong slashes. On Windows you need to use “C:/…/” instead of “C:\…\”.

      I hope it helps you a bit?

      #
      105.2
      Feb 22, 2012 at 6:50 pm
      • anpaw76
        Posts: 8

        hm… it removes spaces, so let’s try again. Sorry for multiposting. I can’t find a way to edit or delete my post. Sorry again.

        def spiral (X, Y):
        ##smth
        ##smth
        ####smth2
        ####smth2
        spiral(rows, columns)

        take # as spaces in Blender’s text editor.

        #
        105.2.1
        Feb 22, 2012 at 6:52 pm
  106. Posts: 1

    Hello!
    I’m new to blender but the learning process goes pretty well and easy. Regarding this tutorial I have a question: Can you move the cubes in an aleatory position? like click and drag?
    For example in a project for school I want to make the buildings from a 3d presentation of a landscape architecture project move like an audio visualizer or an vu meter.
    Could that be done in blender?
    Thank you!

    #
    106
    Jan 12, 2012 at 3:45 am
  107. anpaw76
    Posts: 8

    Hi All,

    I am struggling with the ‘unsupported audio file’ error. I have tried mp3 16-bit, 32-bit fload, wav, different files. Always the same error.

    It works when I am using the method explained by Patrick at the begining of video – to manually bake it to the graph editor.

    Any ideas why it doesn’t work via script, please?

    #
    107
    Feb 22, 2012 at 5:44 am
    • anpaw76
      Posts: 8

      And I have got answer to my question… I didn’t paid attention to the slashes in the filepath.
      On Windows it should be : “C:/folder/file.extension” and NOT “C:\folder\file.extension”.
      Tiny thing which is easy to miss out… ;)

      #
      107.1
      Feb 22, 2012 at 6:45 pm
  108. Cinos
    Posts: 5

    can somebody help me every time i run my script it says there’s a problem with

    c += 1

    thats all if gives me

    File “\Text”, line 40
    c += 1
    ^
    SyntaxError: Invalid syntax
    location::-1
    Python script fail,look in console for now…

    #
    108
    Feb 24, 2012 at 1:39 am
    • anpaw76
      Posts: 8

      Have you tried :
      c = c + 1
      ?

      I also had an arrow with c += 1, so I changed it to the above and it worked.

      #
      108.1
      Feb 25, 2012 at 2:14 pm
      • Cinos
        Posts: 5

        Thanks for the help but it still isn’t working, here my script so far maybe you can find whats wrong, if you want to make some adjustments to it go ahead.

        import bpy

        rows = 5
        columns = 5

        r = 0
        c = 0

        for i in range(0, rows*columns):
        if c == columns:
        r += 1
        c = 0

        bpy.ops.mesh.primitive_cube_add(location = (r*2, c*2, 0))
        bpy.context.scene.cursor_location = bpy.context.active_object.location
        bpy.context.scene.cursor_location.z -= 1
        bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)
        ####
        bpy.context.active_object.scale.x = 0.5
        bpy.context.active_object.scale.y = 0.5
        bpy.context.active_object.scale.z = 5
        bpy.ops.object.transform_apply(scale=True)

        bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
        bpy.context.active_object.animation_data.action.fcurves[0].lock = True
        bpy.context.active_object.animation_data.action.fcurves[1].lock = True

        bpy.context.area.type = ‘GRAPH_EDITOR’

        step = 20000/ (rows*columns)
        bpy.ops.graph.sound_bake(“C:\Users\DJ\Desktop\HAX\Minecraft2.mp3″ (low=i*step, high=i*step + step)

        c = c + 1

        #
        108.1.1
        Feb 26, 2012 at 9:39 am
      • anpaw76
        Posts: 8

        on first look I would suggest to change the link to the file. Use “/” instead of “\” :
        C:/Users/DJ/Desktop/HAX/Minecraft2.mp3

        #
        Feb 26, 2012 at 1:15 pm
      • Cinos
        Posts: 5

        i had it like that before the c += 1 problem came up and changed it, thanks for the idea, this maybe important but i’m on a windows computer! thanks.

        #
        Feb 29, 2012 at 2:02 pm
      • anpaw76
        Posts: 8

        I’m on windows as well. Normal slashes don’t work for Blender, you need to use / instead.

        #
        Mar 3, 2012 at 7:24 am
  109. Monchi
    Posts: 2

    had some problems with the script, but finally it works fine ! ^^
    Blender version is 2.61
    Blender 2.53 gives an Error @bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)

    import bpy

    rows = 1
    columns = 3

    r = 0
    c = 0

    for i in range(0,rows*columns):
    if c == columns:
    r += 1
    c = 0

    bpy.ops.mesh.primitive_cube_add (location = (r, c, 0))
    bpy.context.scene.cursor_location = bpy.context.active_object.location
    bpy.context.scene.cursor_location.z -= 1
    bpy.ops.object.origin_set(type=’ORIGIN_CURSOR’)

    bpy.context.active_object.scale.x = 0.5
    bpy.context.active_object.scale.y = 0.5
    bpy.context.active_object.scale.z = 5
    bpy.ops.object.transform_apply(scale=True)

    bpy.ops.anim.keyframe_insert_menu(type=’Scaling’)
    bpy.context.active_object.animation_data.action.fcurves[0].lock = True
    bpy.context.active_object.animation_data.action.fcurves[1].lock = True
    bpy.context.area.type = ‘GRAPH_EDITOR’

    step = 20000 / (rows*columns)
    bpy.ops.graph.sound_bake(filepath=”1.mp3″,low=i*step, high=i*step+step)
    bpy.context.active_object.animation_data.action.fcurves[2].lock = True
    c += 1

    #
    109
    Mar 2, 2012 at 4:21 am
    • Cinos
      Posts: 5

      thanks for the help but where do i put the music into this script? im asking before hand because i feel as though if i edit something i may brake it

      #
      109.1
      Mar 2, 2012 at 2:32 pm
  110. Monchi
    Posts: 2

    bpy.ops.graph.sound_bake(filepath=”1.mp3″,low=i*step, high=i*step+step)
    put the file 1.mp3 and the .blend file into the same folder.

    #
    110
    Mar 3, 2012 at 4:24 am
    • Cinos
      Posts: 5

      1.mp3 and .blend files, what are those, i havent heard of that yet

      #
      110.1
      Mar 3, 2012 at 6:38 am
  111. James
    Posts: 2

    I keep getting this error “BYpOpsSubMod not callable” and it focuses around the calling of the function (“sample (rows, columns)” and the “low i=…”
    did the API change drastically since this tut? I am using 2.6 btw

    #
    111
    Mar 18, 2012 at 3:40 pm
    • James
      Posts: 2

      I guess I should include the code I am using:

      import bpy

      rows = 5
      columns = 5

      r = 0
      c = 0

      def spiral(X, Y):
      x = y = 0
      dx = 0
      dy = -1
      for i in range(max(X, Y)**2):
      if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
      bpy.ops.mesh.primitive_cube_add(location=(x, y, 0))
      bpy.context.scene.cursor_location = bpy.context.active_object.location
      bpy.context.scene.cursor_location.z -= 1
      bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')
      ####
      bpy.context.active_object.scale.x = 0.5
      bpy.context.active_object.scale.y = 0.5
      bpy.context.active_object.scale.z = 5
      bpy.ops.object.transform_apply(scale=True)

      bpy.ops.anim.keyframe_insert_menu(type = 'Scaling')
      bpy.context.active_object.animation_data.action.fcurves[0].lock = True
      bpy.context.active_object.animation_data.action.fcurves[1].lock = True

      bpy.context.area.type = 'GRAPH_EDITOR'

      step = 20000/ (rows*columns)

      bpy.ops.sound_bake(filepath=r"C:/Users/Home/Desktop/Angular Momentum.mp3", low=i*step, high=i*step + step)

      bpy.context.active_object.animation_data.action.fcurves[2].lock = True

      if x == y or (x 0 and x == 1-y):
      dx, dy = -dy, dx
      x, y = x+dx, y+dy

      spiral(rows, columns)

      #
      111.1
      Mar 19, 2012 at 11:28 am
      • Posts: 43

        hello. Above you have if x == y or (x 0 and x == 1-y):
        and I do believe that you require:
        if x == y or (x 0 and x == 1-y):
        You forgot the < (less than) symbol.
        Hope this helps

        #
        111.1.1
        Apr 13, 2012 at 9:54 am
  112. Posts: 2

    Help please!
    I keep getting errors starting at line 17 (bpy.context.scene etc.)
    The error only says “Invalid syntax”
    I am using Blender 2.62.
    Thank you in advance :)

    #
    112
    Mar 26, 2012 at 1:37 pm
  113. Posts: 43

    Hello Patrick, firstly I would like to congratulate you on such an amazing tutorial, very straightforward and easy to follow.
    That being said I have run into a problem, when I run my script I only get 3 Columns appear and they are in a diagonal fashion.

    e.g.
    |
    |
    |

    They still pulsate.

    I have accommodated for the new release and modified my script accordingly.
    I am really frustrated by this as I have spent an hour and a half going over and over my script comparing it to yours and I have concluded that it is identical apart from one little change. (applying scale is different and true is defined)
    but I can’t be as a result of the change because the script worked perfectly before I added the spiral.

    I tried increasing the column and row values to 10 but it only resulted in me having 5 diagonal columns instead of 3.

    here is a copy of my script.

    http://www.pasteall.org/30891/python

    All help welcome. I am at a point of desperation and a quick response would be greatly appreciated

    #
    113
    Apr 13, 2012 at 9:35 am
    • Posts: 43

      ignore the example, the posting system screwed it up

      #
      113.1
      Apr 13, 2012 at 9:36 am
  114. Posts: 2

    Oh, this tutorial is incredibly helpful to me right now – thank you very much!

    #
    114
    May 5, 2012 at 9:45 am

Leave a Comment