How to create a custom object list in a panel

Hi everyone,

I’m looking for an example of a script showing how to create an object list… But of a specific kind. There are two kinds of lists in blender :
Let’s look at the properties panel (n key).

  • in the transform block there is a list for rotation mode (XYZ Euler, etc…). I know how to do with that.
  • in the view block, there is a list under “Lock to Object”. Maybe “list” is not the right word, but this is what I’m looking for.

Does anybody know how to do this ?
Thanks a lot.

Thats a prop_search layout

http://www.blender.org/documentation/blender_python_api_2_69_7/bpy.types.UILayout.html?highlight=uilayout#bpy.types.UILayout.prop_search


import bpy




class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"


    def draw(self, context):
        scene = context.scene
        layout = self.layout


        obj = context.object


        row = layout.row()
        row.prop_search(scene, "my_search", scene, "objects")


        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")


        row = layout.row()
        row.operator("mesh.primitive_cube_add")




def register():
    bpy.types.Scene.my_search = bpy.props.StringProperty()
    bpy.utils.register_class(HelloWorldPanel)




def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)




if __name__ == "__main__":
    register()

Hi and thank you batFINGER.

I still have 2 problems.

  1. In my “list” I want the cameras of the scene.
    prop_search(scene, “my_search”, scene, “cameras”) just fails, and
    prop_search(scene, “my_search”, bpy.data, “cameras”) returns all the cameras of all scenes.
    do you know how to do ?

  2. I realized that in the camera list (returned by bpy.data, “cameras”), it’s the camera names, not the object names. And of course, I want the object name.
    Do you know if instead of “cameras”, “objects”, “armatures” or whatever, it would be possible to put the result of such a “filter” ?
    cameras=[]
    for obj in scene.objects
    if obj.type==‘CAMERA’:
    cameras.extend([obj])

Thank you again

Yeah like layout.prop(scene, “camera”) in the scene panel does.

You might have to set up a collection property, or sometimes a menu is simpler.


cameras = [c for c in scene.objects if c.type == 'CAMERA']

oh sorry, I tried very hard, I promise, but here, it’s too difficult for me. I never used the collection property, and despite searching the internet, I don’t see…

in the register section :
bpy.types.Scene.mycameras = bpy.props.CollectionProperty(???)
in the draw section :
prop_search(scene, “my_search”, bpy.data, “mycameras”)
in a particular section ? :

mycameras = [c for c in scene.objects if c.type == ‘CAMERA’]
I tried different things but I won’t succeed by chance.
Would you give me a little bit more details ? Thanks

Menu layout


import bpy


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    cam = bpy.props.StringProperty(default="", options={'SKIP_SAVE'})


    @classmethod
    def poll(cls, context):
        return context.active_object is not None


    def execute(self, context):
        print("CAMERA", self.cam)
        cam = context.scene.objects.get(self.cam)
        context.scene.camera = cam
        return {'FINISHED'}
    
    
class SimpleCustomMenu(bpy.types.Menu):
    bl_label = "Simple Custom Menu"
    bl_idname = "OBJECT_MT_simple_custom_menu"


    def draw(self, context):
        layout = self.layout
        cams = [c for c in context.scene.objects if c.type == 'CAMERA']
        print(cams)
        for c in cams:
            op = layout.operator("object.simple_operator",text=c.name)
            op.cam = c.name
            
        




def register():
    bpy.utils.register_class(SimpleCustomMenu)
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleCustomMenu)
    bpy.utils.register_class(SimpleOperator)
if __name__ == "__main__":
    register()


    # The menu can also be called from scripts
    bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname)

Thank you.

I know the menu layout, but it’s not my need. I want to choose a camera, not to make it active. My script calculates vertices that are in the field of the camera. I can choose “Active camera” with a check box or another camera in a list if the box is unchecked. Until now, I used a operator_menu_enum that worked fine. But for interface consistency I would prefer a prop_search as you suggested. You spoke about CollectionProperty to customize the “cameras” list. I just wanted to know how you do that. I don’t understand collectionproperty…