armory/blender/arm/props_traits_props.py

161 lines
5.5 KiB
Python
Raw Permalink Normal View History

2017-07-24 02:27:22 +02:00
import bpy
from bpy.props import *
2021-08-04 22:49:38 +02:00
__all__ = ['ArmTraitPropWarning', 'ArmTraitPropListItem', 'ARM_UL_PropList']
PROP_TYPE_ICONS = {
"String": "SORTALPHA",
"Int": "CHECKBOX_DEHLT",
"Float": "RADIOBUT_OFF",
"Bool": "CHECKMARK",
2020-01-07 20:26:12 +01:00
"Vec2": "ORIENTATION_VIEW",
"Vec3": "ORIENTATION_GLOBAL",
"Vec4": "MESH_ICOSPHERE",
2020-01-07 22:00:45 +01:00
"Object": "OBJECT_DATA",
"CameraObject": "CAMERA_DATA",
"LightObject": "LIGHT_DATA",
"MeshObject": "MESH_DATA",
"SpeakerObject": "OUTLINER_DATA_SPEAKER"
}
2020-01-07 22:00:45 +01:00
def filter_objects(item, b_object):
if item.type == "CameraObject":
return b_object.type == "CAMERA"
if item.type == "LightObject":
return b_object.type == "LIGHT"
if item.type == "MeshObject":
return b_object.type == "MESH"
if item.type == "SpeakerObject":
return b_object.type == "SPEAKER"
if item.type == "Object":
return True
class ArmTraitPropWarning(bpy.types.PropertyGroup):
propName: StringProperty(name="Property Name")
warning: StringProperty(name="Warning")
2017-08-21 12:17:55 +02:00
class ArmTraitPropListItem(bpy.types.PropertyGroup):
"""Group of properties representing an item in the list."""
2018-12-18 23:48:38 +01:00
name: StringProperty(
name="Name",
description="The name of this property",
default="Untitled")
type: EnumProperty(
items=(
# (Haxe Type, Display Name, Description)
("String", "String", "String Type"),
("Int", "Integer", "Integer Type"),
("Float", "Float", "Float Type"),
2020-01-07 20:26:12 +01:00
("Bool", "Boolean", "Boolean Type"),
("Vec2", "Vec2", "2D Vector Type"),
("Vec3", "Vec3", "3D Vector Type"),
2020-01-07 21:09:39 +01:00
("Vec4", "Vec4", "4D Vector Type"),
2020-01-07 22:00:45 +01:00
("Object", "Object", "Object Type"),
("CameraObject", "Camera Object", "Camera Object Type"),
("LightObject", "Light Object", "Light Object Type"),
("MeshObject", "Mesh Object", "Mesh Object Type"),
("SpeakerObject", "Speaker Object", "Speaker Object Type")),
name="Type",
description="The type of this property",
default="String")
2020-01-07 21:38:23 +01:00
# === VALUES ===
value_string: StringProperty(name="Value", default="")
value_int: IntProperty(name="Value", default=0)
value_float: FloatProperty(name="Value", default=0.0)
value_bool: BoolProperty(name="Value", default=False)
value_vec2: FloatVectorProperty(name="Value", size=2)
value_vec3: FloatVectorProperty(name="Value", size=3)
value_vec4: FloatVectorProperty(name="Value", size=4)
2020-01-07 22:00:45 +01:00
value_object: PointerProperty(
name="Value", type=bpy.types.Object, poll=filter_objects)
2020-01-07 20:26:12 +01:00
def set_value(self, val):
2020-01-07 21:09:39 +01:00
# Would require way too much effort, so it's out of scope here.
2020-01-07 22:00:45 +01:00
if self.type.endswith("Object"):
2020-01-07 21:09:39 +01:00
return
if self.type == "Int":
self.value_int = int(val)
elif self.type == "Float":
self.value_float = float(val)
elif self.type == "Bool":
2020-02-07 19:29:55 +01:00
self.value_bool = val == "true"
2020-01-07 20:26:12 +01:00
elif self.type in ("Vec2", "Vec3", "Vec4"):
if isinstance(val, str):
dimensions = int(self.type[-1])
# Parse "new VecX(...)"
val = val.split("(")[1].split(")")[0].split(",")
val = [value.strip() for value in val]
# new VecX() without parameters
if len(val) == 1 and val[0] == "":
# Use default value
return
# new VecX() with less parameters than its dimensions
while len(val) < dimensions:
val.append(0.0)
val = [float(value) for value in val]
setattr(self, "value_" + self.type.lower(), val)
else:
self.value_string = str(val)
def get_value(self):
if self.type == "Int":
return self.value_int
if self.type == "Float":
return self.value_float
if self.type == "Bool":
return self.value_bool
2020-01-07 20:26:12 +01:00
if self.type in ("Vec2", "Vec3", "Vec4"):
return list(getattr(self, "value_" + self.type.lower()))
2020-01-07 22:00:45 +01:00
if self.type.endswith("Object"):
if self.value_object is not None:
return self.value_object.name
return ""
2020-01-07 21:09:39 +01:00
return self.value_string
2017-07-24 02:27:22 +02:00
class ARM_UL_PropList(bpy.types.UIList):
2017-07-24 02:27:22 +02:00
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
item_value_ref = "value_" + item.type.lower()
2020-01-07 22:00:45 +01:00
custom_icon = PROP_TYPE_ICONS[item.type]
2020-01-08 16:37:48 +01:00
sp = layout.split(factor=0.3)
2020-01-07 22:00:45 +01:00
sp.label(text=item.type, icon=custom_icon)
sp = sp.split(factor=0.6)
sp.label(text=item.name)
2017-07-24 02:27:22 +02:00
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
2020-01-07 22:00:45 +01:00
if item.type.endswith("Object"):
sp.prop_search(item, "value_object", context.scene, "objects", text="", icon=custom_icon)
2020-01-07 21:09:39 +01:00
else:
2020-01-08 16:37:48 +01:00
use_emboss = item.type in ("Bool", "String")
2020-01-07 21:09:39 +01:00
sp.prop(item, item_value_ref, text="", emboss=use_emboss)
2017-07-24 02:27:22 +02:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
2017-07-24 02:27:22 +02:00
def register():
bpy.utils.register_class(ArmTraitPropWarning)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmTraitPropListItem)
bpy.utils.register_class(ARM_UL_PropList)
2017-07-24 02:27:22 +02:00
2017-07-24 02:27:22 +02:00
def unregister():
bpy.utils.unregister_class(ARM_UL_PropList)
bpy.utils.unregister_class(ArmTraitPropListItem)
bpy.utils.unregister_class(ArmTraitPropWarning)