3
$\begingroup$

I need a help with this. I know it exist in N panel Item tab

Text

I want this block in my custom addon panel, no need to be editable fields, just show values if edge/s selected.

$\endgroup$
3

2 Answers 2

3
$\begingroup$

So if anyone interested this is what I wanted to achieve

enter image description here

    import bpy, bmesh
    from bpy.types import PropertyGroup, Panel, Operator, Menu
    
    # Add-on info
    bl_info = {
        "name": "Crease and Bevel",
        "author": "TLousky, APEC, deadpin",
        "version": (0, 0, 1),
        "blender": (2, 83, 3),
        "location": "View3D > Properties > CaB",
        "description": "Shortcut UI panel for Mesh edge crease and bevel weight properties", 
        "doc_url": "",
        "tracker_url": "",      
        "category": "3D View"
    }
    
    ###########################################################################################
    ################################### Functions #############################################
    ###########################################################################################
    
    #https://blender.stackexchange.com/questions/44787/python-code-for-edge-data-bevel-and-crease-weight/187853#187853
    class creaseAndBevelPG(PropertyGroup):
        ## Update functions
        def update_bevelWeight( self, context ):
            ''' Update function for bevelWeight property '''
    
            o  = bpy.context.object
            d  = o.data
            bm = bmesh.from_edit_mesh( d )
    
            #bevelWeightLayer = bm.edges.layers.bevel_weight['BevelWeight']
            bevelWeightLayer = bm.edges.layers.bevel_weight.verify()
    
            if self.whoToInfluence == 'Selected Elements':
                selectedVerts = [ v for v in bm.edges if v.select ]
                for v in selectedVerts: v[ bevelWeightLayer ] = self.bevelWeight
            else:
                for v in bm.edges: v[ bevelWeightLayer ] = self.bevelWeight
    
            bmesh.update_edit_mesh( d )
    
        def update_edgeCrease( self, context ):
            ''' Update function for edgeCrease property '''
    
            o  = bpy.context.object
            d  = o.data
            bm = bmesh.from_edit_mesh( d )
    
            #creaseLayer = bm.edges.layers.crease['SubSurfCrease']
            creaseLayer = bm.edges.layers.crease.verify()
    
            if self.whoToInfluence == 'Selected Elements':
                selectedEdges = [ e for e in bm.edges if e.select ]
                for e in selectedEdges: e[ creaseLayer ] = self.edgeCrease
            else:
                for e in bm.edges: e[ creaseLayer ] = self.edgeCrease
    
            bmesh.update_edit_mesh( d )
            
    #        selectedEdges = [ e for e in bm.edges if e.select ]
    #        for e in selectedEdges: print(e[ creaseLayer ])
    
        ## Properties
        items = [
            ('All', 'All', ''),
            ('Selected Elements', 'Selected Elements', '')
        ]
    
        whoToInfluence = bpy.props.EnumProperty( # Material distribution method
            description = "Influence all / selection",
            name        = "whoToInfluence",
            items       = items,
            default     = 'Selected Elements'
        )
    
        bevelWeight = bpy.props.FloatProperty(
            description = "Bevel Weight",
            name        = "Set bevel Weight",
            min         = 0.0,
            max         = 1.0,
            step        = 0.01,
            default     = 0,
            update      = update_bevelWeight
        )
    
        edgeCrease = bpy.props.FloatProperty(
            description = "Edge Crease",
            name        = "Set edge Crease",
            min         = 0.0,
            max         = 1.0,
            step        = 0.01,
            default     = 0,
            update      = update_edgeCrease
        )    
    
    def get_crease_selected():
        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh( d )
    
        creaseLayer = bm.edges.layers.crease.verify()
    
        selectedEdges = ""
        for e in bm.edges:
            if e.select:
                selectedEdges += (str(round(e[ creaseLayer ], 2)) + ' ')
        return selectedEdges
    
    def get_bevelWeight_selected():
        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh( d )
    
        bevelWeightLayer = bm.edges.layers.bevel_weight.verify()
    
        selectedVerts = ""
        for v in bm.edges:
            if v.select:
                selectedVerts += (str(round(v[ bevelWeightLayer ], 2)) + ' ')
        return selectedVerts
    ###########################################################################################
    ###################################### UI #################################################
    ###########################################################################################
    
    class creaseAndBevelPanel(Panel):
        bl_label = "Crease and Bevel"
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'UI'
        bl_category = 'CaB'
    
    #    @classmethod
    #    def poll( self, context ):
    #        ''' Only show panel if there is an active mesh object '''
    #        return context.object and context.object.type == 'MESH'
     
        def draw( self, context ):
                layout = self.layout
                props  = context.scene.creaseAndBevelPG # Create reference to property group
                            
                #box = layout.box()                    # Draw a box
                #col = box.column( align = True )      # Create a column
    #            col = layout.column( align = True )
    #            if (context.active_object is not None) and (context.active_object.mode == 'OBJECT'):
    #                col.enabled = False
    #            col.operator("select.edge_crease_value", icon = 'SORTBYEXT')
    #            col.prop( props, "whoToInfluence"  )  # Add properites to panel            
                col = layout.column( align = True )
                if context.active_object.mode == 'EDIT':
                    #col.label(text='Weight value: ' + "%.2f"% round(get_bevelWeight_selected(), 2))
                    col.label(text='Weight value: ' + get_bevelWeight_selected())
                    col.label(text='Crese value: ' + get_crease_selected())
                if (context.active_object is not None) and (context.active_object.mode == 'OBJECT'):
                    col.enabled = False
                col.prop( props, "bevelWeight"     )
                col.prop( props, "edgeCrease"      )      
                                  
    ###########################################################################################
    ##################################### Register ############################################
    ########################################################################################### 
     
    classes = (
                creaseAndBevelPG,
                creaseAndBevelPanel,
    )
    
    def register():
        from bpy.utils import register_class
        for cls in classes:
            register_class(cls)
        
        bpy.types.Scene.creaseAndBevelPG = bpy.props.PointerProperty( type = creaseAndBevelPG )
        
    def unregister():
        from bpy.utils import unregister_class
        for cls in reversed(classes):
            unregister_class(cls)
            
    if __name__ == "__main__":
        register()
$\endgroup$
5
  • $\begingroup$ Hello, is there a chance to update the addon including "Vertex Crease"? Thanks. $\endgroup$
    – Zyzio
    Jan 24, 2023 at 8:29
  • $\begingroup$ Hello. This all exist in N panel "Item" tab (no need addon). This code was only needed for own usage and study code. $\endgroup$
    – APEC
    Jan 24, 2023 at 9:08
  • $\begingroup$ I understand, and in that case do you know how to change the position of the "Vertex Crease" slider to a more accessible one for the user? I am using the "Pie menu editor" addon, but I am not able to move the "Vertex Crease" slider to my custom panel. $\endgroup$
    – Zyzio
    Jan 24, 2023 at 9:25
  • 1
    $\begingroup$ Ok, I will look the code to update addon $\endgroup$
    – APEC
    Jan 24, 2023 at 11:29
  • 1
    $\begingroup$ Added updated variant with vertex crease $\endgroup$
    – APEC
    Jan 24, 2023 at 13:07
1
$\begingroup$

Updated version with Vertex Crease.

With diff options for Vertex and Edge selection mode,

for other mode or if no elements is selected it shows a warning message.

enter image description here

import bpy, bmesh
from bpy.types import PropertyGroup, Panel, Operator, Menu

# Add-on info
bl_info = {
    "name": "Crease and Bevel",
    "author": "TLousky, APEC, deadpin",
    "version": (0, 0, 2),
    "blender": (3, 4, 0),
    "location": "View3D > N-panel > CaB",
    "description": "UI panel for Mesh vertex / edge crease and bevel weight properties", 
    "doc_url": "",
    "tracker_url": "",      
    "category": "3D View"
}

###########################################################################################
################################### Functions #############################################
###########################################################################################

#https://blender.stackexchange.com/questions/44787/python-code-for-edge-data-bevel-and-crease-weight/187853#187853
class CAB_PG_Prop(PropertyGroup):
    ## Update functions
    def update_edge_bevelWeight(self, context):
        ''' Update function for bevelWeight property '''

        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh(d)

        bevelWeightLayer = bm.edges.layers.bevel_weight.verify()

        if self.whoToInfluence == 'Selected Elements':
            selectedEdges = [e for e in bm.edges if e.select]
            for e in selectedEdges: e[bevelWeightLayer] = self.edge_bevelWeight
        else:
            for e in bm.edges: e[bevelWeightLayer] = self.edge_bevelWeight

        bmesh.update_edit_mesh(d)

    def update_vert_bevelWeight(self, context):
        ''' Update function for bevelWeight property '''

        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh(d)

        bevelWeightLayer = bm.verts.layers.bevel_weight.verify()

        if self.whoToInfluence == 'Selected Elements':
            selectedVerts = [v for v in bm.verts if v.select]
            for v in selectedVerts: v[bevelWeightLayer] = self.vert_bevelWeight
        else:
            for v in bm.edges: v[bevelWeightLayer] = self.vert_bevelWeight

        bmesh.update_edit_mesh(d)

    def update_edge_Crease(self, context):
        ''' Update function for edgeCrease property '''

        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh(d)

        creaseLayer = bm.edges.layers.crease.verify()

        if self.whoToInfluence == 'Selected Elements':
            selectedEdges = [e for e in bm.edges if e.select]
            for e in selectedEdges: e[creaseLayer] = self.edge_Crease
        else:
            for e in bm.edges: e[creaseLayer] = self.edge_Crease

        bmesh.update_edit_mesh(d)

    def update_vert_Crease(self, context):
        ''' Update function for edgeCrease property '''

        o  = bpy.context.object
        d  = o.data
        bm = bmesh.from_edit_mesh(d)

        creaseLayer = bm.verts.layers.crease.verify()

        if self.whoToInfluence == 'Selected Elements':
            selectedVerts = [v for v in bm.verts if v.select]
            for v in selectedVerts: v[creaseLayer] = self.vert_Crease
        else:
            for v in bm.verts: v[creaseLayer] = self.vert_Crease

        bmesh.update_edit_mesh(d)

    ## Properties
    items = [
        ('All', 'All', ''),
        ('Selected Elements', 'Selected Elements', '')
    ]

    whoToInfluence: bpy.props.EnumProperty(
        description = "Influence all / selection",
        name        = "whoToInfluence",
        items       = items,
        default     = 'Selected Elements'
    )

    edge_bevelWeight: bpy.props.FloatProperty(
        description = "Edge Bevel Weight",
        name        = "Set bevel Weight",
        min         = 0.0,
        max         = 1.0,
        step        = 0.1,
        default     = 0,
        update      = update_edge_bevelWeight
    )

    vert_bevelWeight: bpy.props.FloatProperty(
        description = "Vertex Bevel Weight",
        name        = "Set bevel Weight",
        min         = 0.0,
        max         = 1.0,
        step        = 0.1,
        default     = 0,
        update      = update_vert_bevelWeight
    )

    edge_Crease: bpy.props.FloatProperty(
        description = "Edge Crease",
        name        = "Set edge Crease",
        min         = 0.0,
        max         = 1.0,
        step        = 0.1,
        default     = 0,
        update      = update_edge_Crease
    )    

    vert_Crease: bpy.props.FloatProperty(
        description = "Vertex Crease",
        name        = "Set vertex Crease",
        min         = 0.0,
        max         = 1.0,
        step        = 0.1,
        default     = 0,
        update      = update_vert_Crease
    )


def get_edge_crease_selected():
    o  = bpy.context.object
    d  = o.data
    bm = bmesh.from_edit_mesh(d)

    creaseLayer = bm.edges.layers.crease.verify()

    selectedEdges = ""
    for e in bm.edges:
        if e.select:
            selectedEdges += (str(round(e[creaseLayer], 2)) + ' ')
    return selectedEdges

def get_edge_bevelWeight_selected():
    o  = bpy.context.object
    d  = o.data
    bm = bmesh.from_edit_mesh(d)

    bevelWeightLayer = bm.edges.layers.bevel_weight.verify()

    selectedEdges = ""
    for e in bm.edges:
        if e.select:
            selectedEdges += (str(round(e[bevelWeightLayer], 2)) + ' ')
    return selectedEdges

def get_vert_crease_selected():
    o  = bpy.context.object
    d  = o.data
    bm = bmesh.from_edit_mesh(d)

    creaseLayer = bm.verts.layers.crease.verify()

    selectedVerts = ""
    for v in bm.verts:
        if v.select:
            selectedVerts += (str(round(v[creaseLayer], 2)) + ' ')
    return selectedVerts

def get_vert_bevelWeight_selected():
    o  = bpy.context.object
    d  = o.data
    bm = bmesh.from_edit_mesh(d)

    bevelWeightLayer = bm.verts.layers.bevel_weight.verify()

    selectedVerts = ""
    for v in bm.verts:
        if v.select:
            selectedVerts += (str(round(v[bevelWeightLayer], 2)) + ' ')
    return selectedVerts

###########################################################################################
###################################### UI #################################################
###########################################################################################

class CAB_PT_Panel(Panel):
    bl_label = "Crease and Bevel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'CaB'
 
    def draw(self, context):
        layout = self.layout
        props  = context.scene.CAB_PG_Prop # Create reference to property group
        
        # sel_mode[0] or sel_mode[1] or sel_mode[2]
        sel_mode = context.tool_settings.mesh_select_mode[:]
        
        col = layout.column(align = True)

        # if Vertex edit mode
        if context.active_object.mode == 'EDIT' and sel_mode[0] and len(get_vert_crease_selected()) != 0:
            col.label(text='Vertex edit mode:', icon='VERTEXSEL')
            bcol = col.box()
            bcol.label(text='Weight value: ' + get_vert_bevelWeight_selected())
            bcol.label(text='Crese value: ' + get_vert_crease_selected()) 
                       
            bcol.prop(props, "vert_bevelWeight")          
            bcol.prop(props, "vert_Crease")
        
        # if Edge edit mode
        elif context.active_object.mode == 'EDIT' and sel_mode[1] and len(get_edge_crease_selected()) != 0:
            col.label(text='Edge edit mode:', icon='EDGESEL')
            bcol = col.box()
            bcol.label(text='Weight value: ' + get_edge_bevelWeight_selected())
            bcol.label(text='Crese value: ' + get_edge_crease_selected())  
                      
            bcol.prop(props, "edge_bevelWeight")
            bcol.prop(props, "edge_Crease")
        
        # if other mode
        else:
            col.label(text='Need to select elements')
            col.label(text='in Vertex or Edge mode')
                              
###########################################################################################
##################################### Register ############################################
########################################################################################### 
 
classes = (
            CAB_PG_Prop,
            CAB_PT_Panel,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    
    bpy.types.Scene.CAB_PG_Prop = bpy.props.PointerProperty(type = CAB_PG_Prop)
    
def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
        
if __name__ == "__main__":
    register()

$\endgroup$
1
  • $\begingroup$ Works great, thank you :) $\endgroup$
    – Zyzio
    Jan 25, 2023 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .