2018-11-18 21:52:50 +01:00
|
|
|
import bpy
|
|
|
|
from bpy.types import Menu, Panel, UIList
|
|
|
|
from bpy.props import *
|
|
|
|
|
|
|
|
class ArmPropertyListItem(bpy.types.PropertyGroup):
|
2018-12-18 23:48:38 +01:00
|
|
|
type_prop: EnumProperty(
|
2018-11-18 21:52:50 +01:00
|
|
|
items = [('string', 'String', 'String'),
|
|
|
|
('integer', 'Integer', 'Integer'),
|
|
|
|
('float', 'Float', 'Float'),
|
|
|
|
('boolean', 'Boolean', 'Boolean'),
|
|
|
|
],
|
|
|
|
name = "Type")
|
2018-12-18 23:48:38 +01:00
|
|
|
name_prop: StringProperty(name="Name", description="A name for this item", default="my_prop")
|
|
|
|
string_prop: StringProperty(name="String", description="A name for this item", default="text")
|
|
|
|
integer_prop: IntProperty(name="Integer", description="A name for this item", default=0)
|
|
|
|
float_prop: FloatProperty(name="Float", description="A name for this item", default=0.0)
|
|
|
|
boolean_prop: BoolProperty(name="Boolean", description="A name for this item", default=False)
|
2018-11-18 21:52:50 +01:00
|
|
|
|
2019-03-29 14:55:42 +01:00
|
|
|
class ARM_UL_PropertyList(bpy.types.UIList):
|
2018-11-18 21:52:50 +01:00
|
|
|
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
|
2021-02-19 20:59:54 +01:00
|
|
|
layout.use_property_split = False
|
2018-11-18 21:52:50 +01:00
|
|
|
# Make sure your code supports all 3 layout types
|
|
|
|
if self.layout_type in {'DEFAULT', 'COMPACT'}:
|
2021-02-19 20:59:54 +01:00
|
|
|
layout.prop(item, "name_prop", text="", emboss=False, icon="OBJECT_DATAMODE")
|
|
|
|
layout.prop(item, item.type_prop + "_prop", text="", emboss=(item.type_prop == 'boolean'))
|
2018-11-18 21:52:50 +01:00
|
|
|
elif self.layout_type in {'GRID'}:
|
|
|
|
layout.alignment = 'CENTER'
|
|
|
|
layout.label(text="", icon="OBJECT_DATAMODE")
|
|
|
|
|
|
|
|
class ArmPropertyListNewItem(bpy.types.Operator):
|
|
|
|
# Add a new item to the list
|
|
|
|
bl_idname = "arm_propertylist.new_item"
|
|
|
|
bl_label = "New"
|
|
|
|
|
2018-12-18 23:48:38 +01:00
|
|
|
type_prop: EnumProperty(
|
2018-11-18 21:54:51 +01:00
|
|
|
items = [('string', 'String', 'String'),
|
|
|
|
('integer', 'Integer', 'Integer'),
|
|
|
|
('float', 'Float', 'Float'),
|
|
|
|
('boolean', 'Boolean', 'Boolean'),
|
2018-11-18 21:52:50 +01:00
|
|
|
],
|
|
|
|
name = "Type")
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
wm = context.window_manager
|
|
|
|
return wm.invoke_props_dialog(self)
|
|
|
|
|
|
|
|
def draw(self,context):
|
|
|
|
layout = self.layout
|
|
|
|
layout.prop(self, "type_prop", expand=True)
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
obj = bpy.context.object
|
|
|
|
prop = obj.arm_propertylist.add()
|
|
|
|
prop.type_prop = self.type_prop
|
|
|
|
obj.arm_propertylist_index = len(obj.arm_propertylist) - 1
|
|
|
|
return{'FINISHED'}
|
|
|
|
|
|
|
|
class ArmPropertyListDeleteItem(bpy.types.Operator):
|
|
|
|
# Delete the selected item from the list
|
|
|
|
bl_idname = "arm_propertylist.delete_item"
|
|
|
|
bl_label = "Deletes an item"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(self, context):
|
|
|
|
""" Enable if there's something in the list """
|
|
|
|
obj = bpy.context.object
|
|
|
|
if obj == None:
|
|
|
|
return False
|
|
|
|
return len(obj.arm_propertylist) > 0
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
obj = bpy.context.object
|
|
|
|
lst = obj.arm_propertylist
|
|
|
|
index = obj.arm_propertylist_index
|
|
|
|
|
|
|
|
if len(lst) <= index:
|
|
|
|
return{'FINISHED'}
|
|
|
|
|
|
|
|
lst.remove(index)
|
|
|
|
|
|
|
|
if index > 0:
|
|
|
|
index = index - 1
|
|
|
|
|
|
|
|
obj.arm_propertylist_index = index
|
|
|
|
return{'FINISHED'}
|
|
|
|
|
|
|
|
class ArmPropertyListMoveItem(bpy.types.Operator):
|
|
|
|
# Move an item in the list
|
|
|
|
bl_idname = "arm_propertylist.move_item"
|
|
|
|
bl_label = "Move an item in the list"
|
2018-12-18 23:48:38 +01:00
|
|
|
direction: EnumProperty(
|
2018-11-18 21:52:50 +01:00
|
|
|
items=(
|
|
|
|
('UP', 'Up', ""),
|
|
|
|
('DOWN', 'Down', ""),))
|
|
|
|
|
|
|
|
def move_index(self):
|
|
|
|
obj = bpy.context.object
|
|
|
|
index = obj.arm_propertylist_index
|
|
|
|
list_length = len(obj.arm_propertylist) - 1
|
|
|
|
new_index = 0
|
|
|
|
|
|
|
|
if self.direction == 'UP':
|
|
|
|
new_index = index - 1
|
|
|
|
elif self.direction == 'DOWN':
|
|
|
|
new_index = index + 1
|
|
|
|
|
|
|
|
new_index = max(0, min(new_index, list_length))
|
2018-11-22 13:31:15 +01:00
|
|
|
obj.arm_propertylist.move(index, new_index)
|
|
|
|
obj.arm_propertylist_index = new_index
|
2018-11-18 21:52:50 +01:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
obj = bpy.context.object
|
|
|
|
list = obj.arm_propertylist
|
|
|
|
index = obj.arm_propertylist_index
|
|
|
|
|
|
|
|
if self.direction == 'DOWN':
|
|
|
|
neighbor = index + 1
|
|
|
|
self.move_index()
|
|
|
|
|
|
|
|
elif self.direction == 'UP':
|
|
|
|
neighbor = index - 1
|
|
|
|
self.move_index()
|
|
|
|
else:
|
|
|
|
return{'CANCELLED'}
|
|
|
|
return{'FINISHED'}
|
|
|
|
|
|
|
|
def draw_properties(layout, obj):
|
2018-12-19 13:33:17 +01:00
|
|
|
layout.label(text="Properties")
|
2018-11-18 21:52:50 +01:00
|
|
|
rows = 2
|
|
|
|
if len(obj.arm_traitlist) > 1:
|
|
|
|
rows = 4
|
|
|
|
row = layout.row()
|
2019-03-29 14:55:42 +01:00
|
|
|
row.template_list("ARM_UL_PropertyList", "The_List", obj, "arm_propertylist", obj, "arm_propertylist_index", rows=rows)
|
2018-11-18 21:52:50 +01:00
|
|
|
col = row.column(align=True)
|
2018-12-18 23:48:38 +01:00
|
|
|
op = col.operator("arm_propertylist.new_item", icon='ADD', text="")
|
|
|
|
op = col.operator("arm_propertylist.delete_item", icon='REMOVE', text="")
|
2018-11-18 21:52:50 +01:00
|
|
|
if len(obj.arm_propertylist) > 1:
|
|
|
|
col.separator()
|
|
|
|
op = col.operator("arm_propertylist.move_item", icon='TRIA_UP', text="")
|
|
|
|
op.direction = 'UP'
|
|
|
|
op = col.operator("arm_propertylist.move_item", icon='TRIA_DOWN', text="")
|
|
|
|
op.direction = 'DOWN'
|
|
|
|
|
|
|
|
def register():
|
|
|
|
bpy.utils.register_class(ArmPropertyListItem)
|
2019-03-29 14:55:42 +01:00
|
|
|
bpy.utils.register_class(ARM_UL_PropertyList)
|
2018-11-18 21:52:50 +01:00
|
|
|
bpy.utils.register_class(ArmPropertyListNewItem)
|
|
|
|
bpy.utils.register_class(ArmPropertyListDeleteItem)
|
|
|
|
bpy.utils.register_class(ArmPropertyListMoveItem)
|
2018-12-18 23:48:38 +01:00
|
|
|
bpy.types.Object.arm_propertylist = CollectionProperty(type=ArmPropertyListItem)
|
|
|
|
bpy.types.Object.arm_propertylist_index = IntProperty(name="Index for arm_propertylist", default=0)
|
2018-11-18 21:52:50 +01:00
|
|
|
|
|
|
|
def unregister():
|
|
|
|
bpy.utils.unregister_class(ArmPropertyListItem)
|
2019-03-29 14:55:42 +01:00
|
|
|
bpy.utils.unregister_class(ARM_UL_PropertyList)
|
2018-11-18 21:52:50 +01:00
|
|
|
bpy.utils.unregister_class(ArmPropertyListNewItem)
|
|
|
|
bpy.utils.unregister_class(ArmPropertyListDeleteItem)
|
2021-02-19 20:59:54 +01:00
|
|
|
bpy.utils.unregister_class(ArmPropertyListMoveItem)
|