Getting the viewport clicked in via mousecallback

Hi there,

I am trying to understand this sample (found at the bottom of this page):

from Rhino import *
from Rhino.Commands import *
from Rhino.UI import *

class MyMouseCallback(MouseCallback):
  def OnMouseDown(self, e):
    print "active viewport: {0}".format(e.View.ActiveViewport.Name)

def RunCommand():
  m_mc = MyMouseCallback()
  m_mc.Enabled = True

  RhinoApp.WriteLine("Click somewhere in a vieport ...")
  return Result.Success

if __name__ == "__main__":
  RunCommand()

If I remove the #$@% stars and substitute the full paths, I get (I think):

import Rhino

class MyMouseCallback(Rhino.UI.MouseCallback):
    def OnMouseDown(self, e):
        print "active viewport: {0}".format(e.View.ActiveViewport.Name)

def RunCommand():
    m_mc = MyMouseCallback()
    m_mc.Enabled = True

    Rhino.RhinoApp.WriteLine("Click somewhere in a viewport ...")
    return Rhino.Commands.Result.Success

RunCommand()

First, when running this in the editor, the editor panel disappears when you launch the script (normal) and "Click somewhere in a viewport ..." is indeed printed to the command line. But, the editor comes back up before you click in a viewport - which is puzzling, as the script hasn’t finished and is still waiting for you to click somewhere.

Then, if I click in a viewport (the first run), it does print the viewport name I clicked in to the command line. If I run it again, the (correct) viewport name gets printed agan, but twice. Run it again, I get three prints. And that continues for as many times as I run it successively during a session, every run one more line is printed.

Now, all I really want is (for example) to select an object, then click in a viewport and be able to get its name or ID as data, then continue on with the script and do other stuff (using the viewport name for some operation later on)…

But, I have no idea how to actually get the viewport name out of the class - yes, it prints, but I need it outside the class elsewhere in the script. I really haven’t got a clue when it comes to classes and event handling… I tried a bunch of stuff, but nothing has worked so far.

Is there anyone that can explain what I need to do so relatively simply? Also, explain why the posted sample itself doesn’t work correctly after the first run?

TIA, --Mitch

Hi @Helvetosaur,

Well then you do not want to use a MouseCallback, which acts more like an event watcher.

How about using RhinoGet.GetView?

– Dale

Hmm, that would be great if I could get it to work…

import Rhino
rc,view=Rhino.Input.RhinoGet.GetView("Click in a viewport")
if rc!=Rhino.Commands.Result.Success:
    print "Failed!"
else:
    print view

It doesn’t fail, but ‘view’ is always None

–Mitch

Hi Mitch,

I see this works in V5 but not in V6. I’m on it.

https://mcneel.myjetbrains.com/youtrack/issue/RH-43489

– Dale

RH-43489 is fixed in the latest Rhino 6 Service Release Candidate