Finding the nearest point to 3d cursor

Hello I wrote such a script who should find the nearest point from 3d cursor “” def execute(self, context):
cursor = bpy.context.scene.cursor
obj = bpy.context.active_object

    bpy.ops.object.mode_set( mode = 'OBJECT' )
    faceId = obj.closest_point_on_mesh( cursor )[-1]

    if faceId != -1:
        o.data.polygons[ faceId ].select = True

    bpy.ops.object.mode_set( mode = 'EDIT' )""

but I have such a error "Object.closest_point_on_mesh<>: error with argument 1, “origin” - sequence expected at dimension 1, not ‘View3DCursor’
Maybe someone know, what is wrong in this code ?
I will be very grateful for help :slight_smile:
Greets

The method closest_point_on_mesh() expects the first argument to be a Vector type, or a tuple of 3 floats. Your cursor variable is neither of these.

print(type(cursor))

gives:

<class 'bpy.types.View3DCursor'>

What you want is the cursor’s .location property:

faceId = obj.closest_point_on_mesh(cursor.location)[-1]

Also a discorse tip: Use triple backticks for code blocks:

```
(code)

```

Thanks for help It’s working…
But I have problem with registering
bpy.types.View3DCursor…

class Kill_segment(bpy.types.View3DCursor):
#bpy.types.View3DCursor    
    """Kill segment"""
    bl_idname = "fp.kill_segment"
    bl_label = "Kill segment"

    def execute(self, context):
        cursor = bpy.context.scene.cursor
        obj = bpy.context.active_object
        bpy.ops.object.mode_set( mode = 'OBJECT' )
        faceId = obj.closest_point_on_mesh(cursor.location)[3]

        if faceId != -1:
            obj.data.polygons[ faceId ].select = True
    
        bpy.ops.object.mode_set( mode = 'EDIT' )

Of course I’m not registering a class, and script is running.
But if I do anything in the code, nothing changes after run script. I can delete the whole class and after run script nothing changes. The button, as it was, still is.
I have no idea what’s going on.

I will be very grateful for help :slight_smile:
Greets

You can’t inherit from the cursor. Use bpy.types.Operator instead.
The cursor and 90% other classes exist only to expose some properties from C. They are otherwise internal.

So, you’ll need to subclass operators, then make sure it’s registered afterwards.