Hi? Please see the code below.
When I run the script with the command !-RunPythonScript, it worked.
But after removing the first line to declare using CPython ( #! python 3 ), it failed.
Is overriding BeforeTransformObjects not allowed anymore or is it a bug?
#! python 3
import Rhino
import scriptcontext as sc
class TransformEventHandler():
def __init__(self):
self.is_attached = False
def onTransform(self, sender, e):
print('onTransform test')
print(sender)
print(e)
def attach(self):
if not self.is_attached:
Rhino.RhinoDoc.BeforeTransformObjects += self.onTransform
self.is_attached = True
def detach(self):
if self.is_attached:
Rhino.RhinoDoc.BeforeTransformObjects -= self.onTransform
self.is_attached = False
def TestSampleEventHandler():
key = 'transform_event_handler'
handler = sc.sticky.pop(key, None)
if handler:
handler.detach()
handler = None
print(' SampleEventHandler disabled')
else:
handler = TransformEventHandler()
sc.sticky[key] = handler
handler.attach()
print(' SampleEventHandler enabled')
if __name__ == '__main__':
TestSampleEventHandler()