Basic python question


#1

Hi guys, I am hacking away in python and I have trouble getting the data I need.
I could not find the rhinoscriptsyntax GetCursorPos in python so I looked elsewhere.
But I obviously lack the basic knowledge to understand why this isn’t giving me the X coordinate…

import ctypes as ct
currPos = ct.windll.user32.GetCursorPos()
print currPos[0]

#2

You could use this .NET method, seems to work:

import System

# Get the mouse position
mp = System.Windows.Forms.Control.MousePosition

# Extract the coordinates
xPos = mp.X
yPos = mp.Y

print xPos,yPos

#3

Thanks Anders!
Python surely opens a lot of doors to different libraries, and it is difficult to know how to use all this info.


#4

I need some more help:

So now I have a script that monitors the cursor and rotates camera. It’s a “look around mode” similar to pressing Ctrl-Alt + RMB. (It’s going to become a walkaround plugin)

But the cursor lags when passing over objects, icons and viewport edges so I am wondering if I can use Rhino.UI.MouseCallback in some way, but I don’t understand how to use it… how do I even look it up?

Here is what I have built so far.

import System
import scriptcontext
import rhinoscriptsyntax as rs
import ctypes as ct

intAngle=20
rs.EnableRedraw(False)

# Get the mouse position
while True:
    mp1 = System.Windows.Forms.Control.MousePosition
    rs.EnableRedraw(True)
    rs.EnableRedraw(False)
    mp2 = System.Windows.Forms.Control.MousePosition

    if mp2.X > mp1.X:
        Xangle=(mp2.X-mp1.X)/intAngle
        rs.RotateCamera(direction=0, angle=Xangle)
    if mp2.X < mp1.X:
        Xangle=(mp1.X-mp2.X)/intAngle
        rs.RotateCamera(direction=1, angle=Xangle)
    if mp2.Y > mp1.Y:
        Yangle=(mp2.Y-mp1.Y)/intAngle
        rs.RotateCamera(direction=2, angle=Yangle)
    if mp2.Y < mp1.Y:
        Yangle=(mp1.Y-mp2.Y)/intAngle
        rs.RotateCamera(direction=3, angle=Yangle)

#    ct.windll.user32.SetCursorPos(500, 500)

#check for esc press
    if scriptcontext.escape_test(False):
        print "ESC pressed "
        rs.EnableRedraw(True) 
        MouseCallback(True)
        break      #get out of the loop

(Steve Baer) #5

I just added GetCursorPos to rhinoscriptsyntax for SR8.


(Steve Baer) #6

MouseCallback is a class that is subclassed to get callbacks in virtual functions. In your set-up it should do pretty much the same as what you are already doing. Here’s a sample of the same thing using the MouseCallback class instead of checking on mouse position

import System
import scriptcontext
import rhinoscriptsyntax as rs
import Rhino


class MyMouseCallback(Rhino.UI.MouseCallback):
    def __init__(self):
        self.mp1 = None
        self.mp2 = None
        
    def OnMouseMove(self, args):
        intAngle = 20
        self.mp2 = args.ViewportPoint
        if self.mp1:
            if self.mp2.X > self.mp1.X:
                Xangle=(self.mp2.X-self.mp1.X)/intAngle
                rs.RotateCamera(direction=0, angle=Xangle)
            if self.mp2.X < self.mp1.X:
                Xangle=(self.mp1.X-self.mp2.X)/intAngle
                rs.RotateCamera(direction=1, angle=Xangle)
            if self.mp2.Y > self.mp1.Y:
                Yangle=(self.mp2.Y-self.mp1.Y)/intAngle
                rs.RotateCamera(direction=2, angle=Yangle)
            if self.mp2.Y < self.mp1.Y:
                Yangle=(self.mp1.Y-self.mp2.Y)/intAngle
                rs.RotateCamera(direction=3, angle=Yangle)
        self.mp1 = self.mp2

cb = MyMouseCallback()
cb.Enabled = True
# Get the mouse position
while True:
    Rhino.RhinoApp.Wait() #let the message pump stay alive
    #check for esc press
    if scriptcontext.escape_test(False):
        print "ESC pressed "
        break      #get out of the loop
cb.Enabled = False

#7

Thanks, that’s very interesting.
I put the project on hold as I need to figure out how to control the cursor in Rhino with out interfering with other programs.
I will look into this when I have SR8.

THANKS!