使用python和pov-ray进行三维渲染
Vapor的Python项目详细描述
蒸汽
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库生态系统中很好地集成(参见this blog post示例)
Vapory是一款开源软件,最初由Zulko编写,在麻省理工学院的许可下发布,托管在Github上,欢迎大家参与或寻求支持。
安装
Vapory应该可以在Python2.7+或Python3的任何平台上工作。
首先需要安装pov-ray。有关windows二进制文件,请参见here。对于linux/macos,您必须compile the source(在ubuntu上测试,很容易)。
如果安装了PIP,您可以:
(sudo) pip install vapory
如果您既没有安装setuptools,也没有安装ez_setup,上面的命令将失败,在安装之前是否键入以下命令:
(sudo) pip install ez_setup
Vapory也可以手动安装,方法是将源代码解压缩到一个目录中,然后键入终端:
(sudo) python setup.py install
开始
在vapory中创建一个场景,然后渲染它:
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 documentation,它将为您提供每个对象的所有可能用途(可能有很多!)。此文档很容易从vapory访问,只需键入`Sphere.help()、Plane.help()等,它就会在浏览器中打开。
- 最后,很容易在网上找到pov射线的例子并将其转录回vapory。
缺少功能
目前已经实现了许多功能(球体、雾等),但并不是所有功能(pov-ray有很多可能的形状和功能)。
添加新特性非常容易,因为它们基本上都做相同的事情,只是空的类。例如,这里是相机的实现方式:
class Camera(POVRayElement): """ Camera([type,] 'location', [x,y,z], 'look_at', [x,y,z]) """
是的,仅此而已,但只要类的名称就足以让vapory理解这将转换为pov-ray代码camera{...}。因此,在大多数情况下,创建自己的新功能并不难。如果需要包中包含未实现的功能,只需打开问题或推送提交即可。