Sunday, July 11, 2010

Dropdown menu for selecting an object.

The script I'm going to write about is a script I made to test out dropdown boxes and the selection of objects from a script.

You can find the complete script here. I based my script on a script found in this forum post by Crouch. Be sure to download the newest Blender build from GraphicAll, because I spend much time trying to run Crouch's script, only to come to the conclusion that my Blender version was a few days to old.

I will try to explain my script below:


class OBJECT_PT_SelectObjects(bpy.types.Panel):
    '''
    Class to represent a panel that allows you to browse and select objects.
    '''
    bl_space_type = "PROPERTIES" #window type where the panel will be displayed
    bl_region_type = "WINDOW"
    bl_context = "object" #where to show panel in space_type
    bl_label = "Select Object" #panel name displayed in header

    def draw(self, context):
        '''
        Function used by blender to draw the panel.
        '''
        update() #update obj_list
        scene = context.scene
        layout = self.layout
        layout.prop(scene, "obj_list", text="Objects") #draw dropdown box on panel
        layout.separator()
        row = layout.row()
        row.operator("selectObject_button") #draw select button on panel
        row = layout.row()
        row.label(text="Active object is: " + bpy.context.active_object.name) #display the name of the active object

This piece of code defines a class for our panel that allows us to select different objects.

  • The 10th line defines that function that is responsible for drawing our buttons and text on the panel.
  • The 14th line calls the update function, described below, that updates the list with all the objects in the scene.
  • Line 17 draws the dropdown box on the panel. layout.prop() displays a property. scene is the type that has the property, and "obj_list" is the name of the property that we want to display. This property will be displayed as a dropdown box since this property is an enumeration.
  • Line 20 draws the select object button on the panel.
  • Line 22 draws the name of the current active object on the panel.


def update():
    '''
    Function used to update the objects list (obj_list) used by the dropdown box.
    '''
    objects = [] #list containing tuples of each object
    for index, object in enumerate(bpy.context.scene.objects): #iterate over all objects
        objects.append((str(index), object.name, str(index))) #put each object in a tuple and add this to the objects list

    #create an EnumProperty wich can be used by the dropdown box to display the differnt objects
    bpy.types.Scene.EnumProperty( attr="obj_list", name="Objects", description="Choose an object", items=objects, default='0')

This function creates or updates our obj_list enumeration property when it is called.

  • Line 5 contains the list of the objects, this list will be appended in the next lines, and then converted into the EnumProperty.
  • Lines 6 and 7 append the object list with tuples that contain the index and name of the different objects in the current scene.
  • Line 10 adds a EnumProperty to the Scene type. This custom property is referenced by "obj_list", and is made from the list of tuples of objects. Check this link if you want to read more about properties.


class CUSTOM_OT_SelectObjectButton(bpy.types.Operator):
    '''
    Class to represent a button that can be used to select and make active the object selected by the dropdown box.
    '''
    bl_idname = "selectObject_button" #name used to refer to this operator
    bl_label = "select" #button label
    bl_description = "Select the chosen object" #tooltip
    
    def invoke(self, context, event):
        '''
        Function used to process a click on the deselect button.
        '''
        obj = bpy.context.scene.objects[int(bpy.context.scene.obj_list)] #get the object selected by the dropdown box
        for object in bpy.data.objects: # deselect all objects
            object.selected = False
        obj.selected = True #select the object selected by the dropdown box
        bpy.context.scene.objects.active = obj #make the selected object the active object
        return{'FINISHED'}

This code defines the class for the select button. The object selected in the dropdown box will be selected and made active in blender after pressing this button.

  • Line 9 is the start of the function that will be invoke by Blender when the user clicks on the button.
  • Line 13 gets the index of the object selected by the dropdown box and uses this index to get a reference of the selected object, this reference is hold in obj.
  • Lines 14 and 15 make sure all the objects are deselected.
  • Line 16 selects our selected object in Blender.
  • Line 17 makes our selected object the active object.

3 comments:

  1. I finally found out how to do it the proper way:
    prop = box.prop_search(data = context.scene,
    property = 'chosenObject',
    search_data = bpy.context.scene,
    search_property = 'objects',
    text = 'Select Object')

    ReplyDelete
  2. Grappig, ik was dropdown boxes aan het googlen en ik kwam hier uit. Kleine wereld :p

    ReplyDelete
  3. thanks, it helped.
    can you please write a post about how to do list , which had few options (not dropdown) directly list of items in panel. and querying which one item is selected...

    ReplyDelete