使用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> }
所有对象(球体、盒子、平面等,有一些例外)都按相同的方式工作。因此,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{...}。所以大多数情况下,创建自己的新功能不应很困难。如果您需要将未实现的功能包含在包中,只需打开一个问题或推送一个提交。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建分发
Vapory-0.1.2.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e0213caaac5cbb1d336ef7455a18e7ffcc8b864093040958ef45bddac8204231 |
|
MD5 | 1005a15fc3e04e6b1df5d8d5976970fe |
|
BLAKE2b-256 | fb4e76ad473b54c7d12b4625e067ff736432dc25602d6ec07e82aec5a670e698 |
Vapory-0.1.2-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 58e4c09f6ab689e812cdf57e5a46056c8fb466af6d95395e9ffc565ac17806c5 |
|
MD5 | 627e98fffff07c96fff2dec5fadcb703 |
|
BLAKE2b-256 | 3ac347335d0cbd3b9eeff8383aa7f634bb2191587a2763756a92c255d854cdff |