Files
LNXSDK/leenkx/blender/lnx/props_properties.py

264 lines
11 KiB
Python

import bpy
from bpy.types import Menu, Panel, UIList
from bpy.props import *
class LnxArrayItem(bpy.types.PropertyGroup):
# Name property for each array item
name_prop: StringProperty(name="Name", default="Item")
index_prop = bpy.props.IntProperty(
name="Index",
description="Index of the item",
default=0,
options={'HIDDEN', 'SKIP_SAVE'}
)
# Properties for each type
string_prop: StringProperty(name="String", default="")
integer_prop: IntProperty(name="Integer", default=0)
float_prop: FloatProperty(name="Float", default=0.0)
boolean_prop: BoolProperty(name="Boolean", default=False)
vector_prop: FloatVectorProperty(name="Vector", size=3, default=[0.0, 0.0, 0.0])
class LnxPropertyListItem(bpy.types.PropertyGroup):
type_prop: EnumProperty(
items = [('string', 'String', 'String'),
('integer', 'Integer', 'Integer'),
('float', 'Float', 'Float'),
('boolean', 'Boolean', 'Boolean'),
('vector', 'Vector', 'Vector'),
('array', 'Array', 'Array'),
],
name = "Type")
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)
vector_prop: FloatVectorProperty(name="Vector", size=3, default=[0.0, 0.0, 0.0])
array_prop: CollectionProperty(type=LnxArrayItem)
array_item_type: EnumProperty(
items = [
('string', 'String', 'String'),
('integer', 'Integer', 'Integer'),
('float', 'Float', 'Float'),
('boolean', 'Boolean', 'Boolean'),
('vector', 'Vector', 'Vector'),
],
name = "New Item Type",
default = 'string'
)
class LNX_UL_ArrayItemList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# Use the item's index within the array as its uneditable name
array_type = data.array_item_type
if self.layout_type in {'DEFAULT', 'COMPACT'}:
# Display the index of the item as its name in a non-editable label
layout.label(text=str(index))
# Display the appropriate property based on the array type
if array_type == 'string':
layout.prop(item, "string_prop", text="")
elif array_type == 'integer':
layout.prop(item, "integer_prop", text="")
elif array_type == 'float':
layout.prop(item, "float_prop", text="")
elif array_type == 'boolean':
layout.prop(item, "boolean_prop", text="")
elif array_type == 'vector':
layout.prop(item, "vector_prop", text="")
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon="OBJECT_DATAMODE")
def get_lnx_target(context):
if context.object is not None and context.object.mode == 'OBJECT':
return context.object
return context.scene
class LnxArrayAddItem(bpy.types.Operator):
bl_idname = "lnx_array.add_item"
bl_label = "Add Array Item"
def execute(self, context):
target = get_lnx_target(context)
if target.lnx_propertylist and target.lnx_propertylist_index < len(target.lnx_propertylist):
selected_item = target.lnx_propertylist[target.lnx_propertylist_index]
# Create a new item in the array
new_item = selected_item.array_prop.add()
# Set the type of the new item based on the selected type
new_item_type = selected_item.array_item_type
if new_item_type == 'string':
new_item.string_prop = ""
elif new_item_type == 'integer':
new_item.integer_prop = 0
elif new_item_type == 'float':
new_item.float_prop = 0.0
elif new_item_type == 'boolean':
new_item.boolean_prop = False
elif new_item_type == 'vector':
new_item.vector_prop = [0.0, 0.0, 0.0]
# Set the index of the new item
new_item_index = len(selected_item.array_prop) - 1
new_item.index_prop = new_item_index
# Update the array index
selected_item.array_index = new_item_index
return {'FINISHED'}
# Operator to remove an item from the array
class LnxArrayRemoveItem(bpy.types.Operator):
bl_idname = "lnx_array.remove_item"
bl_label = "Remove Array Item"
def execute(self, context):
target = get_lnx_target(context)
if target.lnx_propertylist and target.lnx_propertylist_index < len(target.lnx_propertylist):
selected_item = target.lnx_propertylist[target.lnx_propertylist_index]
if selected_item.array_prop:
selected_item.array_prop.remove(selected_item.array_index)
if selected_item.array_index > 0:
selected_item.array_index -= 1
return {'FINISHED'}
class LNX_UL_PropertyList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
layout.use_property_split = False
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "name_prop", text="", emboss=False, icon="OBJECT_DATAMODE")
layout.prop(item, item.type_prop + "_prop", text="", emboss=(item.type_prop == 'boolean'))
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon="OBJECT_DATAMODE")
class LnxPropertyListNewItem(bpy.types.Operator):
bl_idname = "lnx_propertylist.new_item"
bl_label = "New"
type_prop: EnumProperty(
items = [('string', 'String', 'String'),
('integer', 'Integer', 'Integer'),
('float', 'Float', 'Float'),
('boolean', 'Boolean', 'Boolean'),
('vector', 'Vector', 'Vector'),
('array', 'Array', 'Array'),
],
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):
target = get_lnx_target(context)
prop = target.lnx_propertylist.add()
prop.type_prop = self.type_prop
target.lnx_propertylist_index = len(target.lnx_propertylist) - 1
return{'FINISHED'}
class LnxPropertyListDeleteItem(bpy.types.Operator):
bl_idname = "lnx_propertylist.delete_item"
bl_label = "Deletes an item"
@classmethod
def poll(self, context):
target = get_lnx_target(context)
return len(target.lnx_propertylist) > 0
def execute(self, context):
target = get_lnx_target(context)
target.lnx_propertylist.remove(target.lnx_propertylist_index)
target.lnx_propertylist_index = max(0, target.lnx_propertylist_index - 1)
return {'FINISHED'}
class LnxPropertyListMoveItem(bpy.types.Operator):
bl_idname = "lnx_propertylist.move_item"
bl_label = "Move"
direction: EnumProperty(items=(('UP', 'Up', ""), ('DOWN', 'Down', "")))
def execute(self, context):
target = get_lnx_target(context)
idx = target.lnx_propertylist_index
new_idx = idx - 1 if self.direction == 'UP' else idx + 1
if 0 <= new_idx < len(target.lnx_propertylist):
target.lnx_propertylist.move(idx, new_idx)
target.lnx_propertylist_index = new_idx
return {'FINISHED'}
def draw_properties(layout, obj):
layout.use_property_split = False
main_col = layout.column()
main_col.label(text="Properties")
row = main_col.row()
row.template_list("LNX_UL_PropertyList", "", obj, "lnx_propertylist", obj, "lnx_propertylist_index", rows=4)
btn_col = row.column(align=True)
btn_col.operator("lnx_propertylist.new_item", icon='ADD', text="")
btn_col.operator("lnx_propertylist.delete_item", icon='REMOVE', text="")
if len(obj.lnx_propertylist) > 1:
btn_col.separator()
btn_col.operator("lnx_propertylist.move_item", icon='TRIA_UP', text="").direction = 'UP'
btn_col.operator("lnx_propertylist.move_item", icon='TRIA_DOWN', text="").direction = 'DOWN'
if len(obj.lnx_propertylist) > 0 and obj.lnx_propertylist_index < len(obj.lnx_propertylist):
item = obj.lnx_propertylist[obj.lnx_propertylist_index]
if item.type_prop == 'array':
main_col.separator()
main_col.label(text="Array Items")
main_col.prop(item, "array_item_type", text="Type")
row_arr = main_col.row()
row_arr.template_list("LNX_UL_ArrayItemList", "", item, "array_prop", item, "array_index", rows=4)
btn_arr = row_arr.column(align=True)
btn_arr.operator("lnx_array.add_item", icon='ADD', text="")
btn_arr.operator("lnx_array.remove_item", icon='REMOVE', text="")
class LNX_PT_ObjectPropsPanel(bpy.types.Panel):
bl_label = "Leenkx Props"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"
def draw(self, context):
draw_properties(self.layout, context.object)
class LNX_PT_ScenePropsPanel(bpy.types.Panel):
bl_label = "Leenkx Props"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
def draw(self, context):
draw_properties(self.layout, context.scene)
__REG_CLASSES = (
LnxArrayItem, LnxPropertyListItem, LNX_UL_PropertyList, LNX_UL_ArrayItemList,
LnxPropertyListNewItem, LnxPropertyListDeleteItem, LnxPropertyListMoveItem,
LnxArrayAddItem, LnxArrayRemoveItem, LNX_PT_ObjectPropsPanel, LNX_PT_ScenePropsPanel,
)
def register():
for cls in __REG_CLASSES:
bpy.utils.register_class(cls)
bpy.types.Object.lnx_propertylist = CollectionProperty(type=LnxPropertyListItem)
bpy.types.Object.lnx_propertylist_index = IntProperty(name="Index", default=0)
bpy.types.Scene.lnx_propertylist = CollectionProperty(type=LnxPropertyListItem)
bpy.types.Scene.lnx_propertylist_index = IntProperty(name="Index", default=0)
def unregister():
for cls in reversed(__REG_CLASSES):
bpy.utils.unregister_class(cls)
del bpy.types.Object.lnx_propertylist
del bpy.types.Object.lnx_propertylist_index
del bpy.types.Scene.lnx_propertylist
del bpy.types.Scene.lnx_propertylist_index