Basic Add On Scripting Questions

So I have began work on a series of scripts that would assist me in hard surface workflows by doing monotonous operations and allowing me to work faster however as I learn and relearn python I have come into some issues. I can only ask it in the form of questions but I am sure I am missing the point somewhere. I want to make an operator that basically,

go into edit mode / deselects all, selects sharp , mark sharp / bevel weight 1 / mark crease / go out of edit mode.

However I would like to have a cool panel that is like the F6 panel of operators that would allow me to select no sharp or bevel or crease setting post operator. Is that possible?

Also I wanted to create an option that

turns off relationship lines / sets only render / hides the grid floor / sets matcap to bigred(mmmmn bigred)
but work with a toggle so if it’s hit twice in the floating menu it goes back to normal.

Another example I’ve been trying to figure out is

vert bevel (divide 4) >> looptools (circle) inset >> extrude (cancel) >> alt + s scale

but i want to make it a stepped modal operator if thats possible. Where when you start it has you setting the size then on application it turns it to a circle and it auto insets and extrudes in place and the next modal operator is choosing depth.

I hope I wrote that clearly and not sound like a crazy person but I’ve been trying to figure it out but I think I lack the terminology understanding to use google-fu efficiently so ive just been stalking this board and trying to read other code. Thanks!

No shame in stalking this board. Hell, I stalk it all the time. You’ve got it half right in that you’re reading other people’s code. The other half is playing with it and seeing what happens when you poke it with the proverbial stick. That and your own project experiences should inform you on what kind of tools you really need to write.

Above all, have fun!:slight_smile:

                  So how would i script an operator to ignore a bevel that is on the  object but add a bevel if it is already on the object. But also if a  bevel is there to keep that width value instead of modifying it.

I think at some point I got confused as to how the script works which could be whats making it hard. But the expected behavior is always if a bevel isnt present then add one but now I have a 2nd operator that puts a bevel on the object just to allow this operator to work without error.

class csharpenOperator(bpy.types.Operator):
‘’‘Sharpen With Modifiers and Bevelling’’’
bl_idname = “csharpen.objects”
bl_label = “CSharpen”
bl_options = {‘REGISTER’, ‘UNDO’}

items = [(x.identifier, x.name, x.description, x.icon) 
         for x in bpy.types.Modifier.bl_rna.properties['type'].enum_items]

modtypes = EnumProperty(name="Modifier Types",
                       items=[(id, name, desc, icon, 2**i) for i, (id, name, desc, icon) in enumerate(items)
                               if id in ['BOOLEAN', 'MIRROR', 'BEVEL', 'SOLIDIFY']],
                       description="Don't apply these",
                       default={'BEVEL'},
                       options={'ENUM_FLAG'})

angle = FloatProperty(name="AutoSmooth Angle",
                      description="Set AutoSmooth angle",
                      default= radians(60.0),
                      min = 0.0,
                      max = radians(180.0),
                      subtype='ANGLE')

bevelwidth = FloatProperty(name="Bevel Width Amount",
                           description="Set Bevel Width",
                           default=0.0200,
                           min =
                           0.002,
                           max =0.25)

apply_all = BoolProperty(default = True)

original_bevel = FloatProperty()

@classmethod
def poll(cls, context):
    ob = context.object
    if ob is None:
        return False
    return (ob.type == 'MESH')

    #F6 MENU
def draw(self, context):
    layout = self.layout
    box = layout.box()
    # DRAW YOUR PROPERTIES IN A BOX
    #box.prop( self, 'ssharpangle', text = "SsharpAngle")
    col = box.column()
    col.prop(self, "modtypes", expand=True)
    box.prop( self, 'angle', text = "SmoothingAngle" )
    box.prop( self, 'bevelwidth', text = "BevelWidth")
    box.prop( self, 'apply_all', text = "ApplyAll")

def execute(self, context):
    scene = context.scene
    ob = context.object  # soapbox call don't use bpy.context as context is passed
    obs = context.selected_objects
    angle = self.angle
    original_bevel = self.original_bevel
    bevelwidth = self.bevelwidth
    
    #Sets Original Bevel To Initial Value
    #original_bevel = 0.2
    original_bevel = bpy.context.object.modifiers["Bevel"].width

    """
    
    obj.modifiers.get("Bevel")
    
    #Just Trying To Make It Work
    bpy.ops.object.modifier_remove(modifier="Bevel")
    bpy.ops.object.modifier_remove(modifier="Solidify")
    #so that the csharp doesnt mesh up the object
    
    #keep the old here for now
    bpy.ops.object.modifier_add(type='BEVEL')
    bpy.context.object.modifiers["Bevel"].use_clamp_overlap = False
    bpy.context.object.modifiers["Bevel"].show_in_editmode = False
    bpy.context.object.modifiers["Bevel"].width = bevelwidth
    bpy.context.object.modifiers["Bevel"].segments = 3
    bpy.context.object.modifiers["Bevel"].profile = 0.70
    bpy.context.object.modifiers["Bevel"].limit_method = 'WEIGHT'
    """
    
    mod_dic = {}
    if self.apply_all:
        #remove modifiers no one would want applied in this instance

        #bpy.ops.object.modifier_remove(modifier="Bevel")
        #bpy.ops.object.modifier_remove(modifier="Solidify")
        # replace with       
        mods = [m for m in ob.modifiers if m.type in self.modtypes]
        for mod in mods:

            mod_dic[mod.name] = {k:getattr(mod, k) for k in mod.bl_rna.properties.keys()
                                 if k not in ["rna_type"]}
            print(mod_dic)
            ob.modifiers.remove(mod)

        #convert to mesh for sanity
        #bpy.ops.object.convert(target='MESH')            
        #Object.to_mesh(scene, apply_modifiers, settings, calc_tessface=True, calc_undeformed=False)

        mesh_name = ob.data.name
        ob.data.name = 'XXXX'
        # remove the old mesh
        if not ob.data.users:
            bpy.data.meshes.remove(ob.data)               
        mesh = ob.to_mesh(scene, True, 'PREVIEW') # or 'RENDER'
        ob.modifiers.clear()
        mesh.name = mesh_name
        ob.data = mesh

        for name, settings in mod_dic.items():
            m = ob.modifiers.new(name, settings["type"])
            for s, v in settings.items():
                if s == "type":
                    continue
                setattr(m, s, v)
 
    return {'FINISHED'}

Hi,

To get the code markup,

[noparse]


Add code here

[/noparse]

ah ok i was wondering about that.


class csharpenOperator(bpy.types.Operator):
    '''Sharpen With Modifiers and Bevelling'''
    bl_idname = "csharpen.objects"
    bl_label = "CSharpen"
    bl_options = {'REGISTER', 'UNDO'} 
    
    items = [(x.identifier, x.name, x.description, x.icon) 
             for x in bpy.types.Modifier.bl_rna.properties['type'].enum_items]

    modtypes = EnumProperty(name="Modifier Types",
                           items=[(id, name, desc, icon, 2**i) for i, (id, name, desc, icon) in enumerate(items)
                                   if id in ['BOOLEAN', 'MIRROR', 'BEVEL', 'SOLIDIFY']],
                           description="Don't apply these",
                           default={'BEVEL'},
                           options={'ENUM_FLAG'})

    angle = FloatProperty(name="AutoSmooth Angle",
                          description="Set AutoSmooth angle",
                          default= radians(60.0),
                          min = 0.0,
                          max = radians(180.0),
                          subtype='ANGLE')

    bevelwidth = FloatProperty(name="Bevel Width Amount",
                               description="Set Bevel Width",
                               default=0.0200,
                               min =
                               0.002,
                               max =0.25)

    apply_all = BoolProperty(default = True)

    original_bevel = FloatProperty()

    @classmethod
    def poll(cls, context):
        ob = context.object
        if ob is None:
            return False
        return (ob.type == 'MESH')

        #F6 MENU
    def draw(self, context):
        layout = self.layout
        box = layout.box()
        # DRAW YOUR PROPERTIES IN A BOX
        #box.prop( self, 'ssharpangle', text = "SsharpAngle")
        col = box.column()
        col.prop(self, "modtypes", expand=True)
        box.prop( self, 'angle', text = "SmoothingAngle" )
        box.prop( self, 'bevelwidth', text = "BevelWidth")
        box.prop( self, 'apply_all', text = "ApplyAll")

    def execute(self, context):
        scene = context.scene
        ob = context.object  # soapbox call don't use bpy.context as context is passed
        obs = context.selected_objects
        angle = self.angle
        original_bevel = self.original_bevel
        bevelwidth = self.bevelwidth
        
        mod_dic = {}
        if self.apply_all:
            #remove modifiers no one would want applied in this instance

            #bpy.ops.object.modifier_remove(modifier="Bevel")
            #bpy.ops.object.modifier_remove(modifier="Solidify")
            
            # replace with       
            mods = [m for m in ob.modifiers if m.type in self.modtypes]
            for mod in mods:

                mod_dic[mod.name] = {k:getattr(mod, k) for k in mod.bl_rna.properties.keys()
                                     if k not in ["rna_type"]}
                print(mod_dic)
                ob.modifiers.remove(mod)

            #convert to mesh for sanity
            #bpy.ops.object.convert(target='MESH')            
            #Object.to_mesh(scene, apply_modifiers, settings, calc_tessface=True, calc_undeformed=False)

            mesh_name = ob.data.name
            ob.data.name = 'XXXX'
            # remove the old mesh
            if not ob.data.users:
                bpy.data.meshes.remove(ob.data)               
            mesh = ob.to_mesh(scene, True, 'PREVIEW') # or 'RENDER'
            ob.modifiers.clear()
            mesh.name = mesh_name
            ob.data = mesh

            for name, settings in mod_dic.items():
                m = ob.modifiers.new(name, settings["type"])
                for s, v in settings.items():
                    if s == "type":
                        continue
                    setattr(m, s, v)
                    
        return {'FINISHED'}


just testing out the code markup