Adding a checkbox to a ui panel

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

qutorial
Posts: 1
Joined: Mon Aug 20, 2012 2:58 am

Adding a checkbox to a ui panel

Postby qutorial » Mon Aug 20, 2012 3:01 am

Can someone explain why my booleans aren't showing up as checkboxes in my display panel (the panel shows up in the 3d view tools panel)?

Code: Select all

import bpy, bmesh, mathutils

# class VEF_tools:
    # # Temporary var storage for alpha vector slide function, use operator props later
    # p1 = mathutils.Vector( (0.0, 0.0, 0.0) )
    # p2 = mathutils.Vector( (0.0, 0.0, 0.0) )
    # vec = mathutils.Vector( (0.0, 0.0, 0.0) )
    # bpy.context.scene["p1"] = p1
    # bpy.context.scene["p2"] = p2
    # bpy.context.scene["vec"] = vec
   
    # def constructTransfVec():
        # vec = mathutils.Vector( (p2.x - p1.x, p2.y - p1.y, p2.z - p1.z ) )




# Initalize ID props needed for storage if they don't already exist
try:
    bpy.context.scene['e1v1']
except KeyError:
    bpy.context.scene['e1v1'] = [0.0, 0.0, 0.0]

try:
    bpy.context.scene['e1v2']
except KeyError:
    bpy.context.scene['e1v2'] = [0.0, 0.0, 0.0]

try:
    bpy.context.scene['flipDirection']
except KeyError:
    bpy.context.scene['flipDirection'] = False

try:
    bpy.context.scene['makeUnitVector']
except KeyError:
    bpy.context.scene['makeUnitVector'] = False


#############################################
# SECTION: UI ELEMENTS (Panels, buttons etc.)
#############################################

# Main panel containing VEF Tools' UI
class VEF_tools_panel(bpy.types.Panel):
    bl_label = "VEF Tools Alpha, Vector Slide"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
 
    def draw(self, context):
        self.layout.operator("vef.e1v1", text = 'e1v1')
        self.layout.operator("vef.e1v2", text = 'e1v2')
        self.layout.operator("vef.vec", text = 'Vector Slide')
        self.layout.prop(context.scene, 'flipDirection', text = "Flip Direction", Toggle = True)
        self.layout.prop(context.scene, 'makeUnitVector', text = "Make Unit Vector", Toggle = True)


# Consider rewriting to reduce code duplication...not sure if execute can take extra custom params
class StoreEdge1Vert1Button(bpy.types.Operator):
    bl_idname = "vef.e1v1"
    bl_label = "Store as e1v1"

    def execute(self, context):
        tempMesh = bmesh.new()
        # Don't do anything if not in edit mode with one vert selected
        if (bpy.context.active_object.type == 'MESH' and bpy.context.active_object.mode == 'EDIT'):

            # Init mesh data. Switch to object then back to edit mode first
            # to make sure vert selection data is updated/not stale.
            bpy.ops.object.mode_set(mode='OBJECT')
            bpy.ops.object.mode_set(mode='EDIT')
            tempMesh.from_mesh( bpy.context.active_object.data )
           
            # Init point var for the point we're going to grab
            p1 = mathutils.Vector( (0.0, 0.0, 0.0) )
           
            # Make sure only one vert is selected, iterate through the
            # mesh verts looking for the selected vert, and store
            # +++++
            # Note: Check again if select_history will work reliably here later
            if (bpy.context.active_object.data.total_vert_sel == 1):

                for v in tempMesh.verts:
               
                    if (v.select):
                        p1 = v.co
                        bpy.context.scene['e1v1'] = p1
           
            else:
           
                print("Please select one vert only.")
               
        else:
            print("Select a mesh object and enter edit mode to grab vert data.")

        # Free the mesh, then exit
        tempMesh.free()
        return{'FINISHED'}


# Consider rewriting to reduce code duplication...not sure if execute can take extra custom params
class StoreEdge1Vert2Button(bpy.types.Operator):
    bl_idname = "vef.e1v2"
    bl_label = "Store as e1v2"

    def execute(self, context):
        tempMesh = bmesh.new()
        # Don't do anything if not in edit mode with one vert selected
        if (bpy.context.active_object.type == 'MESH' and bpy.context.active_object.mode == 'EDIT'):

            # Init mesh data. Switch to object then back to edit mode first
            # to make sure vert selection data is updated/not stale.
            bpy.ops.object.mode_set(mode='OBJECT')
            bpy.ops.object.mode_set(mode='EDIT')
            tempMesh.from_mesh( bpy.context.active_object.data )
           
            # Init point var for the point we're going to grab
            p2 = mathutils.Vector( (0.0, 0.0, 0.0) )
           
            # Make sure only one vert is selected, iterate through the
            # mesh verts looking for the selected vert, and store
            # +++++
            # Note: Check again if select_history will work reliably here later
            if (bpy.context.active_object.data.total_vert_sel == 1):

                for v in tempMesh.verts:
               
                    if (v.select):
                        p2 = v.co
                        bpy.context.scene['e1v2'] = p2
           
            else:
           
                print("Please select one vert only.")
               
        else:
            print("Select a mesh object and enter edit mode to grab vert data.")
       
        # Free the mesh, then exit
        tempMesh.free()
        return{'FINISHED'}


# Performs the transformation, vector slide
class VectorSlideButton(bpy.types.Operator):
    bl_idname = "vef.vec"
    bl_label = "Vector Slide"
 
    def execute(self, context):

        # Init mesh to active obj
        tempMesh = bmesh.new()
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.mode_set(mode='EDIT')
        tempMesh.from_mesh( bpy.context.active_object.data )

        # Set up the transformation
        transf = mathutils.Matrix()
        vec = mathutils.Vector( (bpy.context.scene['e1v2'][0] - bpy.context.scene['e1v1'][0], bpy.context.scene['e1v2'][1] - bpy.context.scene['e1v1'][1], bpy.context.scene['e1v2'][2] - bpy.context.scene['e1v1'][2] ) )
        print(vec)
        transf = transf.Translation(vec)
        print(transf)
       
        # Perform the trans
        tempMesh.transform(transf)

        # Write the transf to the mesh datablock and release the tempMesh
        bpy.ops.object.mode_set(mode='OBJECT')
        tempMesh.to_mesh(bpy.context.active_object.data)
        tempMesh.free()
        return{'FINISHED'}


bpy.utils.register_module(__name__)
[/code]

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue Aug 21, 2012 1:05 pm

what you do is checking for a certain scene:
bpy.context.scene['flipDirection']

and if it doesn't exist, you set this key to False: bpy.context.scene['flipDirection'] = False

but this won't create a BoolProperty type, but an int type.

If you wanna store a BoolProperty in a scene, you gotta add this prop to all scenes, like:
bpy.types.Scene.flipDirection = BoolProperty(...)

or if you need it temporarily only, create it inside of the panel class:

Code: Select all

flipDirection = bpy.props.BoolProperty(...)

def invoke(self, context, event):
    try:
        context.scene['flipDirection']
        self.flipDirection = True
    except KeyError:
        self.flipDirection = False
    # Might have to call parent's invoke here...
    #return super().invoke(context, event)

def draw(self, context):
    layout = self.layout
    layout.prop(self, "flipDirection", text="Flip Direction")


but i wonder what this is good for... What are you actually after?
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Coding Blender”

Who is online

Users browsing this forum: No registered users and 2 guests