F3D 命令系统完全指南:交互式控制台、命令脚本与 libf3d 命令参考
【免费下载链接】f3dFast and minimalist 3D viewer.项目地址: https://gitcode.com/GitHub_Trending/f3/f3d
F3D 是一个快速且极简的 3D 查看器,除了命令行参数外,它还提供了一套内置的命令(Commands)系统,用于触发那些无法通过命令行直接完成的行为。本文以 doc/user/07-COMMANDS.md 为主体,结合application/F3DStarter.cxx、library/src/interactor_impl.cxx等源码与testing/scripts/下的真实脚本测试,系统讲解命令的语法规则、libf3d 内置命令、F3D 应用层命令、选项域(Domain)机制,以及命令脚本与交互式控制台两种执行方式,帮助你从命令行、配置绑定、脚本自动化三个维度全面掌握 F3D 的命令能力。
注意:命令系统目前仍处于实验性阶段,行为、动作可能在没有弃用通知的情况下被添加或移除,动作名与参数也可能随时变化(见 doc/user/07-COMMANDS.md)。
命令的三种访问入口
F3D 命令可通过以下三种方式触发:
- 交互式控制台(Interactive Console):构建时启用
F3D_MODULE_UI后,按Esc打开控制台输入命令; - 命令脚本(Command Script):通过
--command-script命令行选项执行一个纯文本脚本文件,例如f3d --command-script path/to/command_script.txt; - 按键绑定配置(Bindings):在 配置文件 中将命令绑定到键盘按键,交互时按对应键即可触发。
所有命令采用统一的语法:action [args],即“动作名 + 可选参数”。
命令语法:类 Bash 的 Token 解析规则
命令的语法与 bash 类似,会按 “token” 切分后再处理。具体规则如下(均来自 doc/user/07-COMMANDS.md):
Token 以空格分隔:例如
set scene.up.direction +Z会被拆成set、scene.up.direction、+Z三个 token。引号包裹以支持空格:例如
set render.hdri.file "/path/to/file with spaces.png"。支持多种引号:
`、'、"均可,例如set render.hdri.file '/path/to/file with spaces.png'。引号可嵌套:例如
set render.hdri.file "/path/to/file'with'quotes.png"。引号与空格可转义:例如
set render.hdri.file /path/to/file\ with\ spaces\ and\ \'quotes\".png。注释:使用
#,其后的所有字符都会被忽略;使用\#可以按字面量输出#。[!NOTE] 注释仅在命令脚本中有效;在交互式控制台中,
#及其后的所有字符都会按普通字符处理。转义符本身可被转义:例如 Windows 路径
set render.hdri.file C:\\path\\to\\windows\\file.png。其他转义字符按原样处理:例如
set scene.up.direction +\Z中的\Z等价于Z。未闭合的引号段无效:例如
set scene.up.direction "+Z是非法命令。结尾的转义符同样无效:例如
set scene.up.direction +Z\是非法命令。选项值按其类型解析:具体规则见 解析文档。
从源码看,命令的解析与执行位于library/src/interactor_impl.cxx中interactor_impl::interactor_impl构造函数内的大段addCommand注册逻辑;而命令脚本的执行入口在application/F3DStarter.cxx(约 L1756-L1785):F3DStarter读取AppOptions.CommandScriptFile,逐行读取脚本文件并调用interactor.triggerCommand(command),一旦某条命令执行失败,会输出Error in command script, stopping script execution并中止脚本。
libf3d 提供的命令
libf3d 内置了一批命令,其中很多与 libf3d 选项 的读写相关。以下逐一说明(全部命令的注册与文档字符串可在 library/src/interactor_impl.cxx 中检索到)。
选项读写类命令
| 命令 | 说明 | 示例 |
|---|---|---|
set option.name values | 设置一个 libf3d 选项 | set scene.up.direction +Z、set render.hdri.file "/path/to/file with spaces.png" |
toggle option.name | 切换布尔选项 | toggle ui.scalar_bar |
reset option.name | 将选项重置为默认值 | reset render.background.blur.coc |
print option.name | 打印选项当前值 | print scene.up.direction |
set_reader_option Reader.option_name value | 设置 reader 选项 | set_reader_option QuakeMDL.skin_index 1 |
increase option.name | 按范围域(range domain)递增选项 | increase render.light.intensity |
decrease option.name | 按范围域递减选项 | decrease render.light.intensity |
cycle option.name | 按枚举域(enum domain)循环选项 | cycle render.effect.blending.mode |
源码佐证(library/src/interactor_impl.cxx):
set内部调用f3d::options::set系方法,print使用Options.getAsString(args[0])输出;set_reader_option内部调用f3d::engine::setReaderOption(args[0], args[1])(见set_reader_option注册处),并支持对 reader 选项名自动补全;print命令带有complOptionNames补全器,可在交互式控制台中按Tab补全选项名。
场景与渲染信息类命令
| 命令 | 说明 |
|---|---|
print_scene_info | 打印场景信息,无参数 |
print_coloring_info | 打印着色(coloring)设置信息,无参数 |
print_mesh_info | 打印网格导入器提供的信息,无参数 |
print_options_info | 打印当前有值的 libf3d 选项,无参数 |
print_config_info | 打印配置文件信息,无参数 |
对应的测试(application/testing/tests.features.cmake)验证了这些命令的输出:例如TestCommandScriptPrintScene检查Camera position: 2.23745, 3.83305, 507.598,TestCommandScriptPrintMesh检查Number of points: 13268,TestCommandScriptPrintOptions检查interactor.invert_zoom: false,TestCommandScriptPrintColoring检查Not coloring,TestCommandScriptPrintConfig检查Found available config path。这些断言可以直接用作脚本输出的期望值参考。
相机操作类命令
| 命令 | 说明 | 示例 |
|---|---|---|
roll_camera value | 让相机绕自身轴滚动,参数为角度(度) | roll_camera 120 |
elevation_camera value | 相机上下俯仰,参数为角度(度) | elevation_camera 120 |
azimuth_camera value | 相机左右旋转(方位角),参数为角度(度) | azimuth_camera 120 |
set_camera front/top/right/back/bottom/left/isometric | 将相机定位到相对模型指定方位 | set_camera top |
reset_camera | 将相机重置到初始位置,无参数 | — |
源码细节:roll_camera在interactor_impl.cxx中实现为Window.getCamera().roll(options::parse<int>(args[0])),且当交互风格为2d时直接返回(2D 模式下不生效);执行后还会调用Style->SetTemporaryUp(...)同步临时上方向。按键绑定层中,数字键4/6分别绑定了roll_camera -90/roll_camera 90(见 library/src/interactor_impl.cxx 中addBinding部分)。
着色(Scivis)相关命令
| 命令 | 说明 |
|---|---|
cycle_coloring field/array/component | 基于模型信息循环切换着色方式,支持field、array、component三种参数,详见 着色循环 |
toggle_volume_rendering | 切换model.volume.enable并打印着色信息,无参数 |
源码细节:cycle_coloring在 interactor_impl.cxx 中通过vtkF3DRenderer的CycleFieldForColoring()/CycleArrayForColoring()/CycleComponentForColoring()实现,参数不合法时抛出invalid_args_exception;执行后调用SynchronizeScivisOptions同步选项,并以 DEBUG 级别打印着色描述。默认按键绑定中,C/S/Y分别触发cycle_coloring field/cycle_coloring array/cycle_coloring component。
动画控制类命令
| 命令 | 说明 | 示例 |
|---|---|---|
cycle_animation | 基于模型信息循环scene.animation.index选项,无参数 | — |
toggle_animation | 开始/停止动画,无参数 | — |
toggle_animation_backward | 开始/停止反向播放动画,无参数 | — |
jump_to_frame | 跳到指定帧,参数为帧索引 | jump_to_frame 0(第 0 帧)、jump_to_frame -1(最后一帧)、jump_to_frame -2(倒数第二帧) |
jump_to_frame_relative | 相对当前帧移动若干帧,参数为帧偏移 | jump_to_frame_relative 1(下一帧)、jump_to_frame_relative -1(上一帧) |
jump_to_keyframe | 跳到指定关键帧,参数为关键帧索引 | jump_to_keyframe 0(动画起始帧)、jump_to_keyframe 10(第 10 个关键帧) |
jump_to_keyframe_relative | 相对当前关键帧移动,参数为关键帧偏移 | jump_to_keyframe_relative 0(最近关键帧)、jump_to_keyframe_relative 1(下一关键帧)、jump_to_keyframe_relative -1(上一关键帧)、jump_to_keyframe_relative 10(前进 10 个关键帧) |
jump_to_time | 跳到指定时间,参数为秒 | jump_to_time 2.5 |
jump_to_time_relative | 相对当前时间移动,参数为秒偏移 | jump_to_time_relative 0.5(前进 0.5 秒)、jump_to_time_relative -0.5(后退 0.5 秒) |
jump_to_keyframe/jump_to_keyframe_relative在跳转时会把目标关键帧索引自动约束在可用关键帧总数范围内,避免非法访问。目前这两条命令仅被以下 reader 支持:
vtkF3DGLTFImportervtkF3DQuakeMDLImporter
其他 reader 的动画(时间步)支持进展可跟踪 F3D 项目的动画系统改进 Issue(F3D Issue #2637)。源码中jump_to_frame等命令经由AnimationManager->JumpToFrame(frame, relative)/JumpToTime(time, relative)实现(见 library/src/interactor_impl.cxx 中jump_to_frame等注册处)。
关于帧号负数的约定:负索引表示从末尾倒数,例如
-1是最后一帧、-2是倒数第二帧;相对偏移中0表示跳到最近的关键帧。
文件与状态管理类命令(libf3d 层)
libf3d 提供以下命令,但它们会被 F3D 应用层的同名命令覆盖(见下文):
| 命令 | 说明 |
|---|---|
add_files [path/to/file1] [path/to/file2] | 向场景添加文件,可接一个或多个文件 |
save_statefile [path/to/file] | 将当前状态保存到指定 statefile |
load_statefile [path/to/file] | 从指定 statefile 恢复状态 |
save_statefile_to_clipboard | 将当前状态保存到系统剪贴板,需要启用clip模块构建 |
load_statefile_from_clipboard | 从系统剪贴板恢复状态,需要启用clip模块构建 |
其他工具类命令
| 命令 | 说明 | 示例 |
|---|---|---|
clear | 清空控制台,无参数 | — |
cycle_verbose_level | 在Debug、Info、Warning、Error、Quiet之间循环切换日志级别,无参数 | — |
alias [alias_name] [command] | 为某条命令创建别名 | alias myrotate roll_camera 90 |
help [command] | 打印指定命令的帮助信息 | help set_camera |
stop_interactor | 停止交互器并退出应用,无参数 | — |
源码细节:alias命令在interactor_impl.cxx中注册,作用是把一个命令名映射到一串命令文本,之后即可直接调用别名;help会输出注册命令时的command_documentation_t描述。测试TestCommandScriptHelp检查help set输出包含set a libf3d option,TestCommandScriptAlias则直接执行alias myrotate roll_camera 90后调用myrotate(见 testing/scripts/TestCommandScriptAlias.txt 与 application/testing/tests.features.cmake)。
F3D 应用层提供的专用命令
F3D 应用(application/F3DStarter.cxx)在 libf3d 命令之上额外提供了以下命令,主要涉及文件组管理、截图、状态文件与文件对话框:
文件组(File Group)管理
| 命令 | 说明 |
|---|---|
load_previous_file_group [keep_camera] | 加载上一个文件或文件组;keep_camera为true时保持相机状态(默认false) |
load_next_file_group [keep_camera] | 加载下一个文件或文件组;相机保持规则同上 |
reload_current_file_group | 重新加载当前文件或文件组,无参数 |
add_current_directories | 将当前文件/文件组所在目录下的所有文件加入场景,无参数 |
add_files [path/to/file1] [path/to/file2] | 按当前分组逻辑向场景添加文件(覆盖 libf3d 同名命令),可接一个或多个文件 |
remove_current_file_group | 移除当前文件组并加载下一个文件组(如果有),无参数 |
remove_file_groups | 移除所有文件,无参数 |
注意:
load_previous_file_group的参数最多为 1 个,TestCommandScriptParseOptionalBoolExtraArg测试验证了传入多余参数会报错:Command: load_previous_file_group takes at most 1 argument, got 2 arguments instead.(见 application/testing/tests.features.cmake)。
相关测试包括TestCommandScriptReset(reset render.show_edges; load_next_file_group)、TestCommandScriptRemoveCurrentFileGroup、TestCommandScriptRemoveFileGroups(见 application/testing/tests.features.cmake)。
截图命令
| 命令 | 说明 |
|---|---|
take_screenshot [filename] | 截图;未指定文件名时使用--screenshot-filenameCLI 选项 |
take_minimal_screenshot [filename] | 截取最小化截图;未指定文件名时同样回退到--screenshot-filename |
截图方式详见 截图交互,示例:take_screenshot path/to/file.png。
文件对话框与 HDRI
| 命令 | 说明 |
|---|---|
open_file_dialog | 打开文件对话框选择要加载的文件,无参数 |
set_hdri [path/to/hdri] | 设置并使用 HDRI 图像,参数为 HDRI 文件 |
add_files_or_set_hdri [path/to/file1] [path/to/file2] | 逐个处理文件:若文件扩展名是已识别的 HDR 扩展则执行set_hdri,否则执行add_files |
示例:set_hdri /path/to/file.hdr、add_files_or_set_hdri /path/to/dragon.vtu /path/to/file.hdr。
状态文件(Statefile)管理(F3D 层)
| 命令 | 说明 |
|---|---|
save_statefile [filename] | 保存当前状态到 statefile,包含所有文件组(包括未加载的);未指定文件名时使用--statefile-filenameCLI 选项,为空则使用默认文件名;使用-输出到标准输出 |
save_statefile_dialog | 通过文件对话框选择保存位置,需要启用tinyfiledialogs模块构建 |
load_statefile [filename] | 从 statefile 恢复状态,恢复所有已保存的文件组并重新加载当前文件组;未指定文件名时规则同上;使用-从标准输入读取;文件不存在时仅警告不报错 |
load_statefile_dialog | 通过文件对话框选择 statefile 恢复,需要启用tinyfiledialogs模块构建 |
save_statefile_to_clipboard | 保存状态到系统剪贴板,需要启用clip模块构建 |
load_statefile_from_clipboard | 从系统剪贴板恢复状态并重新加载文件,需要启用clip模块构建 |
示例:save_statefile path/to/state.json、load_statefile path/to/state.json。
选项域(Domains):increase / decrease / cycle 的底层机制
部分 libf3d 选项带有域(domain),命令系统据此提供了三种操作方式(见 doc/user/07-COMMANDS.md 的 Domains 一节):
- 范围域(Range domain):通过
increase/decrease操作。具有包含的最小值和最大值,以及一个增量。increase/decrease按增量增减,并在达到最大/最小值处截断。 - 枚举域(Enum domain):通过
cycle操作。只列出选项可能的取值,cycle依次遍历并在到达末尾后循环回起点。 - 索引域(Index domain):
cycle与increase/decrease均可使用。- 对
increase/decrease,它等价于一个[0, max]范围域,增量为 1; - 对
cycle,它等价于一个包含 0 到 max 之间所有取值的枚举域。
- 对
例如increase render.light.intensity会按域定义逐步提升光照强度,cycle render.effect.blending.mode会在混合模式枚举间循环。域的定义与解析可以在 doc/libf3d/03-OPTIONS.md 与library/private/options_generated.h.in/library/src/options.cxx中追溯,测试方面TestSDKOptionsDomains.cxx(library/testing/TestSDKOptionsDomains.cxx)覆盖了域相关行为。
命令脚本(--command-script)
F3D 支持通过--command-scriptCLI 选项 从脚本文件执行命令序列,便于自动化:f3d --command-script path/to/command_script.txt。
脚本规则:命令之间以换行分隔,支持注释。官方示例:
# A comment roll_camera 90 toggle ui.scalar_bar print_scene_info # Another comment increase_light_intensity仓库中真实的脚本示例(testing/scripts/TestCommandScriptBasic.txt):
# A comment at the beginning of the script roll_camera 90 # A comment at the end of a command toggle ui.scalar_bar # A comment in the middle of the script print_scene_info脚本执行流程(源码依据 application/F3DStarter.cxx):F3D 启动时若指定了--command-script,会打开脚本文件并逐行std::getline读取,对非空行调用interactor.triggerCommand(command);任一条命令执行失败即输出错误日志并停止后续执行;文件无法打开时输出Unable to open command script file并返回失败。对应测试见 application/testing/tests.features.cmake(TestCommandScriptMissingFile验证了文件打不开时的报错路径)。
交互式控制台(Interactive Console)
当 F3D 以F3D_MODULE_UI构建时,交互窗口中按Esc即可打开控制台:
- 在输入框中输入任意命令,按Enter立即执行;
- 按Tab自动补全命令并显示建议(选项名、reader 选项名等均支持补全);
- 按↑/↓在命令历史中上下浏览;
- 再次按Esc关闭控制台。
结合前文可知,控制台内的补全能力由interactor_impl.cxx中每个命令注册时挂载的补全器提供(如complOptionNames、reader 选项名补全等)。
从测试与绑定看命令的实战组合
命令系统不仅在脚本与控制台中直接使用,还被按键绑定大量复用。interactor_impl.cxx中的addBinding把命令与默认按键关联,例如C→cycle_coloring field、S→cycle_coloring array、Y→cycle_coloring component、4→roll_camera -90、6→roll_camera 90。也就是说,你看到的交互层“快捷键”本质上就是命令,这也解释了命令文档与 交互文档 之间的对应关系。
仓库中大量TestCommandScript*.txt(见 testing/scripts 目录)展示了命令的典型组合,例如:
TestCommandScriptJumpToPreviousKeyFrame.txt、TestCommandScriptJumpToClosestKeyFrame.txt等覆盖jump_to_keyframe*系列(配合soldier_animations.mdl与--animation-indices=2等参数);TestCommandScriptCycleCameraIndex.txt、TestCommandScriptIncreaseDecreaseCameraIndex.txt覆盖相机索引切换;TestCommandScriptAxesGridAnimation.txt结合--axes-grid使用。
这些脚本直接位于testing/scripts/,可作为自动化场景下的参考模板。
小结
F3D 的命令系统以action [args]为统一语法,提供三大入口(交互式控制台、命令脚本、按键绑定配置),覆盖选项读写(set/toggle/reset/print/increase/decrease/cycle)、相机控制、着色循环、动画跳转、文件组管理、截图与状态文件等能力。掌握这套命令体系,意味着你可以:
- 在交互窗口中即时探索并修改任意选项;
- 用
--command-script将重复性操作固化为可复用的自动化脚本(配合 CI/测试); - 在 配置文件 中自定义按键绑定,把常用命令映射到顺手的位置。
由于命令系统仍处于实验阶段,建议在实际使用中通过help <command>查询当前构建下的命令帮助,并以本仓库当前版本的源码(library/src/interactor_impl.cxx)与测试脚本为准。
【免费下载链接】f3dFast and minimalist 3D viewer.项目地址: https://gitcode.com/GitHub_Trending/f3/f3d
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考