在Blender中使用graph.sound_bake的Unicode语法错误

Cal*_*lum 1 python syntax-error

我正在尝试使用Python在Blender中制作一个音频可视化器,而我的代码不断抛出此语法错误。

SyntaxError:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape location: unknown location :-1
Run Code Online (Sandbox Code Playgroud)
SyntaxError:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape location: unknown location :-1
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 5

编辑:

在这行上:

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

文件\U路径字符串中的被解释为转义序列:

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

要解决此问题,请r在字符串前放置一个:

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

这会将其转换为原始字符串,不处理转义序列:

>>> r'\U'
'\\U'
>>>
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是将反斜杠转换为正斜杠:

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

即使您正在运行Windows,Python在文件路径中使用正斜杠也可以正常工作。