4
$\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$
4
$\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_module(__name__)
    bpy.types.Scene.mychosenObject = bpy.props.PointerProperty(
        type=bpy.types.Object,
        poll=scene_mychosenobject_poll
    )


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.mychosenObject


if __name__ == "__main__":
    register()

See the documentation 1

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$
1
$\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_module(__name__)
    bpy.types.Scene.mychosenObject = bpy.props.StringProperty()


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.mychosenObject


if __name__ == "__main__":
    register()

Explanation: https://blender.stackexchange.com/a/43982/31447

$\endgroup$
  • 2
    $\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 Mar 8 '17 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.