1
$\begingroup$

I've found a script that I want to test and use from a thread around, which was marked as "SOLVED" with this script. However, I can't get how this script is used:

import bpy
import bmesh

class SimpleOperator(bpy.types.Operator):
     """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return (context.object is not None and
            context.object.type == 'MESH' and
            context.object.data.is_editmode)

    def invoke(self, context, event):
        me = context.object.data
        bm = bmesh.from_edit_mesh(me)
        verts_sel = [v.select for v in bm.verts]
        edges_sel = [e.select for e in bm.edges]
        faces_sel = [f.select for f in bm.faces]

        loc = event.mouse_region_x, event.mouse_region_y
        try:
            geom = bm.select_history[-1]
        except IndexError:
            geom = None

        ret = bpy.ops.view3d.select(extend=True, location=loc)
        if ret == {'PASS_THROUGH'}:
            self.report({'INFO'}, "no close-by geom")
            return {'CANCELLED'}

        try:
            geom2 = bm.select_history[-1]
            print("geom2 sel 1st", geom2.select)
        except IndexError:
            geom2 = None

        if geom is None:
            geom = geom2

        if isinstance(geom, bmesh.types.BMVert):
            geom_sel = verts_sel
            bm_geom = bm.verts
        elif isinstance(geom, bmesh.types.BMEdge):
            geom_sel = edges_sel
            bm_geom = bm.edges
        elif isinstance(geom, bmesh.types.BMFace):
            geom_sel = faces_sel
            bm_geom = bm.faces

        for sel, g in zip(geom_sel, bm_geom):
            if sel != g.select:
                g.select_set(True)
                bm.select_history.remove(g)
                bm.select_flush_mode()
                break


        self.report({'INFO'}, repr(geom))
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

The thread I am mention is here: Get Edge / Vertex / Face from mouse position Please, if you can explain what I have to do with this code.

Regards.

$\endgroup$
1
$\begingroup$

Well, basically if you don't define an interface for your operator, you can run your code and then press the Spacebar and search it with the name of "simple object operator" in the box that appears. When you find it, simply click it and it will run. enter image description here

$\endgroup$
3
  • $\begingroup$ Hello, I've tried this, 'Spacebar' on the context next to the script editor, however the operator is not present there. $\endgroup$ May 23 '17 at 11:00
  • $\begingroup$ Probably because the poll function doesn't show that to you. In fact, this function explains where are the conditions for the operator to be applicable . If you remove that part, the operator will be always applicable. I will update my answer with a screenshot anyway. $\endgroup$
    – Rexam
    May 23 '17 at 11:16
  • $\begingroup$ Your poll function is saying: "You can apply this operator if context.object is not None, if your object is a mesh and if you are in edit mode". In the image I uploaded I just removed this function, but it works perfectly even with that. $\endgroup$
    – Rexam
    May 23 '17 at 11:26

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.