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 got the author wrong again
This sounds like an awesome tutorial, though.
Ah man! Fixed again
-Jonathan
Hello sorry to go of topic i was wondering how to send a request for a tutorial
You can submit tutorial requests on our support forums here: http://support.cgcookie.com/categories/20004388-tutorial-requests
-Jonathan
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!!
Is there a possibility to multiply/scale the F-Curves?
i got the same error..plz help m8
I think you have to use / instead of \ for the path
Stefan is correct, I encountered the same problem because I am running windows the / in the hierarchie appears as a \ and blender only reads /.
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
. Keep up the great work!
Awesome; welcome aboard the Python train! =D
haha nice,
I wanted to do something like this a while ago, now I have it
Thank you!
awesome I’ve always wanted to do this, but since the video isn’t working could we have a link to it?
Its saying that its having a problem with this line… bpy.context.active_object.animation_data.action.fcurves(0).lock = true
Any ideas?
same problem, but i wrote [] instead of ()
bpy.context.active_object.animation_data.action.fcurves[0].lock = true
Have you tried using “True” with a capital T?
Yes i did try the caps True and it still didnt work.
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
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″.
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
———————————————
Can I make a job out of doing tutorials all day long??! There’s never enough time!!!
Jeremy Deighan
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
i´ve got a problem with the last line of the script (spiral(rows, columns)
it´s says python script fail
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.
it says Error: Python script fail, look in the console for now
Could you paste what it says in the console? It should give you a line number and a description of the error.
there is nothing else in the console, just the usual stuff.:(
That’s really weird. Would you mind uploading your script to pasteall.org or pastebin.com so I can take a look at it?
there it is http://www.pasteall.org/22143/text
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.
Thanks for jumping in Dudeplayer; you’re absolutely right. =)
Thanks for the help^^
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
This is a very cool tutorial. Thanks a lot for it! Will try it out!!
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
This is really nice! I’d like to see more videos on Python, too.
tx
Btw, how did you create that cool lightning for the picture?
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. =)
Great tutotial!!!
Patrick Booleans
I better be a True one then.
=P
THANKS !!
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.
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. =)
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
@ 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
@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
.
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
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
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
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
thanx, i had this problem. My song started with 2….
Got the same probleme. Thanks;)
OMG this is so geeky i just soiled my pants with binary, excuse me while i go change them!
Great tutorial!
Be sure to wash out all the dirty Bits later!
[/geekjoke]
Glad you liked the tutorial! =)
When I click run script nothing happens
Hey Chaosbadger,
Did you make sure to call the spiral-function using spiral(rows, columns)?
Yep
In that case, could you please upload the script to pasteall.org or pastebin.com so I could take a look at it?
http://pastebin.com/p7qwdfcr
Blender v. 2.57
http://pastebin.com/p7qwdfcr
I am using blender 2.57
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
Gaah, another API change?! x.x Thanks for the heads up Thomas!
You’re welcome! Great tutorial, the first one I bought here on this site! I love Audio!
thats what I was waiting for I’m saying this even I havent seen the video yet
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
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
uuh, It might look like I’m just spamming here right now, but that’s not the case
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?
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
ow, ok, that’s kind of stupid of me
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
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?
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”
http://www.pasteall.org/22216/python
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
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
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!
Great tutorial, my first to Python. I have one question, how add color to animation in the Python?
You can just insert a keyframe for any specific channel (ie: diffuse_color) and bake the curve to that. =)
how would the python code look for having the emit value of a new material baked to sound, instead of scaling (or with scaling)?
Hey Ethan,
I’ve uploaded the script to bake it to the emit channel here: http://www.pasteall.org/22189/python
Hope this helped! =)
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’
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?
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]‘
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
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?
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?
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)
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. =/
Im getting an error: python script fail, look in console for now…
this is my code http://www.pasteall.org/22164/python
any ideas?
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! =)
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?
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)
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.
This was the problem i figured out after a couple of tries.
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!
Hey Javier,
After the “step = 20000/ (rows*columns)” you have to make a new line for the rest of that.
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
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.
I thought I fixed that at some point… Thanks, now it works!
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
That’s great to hear; congratulations! =)
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….
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
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’))
it works now but the last line of code has and error:
spiral(rows, columns)
the script
http://www.pasteall.org/22195/python
How weird, I can’t seem to be able to reproduce the error on my end. Is the console telling you anything useful? =/
I like the tut but I keep having a script error with line 16 and I’m not sure why?
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?
Here is what I have so far
http://pastebin.com/3DJwUvUZ
Thanks for the help!
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! =)
Thanks so much!!!
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.
Just great !! I like it
Thanks !!
http://pastebin.com/p7qwdfcr
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! =)
That fixed the script, but I then get a Microsoft Visual C++ Runtime Library error
I have the latest update for the C++ but I still get the error
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?
Yep, that helped, thanks
Wow! Thanks. Great tutorial!!!
Hi. I’m really stuck quite early on, just can’t get the script to do anything when I run it – any ideas?
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. =)
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
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. =)
Terrific tutorial Patrick, You just light my fire! The bakery only keeps getting better.
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.
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
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?
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.
Hello, can this bring back the .blend to the audio file?
ps: didn’t watch the tut yet.
Hi Dhia,
I’m afraid I don’t understand what you’re asking. Could you rephrase your question please?
Its says i got this line wrong:
bpy.ops.mesh.primitive_cube_add(location = (r*2, c*2, 0))
so do i!
That’s really odd.. Did you make sure to import bpy at the top of your script?
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.
should read – I’m going for 20000/25 = 800/25 = 32
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?
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! =)
Thanks again for this tutorial. My first video in blender: http://youtu.be/AW_xhxCW07A . Thanks again
Hey, it looks awesome; great job! =D
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.
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?
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
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
Cool, it seems to work perfectly! Thanks for sharing! =)
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
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) =)
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)
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
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?
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!
Hi Stephen, did you make sure to type ‘import bpy’ at the top of your script?
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?
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)
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.
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
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
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!
It says that the python script fail check in the console.
Any ideas?
-Xiong
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. =)
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
Hey Peter,
Seems you’ve forgotten some indentation from line 37 onwards. That should do the trick! =)
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.
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.
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!
Thank you both of you.
Using what both of you have said, the scene crashed. The sound file was only 150 Kb and it is .mp3. Have any ideas?
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.
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
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!
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?
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
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. =)
Hey you are so great, Patrick!!!
Thanks a hundred, no a thousand times!
Keep your awesome tutorials coming please!
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
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
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.
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! =)
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
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
Oh, good call! =)
Here is mine
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
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! =)
And of course spaces don’t work here… Oops! Here’s the example as it should be: http://www.pasteall.org/22288/python
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.
Perfect explanation! Thanks a bunch for helping me answer these questions by the way, it’s much appreciated! =)
I have an error in the last line (line 42)
and I doun´t know why.
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
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.
Thanks a ton, i am not really much of a programmer. Thanks for the help
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
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.
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!
).
I think i will go and buy it, i didn’t even realize i could i am new to this site.
thanks
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
Hey Nino!
The script seems to work fine here. Are you sure you got the file path right? =)
@ 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.
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.
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
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.
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
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
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
Very helpful, thanks a lot.
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
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.
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
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.
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.
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
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?
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…
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
10Q very much, im noob in python still much to learn
again thnx this fix my problem
So here is my result:
http://www.youtube.com/user/DDDGamer?feature=mhee#p/u/0/a75MQnQHt0Y
Thanks BlenderCookie!!!
Fantastic tutorial.
Really amazing effect. Thank you!
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?
I’m just guessing but maybe you spelt your name wrong
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).
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:/…….
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. =)
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!
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. =)
thnx, i have windows and ubuntu so im trying in both of them to make it work but so far i haven’t
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! =)
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
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.
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.
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?
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.
Sorry I was writing the comment while watching the audio XD
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]
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?
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
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’
Solved again. It’s bpy.context.active_object.animation_data.action.fcurves[0].lock = True like the video says later
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.
How did you rendered the Picture on top of the page?
It looks really awesome!
Is it made with blender?
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…
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. =]
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
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
Here’s mine!
http://www.youtube.com/watch?v=F3IMWfHzDfE
loving the music!
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 =)
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″.
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
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
how to remove sound_baked curve without remove cube?
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
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!
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
Patrick (or anyone for that matter),
how did you apply that great luminescent blue to the squares? is this also done programmatically?
my version with dinamically change color of cubes
http://youtu.be/c1I0XjSaVmg?hd=1
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
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.
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.
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.
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?
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
can someone tell me if that would work in Blenders GameEngine?? Thx in advance
as ivan2340
can you do that in realtime ?
game engine script ?
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!
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
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.
I am using blender 2.61
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
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.
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.
@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?
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.
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!
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?
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…
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…
Have you tried :
c = c + 1
?
I also had an arrow with c += 1, so I changed it to the above and it worked.
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
on first look I would suggest to change the link to the file. Use “/” instead of “\” :
C:/Users/DJ/Desktop/HAX/Minecraft2.mp3
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.
I’m on windows as well. Normal slashes don’t work for Blender, you need to use / instead.
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
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
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.
1.mp3 and .blend files, what are those, i havent heard of that yet
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
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)
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
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
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
ignore the example, the posting system screwed it up
Oh, this tutorial is incredibly helpful to me right now – thank you very much!