armory/blender/arm/props_lod.py

158 lines
5 KiB
Python
Raw Permalink Normal View History

2016-12-05 01:54:01 +01:00
import bpy
from bpy.props import *
def update_size_prop(self, context):
if context.object == None:
return
mdata = context.object.data
2017-08-21 12:17:55 +02:00
i = mdata.arm_lodlist_index
ar = mdata.arm_lodlist
2016-12-05 01:54:01 +01:00
# Clamp screen size to not exceed previous entry
if i > 0 and ar[i - 1].screen_size_prop < self.screen_size_prop:
self.screen_size_prop = ar[i - 1].screen_size_prop
2017-08-21 12:17:55 +02:00
class ArmLodListItem(bpy.types.PropertyGroup):
2016-12-05 01:54:01 +01:00
# Group of properties representing an item in the list
2018-12-18 23:48:38 +01:00
name: StringProperty(
2016-12-05 01:54:01 +01:00
name="Name",
description="A name for this item",
default="")
2020-04-10 21:46:28 +02:00
2018-12-18 23:48:38 +01:00
enabled_prop: BoolProperty(
2016-12-05 01:54:01 +01:00
name="",
description="A name for this item",
default=True)
2018-12-18 23:48:38 +01:00
screen_size_prop: FloatProperty(
2016-12-05 01:54:01 +01:00
name="Screen Size",
description="A name for this item",
min=0.0,
max=1.0,
default=0.0,
update=update_size_prop)
class ARM_UL_LodList(bpy.types.UIList):
2016-12-05 01:54:01 +01:00
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
layout.use_property_split = False
2016-12-05 01:54:01 +01:00
if self.layout_type in {'DEFAULT', 'COMPACT'}:
2021-01-14 22:17:42 +01:00
row = layout.row()
row.separator(factor=0.1)
row.prop(item, "enabled_prop")
2016-12-05 01:54:01 +01:00
name = item.name
if name == '':
name = 'None'
row.label(text=name, icon='OBJECT_DATAMODE')
2017-08-22 15:57:49 +02:00
col = row.column()
col.alignment = 'RIGHT'
2018-09-05 10:20:02 +02:00
col.label(text="{:.2f}".format(item.screen_size_prop))
2016-12-05 01:54:01 +01:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon='OBJECT_DATAMODE')
2016-12-05 01:54:01 +01:00
2017-08-21 12:17:55 +02:00
class ArmLodListNewItem(bpy.types.Operator):
2016-12-05 01:54:01 +01:00
# Add a new item to the list
2017-08-21 12:17:55 +02:00
bl_idname = "arm_lodlist.new_item"
2016-12-05 01:54:01 +01:00
bl_label = "Add a new item"
bl_options = {'UNDO'}
2016-12-05 01:54:01 +01:00
def execute(self, context):
mdata = bpy.context.object.data
2017-08-21 12:17:55 +02:00
mdata.arm_lodlist.add()
mdata.arm_lodlist_index = len(mdata.arm_lodlist) - 1
2016-12-05 01:54:01 +01:00
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmLodListDeleteItem(bpy.types.Operator):
2016-12-05 01:54:01 +01:00
# Delete the selected item from the list
2017-08-21 12:17:55 +02:00
bl_idname = "arm_lodlist.delete_item"
2016-12-05 01:54:01 +01:00
bl_label = "Deletes an item"
bl_options = {'INTERNAL', 'UNDO'}
2016-12-05 01:54:01 +01:00
@classmethod
def poll(cls, context):
2016-12-05 01:54:01 +01:00
""" Enable if there's something in the list """
if bpy.context.object is None:
return False
2016-12-05 01:54:01 +01:00
mdata = bpy.context.object.data
2017-08-21 12:17:55 +02:00
return len(mdata.arm_lodlist) > 0
2016-12-05 01:54:01 +01:00
def execute(self, context):
mdata = bpy.context.object.data
2018-08-01 10:20:42 +02:00
lodlist = mdata.arm_lodlist
2017-08-21 12:17:55 +02:00
index = mdata.arm_lodlist_index
2016-12-05 01:54:01 +01:00
2018-08-01 10:20:42 +02:00
n = lodlist[index].name
2020-04-10 21:46:28 +02:00
if n in context.scene.collection.objects:
2018-08-01 10:20:42 +02:00
obj = bpy.data.objects[n]
2020-04-10 21:46:28 +02:00
context.scene.collection.objects.unlink(obj)
2018-08-01 10:20:42 +02:00
lodlist.remove(index)
2016-12-05 01:54:01 +01:00
if index > 0:
index = index - 1
2017-08-21 12:17:55 +02:00
mdata.arm_lodlist_index = index
2016-12-05 01:54:01 +01:00
return{'FINISHED'}
2018-11-22 13:31:15 +01:00
class ArmLodListMoveItem(bpy.types.Operator):
# Move an item in the list
bl_idname = "arm_lodlist.move_item"
bl_label = "Move an item in the list"
bl_options = {'INTERNAL', 'UNDO'}
2018-12-18 23:48:38 +01:00
direction: EnumProperty(
2018-11-22 13:31:15 +01:00
items=(
('UP', 'Up', ""),
('DOWN', 'Down', ""),))
def move_index(self):
# Move index of an item render queue while clamping it
mdata = bpy.context.object.data
index = mdata.arm_lodlist_index
list_length = len(mdata.arm_lodlist) - 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))
mdata.arm_lodlist.move(index, new_index)
mdata.arm_lodlist_index = new_index
def execute(self, context):
mdata = bpy.context.object.data
list = mdata.arm_lodlist
index = mdata.arm_lodlist_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'}
2016-12-05 01:54:01 +01:00
def register():
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmLodListItem)
bpy.utils.register_class(ARM_UL_LodList)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmLodListNewItem)
bpy.utils.register_class(ArmLodListDeleteItem)
2018-11-22 13:31:15 +01:00
bpy.utils.register_class(ArmLodListMoveItem)
2017-08-21 12:17:55 +02:00
2018-12-18 23:48:38 +01:00
bpy.types.Mesh.arm_lodlist = CollectionProperty(type=ArmLodListItem)
bpy.types.Mesh.arm_lodlist_index = IntProperty(name="Index for my_list", default=0)
bpy.types.Mesh.arm_lod_material = BoolProperty(name="Material Lod", description="Use materials of lod objects", default=False)
2016-12-05 01:54:01 +01:00
def unregister():
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmLodListItem)
bpy.utils.unregister_class(ARM_UL_LodList)
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmLodListNewItem)
bpy.utils.unregister_class(ArmLodListDeleteItem)
2018-11-22 13:31:15 +01:00
bpy.utils.unregister_class(ArmLodListMoveItem)