Mesh reverts back to the state before the script was run

by Armored Wolf   Last Updated May 15, 2019 21:15 PM

I created a script (operator) that converts a selection of quads into a diamond pattern.

It works fine but when I try to Inset the newly created selection and change any of the options (like switching to individual face insets) the mesh seems to revert back to the original topology before my script was even run (see image below).

Image Example: enter image description here

I noticed that if I change the selection somehow after running my operator I stop having the Inset issue, so I'm assuming my script is missing some command to fully apply the changes it makes to the mesh or something.

Here's my operator:

import bpy
import bmesh

def main(context):

    context = bpy.context
    ob = context.edit_object
    me = ob.data
    bm = bmesh.from_edit_mesh(me)

    bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')

    originalEdges = set(e for e in bm.edges if e.select)

    bpy.ops.mesh.poke()

    allEdges = set(e for e in bm.edges if e.select)

    bpy.ops.mesh.region_to_loop()

    borderEdges = set(e for e in bm.edges if e.select)

    diamondEdges = allEdges - originalEdges - borderEdges

    originalWithoutBorder = originalEdges - borderEdges

    bpy.ops.mesh.select_all(action='DESELECT')

    for e in originalWithoutBorder:
       e.select = True

    bpy.ops.mesh.dissolve_mode(use_verts=True)

    for e in diamondEdges:
        e.select = True

    bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
    bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')   

    newFaces = set(f for f in bm.faces if f.select)
    bpy.ops.mesh.select_face_by_sides(number=3, type='EQUAL', extend=False)
    triangleFaces = set(f for f in bm.faces if f.select)

    newFaces = newFaces - triangleFaces
    bpy.ops.mesh.select_all(action='DESELECT')
    for f in newFaces:
       f.select = True

    bmesh.update_edit_mesh(me)

class VitalyPoke(bpy.types.Operator):

    """

    [Vitaly Poke]

    Description:
    Uses the Poke operator and some caveman code to convert a selection of quads into diamond-looking faces

    """

    bl_idname = "arc.vitaly_poke"
    bl_label = "Vitaly Poke (ARC)"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}

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

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

if __name__ == "__main__":
    register()


Related Questions


Updated August 10, 2015 17:06 PM

Updated August 11, 2015 17:06 PM

Updated June 09, 2016 08:06 AM

Updated August 01, 2015 13:06 PM

Updated May 31, 2016 08:06 AM