3
$\begingroup$

How can I find out if the mouse is over any object in the 3D View area with Python?

Cube in 3D View with Translate Manipulator and 3D Cursor

$\endgroup$
3
$\begingroup$

There's no property or function to find it out, there's an example script to find mesh objects under the mouse however. It casts a ray on LMB and selects an object if there is one (only mesh objects and duplis).

You find the script in the built-in Text Editor, in the menu:

Templates > Python > Operator Modal View3D Raycast

AFAIK it can't be changed to also hit objects like lamps and cameras, 'cause they don't have actual faces (they are kind of virtual objects only).

If you tell more about your ultimate goal, we might find a different approach that doesn't require to know whether there's something under the mouse cursor or not.

$\endgroup$
  • $\begingroup$ Goal: If the mouse is over to an object, it should be Highlighted in object mode. $\endgroup$ – Rasa Mohamed Feb 1 '14 at 4:23
  • $\begingroup$ By highlighted you mean just indicate that there is an object under the cursor, or make it selected/active? Such a behavior is available for mesh geometry in the Preselection build, perhaps ask paleajed to add highlighting for objects to it? $\endgroup$ – CoDEmanX Feb 1 '14 at 14:46
  • $\begingroup$ Yes, just indicate an object while Mouse is over. And Preselection build is only for Edit Mode right My goal is to Highlight in an Object Mode and also in Edit Mode. Already Edit Mode Highlighting, We have. $\endgroup$ – Rasa Mohamed Feb 2 '14 at 4:13
  • 1
    $\begingroup$ Dunno what you understand by Object coordinates. Could be the origin, which isn't necessarily "part" of the object (can be anywhere, far away from the center of mass based on the faces). Or every single vertex? It's possible to project the mouse into 3d space and calculate the distance to whatever you have there... Maybe utilize mathutils.kdtree for quick search in all verts? $\endgroup$ – CoDEmanX Feb 5 '14 at 3:57
  • 1
    $\begingroup$ mathutils.kdtree (docs) is not available in the current stable release (2.69), you need to get a very recent build, e.g. builder.blender.org/download $\endgroup$ – CoDEmanX Feb 5 '14 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.