Object.raycast() does not detect intersections where it should #53714

Closed
opened 2018-01-07 22:54:07 +01:00 by dima glib · 5 comments
Member

System Information
Windows 7, GPU: AMD Radeon HD 8670D

Blender Version
Broken: 2.77-2.79 (up to builder.blender.org version)
Worked: 2.75, 2.76

Short description of error
Object.raycast() does not detect intersection where it should.

Exact steps for others to reproduce the error

  • create the default scene
  • select the cube,
  • paste the following snippet, run it, launch the operator in 3D view
    The debug information will be printed to console

In Blender 2.75 and 2.76, raycast detects the cube when the mouse is over any part of it.
In Blender 2.77-2.79, raycast detects the cube only when the mouse is noticeably inside of the cube.

import bpy
 
from bpy_extras.view3d_utils import (
    region_2d_to_location_3d,
    )
 
from mathutils import (
    Vector,
    )
 
class MoveXYOperator(bpy.types.Operator):
    """Test raycast"""
    bl_idname = "view3d.move_xy"
    bl_label = "Move XY"
 
    @classmethod
    def poll(cls, context):
        return context.object
 
    def modal(self, context, event):
        if event.type == 'MOUSEMOVE':
            self.move(context, event)
 
        elif event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'}:
            return {'FINISHED'}
 
        return {'RUNNING_MODAL'}
 
    def invoke(self, context, event):
        if context.space_data.type == 'VIEW_3D':
            self.ob = context.object
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "Active space must be a View3d")
            return {'CANCELLED'}
 
    def move(self, context, event):
        mxy = (event.mouse_region_x, event.mouse_region_y)
        xy0 = region_2d_to_location_3d(
                context.region, context.space_data.region_3d,
                mxy, Vector((0,0,100)))
        xy1 = region_2d_to_location_3d(
                context.region, context.space_data.region_3d,
                mxy, Vector((0,0,-100)))
        - print(xy0)
        - print(xy1)
 
        obj = self.ob
       
        m = obj.matrix_world.copy()
        try:
            mi = m.inverted()
        except:
            # this is some degenerate object
            print("degenerate matrix")
            return
        la = mi * xy0
        lb = mi * xy1
        - print(la)
        - print(lb)
       
        res = obj.ray_cast(la, lb)
        print(res[-1])
 
def register():
    bpy.utils.register_class(MoveXYOperator)
 
def unregister():
    bpy.utils.unregister_class(MoveXYOperator)
 
if __name__ == "__main__":
    register()
**System Information** Windows 7, GPU: AMD Radeon HD 8670D **Blender Version** Broken: 2.77-2.79 (up to builder.blender.org version) Worked: 2.75, 2.76 **Short description of error** Object.raycast() does not detect intersection where it should. **Exact steps for others to reproduce the error** * create the default scene * select the cube, * paste the following snippet, run it, launch the operator in 3D view The debug information will be printed to console In Blender 2.75 and 2.76, raycast detects the cube when the mouse is over any part of it. In Blender 2.77-2.79, raycast detects the cube only when the mouse is noticeably inside of the cube. ``` import bpy from bpy_extras.view3d_utils import ( region_2d_to_location_3d, ) from mathutils import ( Vector, ) class MoveXYOperator(bpy.types.Operator): """Test raycast""" bl_idname = "view3d.move_xy" bl_label = "Move XY" @classmethod def poll(cls, context): return context.object def modal(self, context, event): if event.type == 'MOUSEMOVE': self.move(context, event) elif event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'}: return {'FINISHED'} return {'RUNNING_MODAL'} def invoke(self, context, event): if context.space_data.type == 'VIEW_3D': self.ob = context.object context.window_manager.modal_handler_add(self) return {'RUNNING_MODAL'} else: self.report({'WARNING'}, "Active space must be a View3d") return {'CANCELLED'} def move(self, context, event): mxy = (event.mouse_region_x, event.mouse_region_y) xy0 = region_2d_to_location_3d( context.region, context.space_data.region_3d, mxy, Vector((0,0,100))) xy1 = region_2d_to_location_3d( context.region, context.space_data.region_3d, mxy, Vector((0,0,-100))) - print(xy0) - print(xy1) obj = self.ob m = obj.matrix_world.copy() try: mi = m.inverted() except: # this is some degenerate object print("degenerate matrix") return la = mi * xy0 lb = mi * xy1 - print(la) - print(lb) res = obj.ray_cast(la, lb) print(res[-1]) def register(): bpy.utils.register_class(MoveXYOperator) def unregister(): bpy.utils.unregister_class(MoveXYOperator) if __name__ == "__main__": register() ```
Author
Member

Added subscriber: @dairin0d

Added subscriber: @dairin0d

Added subscriber: @ideasman42

Added subscriber: @ideasman42
`ray_cast` now takes an origin-direction. https://docs.blender.org/api/blender_python_api_2_78c_release/bpy.types.Object.html#bpy.types.Object.ray_cast See: 08fb55eea2

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Campbell Barton self-assigned this 2018-01-08 01:23:42 +01:00
Author
Member

Sorry! I'm so stupid :-(
Mysteriously, this change in semantics somehow did not affect my 3D cursor addon in perspective view, and I only learned that there's a problem when a user pointed out that the addon's snapping doesn't work correctly in orthographic view.

Sorry! I'm so stupid :-( Mysteriously, this change in semantics somehow did not affect my 3D cursor addon in perspective view, and I only learned that there's a problem when a user pointed out that the addon's snapping doesn't work correctly in orthographic view.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#53714
No description provided.