Files
Dynamic-Link-Manager/ui.py

295 lines
12 KiB
Python
Raw Normal View History

2025-08-20 17:07:03 -06:00
import bpy
2025-08-22 12:25:05 -06:00
from bpy.types import Panel, PropertyGroup, AddonPreferences, UIList
2025-08-21 17:18:54 -06:00
from bpy.props import IntProperty, StringProperty, BoolProperty, CollectionProperty
2025-08-22 12:25:05 -06:00
# Properties for search paths
class SearchPathItem(PropertyGroup):
path: StringProperty(
name="Search Path",
description="Path to search for missing linked libraries",
subtype='DIR_PATH'
)
2025-08-21 17:18:54 -06:00
# Properties for individual linked datablocks
class LinkedDatablockItem(PropertyGroup):
name: StringProperty(name="Name", description="Name of the linked datablock")
type: StringProperty(name="Type", description="Type of the linked datablock")
2025-08-22 16:53:38 -06:00
# UIList for linked libraries
class DLM_UL_library_list(UIList):
2025-08-22 12:25:05 -06:00
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
custom_icon = 'FILE_BLEND'
if self.layout_type in {'DEFAULT', 'COMPACT'}:
2025-08-22 16:53:38 -06:00
# Library name and status
2025-08-22 12:25:05 -06:00
layout.scale_x = 0.4
layout.label(text=item.name)
# Status indicator
layout.scale_x = 0.3
if item.is_missing:
layout.label(text="MISSING", icon='ERROR')
elif item.is_indirect:
layout.label(text="INDIRECT", icon='INFO')
2025-08-22 12:25:05 -06:00
else:
layout.label(text="OK", icon='FILE_BLEND')
2025-08-22 14:38:00 -06:00
# File path (abbreviated)
2025-08-22 12:25:05 -06:00
layout.scale_x = 0.3
path_text = item.filepath
2025-08-22 14:38:00 -06:00
if path_text.startswith("\\\\"):
# Network path: show server name
parts = path_text.split("\\")
if len(parts) >= 3:
short_path = f"\\\\{parts[2]}\\"
else:
short_path = "\\\\ (network)"
elif len(path_text) >= 2 and path_text[1] == ':':
# Drive path: show drive letter
short_path = f"{path_text[:2]}\\"
elif path_text.startswith("//"):
# Relative path
short_path = "// (relative)"
else:
short_path = path_text[:15] + "..." if len(path_text) > 15 else path_text
layout.label(text=short_path, icon='FILE_FOLDER')
2025-08-22 12:25:05 -06:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon=custom_icon)
2025-08-21 17:18:54 -06:00
# Properties for a single linked library file
class LinkedLibraryItem(PropertyGroup):
filepath: StringProperty(name="File Path", description="Path to the linked .blend file")
name: StringProperty(name="Name", description="Name of the linked .blend file")
is_missing: BoolProperty(name="Missing", description="True if the linked file is not found")
is_indirect: BoolProperty(name="Is Indirect", description="True if this is an indirectly linked library")
2025-08-22 12:01:37 -06:00
is_expanded: BoolProperty(name="Expanded", description="Whether this library item is expanded in the UI", default=True)
2025-08-21 17:18:54 -06:00
linked_datablocks: CollectionProperty(type=LinkedDatablockItem, name="Linked Datablocks")
2025-08-20 17:07:03 -06:00
class DynamicLinkManagerProperties(PropertyGroup):
2025-08-21 17:18:54 -06:00
linked_libraries: CollectionProperty(type=LinkedLibraryItem, name="Linked Libraries")
2025-08-22 12:25:05 -06:00
linked_libraries_index: IntProperty(
name="Linked Libraries Index",
description="Index of the selected linked library",
default=0
)
2025-08-20 17:07:03 -06:00
linked_assets_count: IntProperty(
name="Linked Assets Count",
description="Number of linked assets found in scene",
default=0
)
2025-08-22 12:01:37 -06:00
linked_libraries_expanded: BoolProperty(
name="Linked Libraries Expanded",
description="Whether the linked libraries section is expanded",
default=True
)
2025-08-20 17:07:03 -06:00
selected_asset_path: StringProperty(
name="Selected Asset Path",
description="Path to the currently selected asset",
default=""
)
2025-08-22 12:25:05 -06:00
# Addon preferences for search paths
class DynamicLinkManagerPreferences(AddonPreferences):
bl_idname = __package__
search_paths: CollectionProperty(
type=SearchPathItem,
name="Search Paths",
description="Paths to search for missing linked libraries"
)
def draw(self, context):
layout = self.layout
2025-08-22 16:53:38 -06:00
# Search paths section
2025-08-22 12:25:05 -06:00
box = layout.box()
2025-08-22 13:25:41 -06:00
box.label(text="Default Search Paths for Missing Libraries")
2025-08-22 12:25:05 -06:00
2025-08-22 13:25:41 -06:00
# Add button - right-justified
2025-08-22 12:25:05 -06:00
row = box.row()
2025-08-22 13:25:41 -06:00
row.alignment = 'RIGHT'
row.operator("dlm.add_search_path", text="Add search path", icon='ADD')
2025-08-22 12:25:05 -06:00
2025-08-22 16:53:38 -06:00
# List search paths
2025-08-22 12:25:05 -06:00
for i, path_item in enumerate(self.search_paths):
row = box.row()
2025-08-22 13:25:41 -06:00
row.prop(path_item, "path", text=f"Search path {i+1}")
2025-08-22 16:53:38 -06:00
# Folder icon for browsing
2025-08-22 13:25:41 -06:00
row.operator("dlm.browse_search_path", text="", icon='FILE_FOLDER').index = i
2025-08-22 16:53:38 -06:00
# Remove button
2025-08-22 13:25:41 -06:00
row.operator("dlm.remove_search_path", text="", icon='REMOVE').index = i
2025-08-20 17:07:03 -06:00
2025-08-22 13:57:51 -06:00
class DLM_PT_main_panel(Panel):
2025-08-20 17:07:03 -06:00
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Dynamic Link Manager'
2025-08-22 13:57:51 -06:00
bl_label = "Dynamic Link Manager"
2025-08-22 14:38:00 -06:00
def get_short_path(self, filepath):
"""Extract a short, readable path from a full filepath"""
if not filepath:
return "Unknown"
# Handle relative paths
if filepath.startswith("//"):
return "// (relative)"
# Handle Windows network paths
if filepath.startswith("\\\\"):
# Extract server name (e.g., \\NAS\ or \\NEXUS\)
parts = filepath.split("\\")
if len(parts) >= 3:
return f"\\\\{parts[2]}\\"
return "\\\\ (network)"
# Handle Windows drive paths
if len(filepath) >= 2 and filepath[1] == ':':
return f"{filepath[:2]}\\"
# Handle Unix-style paths
if filepath.startswith("/"):
parts = filepath.split("/")
if len(parts) >= 2:
return f"/{parts[1]}/"
return "/ (root)"
# Fallback
return "Unknown"
2025-08-22 13:57:51 -06:00
def draw_header(self, context):
layout = self.layout
layout.operator("preferences.addon_show", text="", icon='PREFERENCES').module = __package__
2025-08-20 17:07:03 -06:00
def draw(self, context):
layout = self.layout
props = context.scene.dynamic_link_manager
# Path management buttons
row = layout.row()
row.operator("dlm.make_paths_relative", text="Make Paths Relative", icon='FILE_PARENT')
row.operator("dlm.make_paths_absolute", text="Make Paths Absolute", icon='FILE_FOLDER')
2025-08-22 14:23:35 -06:00
# Main scan section
2025-08-20 17:07:03 -06:00
box = layout.box()
2025-08-22 12:25:05 -06:00
box.label(text="Linked Libraries Analysis")
2025-08-20 17:07:03 -06:00
row = box.row()
2025-08-22 13:25:41 -06:00
row.operator("dlm.scan_linked_assets", text="Scan Linked Assets", icon='FILE_REFRESH')
2025-08-22 16:53:38 -06:00
# Show total count and missing count
missing_count = sum(1 for lib in props.linked_libraries if lib.is_missing)
if missing_count > 0:
row.label(text=f"({props.linked_assets_count} libraries, {missing_count} missing)", icon='ERROR')
else:
row.label(text=f"({props.linked_assets_count} libraries)")
2025-08-20 17:12:21 -06:00
# Show more detailed info if we have results
if props.linked_assets_count > 0:
2025-08-22 14:23:35 -06:00
# Linked Libraries section with single dropdown
2025-08-22 12:25:05 -06:00
row = box.row(align=True)
# Dropdown arrow for the entire section
icon = 'DISCLOSURE_TRI_DOWN' if props.linked_libraries_expanded else 'DISCLOSURE_TRI_RIGHT'
row.prop(props, "linked_libraries_expanded", text="", icon=icon, icon_only=True)
# Section header
row.label(text="Linked Libraries:")
row.label(text=f"({props.linked_assets_count} libraries)")
# Only show library details if section is expanded
if props.linked_libraries_expanded:
2025-08-22 14:23:35 -06:00
# Compact list view
2025-08-22 12:25:05 -06:00
row = box.row()
2025-08-22 16:53:38 -06:00
row.template_list("DLM_UL_library_list", "", props, "linked_libraries", props, "linked_libraries_index")
2025-08-21 17:18:54 -06:00
2025-08-22 14:23:35 -06:00
# Action buttons below the list
2025-08-22 12:25:05 -06:00
row = box.row()
2025-08-22 14:23:35 -06:00
row.operator("dlm.reload_libraries", text="Reload Libraries", icon='FILE_REFRESH')
2025-08-22 12:25:05 -06:00
2025-08-22 14:23:35 -06:00
# Search Paths Management - integrated into Linked Libraries Analysis
2025-08-22 13:25:41 -06:00
if props.linked_assets_count > 0:
# Get preferences for search paths
prefs = context.preferences.addons.get(__package__)
2025-08-22 14:23:35 -06:00
# Search paths list - Each path gets its own row with folder icon
2025-08-22 13:25:41 -06:00
if prefs and prefs.preferences.search_paths:
for i, path_item in enumerate(prefs.preferences.search_paths):
row = box.row()
row.prop(path_item, "path", text=f"Search path {i+1}")
2025-08-22 14:23:35 -06:00
# Folder icon for browsing
2025-08-22 13:25:41 -06:00
row.operator("dlm.browse_search_path", text="", icon='FILE_FOLDER').index = i
2025-08-22 14:23:35 -06:00
# Remove button
2025-08-22 13:25:41 -06:00
row.operator("dlm.remove_search_path", text="", icon='REMOVE').index = i
2025-08-22 14:23:35 -06:00
# Add button - Just the + button, right-justified
2025-08-22 13:25:41 -06:00
row = box.row()
row.alignment = 'RIGHT'
row.operator("dlm.add_search_path", text="Add search path", icon='ADD')
2025-08-22 13:25:41 -06:00
2025-08-22 14:32:29 -06:00
# Main action button
2025-08-22 13:25:41 -06:00
if missing_count > 0:
row = box.row()
2025-08-22 16:53:38 -06:00
row.operator("dlm.find_libraries_in_folders", text="Find libraries in these folders", icon='VIEWZOOM')
2025-08-22 14:32:29 -06:00
# Show details of selected item at the bottom
if props.linked_libraries and 0 <= props.linked_libraries_index < len(props.linked_libraries):
selected_lib = props.linked_libraries[props.linked_libraries_index]
info_box = box.box()
2025-08-22 14:40:18 -06:00
# Make entire box red if library is missing
if selected_lib.is_missing:
2025-08-22 14:40:18 -06:00
info_box.alert = True
2025-08-22 14:32:29 -06:00
info_box.label(text=f"Selected: {selected_lib.name}")
if selected_lib.is_missing:
2025-08-22 14:40:18 -06:00
row = info_box.row()
row.label(text="Status: MISSING", icon='ERROR')
elif selected_lib.is_indirect:
2025-08-22 14:40:18 -06:00
row = info_box.row()
row.label(text="Status: INDIRECT", icon='INFO')
2025-08-22 14:32:29 -06:00
else:
2025-08-22 14:40:18 -06:00
row = info_box.row()
row.label(text="Status: OK", icon='FILE_BLEND')
2025-08-22 14:32:29 -06:00
2025-08-22 14:38:00 -06:00
# Show full path and Open Blend button
row = info_box.row()
row.label(text=f"Path: {selected_lib.filepath}", icon='FILE_FOLDER')
2025-08-22 14:32:29 -06:00
row = info_box.row()
row.operator("dlm.open_linked_file", text="Open Blend", icon='FILE_BLEND').filepath = selected_lib.filepath
2025-08-22 16:53:38 -06:00
# Relocate (context-free): lets the user pick a new .blend and we reload the library
row = info_box.row()
op = row.operator("dlm.relocate_single_library", text="Relocate Library", icon='FILE_FOLDER')
op.target_filepath = selected_lib.filepath
2025-08-22 14:32:29 -06:00
2025-08-20 17:07:03 -06:00
def register():
2025-08-22 12:25:05 -06:00
bpy.utils.register_class(SearchPathItem)
2025-08-21 17:18:54 -06:00
bpy.utils.register_class(LinkedDatablockItem)
2025-08-22 16:53:38 -06:00
bpy.utils.register_class(DLM_UL_library_list)
2025-08-21 17:18:54 -06:00
bpy.utils.register_class(LinkedLibraryItem)
2025-08-20 17:07:03 -06:00
bpy.utils.register_class(DynamicLinkManagerProperties)
2025-08-22 12:25:05 -06:00
bpy.utils.register_class(DynamicLinkManagerPreferences)
2025-08-22 13:57:51 -06:00
bpy.utils.register_class(DLM_PT_main_panel)
2025-08-20 17:07:03 -06:00
# Register properties to scene
bpy.types.Scene.dynamic_link_manager = bpy.props.PointerProperty(type=DynamicLinkManagerProperties)
def unregister():
# Unregister properties from scene
del bpy.types.Scene.dynamic_link_manager
2025-08-22 13:57:51 -06:00
bpy.utils.unregister_class(DLM_PT_main_panel)
2025-08-22 12:25:05 -06:00
bpy.utils.unregister_class(DynamicLinkManagerPreferences)
2025-08-20 17:07:03 -06:00
bpy.utils.unregister_class(DynamicLinkManagerProperties)
2025-08-21 17:18:54 -06:00
bpy.utils.unregister_class(LinkedLibraryItem)
2025-08-22 16:53:38 -06:00
bpy.utils.unregister_class(DLM_UL_library_list)
2025-08-21 17:18:54 -06:00
bpy.utils.unregister_class(LinkedDatablockItem)
2025-08-22 12:25:05 -06:00
bpy.utils.unregister_class(SearchPathItem)