Grasshopper

algorithmic modeling for Rhino

Hello - I cannot access the MouseCallbackEventArgs in GHPython but in RhinoPython it works perfectly fine. Any suggestions?

from Rhino.UI import *
import scriptcontext as sc
import Rhino.RhinoDoc as rhdoc

class MyMouseCallback(MouseCallback):
     def OnMouseDown(self, e):
         print "active viewport: {0}".format(e.View.ActiveViewport.Name)
       ghenv.Component.ExpireSolution(True)
       self.Enabled = False

sc.doc = rhdoc.ActiveDoc
m_mc = MyMouseCallback()
m_mc.Enabled = True

Views: 633

Replies to This Discussion

Hi Jose,

I think this is beyond what should usually be done in Grasshopper components. Usually, you should NOT request a new solution from within a solution. But by using the mouse callback, this becomes actually possible, although really, it should be done by being aware of all possibly unintended consequences (an error might make the solution recursively call itself; a user of your script might find it unexpected; it might be slow to update the whole definition; etc).

That being said, here is a definition that will print the name of the active viewport in the command line. It contains explanations of why and what happens, where.

from Rhino.UI import *
import scriptcontext as sc
import Rhino.RhinoDoc as rhdoc
import Rhino
import random

class MyMouseCallback(MouseCallback):
 def OnMouseDown(self, e):
 self.Enabled = False
 ghenv.Component.ExpireSolution(True)
 Rhino.RhinoApp.WriteLine(
  "active viewport: {0}".format(e.View.ActiveViewport.Name))
 #we cannot print out of the component here because, when ExpireSolution(True)
 #is done, the subsequent lines will be executed AFTER the solution. Does it
 #make sense? However, the command line sticks around.

if "mc_Jose" in sc.sticky: #clean up in case you just F5 Grasshopper
 sc.sticky["mc_Jose"].Enabled = False

m_mc = MyMouseCallback()
m_mc.Enabled = True
sc.sticky["mc_Jose"] = m_mc

#if you want to print out of the component, do so here, NOT in the callback:
print("I am the random oracle and I say...")
print(random.random())

I hope this is helpful,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:

Thanks. 

I decided to not expire the solution and during the first mouse callback I printed some text using Rhino's command line. Why does the print function in ghpython does not work this time?

As in the comments (within the callback):

#we cannot print out of the component here because, when ExpireSolution(True)
#is done, the subsequent lines will be executed AFTER the solution. Does it
#make sense? However, the command line sticks around.

This is the same case. In the callback there is no solution running, so nothing transports your data from inside the component to the output parameters (and eventually further down...)

As I was mentioning, the callback is tricky stuff.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thanks. I think I can use the sc.sticky as a workaround. 

Here is my solution if anyone needs it.

from Rhino.UI import *
import scriptcontext as sc
import Rhino.RhinoDoc as rhdoc
import Rhino
import random

class MyMouseCallback(MouseCallback):
  def OnMouseDown(self, e):
    self.Enabled = False
    #save event arguments as as an entry in the sticky dict
    sc.sticky["MouseCallbackArgs"] = e
    #optional print to Rhino's command line
    Rhino.RhinoApp.WriteLine("active viewport: {0}".format(e.View.ActiveViewport.Name))
    #Expire the solution to get future mouse events
    ghenv.Component.ExpireSolution(True)
    #we cannot print out of the component here because, when ExpireSolution(True)
    #is done, the subsequent lines will be executed AFTER the solution. Does it
    #make sense? However, the command line sticks around.

if "mc_Jose" in sc.sticky: #clean up in case you just F5 Grasshopper
   sc.sticky["mc_Jose"].Enabled = False

m_mc = MyMouseCallback()
m_mc.Enabled = True
sc.sticky["mc_Jose"] = m_mc

#printing out mouse event args
if sc.sticky["MouseCallbackArgs"]:
   print sc.sticky["MouseCallbackArgs"].View.ActiveViewport.Name

I cannot comment as I do not know what you want to do. I hope what I showed was helpful :)

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Edited my last comment. Sorry.

This is slightly shorter. No need to use the sticky document to get the current viewport name. By the time the solution is updated, the viewport name is the new one.

Attachments:

Thanks again.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service