I’m going to explain my problem:
I have a custom property named “Group” and each group has a custom property named “List” that contains all items. I assign to one object an array of Groups, so I have Group[0], Group[1], etc… and for each “Group” I have “List”, so Group[0] has its List with several items, Group[1] equal, etc.
I have this code:
<b>class </b><b>UilistActions</b>(bpy.types.Operator):
bl_idname = "custom.list_action"
bl_label = "List Action"
tag = bpy.props.IntProperty()
action = bpy.props.EnumProperty(items=(('UP', "Up", ""), ('DOWN', "Down", ""),
('REMOVE', "Remove", ""), ('ADD', "Add", "")))
<b>def </b><b>invoke</b>(self, context, event):
mainobject = context.object
mp = mainobject.MyCustomData[0]
mg = mp.grps[self.tag] # This is the line where I need the index of the group
idx = mg.custom_index
<b>try</b>:
item = mg.custom[idx]
<b>except </b>IndexError:
<b>pass
</b><b> else</b>:
<b>if </b>self.action == 'DOWN' <b>and </b>idx < len(mg.custom) - 1:
item_next = mg.custom[idx + 1].name
mg.custom_index += 1
<b>elif </b>self.action == 'UP' <b>and </b>idx >= 1:
item_prev = mg.custom[idx - 1].name
mg.custom_index -= 1
<b>elif </b>self.action == 'REMOVE':
info = 'Item %s removed from list' % mg.custom[mg.custom_index].name
mg.custom_index -= 1
mg.custom.remove(idx)
<b>if </b>self.action == 'ADD':
val = str(mp.allgrp)
# Avoid duplication
<b>for </b>i <b>in </b>mg.custom:
<b>if </b>i.grp == val:
<b>return </b>{"FINISHED"}
item = mg.custom.add()
item.id = len(mg.custom)
item.grp = val
item.lstold = val
mg.custom_index = (len(mg.custom) - 1)
<b>return </b>{"FINISHED"}
In my code, I need the index of the group to detect the group item that contains the corresponding list.
<b>def </b><b>invoke</b>(self, context, event):
mainobject = context.object
mp = mainobject.MyCustomData[0]
mg = mp.grps[self.tag] # This is the line where I need the index of the group
In the panel I have each group, with its UIlist.
<b>for </b>group <b>in </b>all_my_groups:
# group contains the current group object
# I need to pass a reference or index of group to this instance of the list
rows = 3
row = box.row()
row.template_list("UlItems", "", group, "custom", group, "custom_index", rows=rows)
col = row.column(align=<b>True</b>)
col.operator("custom.list_action", icon='ZOOMIN', text="").action = 'ADD'
col.operator("custom.list_action", icon='ZOOMOUT', text="").action = 'REMOVE'
col.separator()
col.operator("custom.list_action", icon='TRIA_UP', text="").action = 'UP'
col.operator("custom.list_action", icon='TRIA_DOWN', text="").action = 'DOWN'
I have tried to pass the tag parameter to the class, but I can’t.