armory/blender/arm/props_traits_params.py

173 lines
5.6 KiB
Python
Raw Normal View History

2016-01-28 12:32:05 +01:00
import bpy
from bpy.types import Menu, Panel, UIList
from bpy.props import *
2017-08-21 12:17:55 +02:00
class ArmTraitParamListItem(bpy.types.PropertyGroup):
2016-07-17 23:29:30 +02:00
# Group of properties representing an item in the list
name = bpy.props.StringProperty(
name="Name",
description="A name for this item",
default="Untitled")
2017-08-21 12:17:55 +02:00
class ArmTraitParamList(bpy.types.UIList):
2016-07-17 23:29:30 +02:00
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# We could write some code to decide which icon to use here...
custom_icon = 'OBJECT_DATAMODE'
2016-01-28 12:32:05 +01:00
2016-07-17 23:29:30 +02:00
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
#layout.prop(item, "enabled_prop")
#layout.label(item.name, icon = custom_icon)
layout.prop(item, "name", text="", emboss=False, icon=custom_icon)
2016-01-28 12:32:05 +01:00
2016-07-17 23:29:30 +02:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label("", icon = custom_icon)
2016-01-28 12:32:05 +01:00
2017-08-21 12:17:55 +02:00
class ArmTraitParamListNewItem(bpy.types.Operator):
2016-07-17 23:29:30 +02:00
# Add a new item to the list
2017-08-21 12:17:55 +02:00
bl_idname = "arm_traitparamslist.new_item"
2016-07-17 23:29:30 +02:00
bl_label = "Add a new item"
2016-01-28 12:32:05 +01:00
2017-08-27 12:50:09 +02:00
is_object = bpy.props.BoolProperty(name="", description="A name for this item", default=False)
2016-07-17 23:29:30 +02:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
trait.arm_traitparamslist.add()
trait.arm_traitparamslist_index = len(trait.arm_traitparamslist) - 1
2016-07-17 23:29:30 +02:00
return{'FINISHED'}
2016-01-28 12:32:05 +01:00
2017-08-21 12:17:55 +02:00
class ArmTraitParamListDeleteItem(bpy.types.Operator):
2016-07-17 23:29:30 +02:00
# Delete the selected item from the list
2017-08-21 12:17:55 +02:00
bl_idname = "arm_traitparamslist.delete_item"
2016-07-17 23:29:30 +02:00
bl_label = "Deletes an item"
2016-01-28 12:32:05 +01:00
2017-08-27 12:50:09 +02:00
is_object = bpy.props.BoolProperty(name="", description="A name for this item", default=False)
2016-07-17 23:29:30 +02:00
@classmethod
def poll(self, context):
""" Enable if there's something in the list """
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if obj == None:
2017-08-22 12:08:44 +02:00
return False
2017-08-27 12:50:09 +02:00
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return False
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
return len(trait.arm_traitparamslist) > 0
2016-01-28 12:32:05 +01:00
2016-07-17 23:29:30 +02:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
list = trait.arm_traitparamslist
index = trait.arm_traitparamslist_index
2016-01-28 12:32:05 +01:00
2016-07-17 23:29:30 +02:00
list.remove(index)
2016-01-28 12:32:05 +01:00
2016-07-17 23:29:30 +02:00
if index > 0:
index = index - 1
2016-01-28 12:32:05 +01:00
2017-08-21 12:17:55 +02:00
trait.arm_traitparamslist_index = index
2016-07-17 23:29:30 +02:00
return{'FINISHED'}
2016-01-28 12:32:05 +01:00
2017-08-21 12:17:55 +02:00
class ArmTraitParamListMoveItem(bpy.types.Operator):
2016-07-17 23:29:30 +02:00
# Move an item in the list
2017-08-21 12:17:55 +02:00
bl_idname = "arm_traitparamslist.move_item"
2016-07-17 23:29:30 +02:00
bl_label = "Move an item in the list"
direction = bpy.props.EnumProperty(
items=(
('UP', 'Up', ""),
('DOWN', 'Down', ""),))
2017-08-27 12:50:09 +02:00
is_object = bpy.props.BoolProperty(name="", description="A name for this item", default=False)
2016-07-17 23:29:30 +02:00
@classmethod
def poll(self, context):
""" Enable if there's something in the list. """
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if obj == None:
2017-08-22 12:08:44 +02:00
return False
2017-08-27 12:50:09 +02:00
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return False
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
return len(trait.arm_traitparamslist) > 0
2016-07-17 23:29:30 +02:00
def move_index(self):
# Move index of an item render queue while clamping it
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
index = trait.arm_traitparamslist_index
list_length = len(trait.arm_traitparamslist) - 1
2016-07-17 23:29:30 +02:00
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))
index = new_index
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
if len(obj.arm_traitlist) == 0:
2016-11-19 11:44:15 +01:00
return
2017-08-27 12:50:09 +02:00
trait = obj.arm_traitlist[obj.arm_traitlist_index]
2017-08-21 12:17:55 +02:00
list = trait.arm_traitparamslist
index = trait.arm_traitparamslist_index
2016-07-17 23:29:30 +02:00
if self.direction == 'DOWN':
neighbor = index + 1
#queue.move(index,neighbor)
self.move_index()
elif self.direction == 'UP':
neighbor = index - 1
#queue.move(neighbor, index)
self.move_index()
else:
return{'CANCELLED'}
return{'FINISHED'}
2016-01-28 12:32:05 +01:00
def register():
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmTraitParamListItem)
bpy.utils.register_class(ArmTraitParamList)
bpy.utils.register_class(ArmTraitParamListNewItem)
bpy.utils.register_class(ArmTraitParamListDeleteItem)
bpy.utils.register_class(ArmTraitParamListMoveItem)
2016-01-28 12:32:05 +01:00
def unregister():
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmTraitParamListItem)
bpy.utils.unregister_class(ArmTraitParamList)
bpy.utils.unregister_class(ArmTraitParamListNewItem)
bpy.utils.unregister_class(ArmTraitParamListDeleteItem)
bpy.utils.unregister_class(ArmTraitParamListMoveItem)