# Unknown Syntax Error in Code

So, I am following a tutorial on CGCookie, and it is using Python code to create an audio visualizer in Blender. Now, I've always been a coding geek, but I don't actually know Python. I've taken a few shots at it but overall, I don't know Python, however, I can read and understand the code because I know basic programming concepts. Now, I'm following this tutorial as closely as I can (the tutorial does not provide code to copy and paste) and I am getting a syntax error. As far as I can tell, there is no syntax error. The line of code (specified by the error message) appears perfect. This line of code is supposed to set the 3D cursor to the active object's origin. Here's the catch: if I run that exact same line of code in the console, it executes just fine.
So here's the error message I get:

File "C:\Me\Blender\Blender Projects\audio visualisation.blend\AudioVisualizer", line 17
bpy.context.scene.cursor_location = bpy.context.active_object.location
^
SyntaxError: invalid syntax
location: <unknown location>:-1


It tells me to look in the console for error message, but the console doesn't output anything.

Here's my overall code:

Thanks!

EDIT: Alright. I've tried all the examples and all the procedures that have been suggested by you guys, but it keeps feeding me errors. According to the console, one of the problems it is spitting out is in C:\Program Files\Blender Foundation\Blender\2.74\scripts\modules\bpy\ops.py. This makes me think there is a bug in the API that is causing one of the many errors.

EDIT: Here are the error messages I get for my code. Original Code:

Error:   File "C:\Gannon\Blender\Blender Projects\audio visualisation.blend\Audi
o Visualizer", line 35
bpy.ops.graph.sound_bake(filepath="C:\Gannon\Blender\Sounds\Music\Batty McFa
dden.mp3", low=i*step high=i*step+step

^
SyntaxError: invalid syntax


gandalf3's Code and zeffii's Code are essentially the same with alternative formatting which changes the line numbers where the error occurs.

Error: Unsupported audio format

Traceback (most recent call last):
File "C:\Gannon\Blender\Blender Projects\audio visualisation.blend\Audio Visua
lizer", line 42, in <module>
File "C:\Gannon\Blender\Blender Projects\audio visualisation.blend\Audio Visua
lizer", line 35, in spiral
File "C:\Program Files\Blender Foundation\Blender\2.74\scripts\modules\bpy\ops
.py", line 189, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Unsupported audio format

• @zeffii it really doesn't look any different. I've tried everything that you and gandalf3 has suggested, so that's basically what it looks like now. – TheMinecraftMan757 May 11 '15 at 22:15
• @zeffii Right. I still get many error messages. – TheMinecraftMan757 May 11 '15 at 22:18
• @zeffii how do you copy error messages to your clipboard? – TheMinecraftMan757 May 11 '15 at 22:34
• in windows, howtogeek.com/howto/windows-vista/… – zeffii May 11 '15 at 22:37

You got pretty close TheMinecraftMan757, i've seen many people struggle with that tutorial over the last 3 or 4 years. I think even some of the function calls have been changed.

as Gandalf points out:

to scale_apply() on line 24 should be transform_apply(scale=True)

At the time the tutorial was written, the function call was scale_apply(), but it was changed to a more general apply function that can apply not just scale, but rotation and location. You would never have figured that out in a million years without a bit of googling or more familiarity with bpy.

here's my minimized interpretation of that code.

import bpy

rows = 5
columns = 5

def spiral(X, Y):
scn = bpy.context.scene  # an alias, lets you use scn shorthand
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):
obj = bpy.context.active_object

scn.cursor_location = obj.location
scn.cursor_location.z -= 1
bpy.ops.object.origin_set(type="ORIGIN_CURSOR")
obj.scale = (0.5, 0.5, 7)

bpy.ops.object.transform_apply(scale=True)

action = obj.animation_data.action
action.fcurves[0].lock = True
action.fcurves[1].lock = True

bpy.context.area.type = 'GRAPH_EDITOR'

step = 20000 / (rows * columns)
bpy.ops.graph.sound_bake(filepath=fp, low=i*step, high=i*step+step)
action.fcurves[2].lock = True

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

spiral(rows, columns)


If you don't set the path to a valid one, then you can get errors like

RuntimeError: Error: Unsupported audio format


remarks:

• On windows use r'C:\your\file\destination.mp3', the r turns the quoted string that follows it into a raw string literal, this is perfect for filepaths.
• On linux you should really know how to compose a path, but if you're new to it, it might be something like: '/homes/your_user_name/music/some_tune.mp3'
• This code can take a long time to bake each graph. Even with a 5*5 grid, it will bake the entire graph 25 times. Here's a version which places a print statement just before it starts baking each iteration, you will need to have the terminal/cmd-prompt open before running the command.

import os

# on windows, string prefix r is used to avoid the need for double backslashes

# on POSIX systems (linux, osx)
# filepath = '/home/zeffii/cookoo_22may_2011.mp3'

found = os.path.exists(filepath)
if found:
print('yes, this file was found')
else:
print('nope, the path does not exist')

• Hey! It actually works! I used your testing script and found that it wouldn't accept the file path. I tried putting it in a completely different folder, and that actually did work! I am kind of wondering if the original file location was somehow restricted. I don't know. Anyway, you fixed it! Thank you @zeffii! – TheMinecraftMan757 May 12 '15 at 17:01
• it's OK, paths have tripped up many coders. onwards! – zeffii May 12 '15 at 17:02

Oftentimes mysterious syntax errors result from something above where the actual error is raised. In this case the syntax error in question is caused by a missing paren on the line above:

In addition to this missing paren you have a few other typos:

• + instead of _ on line 22

• scale_apply() on line 24 should be transform_apply(scale=True).

• Missing a comma and closing paren on line 35

Here's the fixed code:

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

• using r"c:\..\...\...\" is better or forward slashes, which are now universal – zeffii May 11 '15 at 20:15
• @zeffii Hm.. Is the drive letter case sensitive? I always thought they were upper case.. (and what's the r for?) – gandalf3 May 11 '15 at 20:18