9

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.

BlenderBro
  • 1,201
  • 1
  • 10
  • 28

2 Answers2

15

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.
brockmann
  • 12,613
  • 4
  • 50
  • 93
J. Bakker
  • 3,051
  • 12
  • 30
4

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?

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 3
    Sorry if I wasn't clear enough, I want the list to only display objects that have curve data, not the curve data itself. – BlenderBro Mar 08 '17 at 16:46