2

I'm making a panel with buttons to do different actions for modeling. One one of my button I want to merge vertex by distance when I click on it, but merge to center when I CTRL + Click.

I got this working just fine except that I would like my regular click to show the context panel that shows up when you call with from Vertex -> Merge Vertices -> By Distance where you can adjust the threshold and what not.

I mean this window:

enter image description here

I noticed that when I call the function with

row.operator("mesh.remove_doubles")

the context panel does show up, but when I call it inside invoke to get the event.CTRL it does not show up.

def invoke(self, context, event):
    if event.ctrl:
        bpy.ops.mesh.merge(type='CENTER')
    else:
        bpy.ops.mesh.remove_doubles()

    return {'FINISHED'}

Any way to run the same method in the invoke ? Thanks!

Phantom
  • 39
  • 3

1 Answers1

0

Ok so that's what ended up having that works the way I wanted

import bpy

class Weld(bpy.types.Operator):
    bl_idname = "edit.weld"
    bl_label = "Weld"
    bl_description = "Weld vertices by distance\nHold CTRL to weld at center"
    bl_options = {'REGISTER', 'UNDO'}

    merge_dist: bpy.props.FloatProperty(
        name="Merge Distance",
        description="Merge Distance",
        min=0.0,
        step=0.1,
        default=0.02
    )

    @classmethod
    def poll(cls, context):

        if context.active_object.mode != 'EDIT':
            return False        
        if len(context.selected_objects) < 1:
            return False

        return True

    def invoke(self, context, event):
        if event.ctrl:
            bpy.ops.mesh.merge(type='CENTER')
            return{'PASS_THROUGH'}
        else:
            return{'FINISHED'}

    def execute(self, context):
        bpy.ops.mesh.remove_doubles(threshold=self.merge_dist)
        return {'FINISHED'}
Phantom
  • 39
  • 3
  • Cool, do you mind adding any explanation? How this site works: https://blender.stackexchange.com/tour, also please read: https://blender.stackexchange.com/help/how-to-ask -- regarding your code (1) there are conventions for operator class names (2) you should add a check for Edit Mode to the poll methond context.mode == 'EDIT_MESH' (greyed out button in Object Mode) and again (3) I suggest use bmesh it's way faster... – brockmann Mar 27 '20 at 11:16
  • Thanks for the useful information, I'm just getting started with Blender so I really have no idea about all the naming in general yet. About bmesh, I started to look at it, but couldn't get it to work. Maybe I will start a separate thread about my questions for this one. – Phantom Mar 27 '20 at 13:09