Hi everybody,
I’m exploring with a menu following a camera in Rhino.
I implemented a class, so I can create an instance of that class and the objects keep following the camera after the script gets executed - the only thing it does is creating that instance.
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
def multiMenu ():
op1 = rs.AddTextDot ("Rule 1", (-1,0,0))
op2 = rs.AddTextDot ("Rule 2", (+0,0,0))
op3 = rs.AddTextDot ("Rule 3", (+1,0,0))
return [op1, op2, op3]
#menu floating around users head
class Menu:
def __init__(self):
self.dist = 30
self.prevPos = (0,0,0)
#creates initial shape for menu, which will be moved around
#self.object = cubeFromCenter (self.prevPos, 3)
self.object = multiMenu()
self.createEvents()
def createEvents (self):
#Rhino.Display.DisplayPipeline.CalculateBoundingBox += self.OnCameraChange
Rhino.RhinoApp.Idle += self.OnCameraChange
Rhino.RhinoDoc.SelectObjects += self.OnSelectObjects
# CameraChange event handler
def OnCameraChange(self, sender, e):
self.updatePosition()
#print "Camera changed"
def OnSelectObjects(self, sender, e):
#print "selected"
self.menuAction()
def menuAction(self):
#print "selected"
selection = rs.SelectedObjects()
#print len(selection)
menuItems = self.object
#if selection:
#for menuItem in self.object:
#for selected in selection:
#if selected == menuItem:
#print menuItem.text
def updatePosition(self):
camPos = rs.ViewCameraTarget() [0]
camTargetPos = rs.ViewCameraTarget() [1]
camRay = rs.VectorUnitize (rs.VectorCreate (camTargetPos, camPos))
currPos = rs.VectorAdd (camPos, rs.VectorScale (camRay, self.dist))
if (currPos != self.prevPos):
#update prevPos
translation = rs.VectorCreate (currPos, self.prevPos)
self.prevPos = currPos
#rs.DeleteObject (self.object)
#self.object = cubeFromCenter (currPos, 3)
rs.MoveObjects (self.object, translation)
#just for testing purposes
rs.DeleteObjects (rs.AllObjects())
#creates Menu instance
newMenu = Menu()
print newMenu
#delete newMenu
# save the value for use in the future
scriptcontext.sticky["menu_instance"] = newMenu
All is working well, except I am not being able to delete the instance afterwards, which I need to do for testing edits to the script. Everytime I run the script, it seems like it creates a new instance and that there are multiple instances running simultaneously. I am trying to delete the instance with another script, and using sticky for “saving” the instance name/memory address. Here’s the code for the other script:
import rhinoscriptsyntax as rs
import scriptcontext
#delete newMenu
if scriptcontext.sticky.has_key("menu_instance"):
activeMenuInstance = scriptcontext.sticky["menu_instance"]
print activeMenuInstance
del activeMenuInstance
What I get from printing the activeMenuInstance is something like
<__main__.Menu instance at 0x0000000000000081>
(the number is incremented with each new instance…).
How can I delete those instances? Thanks in advance!