使用Python和POV-Ray进行3D渲染
项目描述
Vapory
Vapory是一个Python库,用于使用免费的光线追踪引擎POV-Ray渲染逼真的3D场景。
以下是绘制紫色球体的示例
from vapory import *
camera = Camera( 'location', [0,2,-3], 'look_at', [0,1,2] )
light = LightSource( [2,4,-3], 'color', [1,1,1] )
sphere = Sphere( [0,1,2], 2, Texture( Pigment( 'color', [1,0,1] )))
scene = Scene( camera, objects= [light, sphere])
scene.render("purple_sphere.png", width=400, height=300)
Vapory可以将渲染的图像回传到Python中,并且与Python库生态系统集成得非常好(请参阅这篇博客文章中的示例)
Vapory是一个开源软件,最初由Zulko编写,遵循MIT许可证发布,托管在Github上,欢迎所有人贡献或寻求支持。
安装
Vapory应该在具有Python 2.7+或Python 3的任何平台上工作。
您首先需要安装POV-Ray。有关Windows二进制文件的链接,请参阅此处。对于Linux/MacOS,您必须编译源代码(在Ubuntu上测试过,很容易)。
如果您已安装了PIP,可以使用以下命令:
(sudo) pip install vapory
如果您既未安装setuptools也未安装ez_setup,则上面的命令将失败,在这种情况下,请在安装之前输入以下内容:
(sudo) pip install ez_setup
Vapory也可以通过在一个目录中解压缩源代码并使用终端中的以下命令手动安装:
(sudo) python setup.py install
入门
在Vapory中,您首先创建一个场景,然后渲染它
from vapory import *
scene = Scene( camera = mycamera , # a Camera object
objects= [light, sphere], # POV-Ray objects (items, lights)
atmospheric = [fog], # Light-interacting objects
included = ["colors.inc"]) # headers that POV-Ray may need
scene.render("my_scene.png", # output to a PNG image file
width = 300, height=200, # in pixels. Determines the camera ratio.
antialiasing = 0.01 # The nearer from zero, the more precise the image.
quality=1) # quality=1 => no shadow/reflection, quality=10 is 'normal'
# passing 'ipython' as argument at the end of an IPython Notebook cell
# will display the picture in the IPython notebook.
scene.render('ipython', width=300, height=500)
# passing no 'file' arguments returns the rendered image as a RGB numpy array
image = scene.render(width=300, height=500)
对象通过传递一组参数来定义
camera = Camera( 'location', [0,2,-3], 'look_at', [0,1,2] )
请注意,此代码片段将被转换为POV-Ray代码,每个参数都被转换为字符串,并放置在不同的行上,以生成有效的POV-Ray代码
camera { location <0,1,0> look_at <0,0,0> }
所有对象(Sphere、Box、Plane等,有一些例外)都以相同的方式工作。因此,Vapory的语法与POV-Ray的语法相同。要了解如何使用不同的对象
请查看examples文件夹中的场景
请参阅不同对象的docstring,它提供了基本示例。
请参阅在线POV-Ray文档,其中包含了每个对象的所有可能用途(可能有多个!)。此文档可以从Vapory轻松访问,只需键入`Sphere.help()`、`Plane.help()`等,它将在您的浏览器中打开。
最后,很容易在网上找到POV-Ray示例并将其转录回Vapory。
缺少的功能
目前已实现了许多功能(球体、雾等),但并非全部(POV-Ray有许多可能形状和能力)。
添加新功能非常容易,因为它们基本上都做同样的事情,只是空类。例如,这是Camera的实现方式
class Camera(POVRayElement): """ Camera([type,] 'location', [x,y,z], 'look_at', [x,y,z]) """
是的,就这么多,但只要类名就足够Vapory理解这将转换为POV-Ray代码camera{...}。因此,在大多数情况下,创建您自己的新功能不应该很难。如果您需要将未实现的功能包含在软件包中,只需打开一个问题或提交一个commit。
项目详情
aiidalab-vapory-0.1.2.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fb1587cabae084100ecb6398d6f040b6c2883072f6381695b8a01b43e7d913d6 |
|
MD5 | dd3141a525698ce9821fc42a93b8fefe |
|
BLAKE2b-256 | 6cd66b3202e3fecc0b7e46b106f06251b072573eb1e9f9d611985fdb31114e88 |