1
$\begingroup$

I wrote a operator like that, And when I run this from object mode, everything is fine but if I run from sculpt mode, it cleans all the undo history and I ain't able to undo anything I did before. Why is that?

class MaskDecimate(bpy.types.Operator):
    bl_idname = "flow_tools.decimate"
    bl_label = "Mask Decimate"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}
    
    invoked = False
    
    ratio = bpy.props.FloatProperty(
        name="Ratio",
        description="How much to recuce",
        default=0.5,
        min = 0.0000001,
        max = 1.0
    )
    
    @classmethod
    def poll(cls, context):
        if context.active_object:
            if not context.active_object.mode == "EDIT":
                return context.active_object.type == "MESH"
    
    
    def invoke(self, context, event):
        self.bm = bmesh.new()
        self.bm.from_mesh(context.active_object.data)
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

    
    def execute(self, context):
        
        ob = context.active_object
        bm = self.bm
        bm.to_mesh(ob.data)
        
        layer = bm.verts.layers.paint_mask.verify()
        vg = ob.vertex_groups.new(name = "paint_vertex_group_mask")
        vg.add([v.index for v in bm.verts], 1.0, "REPLACE")
        
        for vert in bm.verts:
            mask_val = vert[layer]
            vg.add([vert.index], 1 - mask_val, "REPLACE")
        
        md = ob.modifiers.new(type="DECIMATE", name="Decmater")
        md.ratio = self.ratio
        md.vertex_group = vg.name
        bpy.ops.object.modifier_apply(modifier=md.name)
        
        bm = bmesh.new()
        bm.from_mesh(ob.data)
        layer = bm.verts.layers.paint_mask.verify()
        
        for vert in bm.verts:
            vert[layer] = 1 - vg.weight(vert.index)
        
        ob.vertex_groups.remove(vg)
        
        bm.to_mesh(ob.data)
        bm.free()
        return {"FINISHED"}
$\endgroup$
1
  • $\begingroup$ I've got the same issue with my addon for 2.92. Maybe this is a bug in sculpt mode. $\endgroup$ – mifth Feb 25 at 14:55
1
$\begingroup$

As a variant of a solution. I could suggest adding history before all the code. But I'm pretty sure this is a bug in Blender's sculpt mode.

if context.mode != 'OBJECT':    
    bpy.ops.ed.undo_push()
$\endgroup$

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.