9
$\begingroup$

Currently working on a piece of custom UI that contains an object selection box.

layout.prop_search(scene, "myselection", scene, "objects")

I'm wondering how I can make it so it only lists objects that contain a certain data type such as curves in the same way that the Follow Path constraint does.

Follow path constraint prop_search

Thanks for your assistance.

$\endgroup$

2 Answers 2

15
$\begingroup$

Since blender 2.79 this is possible using the poll parameter of the bpy.props.PointerProperty.

import bpy    

class OBJECT_PT_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):
        layout = self.layout
        scene = context.scene
        layout.prop(scene, "mychosenObject")

def scene_mychosenobject_poll(self, object):
    return object.type == 'CURVE'

def register():
    bpy.utils.register_class(OBJECT_PT_HelloWorldPanel)
    bpy.types.Scene.mychosenObject = bpy.props.PointerProperty(
        type=bpy.types.Object,
        poll=scene_mychosenobject_poll
    )

def unregister():
    bpy.utils.unregister_class(OBJECT_PT_HelloWorldPanel)
    del bpy.types.Scene.mychosenObject


if __name__ == "__main__":
    register()

See the documentation

bpy.props.PointerProperty(type=None, name="", description="", options={'ANIMATABLE'}, poll=None, update=None)

Returns a new pointer property definition. Parameters:

  • type (class) – A subclass of bpy.types.PropertyGroup or bpy.types.ID.
  • name (string) – Name used in the user interface.
  • description (string) – Text used for the tooltip and api documentation.
  • options (set) – Enumerator in [‘HIDDEN’, ‘SKIP_SAVE’, ‘ANIMATABLE’, ‘LIBRARY_EDITABLE’, ‘PROPORTIONAL’,’TEXTEDIT_UPDATE’].
  • poll (function) – function to be called to determine whether an item is valid for this property. The function must take 2 values (self,object) and return Bool.
  • update (function) – Function to be called when this value is modified, This function must take 2 values (self, context) and return None. Warning there are no safety checks to avoid infinite recursion.
$\endgroup$
0
4
$\begingroup$

Pass the right collection, in this case bpy.data.curves:

import bpy    

class OBJECT_PT_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):
        layout = self.layout
        scene = context.scene
        layout.prop_search(scene, "mychosenObject", bpy.data, "curves")

def register():
    bpy.utils.register_class(OBJECT_PT_HelloWorldPanel)
    bpy.types.Scene.mychosenObject = bpy.props.StringProperty()


def unregister():
    bpy.utils.unregister_class(OBJECT_PT_HelloWorldPanel)
    del bpy.types.Scene.mychosenObject


if __name__ == "__main__":
    register()

Explanation: How to make my prop search show the objects in my scene?

$\endgroup$
1
  • 3
    $\begingroup$ Sorry if I wasn't clear enough, I want the list to only display objects that have curve data, not the curve data itself. $\endgroup$
    – BlenderBro
    Commented Mar 8, 2017 at 16:46

You must log in to answer this question.

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