VS Code配置¶
此节基于Bilibili Up主 pdcxs 的文章: 配置Manim自动编译环境(仅限VS Code Win) 进行改进.
如果你使用的是Visual Studio Code的话,可以配置自动编译,而不需要每一次手动输入 python manim.py example_scenes.py SquareToCircle -pl
这样一长串的代码.
配置tasks.json¶
在工作目录下的 .vscode
中(如果没有就创建一个)创建一个 tasks.json
文件(如果已经有了,直接修改即可).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | {
"version": "2.0.0",
"tasks": [
{
"label": "l",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}\\compile_shell.py",
"${relativeFile}",
"${lineNumber}",
"-pl"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "best",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}\\compile_shell.py",
"${relativeFile}",
"${lineNumber}",
"-p"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "m",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}\\compile_shell.py",
"${relativeFile}",
"${lineNumber}",
"-pm"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "s",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}\\compile_shell.py",
"${relativeFile}",
"${lineNumber}",
"-s"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "i",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}\\compile_shell.py",
"${relativeFile}",
"${lineNumber}",
"-il"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
]
}
|
每一个任务都有一个标签(可以自定义修改),与其任务的输出格式是相对应的;如第 5
行的 l
代表的是低分辨率输出,相应的 第 12
行 -pl
代表低分辨率渲染,并用默认播放器打开;其他任务同理.
l
:低分辨渲染m
:中分辨渲染b
:最高分辨渲染,输出品质i
:输出gif格式s
:低分辨率保存最后一帧
可以自定义添加更多的指令.渲染指令请参考渲染设置.
配置compile_shell.py¶
在主工作目录下,添加 compile_shell.py
文件.这个文件是用于寻找当前要编译的动画类的名称.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import sys
import os
import linecache
file_relative_dir=sys.argv[1]
linenumber=int(sys.argv[2])
setting=sys.argv[3]
while linenumber:
text=linecache.getline(file_relative_dir,linenumber)
if 'class' in text:
escape_index=text.find(' ')
brackets_index=text.find('(')
class_name=text[escape_index:brackets_index]
linecache.clearcache()
break
else:
linenumber-=1
commands=[
'D:\Anaconda3\python.exe', # my anaconda dir -> to replace it with yours
'manim.py',
file_relative_dir,
class_name,
setting
]
os.system(' '.join(commands))
|
第 20-26
行是编译的命令,第 21
行是指定的编译器,由于我的电脑同时安装了Anaconda和Python,为了避免冲突,这里可以自行指定编译器;你需要自行替换一下.如果选择使用默认的Python,把第 21
行改为 python
即可.
这里的机制是在你的鼠标所在行向上搜索类的名称,然后放入命令行进行自动编译.原参考的解决方案是使用Poweshell,这对于非Windows平台是不支持的,这里改成Python脚本.理论上来说,只要能装上VS Code,都能实现这样的自动编译.