jelle
09-26-2010, 01:42 PM
python's "with" statement is very useful in scripting.
it manages the context of the block; here's an example on how you can use it to execute the block following the "with" statement to execute in in a given layer.
something like this:
with layer(curves):
rs.AddCurve(...)
with layer('surfaces'):
rs.AddLoft(...)
class layer(object):
'''contextual layer managment'''
def __init__(self, layer_name):
self.layer_name = layer_name
def __enter__(self):
if not rs.layer.IsLayer(self.layer_name):
rs.layer.AddLayer(self.layer_name)
self.current_layer = rs.layer.CurrentLayer()
self.layer = rs.layer.CurrentLayer(self.layer_name)
def __exit__(self, type, value, traceback):
rs.layer.CurrentLayer(self.current_layer)[/CODE]
with dontredraw():
# doesnt redraw the viewport
# speeds things up nicely!
class dontredraw(object):
'''contextual layer managment'''
def __enter__(self):
rs.document.EnableRedraw(False)
def __exit__(self, type, value, traceback):
rs.document.EnableRedraw(True)
it manages the context of the block; here's an example on how you can use it to execute the block following the "with" statement to execute in in a given layer.
something like this:
with layer(curves):
rs.AddCurve(...)
with layer('surfaces'):
rs.AddLoft(...)
class layer(object):
'''contextual layer managment'''
def __init__(self, layer_name):
self.layer_name = layer_name
def __enter__(self):
if not rs.layer.IsLayer(self.layer_name):
rs.layer.AddLayer(self.layer_name)
self.current_layer = rs.layer.CurrentLayer()
self.layer = rs.layer.CurrentLayer(self.layer_name)
def __exit__(self, type, value, traceback):
rs.layer.CurrentLayer(self.current_layer)[/CODE]
with dontredraw():
# doesnt redraw the viewport
# speeds things up nicely!
class dontredraw(object):
'''contextual layer managment'''
def __enter__(self):
rs.document.EnableRedraw(False)
def __exit__(self, type, value, traceback):
rs.document.EnableRedraw(True)