[2.8] Smart Duplicate Script

hey guys, I’m trying to write a simple script that duplicates objects or faces selected based on the current selection. Right now it is duplicating in object mode just like i want it to, but I cannot manage to do this in face mode.

class SmartDuplicate(bpy.types.Operator):
	bl_idname = "mesh.smart_duplicate"
	bl_label = "Smart Duplicate"
	bl_description = "Duplicates selection of faces and objects"
	bl_options = {'REGISTER', 'UNDO'}
	
	def execute(self,context):
		if context.mode == 'OBJECT':
			bpy.ops.object.duplicate()
		elif context.mode == 'EDIT_MESH':
			selectionMode = (tuple(bpy.context.scene.tool_settings.mesh_select_mode))
			#if Face is selected
			if selectionMode[2]:
				print("Hello World")
				bpy.ops.mesh.duplicate()
				bpy.ops.mesh.separate(type='SELECTED')

		return {'FINISHED'}

The print message doesnt even show up, which leads me to believe I am not correctly specifying the condition that checks whether selection in face mode…Does anyone know what am I missing?

I was finally able to make it work! It was as easy as removing one pair of ( ) s…

selectionMode = (tuple(bpy.context.scene.tool_settings.mesh_select_mode))

to…

selectionMode = tuple(bpy.context.scene.tool_settings.mesh_select_mode)

…and bam! Now, to finish this script I would have to add still some lines, namely to detach the selected faces into a separate object (already taken care of through the mesh.separate command), and then to get out of EditMode&Select the newly created object, in object mode. Could anyone point me in the right direction as to how to accomplish this?

Finally was able to figure out how to select the duplicated faces! Wouldn’t have been possible without user Iceythe’s help. He’s awesome :slight_smile:

class SmartDuplicate(bpy.types.Operator):
    bl_idname = "mesh.smart_duplicate"
    bl_label = "Smart Duplicate"
    bl_description = "Duplicates selection of faces and objects"
    bl_options = {'REGISTER', 'UNDO'}
    
    def execute(self,context):
        if context.mode == 'OBJECT':
            bpy.ops.object.duplicate()
        elif context.mode == 'EDIT_MESH':
            selectionMode = tuple(bpy.context.scene.tool_settings.mesh_select_mode)
            #if Face is selected
            if selectionMode[2]:
                bpy.ops.mesh.duplicate()
                bpy.ops.mesh.separate(type='SELECTED')

                obj = context.object
                bpy.ops.object.editmode_toggle()
                
                obj.select_set(False)
                context.view_layer.objects.active = context.selected_objects[-1]

        return {'FINISHED'}