PDA

View Full Version : with statement | contextual operations



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)

Steve Baer
10-07-2010, 03:20 PM
This is really cool! I've never used this feature in python and can see how useful it is. I'll have to keep this one in mind for the future.

Thanks,
-Steve