尧图精选

Rerun Rust 示例精讲:用 `log_file_from_path` / `log_file_from_contents` 通过 Importer 机制记录任意文件

🕒 发布时间:2026/9/17 19:45:51 📁 来源:尧图网络
Rerun Rust 示例精讲用log_file_from_path/log_file_from_contents通过 Importer 机制记录任意文件【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun本篇文章围绕仓库中的 examples/rust/log_file 示例展开讲解如何借助 Rerun SDK 的Importer导入器机制用一行 API 调用将任意格式的文件点云、网格、图片、.rrd、蓝图等直接记录进当前录制流。读完本文你将掌握log_file_from_path与log_file_from_contents两个核心 API 的用法、CLI 参数、底层实现链路与相关测试能够在自己编写的 Rust 应用中一键导入并可视化各类资产文件。示例概览一个文件一次调用log_file是官方提供的 Rust 最小示例其核心目标正如文档描述从 SDK 中记录任意文件通过Importer机制走通一行调用的路径。它本身不关心文件是点云、网格还是文本而是把格式识别与数据转换全部交给 Rerun 的导入器管线处理。示例的入口与运行方式非常简单cargo run -p log_file -- examples/assets其中examples/assets是示例数据目录仓库中该目录下恰好准备了多种格式的资产文件examples/assetsexample.glb/example.gltfglTF 网格、example.ply点云、example.objOBJ 网格、example.jpg/example.png图片、example.rrdRerun 录制文件、example.md/example.txt文本等。把整个目录作为参数传入即可观察同一个程序对不同格式文件的导入结果——这正是单一路径可能由多个 importer 处理的直观体现。命令行参数解析位置参数 --from-contents开关示例通过clap解析命令行参数其结构定义在 examples/rust/log_file/src/main.rs#[derive(Debug, clap::Parser)] #[clap(author, version, about)] struct Args { #[command(flatten)] rerun: rerun::clap::RerunArgs, // Log the contents of the file directly (files only -- not supported by external loaders). #[clap(long, default_value false)] from_contents: bool, /// The filepaths to be loaded and logged. filepaths: Vecstd::path::PathBuf, }三个参数的含义如下参数类型默认值说明filepaths位置参数可多个必填要加载并记录的文件的路径列表--from-contentsboolfalse直接按文件内容记录仅限普通文件外部加载器不支持该方式rerunflatten 的RerunArgs子命令参数集—控制数据去向spawn 查看器、保存 rrd、stdout、连接远程等主函数中的初始化流程如下main.rsfn main() - anyhow::Result() { re_log::setup_logging(); use clap::Parser as _; let args Args::parse(); let (rec, _serve_guard) args.rerun.init(rerun_example_log_file)?; run(rec, args)?; Ok(()) }re_log::setup_logging()初始化日志系统args.rerun.init(rerun_example_log_file)根据 CLI 参数创建一个RecordingStreamrerun_example_log_file是应用程序 ID返回的_serve_guard用于保证--serve等后台任务如 Web Viewer 服务在程序生命周期内存活。可复用的RerunArgs决定数据去向rerun::clap::RerunArgs是官方提供的通用 CLI 参数集定义在 crates/top/rerun/src/clap.rs它让所有官方示例具备一致的数据输出方式参数默认值作用--spawntrue启动一个新的 Rerun Viewer 进程并实时喂入数据--save PATH无将数据保存为.rrd文件而非立即可视化-o/--stdout无将日志数据输出到标准输出便于管道传给查看器--connect [URL]rerunhttp://127.0.0.1:9876/proxy连接远程 Rerun Viewerscheme 须为rerun://、rerunhttp://或rerunhttps://路径须为/proxy--serve无连接基于 Web 的 Rerun Viewer需启用web_viewerfeature--server-memory-limit25%gRPC 服务器内存上限可为16GB或50%等超出后丢弃最旧数据--newest-first无新客户端连接时优先回放最新数据--bind IP0.0.0.0服务器绑定的 IP 地址以--save为例你可以不启动查看器而把导入结果落盘cargo run -p log_file -- examples/assets/example.rrd --save out.rrd两种记录 API 的实战对比示例的核心业务逻辑在run函数中main.rsfn run(rec: rerun::RecordingStream, args: Args) - anyhow::Result() { let prefix Some(log_file_example.into()); for filepath in args.filepaths { let filepath filepath.as_path(); if args.from_contents { // …or using its contents if you already have them loaded for some reason. if filepath.is_file() { let contents std::fs::read(filepath)?; rec.log_file_from_contents( filepath, std::borrow::Cow::Borrowed(contents), prefix.clone(), true, /* static */ )?; } } else { // Either log the file using its path… rec.log_file_from_path(filepath, prefix.clone(), true /* static */)?; } } Ok(()) }log_file_from_path按路径记录rec.log_file_from_path(filepath, prefix.clone(), true /* static */)?;这是默认路径不带--from-contentsSDK 会直接访问文件系统读取文件。三个参数分别为filepath要记录的文件路径entity_path_prefix实体路径前缀示例传入Some(log_file_example.into())即导入产生的所有实体都会挂到log_file_example/**之下方便与其他数据隔离传None则不添加前缀static_是否为静态数据不随时间变化。示例传true。log_file_from_contents按内容记录let contents std::fs::read(filepath)?; rec.log_file_from_contents( filepath, std::borrow::Cow::Borrowed(contents), prefix.clone(), true, /* static */ )?;当你出于某种原因已经或只能拿到文件字节时使用该 API——例如文件在内存中、来自网络、或经过解密解压。注意两点filepath在这里仅用于信息用途日志输出、application_id 派生、importer 按扩展名分发底层不会再次从文件系统读取数据见 crates/data_flow/re_importer/src/import_file.rs 的注释pathis only used for informational purposes, no data is ever read from the filesystem源码注释明确提示该方式仅支持普通文件外部加载器external loaders不支持。底层实现链路一次调用背后发生了什么log_file_from_path与log_file_from_contents都定义在 crates/top/re_sdk/src/recording_stream.rs二者最终汇聚到私有方法log_filerecording_stream.rs其执行流程如下校验录制流状态若RecordingStream尚未正确初始化则打印警告并直接返回构造导入通道通过re_log_channel::log_channel创建LogSource::File类型的日志通道(tx, rx)填充ImporterSettings包括当前录制的application_id、recording_id、entity_path_prefix、时间点timepoint由static_决定是否注入当前各时间线的时间及录制 tick、时间线类型TimestampNs等由于prefer_current_recording恒为true还会把当前 store 设为opened_store_id使得多数 importer 倾向于把数据导入到当前打开的录制中分发到 re_importer有内容时调用re_importer::import_from_file_contents否则调用re_importer::import_from_path异步消费消息drop(tx)后SDK 会以文件名为线程名如log_file_from_path(…)spawn一个后台线程循环rx.recv()取出LogMsg并调用this.record_msg(log_msg)写回录制流线程句柄被压入importer_handles管理。import_from_path本身import_file.rs还会做两件关键的事同步检查文件是否存在路径不存在时返回NotFound错误除此之外的失败都是异步的由 importer 自己记录日志派生 application ID若设置中没有指定 application ID则从文件路径推导一个application_id_from_path当导入 LeRobot 数据集时启用lerobotfeature会跳过SetStoreInfo消息因为 LeRobot importer 会自动处理。两个公开 API 的文档注释还强调了一个值得注意的语义recording_stream.rs该方法会阻塞直到至少一个 importer 开始流式传输数据或所有 importer 均失败。单个路径可能被多个 importer 共同处理。导入器的组织re_importer与多格式分发示例依赖的reruncrate 启用了importers相关能力examples/rust/log_file/Cargo.tomlfeatures [web_viewer, clap, log_setup]同时通过rerun聚合了re_importer。re_importercrate 的定位是使用 importer 插件处理来自文件的 Rerun 数据见 crates/data_flow/re_importer/README.md它维护了一组内置 importerRrdImporter.rrd录制文件点云 / 网格 / 图片 / 文本 / 视频等格式的专用 importer蓝图的导入.rbl与重定向处理。正是这套插件化的分发机制让log_file示例面对examples/assets下格式迥异的文件.glb、.ply、.jpg、.obj、.rrd、.md等时只需要同一个 API 调用即可各自命中正确的 importer。测试验证导入蓝图会被重定向到当前应用仓库用专门的集成测试保证log_file_*的行为正确见 crates/top/re_sdk/tests/log_file.rs#![cfg(feature importers)]。测试log_file_from_path_retargets_blueprint_to_current_application验证了一个重要契约先在一个无关应用rerun_example_unrelated_blueprint_application下保存一个蓝图.rbl文件记录其 store ID再用当前应用rerun_example_current_application调用rec.log_file_from_path(rbl_file.path(), None, false)断言导入产物的SetStoreInfo、ArrowMsg、BlueprintActivationCommand三类消息均存在且其application ID 被重定向retarget为当前应用的 application ID而 recording ID 保持不变。该测试还给出了一个调试技巧storage.take()会冲刷录制流并 join 导入线程从而保证返回时所有导入消息都已落地——这也是在你自己的代码中同步等待导入完成的可靠手段。运行效果与验证建议在仓库根目录执行cargo run -p log_file -- examples/assets程序会依次为目录中的每个文件调用导入逻辑默认行为是 spawn 一个 Rerun Viewer 实时展示导入结果--spawn默认开启。可以按需组合RerunArgs验证不同数据去向# 仅导入单个 rrd 文件并保存为新的 rrd cargo run -p log_file -- examples/assets/example.rrd --save result.rrd # 通过 stdout 输出管道给查看器 cargo run -p log_file -- examples/assets/example.ply -o | rerun # 以内容方式记录仅普通文件 cargo run -p log_file --from-contents -- examples/assets/example.txt需要说明的适用前提log_file_from_path/log_file_from_contents在#[cfg(feature importers)]下编译recording_stream.rs--from-contents仅支持普通文件而非外部加载器示例Cargo.toml声明了rust-version 1.96与edition 2024运行前请确保本机工具链满足版本要求。理解上述底层调用链后你可以很自然地把导入任意文件这一能力嵌入到自己的 Rust 应用中无论是批量导入录制数据、加载外部资产还是实现自定义的数据摄取管线。【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
上一篇/下一篇内容由系统自动关联 返回资讯列表 →