我正在尝试使用 Python 在 Blender 中制作音频可视化工具,但我的代码不断抛出此语法错误。
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)
请您参考如下方法:
编辑:
在这一行:
bpy.ops.graph.sound_bake(filepath=r"C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step)
文件路径字符串中的 \U 被解释为转义序列:
>>> '\U'
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
>>>
要解决此问题,请在字符串前放置一个 r:
bpy.ops.graph.sound_bake(filepath=r"C:\Users\Callum\Desktop\Teardrop.mp3", low= i*step, high=i*step+step)
这会将其转换为 raw-string ,不处理转义序列:
>>> r'\U'
'\\U'
>>>
另一种解决方案是将反斜杠变成正斜杠:
bpy.ops.graph.sound_bake(filepath="C:/Users/Callum/Desktop/Teardrop.mp3", low= i*step, high=i*step+step)
即使您运行的是 Windows,Python 在文件路径中使用正斜杠也能正常工作。
