Hi @stevebaer,
As far as I understand by creating a class derived from the MouseCallback class and “overriding” the eventhandlers inside (I’m sorry if eventhandler is not the correct term). I mean these OnMouseDown(self,e)
By instantiating the class you in fact subscribe to all the events that were overridden. Am I correct?
If yes, then is it possible that I create a class that subscribes to a bunch of Rhino.RhinoDoc events upon instantiation?
How do I do this?
Additional Question:
I noticed that OnMouseHover is triggered when changing viewports but not when hovering over 3d objects. Is this done by a different event?
Here’s my current code:
import System
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
import time
tol = sc.doc.ModelAbsoluteTolerance
######################
### EVENT HANDLERS ###
######################
class MouseCallbackClass(Rhino.UI.MouseCallback):
""""""
def OnMouseDown(self, e):
print "OnMouseDown",e.Button
"""
def OnEndMouseDown(self,e):
print "OnEndMouseDown", e.Button
"""
""""""
def OnMouseUp(self,e):
print "OnMouseUp",e.Button
"""
def OnEndMouseUp(self,e):
print "OnEndMouseUp",e.Button
"""
""""""
def OnMouseDoubleClick(self,e):
print "OnMouseDoubleClick",e.Button
"""
def OnMouseMove(self,e):
print "OnMouseMove"
"""
"""
def OnEndMouseMove(self,e):
print "OnEndMouseMove"
"""
""""""
def OnMouseEnter(self,e):
print "OnMouseEnter", e.View.MainViewport.Name
""""""
def OnMouseLeave(self,e):
print "OnMouseLeave", e.View.MainViewport.Name
""""""
def OnMouseHover(self,e):
print "OnMouseHover"
def object_added_event(sender, e):
try:
print "Object Added, Id =", e.ObjectId
except Exception, ex:
print ex
def object_selected_event(sender, e):
try:
for obj in e.RhinoObjects:
obj_type = type(obj)
#if
print "Selected Object Type =", obj_type
except Exception, ex:
print ex
##############################
### EVENT HELPER FUNCTIONS ###
##############################
def obj_event_helper_func(event,sticky_key,event_name):
#Event trigger helper function
if sc.sticky.has_key(sticky_key):
print "removing the callback "+event_name+"."
stky = sc.sticky[sticky_key]
exec("Rhino.RhinoDoc."+event_name+" -= stky")
sc.sticky.Remove(sticky_key)
else:
print "adding the callback "+event_name+"."
stky = event
sc.sticky[sticky_key] = stky
exec("Rhino.RhinoDoc."+event_name+" += stky")
def mouse_event_helper_func():
#Event trigger helper function
if sc.sticky.has_key('TestSampleMouseCallback'):
callback = sc.sticky['TestSampleMouseCallback']
if callback:
callback.Enabled = False
callback = None
sc.sticky.Remove('TestSampleMouseCallback')
else:
callback = MouseCallbackClass()
callback.Enabled = True
sc.sticky['TestSampleMouseCallback'] = callback
#Rhino.RhinoApp.WriteLine("Click somewhere...")
print "Listening for mouse events..."
if __name__ == "__main__":
ts = time.time()
rs.EnableRedraw(False)
obj_event_helper_func(object_added_event,"object_added_event","AddRhinoObject")
obj_event_helper_func(object_selected_event,"object_selected_event","SelectObjects")
mouse_event_helper_func()
print "Elapsed time is {:.2f}".format(time.time()-ts)