Where to prop_search for vertex groups?

This doesn't work:

row.prop_search(context.scene, "vertex_group_density", ob, "vertex_groups", text="Density")

context.scene seems to be the wrong place, but which is the right one? In e.g. the particle system dialog source, there's just a "psys" instead of context.scene, but that doesn't give me a further lead.

Man-pages say "Data from which to take property", I'm not exactly sure what is meant by that:

 prop_search(data, property, search_data, search_property, text="", icon='NONE')


    data (AnyType, (never None)) – Data from which to take property.
    property (string) – Identifier of property in data.
    search_data (AnyType, (never None)) – Data from which to take collection to search in.
    search_property (string) – Identifier of search collection property.
    text (string, (optional)) – Override automatic text of the item.

here a simple answer using one of the TextEditor -> Python templates:

import bpy

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo2"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

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

        scn = context.scene
        row = layout.row()
        row.prop_search(scn, "vgr", context.active_object, "vertex_groups", text="Density")

def register():
    bpy.types.Scene.vgr = bpy.props.StringProperty(name="vertex_group_density")

def unregister():
    del bpy.types.Scene.vgr

if __name__ == "__main__":

enter image description here

Here we use

  • bpy.types.Scene.vgr to register a StringProperty to keep the string name of the prop_search choice.
  • Accessing the 'vertex_group' happens by passing an object reference, this can be done by using context.active_object or ob if you alias it earlier in the code.. you could have something like:

    ob = bpy.data.objects[some_object_name]
    row.prop_search(scn, "vgr", ob, "vertex_groups", text="Density")
  • $\begingroup$ I think it might help if you could add some explanation as to where they were going wrong in their attempt. $\endgroup$ – Ray Mairlot Jun 24 '15 at 10:06

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.