get mouse coordinates when in 3d view

hi

I would like to write a python script that is able to listen to mouse events when focus is on a 3d view (not only on script window). basically i want to create a tool that can “paint” points within a 3d view. I need mouse position when mouse is dragged over a 3d view.
i tryed this (but it just give mouse coordinates when focus is on the script window):

import Blender
from Blender.Draw import *

def event(evt, val):
XY=Blender.Window.GetMouseCoords()
print 'mouse coord: ', XY

Blender.Draw.Register(None, event, None)

thanks in advance for your help

olivier

hi otisee,
read about Space Handler script links:
http://www.blender.org/documentation/248PythonDoc/API_related-module.html

thanks for your answer!
is there any way to get mouse release events ?

o

def event(evt, val):
    if val:
        if evt==Draw.LEFTMOUSE: # click event
            pass
    else:
        if evt==Draw.LEFTMOUSE: # release event
            pass

sorry… may be I misunderstood something but your example is not working when using Space Handler script links, right? actually my question was when using Space Handler script links, how can we get mouse release events since we just have access to event type via Blender.event ( and not value…)?

sorry, i don’t know if it works, i haven’t worked with Space Handler yet

Per the API docs the Event spachandler exposes the following attributes:
Blender.event
Blender.eventValue

Example from the API (with relevant “val” comment added by me):


# SPACEHANDLER.VIEW3D.EVENT

import Blender
from Blender import Draw
evt = Blender.event
val = Blender.eventValue
return_it = False

if evt == Draw.LEFTMOUSE:
  # Handles both Click and Release.  Check value of "val" to know the difference (1 for click and 0 for release)
  print "Swallowing the left mouse button press"
elif evt == Draw.AKEY:
  print "Swallowing an 'a' character"
else:
  print "Let the 3D View itself process this event: %d with value %d" % (evt, val)
  return_it = True

# if Blender should not process itself the passed event:
if not return_it: Blender.event = None

thnaks a lot! I should have seen this. actually a link to an older version of the doc appear when googling for api. anyway.
o

this example doesn’t work. " .eventValue " is not found. I have version 248.1 on mac. this example is in doc 248. is it a bug or am i missing something ?
thanks in advance.
o

Are you importing Blender? i.e. import Blender

I can assure you that Blender.eventValue is available

evt = Blender.event
val = Blender.eventValue

yes but it seems that you can not get release events, i.e. Blender.eventValue is 0 , right ?

when I am attaching the following code to a 3d view window, it only display “event value: 1”, but no release events…

SPACEHANDLER.VIEW3D.EVENT

import Blender
from Blender import Draw
evt = Blender.event
val = Blender.eventValue

if evt == Draw.LEFTMOUSE:
print "event value : ",val

Blender.event = None

Well if that is indeed the case (i.e. you only get clicks) would this not be enough for what you want to do?

there is a solution for it in Apricot branch in svn rev.15114 by ideasman
means eventValue==0 test should work with apricot builds, (so far i understand it)

ok, thank you !