[yet another] little mouse cursor script

Hi,

I just made a little script that makes a 2d cursor follow the mouse. This may be useful for menus and such stuff… I’m sure that there are already tons of scripts such as this one, but I was tired of searching in this forum, because most of the links don’t work any more. And doing that was a good exercise. :slight_smile:

>> Here’s the blender file.

And here are the scripts contained in this file:

# mouse_init.py - This script initializes the cursor settings for the mouse_move.py script.
#
# Copyright 2004 Gabriel Walt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# To make this script work, you have to create the following structure in Blender 2.25:
# Add the following logic bricks to the object that will be your cursor:
# Always [without pulsing] => Python [call this script].

import Rasterizer

Rasterizer.showMouse(0)
Rasterizer.setMousePosition(Rasterizer.getWindowWidth()/2, Rasterizer.getWindowHeight()/2)
# mouse_move.py - A little script that makes an object follow the mouse, as for a cursor.
#
# Copyright 2004 Gabriel Walt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# To make this script work, you have to create the following structure in Blender 2.25:
# Create an object that will be your cursor and add the following logic bricks:
# Mouse [Movement] called "mouse" => Python [call this script] => Motion called "move".

# These parameters may be modified to suit your needs.
# The sensibility is the speed of the mouse, that must be adjusted depending the scale.
# And the x/y parameters define a square in which the cursor movement will be limited.
sensibility = 0.05
xMax = 8
xMin = -8
yMax = 8
yMin = -8

import GameLogic
import Rasterizer

cursorC = GameLogic.getCurrentController()
cursor = cursorC.getOwner()
cursorS = cursorC.getSensor("mouse")
cursorA = cursorC.getActuator("move")
cursorPos = cursor.getPosition()

xPos = cursorS.getXPosition()
yPos = cursorS.getYPosition()

xMid = Rasterizer.getWindowWidth()/2
yMid = Rasterizer.getWindowHeight()/2

if (cursorPos[0]+(xPos-xMid)*sensibility < xMin):
    xPos = xMid+(xMin-cursorPos[0])/sensibility
if (cursorPos[0]+(xPos-xMid)*sensibility > xMax):
    xPos = xMid+(xMax-cursorPos[0])/sensibility
if (cursorPos[1]+(yMid-yPos)*sensibility < yMin):
    yPos = yMid+(cursorPos[1]-yMin)/sensibility
if (cursorPos[1]+(yMid-yPos)*sensibility > yMax):
    yPos = yMid+(cursorPos[1]-yMax)/sensibility

cursorA.setDLoc((xPos-xMid)*sensibility, (yMid-yPos)*sensibility, 0, 0)
GameLogic.addActiveActuator(cursorA, 1)

Rasterizer.setMousePosition(xMid, yMid)

Have fun!

Gabriel

however as this file doesn’t allow a 3d cursor to line up with the system cursor (it locks the cursor in one spot) it is mostly useless to me

also, it seems to work better than another mouse script I had (don’t know if I posted it here), so I may use it yet.

I just found a script that works well, and that may correspond to what you want: http://ash.webpark.sk/download/cursor.blend

And here’s the topic where I found the file: https://blenderartists.org/forum/viewtopic.php?t=15322

I’m still trying to understand why this script works… :-?

Gabriel

the cursor that u found is not bad… but my cursor speed is faster so I have to set “koef = 25.2”

the one that you made is good too…

I have been able to get similar, but again it doesn’t line up with the system cursor

when the need returns I will probably find a better way

ok, i made a small rewrite with few enhancements of ashsid’s script. this script really should follow the system cursor, and thus should be compatible with the mouseover logic bricks! but the problem is that it is dependant of what you see, and the calculations are made from the camera. so this doesn’t work inside blender publisher, since you always see more than your camera limits… make a runtime, because there you only see what your camera shows, and the script should work! i don’t think that there is a better method of doing that…

by the way, this script should also follow the camera, if you move it, since the cursor is parented to the camera, and movements are local transformations. but i didn’t check if this really works…

you can download the file from: http://ysagoon.com/diz/blender/files/cursor.blend

there are a few parameters that may be configured:

  • custom [bool] - this shows/hides the custom [ie blender] cursor.
  • system [bool] - this shows/hides the system [ie windoz/linux/etc] cursor.
  • dist [float] - this is the distance from the camera where the custom cursor will be displayed.
  • lens [float] - the lens focale of the parented camera.
  • x & y [float] - the start position of the cursor.

and again the code… for the complete structure of the parameters and logic brics, download the *.blend file.

# cursorInit - Initialization for the cursorMove script.
#
# Copyright 2003 ashsid
# Copyright 2004 Gabriel Walt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

import Rasterizer

cursorC = GameLogic.getCurrentController()
cursorA = cursorC.getActuator("move")
cursor = cursorC.getOwner()

cursorA.setDLoc(0, 0, -cursor.dist-1, 1)
GameLogic.addActiveActuator(cursorA, 1)
cursor.setVisible(cursor.custom)

Rasterizer.setMousePosition(Rasterizer.getWindowWidth()/2, Rasterizer.getWindowHeight()/2)
Rasterizer.showMouse(cursor.system)
# cursorMove - A script that makes an object follow the mouse, as for a cursor.
#
# Copyright 2003 ashsid
# Copyright 2004 Gabriel Walt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

import Rasterizer

lensCoef = 16

cursorC = GameLogic.getCurrentController()
cursorS = cursorC.getSensor("cursor")
cursorA = cursorC.getActuator("move")
cursor = cursorC.getOwner()

xPos = cursorS.getXPosition()
yPos = cursorS.getYPosition()
xCent = Rasterizer.getWindowWidth()/2
yCent = Rasterizer.getWindowHeight()/2

xCursor = (xPos-xCent)*lensCoef/cursor.lens*cursor.dist/xCent
yCursor = (yCent-yPos)*lensCoef/cursor.lens*cursor.dist/xCent

cursorA.setDLoc(xCursor-cursor.x, yCursor-cursor.y, 0, 1)
GameLogic.addActiveActuator(cursorA, 1)

cursor.x = xCursor
cursor.y = yCursor

Rasterizer.setMousePosition(xPos, yPos)

note: the lensCoef of 16 is in fact [if i understood it well] universal, because a lens of 16mm should have an opening angle of 90°. if you want to make the custom cursor more or less sensitive as the system cursor, you may adjust either this value, or the lens property.

note: by default, before script initialization, the cursor must be 1 blender unit behind the camera. that’s why there is a -1 in the line “cursorA.setDLoc(0, 0, -cursor.dist-1, 1)” of the initialization script. if you move the start cursor z position, you have to modify this line too.

note: there is a small bug with the mouse-movement logic brick, as soon as the mouse goes out through the upper or the left border, the cursor-movement sensor is no longer activated even if the cursor still moves but is outside of the window. this makes that the cursor often stops before it reaches the border [at the last position before moving out]. the last line “Rasterizer.setMousePosition(xPos, yPos)” is a work-around to lower the nasty effects of this bug.

thanks ashsid, your script is really great!! :wink:

gabriel