Error: Object has no mesh data to be used for ray casting

Hey, all.

I’m writing an operator that casts rays from the center towards certain amounts of points and finds the faces rays hit. I’m facing this annoying error, when I change the property of the operator.

I’ve managed to strip the code down to minimal code to reproduce this, that works on default scene


import bpy
import bmesh
from bpy.props import *
from mathutils import *

class SomeOperator(bpy.types.Operator):
    bl_idname = "some_operator"
    bl_label = "Some operator"
    bl_options = {'REGISTER', 'UNDO', 'PRESET'}

    prop = IntProperty(name="Prop",
        description="Prop",
        min=10,
        max=50,
        default=10)

    def execute(self, context):
        
        obj = context.active_object
        
        if bpy.context.mode != 'OBJECT':
            raise Exception("Please switch to object mode")

        cover = [Vector((0,0,1)), Vector((0,0,-1)), Vector((1,0,0)), Vector((-1,0,0))]
        
        for pt in cover:
            dummy1, dummy2, faceInd = obj.ray_cast(Vector((0.0, 0.0, 0.0)), pt)
            
            obj.data.polygons[faceInd].select = True
        
        return {'FINISHED'}


Thanks,
Denis.

Object.raycast does not work for an object that is in edit mode :\ . Maybe you should use the ray_cast of the bvh_tree in place.
http://www.blender.org/api/blender_python_api_2_76_1/mathutils.bvhtree.html?highlight=ray_cast#mathutils.bvhtree.BVHTree.ray_cast

It’s a typo in exception message, I am doing it in object mode.

Had to upgrade to 2.76 to check BVHTree, works perfectly, I’ll use it for my needs.

Thanks!