Blender Panel Adding Prop_search Dynamically

Hi,

Unable to create new prop_search item in the panel when button is clicked.


import bpy


   
# And now we can use this list everywhere in Blender. Here is a small example panel.
class MyPanel(bpy.types.Panel):
    bl_label = "My Panel"
    bl_idname = "my_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Library Texture List"
    bl_context = "objectmode"
    
    t_slotList = 1
    t_slot = bpy.props.StringProperty()
    
    
    def draw(self, context):
        layout = self.layout
        
        layout.operator("obs.to_my_list", icon='ZOOMIN', text="")
        
        #layout.prop_search(self, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')
        for i in range(0,self.t_slotList):
            layout.prop_search(self, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')


class dummyOperator(bpy.types.Operator):
    bl_label = "Operator for appending"
    bl_idname = "obs.to_my_list"
    
    def execute(self, context):
        MyPanel.t_slotList += 1;
        return {"FINISHED"}

def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)



if __name__ == "__main__":
    register()

Your going to need to pull the t_slot out of the panel class and put it in a property group.

import bpy


class MyPanel(bpy.types.Panel):
  bl_label = "My Panel"
  bl_idname = "my_panel"
  bl_space_type = 'VIEW_3D'
  bl_region_type = 'TOOLS'
  bl_category = "Library Texture List"
  bl_context = "objectmode"
  
  t_slotList = 1
  
  def draw(self, context):
    layout = self.layout
    
    layout.operator("obs.to_my_list", icon='ZOOMIN', text="")
    
    #layout.prop_search(self, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')
    for i in range(0,self.t_slotList):
      layout.prop_search(context.scene.MyPanel, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')


class props(bpy.types.PropertyGroup):
  t_slot = bpy.props.StringProperty()


class dummyOperator(bpy.types.Operator):
  bl_label = "Operator for appending"
  bl_idname = "obs.to_my_list"
  
  def execute(self, context):
    MyPanel.t_slotList += 1;
    return {"FINISHED"}


def register():
  bpy.utils.register_module(__name__)
  bpy.types.Scene.MyPanel = bpy.props.PointerProperty(type=props)


def unregister():
  bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

Thanks, but the additional ‘t_slot’ are all instances of the first one. How to make them unique?

Thanks did a little modification and it’s working now :slight_smile:
Your input was very helpful


import bpy


class MyPanel(bpy.types.Panel):
  bl_label = "My Panel"
  bl_idname = "my_panel"
  bl_space_type = 'VIEW_3D'
  bl_region_type = 'TOOLS'
  bl_category = "Library Texture List"
  bl_context = "objectmode"
  
  t_slotList = 1
  
  def draw(self, context):
    layout = self.layout
    col = layout.column(align=True)
    col.operator("obs.to_my_list", icon='ZOOMIN', text="")
    col.operator("remove.from_list", icon='ZOOMOUT', text="")
    
    #layout.prop_search(self, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')
    for each in bpy.context.scene.MyPanel:
      row = col.row(align=True)
      row.prop_search(each, "t_slot", bpy.data, "textures", icon='TEXTURE_DATA')
      


class props(bpy.types.PropertyGroup):
  t_slot = bpy.props.StringProperty()
  #index = bpy.props.IntProperty()


class dummyOperator(bpy.types.Operator):
  bl_label = "Operator for appending"
  bl_idname = "obs.to_my_list"
  
  def execute(self, context):
    bpy.context.scene.MyPanel.add()
    return {"FINISHED"}

class removeOperator(bpy.types.Operator):
  bl_label = "Operator for appending"
  bl_idname = "remove.from_list"
  
  def execute(self, context):
    bpy.context.scene.MyPanel.remove(len(bpy.context.scene.MyPanel)-1)
    return {"FINISHED"}


def register():
  bpy.utils.register_module(__name__)
  #bpy.types.Scene.MyPanel = bpy.props.PointerProperty(type=props)
  bpy.types.Scene.MyPanel = bpy.props.CollectionProperty(type=props)


def unregister():
  bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

You may wish to look into UI lists as well; https://www.blender.org/api/blender_python_api_2_77_1/bpy.types.UIList.html