10

How to add an eyedropper functionality to prop_search in my addon?

col.prop_search(scene, "string_property", context.scene, "objects")

This code will create prop_search just like on the animation below, but without an eyedropper.

eyedropper

David
  • 49,291
  • 38
  • 159
  • 317
Mikhail Rachinskiy
  • 1,069
  • 8
  • 22

2 Answers2

10

I spoke to Campbell Barton (the developer who got eyedropper functionality implemented) about this, he said that this kind of functionality is not available in Python API at the moment.

UPDATE:

Blender 2.79 now allows to create add-on properties which reference datablocks.

custom_property: PointerProperty(type=bpy.types.Object)

Using this property with a prop_search() we'll get search box with an eyedropper.

UPDATE 2:

Usage of prop_search() is not necessary, in this case prop() will do just fine.
Noted by J. Bakker.

Gorgious
  • 30,723
  • 2
  • 44
  • 101
Mikhail Rachinskiy
  • 1,069
  • 8
  • 22
4

A working example based on answers given.

enter image description here

Given the struggles of How to draw Object selection with eyedropper layout using Python script here is an edit to the script from that question

Puts a panel in the "scene" tab of the properties area.

import bpy
from bpy.props import PointerProperty

class TEST_PT_layout_panel(bpy.types.Panel): bl_label = "Prop Panels" bl_category = "Test Panel" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "scene"

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

    col = layout.column()
    col.prop_search(scene, "prop", context.scene, "objects")
    #or 
    col.prop(scene, "prop")


def register(): bpy.types.Scene.prop = PointerProperty(type=bpy.types.Object) bpy.utils.register_class(TEST_PT_layout_panel)

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

if name == "main": register()

See https://blender.stackexchange.com/a/101303/15543 re creating a poll method for the pointer to filter, for example, for objects of type armature in the scene.

batFINGER
  • 84,216
  • 10
  • 108
  • 233