问题描述
您好,
我正在使用python为Renderman输出RIB流。
RIB流是一堆描述
a 3d图像。 Rib标准允许我们通常缩进块以获得更好的可视化,例如:
WorldBegin
颜色[1 1 1 ]
表面恒定
球体(1.0,-1.0,1.0,360)
WorldEnd
我在python中使用CGKit,它有一个Renderman绑定,
所以输出相同的RIB我会写:
RiWorldBegin()
RiColor(1.0,1.0,1.0)
RiSurface(''常数')
RiSphere(1.0,-1.0, 1.0,360)
RiWorldEnd()
但是我收到一个错误,因为python将我的缩进解释为
作为一个块python代码。所以写这个
的唯一方法是没有缩进:
RiWorldBegin()
RiColor(1.0,1.0,1.0) )
RiSurface(''常数'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
但这很难阅读。
有没有办法使用这种视觉 python中的缩进?
谢谢,
Hilbert
Hello,
I''m using python to output RIB streams for Renderman.
The RIB stream is a bunch of statements which describes
a 3d image. The Rib standard allows for blocks which we
usually indent for better visualization for example:
WorldBegin
Color [1 1 1]
Surface "constant"
Sphere(1.0, -1.0, 1.0, 360)
WorldEnd
I''m using CGKit in python which has a Renderman binding,
so to output the same RIB I''d write:
RiWorldBegin()
RiColor(1.0,1.0,1.0)
RiSurface(''constant'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
But I get an error, because python interprets my indentation
as a block in the python code. So the only way to write this
is without the indentation:
RiWorldBegin()
RiColor(1.0,1.0,1.0)
RiSurface(''constant'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
But this is a lot harder to read.
Is there any way to use such "visual" indentation in python?
Thanks,
Hilbert
hi*****@panka.com
推荐答案
如果代码是纯顺序的,那么这样的事情:
import string
def RunTheseStatements(s):
stmts = map(string.strip,s.split(" \ n"))
for stmt in stmts:
eval(stmt)
RunTheseStatements(""
RiWorldBegin()
RiColor(1.0,1.0,1.0)
RiSurface(''常数'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
""")
Al
If the code is purely sequential, how about something like this:
import string
def RunTheseStatements(s):
stmts = map(string.strip, s.split("\n"))
for stmt in stmts:
eval(stmt)
RunTheseStatements("""
RiWorldBegin()
RiColor(1.0,1.0,1.0)
RiSurface(''constant'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
""")
Al
Whitespace *在Python中是重要的,你不应该试图用脏技巧来绕过
。为什么不用
标准方式构建你的世界定义,将属于一起的东西放在单独的函数中
或类?
#disclaimer:不知道CGKit参与以下代码
WHITE =(1.0,1.0,1.0)
def whitesurface():
RiColor(*白色)
RiSurface(''常数')
def firstworld():
whitesurface()
RiSphere(1.0,-1.0,1.0,360)
def secondworld():
whitesurface( )
RiSphere(1.0,-1.0,1.0,360)
世界[firstworld,secondworld]:
RiWorldBegin ()
世界()
RiWorldEnd()
我认为以上内容非常易读,你总是可以分解出来
重复代码块
彼得
Whitespace *is* significant in Python, and you should not try to circumvent
this using dirty tricks. Why not structuring your world definitions in the
standard way, putting the things that belong together in separate functions
or classes?
#disclaimer: no knowledge of CGKit involved in the following code
WHITE = (1.0,1.0,1.0)
def whitesurface():
RiColor(*WHITE)
RiSurface(''constant'')
def firstworld():
whitesurface()
RiSphere(1.0,-1.0,1.0,360)
def secondworld():
whitesurface()
RiSphere(1.0,-1.0,1.0,360)
for world in [firstworld, secondworld]:
RiWorldBegin()
world()
RiWorldEnd()
I think the above is pretty readable, and you can always factor out
repeating chunks of code
Peter
if语句怎么样:
RiWorldBegin( )
如果为真:
RiColor(1.0,1.0,1.0)
RiSurface(''常数'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
我发现它很难看,但它很容易。
What about an if statement:
RiWorldBegin()
if True:
RiColor(1.0,1.0,1.0)
RiSurface(''constant'')
RiSphere(1.0,-1.0,1.0,360)
RiWorldEnd()
I realize it''s ugly, but it''s easy.
这篇关于视觉压痕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!