Callum
Callum

Reputation: 85

Unicode syntax error using graph.sound_bake in Blender

I'm trying to make a audio visualizer in Blender, using Python, and my code keeps throwing this syntax error.

SyntaxError:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape location: unknown location :-1
# python file
bpy.ops.graph.sound_bake(filepath="C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step)

Upvotes: 1

Views: 2059

Answers (2)

user2555451
user2555451

Reputation:

Edit:

On this line:

bpy.ops.graph.sound_bake(filepath=r"C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step)

the \U in the filepath string is being interpreted as an escape sequence:

>>> '\U'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
>>>

To fix the problem, place an r before the string:

bpy.ops.graph.sound_bake(filepath=r"C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step)

This will convert it into a raw-string, which do not process escape sequences:

>>> r'\U'
'\\U'
>>>

An alternate solution would be to turn the backslashes into forwardslashes:

bpy.ops.graph.sound_bake(filepath="C:/Users/Callum/Desktop/Teardrop.mp3", low= i*step, high=i*step+step)

Even though you are running Windows, Python works just fine with forwardslashes in filepaths.

Upvotes: 5

LucasB
LucasB

Reputation: 3543

I see two things:

if c == columns:
r += 1
c = 0

should be

if c == columns:
    r += 1
    c = 0

and

bpy.ops.graph.sound_bake(filepath="C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step

is missing the closing parenthesis.

Upvotes: 1

Related Questions