vse_toolbox/properties.py

293 lines
8.7 KiB
Python
Raw Normal View History

2023-03-14 13:38:04 +01:00
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
import os
from bpy.props import (
BoolProperty,
CollectionProperty,
EnumProperty,
IntProperty,
PointerProperty,
StringProperty,
)
2023-03-16 18:32:17 +01:00
from bpy.types import PropertyGroup, UIList
2023-03-14 13:38:04 +01:00
from pprint import pprint as pp
2023-03-21 18:33:29 +01:00
from vse_toolbox.bl_utils import get_addon_prefs, get_scene_settings
2023-03-20 19:05:22 +01:00
from vse_toolbox.constants import ASSET_PREVIEWS, TRACKERS
2023-03-14 13:38:04 +01:00
from vse_toolbox.file_utils import norm_str
2023-03-17 20:03:38 +01:00
2023-03-14 13:38:04 +01:00
def get_episodes_items(self, context):
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-14 13:38:04 +01:00
project = settings.active_project
if not project:
return [('NONE', 'None', '', 0)]
episodes = project.episodes
if not episodes:
return [('NONE', 'None', '', 0)]
return [(e, e, '', i) for i, e in enumerate(episodes.keys())]
def get_project_items(self, context):
if not self.projects:
return [('NONE', 'None', '', 0)]
return [(p, p, '', i) for i, p in enumerate(self.projects.keys())]
def update_episodes(self, context):
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-14 13:38:04 +01:00
settings['episodes'] = 0
def get_tracker_items(self, context):
return [(norm_str(a.name, format=str.upper), a.name, "", i) for i, a in enumerate(TRACKERS)]
2023-03-17 20:03:38 +01:00
class Asset(PropertyGroup):
2023-03-20 19:05:22 +01:00
name : StringProperty(default='')
2023-03-17 20:03:38 +01:00
id : StringProperty(default='')
2023-03-17 22:54:48 +01:00
norm_name : StringProperty(default='')
2023-03-17 20:03:38 +01:00
asset_type : StringProperty(default='')
2023-03-20 19:05:22 +01:00
tracker_name : StringProperty(default='')
preview : StringProperty(default='')
2023-03-21 18:33:29 +01:00
@property
def label(self):
return f"{self.asset_type} / {self.norm_name}"
2023-03-17 20:03:38 +01:00
2023-03-21 18:33:29 +01:00
@property
def icon_id(self):
ico = ASSET_PREVIEWS.get(self.preview)
if ico:
return ico.icon_id
2023-03-17 20:03:38 +01:00
2023-03-21 10:22:17 +01:00
2023-03-21 18:33:29 +01:00
class AssetCasting(PropertyGroup):
id : StringProperty(default='')
2023-03-21 10:22:17 +01:00
instance : IntProperty(default=1)
2023-03-21 18:33:29 +01:00
def __iter__(self):
return (getattr(self, p) for p in self.bl_rna.properties.keys() if p not in ('rna_type', 'name'))
@property
def asset(self):
settings = get_scene_settings()
project = settings.active_project
return project.assets.get(self.id)
def to_dict(self):
return {k: v for k,v in self.items()}
2023-03-21 10:22:17 +01:00
2023-03-23 17:33:49 +01:00
class MetaDataTypes(PropertyGroup):
choices : EnumProperty(items=[('NONE', 'None', '', 0)])
class MetaData(PropertyGroup):
notes : StringProperty()
2023-03-24 14:57:06 +01:00
ambiance : StringProperty()
fx_compo : StringProperty()
2023-03-23 17:33:49 +01:00
def __iter__(self):
return (getattr(self, k) for k in self.keys())
def keys(self):
return (p for p in self.bl_rna.properties.keys() if p not in ('rna_type', 'name'))
2023-03-21 10:22:17 +01:00
class Episode(PropertyGroup):
id : StringProperty(default='')
@property
def active(self):
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-21 10:22:17 +01:00
return self.get(settings.project_name)
2023-03-17 20:03:38 +01:00
2023-03-14 13:38:04 +01:00
class Project(PropertyGroup):
id : StringProperty(default='')
sequence_increment : IntProperty(
name="Sequence Increment", default=10, min=0, step=10)
shot_increment : IntProperty(
name="Shot Increment", default=10, min=0, step=10)
sequence_template : StringProperty(
name="Sequence Name", default="sq{index:03d}")
episode_template : StringProperty(
name="Episode Name", default="e{index:03d}")
shot_template : StringProperty(
2023-03-17 22:54:48 +01:00
name="Shot Name", default="{episode}s{index:04d}")
2023-03-14 13:38:04 +01:00
episode_name : EnumProperty(items=get_episodes_items)
episodes : CollectionProperty(type=Episode)
2023-03-17 20:03:38 +01:00
assets : CollectionProperty(type=Asset)
2023-03-23 17:33:49 +01:00
metadata_types : CollectionProperty(type=MetaDataTypes)
2023-03-14 13:38:04 +01:00
2023-03-17 20:03:38 +01:00
class VSETB_UL_casting(UIList):
2023-03-16 18:32:17 +01:00
2023-03-20 19:05:22 +01:00
order_by_type : BoolProperty(default=False)
2023-03-16 18:32:17 +01:00
2023-03-17 20:03:38 +01:00
def draw_item(self, context, layout, data, item, icon, active_data,
active_propname, index):
2023-03-16 18:32:17 +01:00
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-17 20:03:38 +01:00
project = settings.active_project
2023-03-23 17:33:49 +01:00
2023-03-21 18:33:29 +01:00
asset = item.asset
if asset is None:
#TODO deal if asset was removed
2023-03-23 17:33:49 +01:00
layout.label(text='Load Assets')
return
2023-03-21 18:33:29 +01:00
icon_id = asset.icon_id
params = {'icon_value': icon_id} if icon_id else {'icon': 'BLANK1'}
2023-03-20 19:05:22 +01:00
2023-03-17 20:03:38 +01:00
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
2023-03-20 19:05:22 +01:00
layout.label(**params)
2023-03-21 10:22:17 +01:00
split = layout.split(factor=0.6)
2023-03-21 18:33:29 +01:00
split.label(text=f"{asset.norm_name.title()}")
split.label(text=f"{asset.asset_type.title()}")
2023-03-21 10:22:17 +01:00
split.prop(item, 'instance', text='')
2023-03-17 20:03:38 +01:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="")
2023-03-16 18:32:17 +01:00
2023-03-17 22:54:48 +01:00
def draw_filter(self, context, layout):
row = layout.row()
subrow = row.row(align=True)
subrow.prop(self, "filter_name", text="")
subrow.prop(self, "use_filter_invert", text="", icon='ARROW_LEFTRIGHT')
subrow.separator()
subrow.prop(self, "order_by_type", text="Order by Type", icon='MESH_DATA')
2023-03-16 18:32:17 +01:00
2023-03-17 22:54:48 +01:00
def filter_items(self, context, data, propname):
"""Filter and order items in the list."""
helper_funcs = bpy.types.UI_UL_list
filtered = []
ordered = []
items = getattr(data, propname)
# Filtering by name
if self.filter_name:
filtered = helper_funcs.filter_items_by_name(self.filter_name, self.bitflag_filter_item, items, "name",
reverse=self.use_filter_sort_alpha)
# Order by types
if self.order_by_type:
2023-03-21 18:33:29 +01:00
_sort = [(idx, casting_item) for idx, casting_item in enumerate(items)]
2023-03-17 22:54:48 +01:00
sort_items = helper_funcs.sort_items_helper
2023-03-21 18:33:29 +01:00
ordered = sort_items(_sort, lambda x: x[1].asset.label)
2023-03-17 22:54:48 +01:00
return filtered, ordered
2023-03-17 20:03:38 +01:00
2023-03-17 22:54:48 +01:00
2023-03-21 18:33:29 +01:00
class VSETB_PGT_scene_settings(PropertyGroup):
2023-03-14 13:38:04 +01:00
projects : CollectionProperty(type=Project)
project_name : EnumProperty(items=get_project_items, update=update_episodes)
tracker_name : EnumProperty(items=get_tracker_items)
2023-03-23 17:33:49 +01:00
toogle_prefs : BoolProperty(
description='Toogle VSE ToolBox Preferences', default=True)
2023-03-17 20:03:38 +01:00
auto_select_strip : BoolProperty(
name='Auto Select Strip',description='Auto select strip', default=True)
2023-03-23 17:33:49 +01:00
2023-03-16 18:32:17 +01:00
channel : EnumProperty(
2023-03-14 13:38:04 +01:00
items=[
('AUDIO', 'Audio', '', 0),
('MOVIE', 'Movie', '', 1),
('SHOTS', 'Shots', '', 2),
('SEQUENCES', 'Sequences', '', 3),
]
)
sequence_channel_name : StringProperty(
name="Sequences Channel Name", default="Sequences")
2023-03-23 17:33:49 +01:00
2023-03-14 13:38:04 +01:00
shot_channel_name : StringProperty(
name="Shot Channel Name", default="Shots")
@property
def active_project(self):
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-14 13:38:04 +01:00
return settings.projects.get(settings.project_name)
@property
def active_episode(self):
project = self.active_project
if project:
return project.episodes.get(project.episode_name)
2023-03-23 17:33:49 +01:00
"""
def load_metadata_types(self):
settings = get_scene_settings()
print('settings: ', settings)
for project in settings.projects:
print('project: ', project)
metadata_props = {'__annotations__': {key : StringProperty() for key in project.metadata_types.keys()}}
MetadataProps = type(f"{project.name}_MetaData", (PropertyGroup,), metadata_props)
bpy.utils.register_class(MetadataProps)
setattr(VSETB_PGT_strip_settings, 'metadata', PointerProperty(type=MetadataProps))
# if "__annotations__" not in MetaData.__dict__:
# setattr(MetaData, "__annotations__", {})
2023-03-21 18:33:29 +01:00
2023-03-23 17:33:49 +01:00
# for metadata_type in project.metadata_types:
# print('metadata_type: ', metadata_type)
# MetaData.__annotations__[metadata_type.name] = StringProperty()
# bpy.utils.unregister_class(MetaData)
# bpy.utils.register_class(MetaData)
"""
2023-03-14 13:38:04 +01:00
2023-03-21 18:33:29 +01:00
class VSETB_PGT_strip_settings(PropertyGroup):
casting : CollectionProperty(type=AssetCasting)
casting_index : IntProperty(name='Casting Index', default=0)
source_name : StringProperty(name='')
2023-03-23 17:33:49 +01:00
metadata : PointerProperty(type=MetaData)
2023-03-16 18:32:17 +01:00
2023-03-14 13:38:04 +01:00
classes=(
2023-03-17 20:03:38 +01:00
Asset,
2023-03-23 17:33:49 +01:00
AssetCasting,
2023-03-14 13:38:04 +01:00
Episode,
2023-03-23 17:33:49 +01:00
MetaData,
MetaDataTypes,
2023-03-14 13:38:04 +01:00
Project,
2023-03-17 20:03:38 +01:00
VSETB_UL_casting,
2023-03-21 18:33:29 +01:00
VSETB_PGT_scene_settings,
VSETB_PGT_strip_settings,
2023-03-14 13:38:04 +01:00
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
2023-03-21 18:33:29 +01:00
bpy.types.Scene.vsetb_settings = PointerProperty(type=VSETB_PGT_scene_settings)
bpy.types.Sequence.vsetb_strip_settings = PointerProperty(type=VSETB_PGT_strip_settings)
2023-03-23 17:33:49 +01:00
# load_metadata_types()
2023-03-14 13:38:04 +01:00
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
2023-03-21 18:33:29 +01:00
del bpy.types.Sequence.vsetb_strip_settings
del bpy.types.Scene.vsetb_settings