用于导入wavefront.obj文件的python库
PyWavefront的Python项目详细描述
pywavefront
PyWavefront读取Wavefront三维对象文件(something.obj
,something.obj.gz
)
和something.mtl
),并为准备渲染的每个材质生成交错顶点数据。
- python 3.4+在1.x版本中受支持
- Python 2.7在0.x版本中受支持
还提供了一个简单(可选)的可视化模块 渲染对象。交织数据也可以由 更现代的渲染者认为是VBOs或VAOs
目前the specification中最常用的功能有 已实施:
- 职位
- 纹理坐标
- 法线
- 顶点颜色
- 材料分析
我们目前不支持参数空间顶点、线元素或平滑组如果缺少所需的功能,请在github上创建问题或请求拉取。
pip install pywavefront
还可以查看roadmap了解未来的计划。
用法
加载obj文件的基本示例:
importpywavefrontscene=pywavefront.Wavefront('something.obj')
更复杂的示例
strict
(默认值:False
)如果在obj或mtl文件中发现不支持的功能,则会引发异常encoding
(默认值:utf-8
)的obj和mtl文件- {
将创建材料 collect_faces
(默认值:False
)将为每个网格收集三角形面数据。如果指定了具有三个以上顶点的面,则将对其进行三角剖分。请参阅^{} 中ObjParser#consume_faces()
的文档。parse
(默认值:True
)决定是否应该立即开始解析。cache
(默认值:False
)将解析的几何体写入二进制文件,以便将来更快地加载
importpywavefrontscene=pywavefront.Wavefront('something.obj',strict=True,encoding="iso-8859-1",parse=False)scene.parse()# Explicit call to parse() needed when parse=False# Iterate vertex data collected in each materialforname,materialinscene.materials.items():# Contains the vertex format (string) such as "T2F_N3F_V3F"# T2F, C3F, N3F and V3F may appear in this stringmaterial.vertex_format# Contains the vertex list of floats in the format described abovematerial.vertices# Material propertiesmaterial.diffusematerial.ambientmaterial.texture# ..
二进制缓存
当cache=True
写入交错顶点数据时
在加载文件后浮动到.bin
文件。一个json
还将生成描述二进制文件内容的文件。
下次我们尝试加载时,将加载二进制文件
obj文件大大缩短了加载时间。
测试显示加载时间减少了10到100倍 取决于原始obj文件的大小和结构。
加载myfile.obj
将在
相同的目录
myfile.obj.bin
myfile.obj.json
Json文件示例:
{"created_at":"2018-07-16T14:28:43.451336","version":"0.1","materials":["lost_empire.mtl"],"vertex_buffers":[{"material":"Stone","vertex_format":"T2F_N3F_V3F","byte_offset":0,"byte_length":5637888},{"material":"Grass","vertex_format":"T2F_N3F_V3F","byte_offset":5637888,"byte_length":6494208}]}
在删除这些文件之前,不会重新创建它们。 bin文件还与gzip一起压缩,以大大减小大小。
可视化
Pyglet是使用可视化模块所必需的。
pip install pyglet
示例:
importpywavefrontfrompywavefrontimportvisualization[createawindowandsetupyourOpenGlcontext]obj=pywavefront.Wavefront('something.obj')[insideyourdrawingloop]visualization.draw(obj)
日志记录
默认日志级别是ERROR
。这是可配置的,包括重写格式化程序。
importloggingimportpywavefrontpywavefront.configure_logging(logging.DEBUG,formatter=logging.Formatter('%(name)s-%(levelname)s: %(message)s'))
示例
这是examples
目录包含一些使用visualization
模块的基本示例以及有关如何运行它们的进一步说明。
用搅拌机生成波前文件
以下假设您使用Blender生成网格:
- 使用“混合器”(Blender)创建具有UV贴图纹理的网格紫外线映射很重要!如果它工作正常,你会看到纹理应用在搅拌机的三维视图
- 使用波前格式(包括法线)从混合器导出网格。
- 参照上面的pywavefront示例中的
*.obj
文件
测试
所有测试都可以在tests
目录中找到。运行测试:
# Install pywavefront in develop mode python setup.py develop # Install required packages for running tests pip install -r test-requirements.txt # Run all tests pytest # Optionally specific tests modules can be runned sepeartely pytest tests/test_parser.py
社区
PyWavefront不一致服务器:https://discord.gg/h3Rh4QN
所有者和维护者
- Einar Forselv(@einarf)-主触点
- Kurt Yoder(@greenmoss)-备份
贡献者
按字母顺序排列:
- ComFreek
- 丹尼尔·科埃略1danielcoelho
- @dav92lee
- 杰雷克鞋匠(intrepid94)
- Marxlp
- 米阿提尤拉马尔
- Oliv4945
- 帕特里克·胡贝尔(patrikhuber)
- 塞尔吉奥阿戈斯蒂尼奥(SergioRAgostinho)
- 佐哈尔·杰克逊
- 香港航空公司(hkarrson)
项目历史
PyWavefront最初由@greenmoss(Kurt Yoder)在2013年启动。 他是该项目的唯一维护者,直到2019年2月 创建了pywavefront维护组织,添加了@einarf (Einar Forselv)作为项目的额外所有者和维护者
许可证
pywavefront是BSD-licensed