2025-08-20 17:07:03 -06:00
|
|
|
import bpy
|
|
|
|
|
import os
|
|
|
|
|
from bpy.types import Operator
|
|
|
|
|
from bpy.props import StringProperty, BoolProperty, EnumProperty
|
|
|
|
|
|
|
|
|
|
class DYNAMICLINK_OT_replace_linked_asset(Operator):
|
|
|
|
|
"""Replace a linked asset with a new file"""
|
|
|
|
|
bl_idname = "dynamiclink.replace_linked_asset"
|
|
|
|
|
bl_label = "Replace Linked Asset"
|
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
|
|
|
|
|
filepath: StringProperty(
|
|
|
|
|
name="File Path",
|
|
|
|
|
description="Path to the new asset file",
|
|
|
|
|
subtype='FILE_PATH'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
|
obj = context.active_object
|
|
|
|
|
if not obj:
|
|
|
|
|
self.report({'ERROR'}, "No object selected")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
2025-08-20 17:28:21 -06:00
|
|
|
# Comprehensive debug info
|
|
|
|
|
debug_info = f"Object: {obj.name}, Type: {obj.type}"
|
|
|
|
|
|
|
|
|
|
# Check object library
|
|
|
|
|
if hasattr(obj, 'library'):
|
|
|
|
|
debug_info += f", Object has library attr: {obj.library is not None}"
|
|
|
|
|
if obj.library:
|
|
|
|
|
debug_info += f", Object library: {obj.library.filepath}"
|
|
|
|
|
|
|
|
|
|
# Check object data
|
|
|
|
|
if obj.data:
|
|
|
|
|
debug_info += f", Has data: {type(obj.data).__name__}, Name: {obj.data.name}"
|
|
|
|
|
|
|
|
|
|
# Check data library attribute
|
|
|
|
|
if hasattr(obj.data, 'library'):
|
|
|
|
|
debug_info += f", Data.library exists: {obj.data.library is not None}"
|
|
|
|
|
if obj.data.library:
|
|
|
|
|
debug_info += f", Data.library.filepath: {obj.data.library.filepath}"
|
|
|
|
|
|
|
|
|
|
# Check if data is in bpy.data collections
|
|
|
|
|
if obj.type == 'ARMATURE' and obj.data.name in bpy.data.armatures:
|
|
|
|
|
armature_data = bpy.data.armatures[obj.data.name]
|
|
|
|
|
debug_info += f", Found in bpy.data.armatures"
|
|
|
|
|
if hasattr(armature_data, 'library'):
|
|
|
|
|
debug_info += f", bpy.data library: {armature_data.library is not None}"
|
|
|
|
|
if armature_data.library:
|
|
|
|
|
debug_info += f", bpy.data library path: {armature_data.library.filepath}"
|
|
|
|
|
|
|
|
|
|
# Check if data is in bpy.data.objects
|
|
|
|
|
if obj.data.name in bpy.data.objects:
|
|
|
|
|
debug_info += f", Data also in bpy.data.objects"
|
2025-08-20 17:07:03 -06:00
|
|
|
|
2025-08-20 17:28:21 -06:00
|
|
|
# Check if object itself is linked
|
|
|
|
|
if hasattr(obj, 'library') and obj.library:
|
|
|
|
|
self.report({'INFO'}, f"Object '{obj.name}' is linked from: {obj.library.filepath}")
|
2025-08-20 17:07:03 -06:00
|
|
|
return {'FINISHED'}
|
2025-08-20 17:28:21 -06:00
|
|
|
|
|
|
|
|
# Check if object's data is linked
|
|
|
|
|
if obj.data and hasattr(obj.data, 'library') and obj.data.library:
|
|
|
|
|
self.report({'INFO'}, f"Object '{obj.name}' has linked data from: {obj.data.library.filepath}")
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
# Check if armature data is linked through bpy.data system
|
|
|
|
|
if obj.type == 'ARMATURE' and obj.data and obj.data.name in bpy.data.armatures:
|
|
|
|
|
armature_data = bpy.data.armatures[obj.data.name]
|
|
|
|
|
if hasattr(armature_data, 'library') and armature_data.library:
|
|
|
|
|
self.report({'INFO'}, f"Armature '{obj.name}' data is linked from: {armature_data.library.filepath}")
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
# If we get here, show debug info
|
|
|
|
|
self.report({'WARNING'}, debug_info)
|
|
|
|
|
self.report({'ERROR'}, "Selected object is not a linked asset")
|
|
|
|
|
return {'CANCELLED'}
|
2025-08-20 17:07:03 -06:00
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
|
context.window_manager.fileselect_add(self)
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
|
|
class DYNAMICLINK_OT_scan_linked_assets(Operator):
|
|
|
|
|
"""Scan scene for all linked assets"""
|
|
|
|
|
bl_idname = "dynamiclink.scan_linked_assets"
|
|
|
|
|
bl_label = "Scan Linked Assets"
|
|
|
|
|
|
|
|
|
|
def execute(self, context):
|
2025-08-21 17:18:54 -06:00
|
|
|
# Clear previous results
|
|
|
|
|
context.scene.dynamic_link_manager.linked_libraries.clear()
|
|
|
|
|
|
|
|
|
|
# Dictionary to store library info with hierarchy
|
|
|
|
|
library_info = {}
|
|
|
|
|
|
|
|
|
|
# Function to check if file exists
|
|
|
|
|
def is_file_missing(filepath):
|
|
|
|
|
if not filepath:
|
|
|
|
|
return True
|
|
|
|
|
# Convert relative paths to absolute
|
|
|
|
|
if filepath.startswith('//'):
|
|
|
|
|
# This is a relative path, we can't easily check if it exists
|
|
|
|
|
# So we'll assume it's missing if it's relative
|
|
|
|
|
return True
|
|
|
|
|
return not os.path.exists(filepath)
|
|
|
|
|
|
|
|
|
|
# Function to get library name from path
|
|
|
|
|
def get_library_name(filepath):
|
|
|
|
|
if not filepath:
|
|
|
|
|
return "Unknown"
|
|
|
|
|
return os.path.basename(filepath)
|
|
|
|
|
|
|
|
|
|
# Scan all data collections for linked items
|
2025-08-20 17:28:21 -06:00
|
|
|
all_libraries = set()
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.objects
|
2025-08-20 17:12:21 -06:00
|
|
|
for obj in bpy.data.objects:
|
2025-08-20 17:28:21 -06:00
|
|
|
if hasattr(obj, 'library') and obj.library:
|
|
|
|
|
all_libraries.add(obj.library.filepath)
|
|
|
|
|
if obj.data and hasattr(obj.data, 'library') and obj.data.library:
|
|
|
|
|
all_libraries.add(obj.data.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.armatures specifically
|
|
|
|
|
for armature in bpy.data.armatures:
|
|
|
|
|
if hasattr(armature, 'library') and armature.library:
|
|
|
|
|
all_libraries.add(armature.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.meshes
|
|
|
|
|
for mesh in bpy.data.meshes:
|
|
|
|
|
if hasattr(mesh, 'library') and mesh.library:
|
|
|
|
|
all_libraries.add(mesh.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.materials
|
|
|
|
|
for material in bpy.data.materials:
|
|
|
|
|
if hasattr(material, 'library') and material.library:
|
|
|
|
|
all_libraries.add(material.library.filepath)
|
2025-08-20 17:07:03 -06:00
|
|
|
|
2025-08-21 17:18:54 -06:00
|
|
|
# Check bpy.data.images
|
|
|
|
|
for image in bpy.data.images:
|
|
|
|
|
if hasattr(image, 'library') and image.library:
|
|
|
|
|
all_libraries.add(image.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.textures
|
|
|
|
|
for texture in bpy.data.textures:
|
|
|
|
|
if hasattr(texture, 'library') and texture.library:
|
|
|
|
|
all_libraries.add(texture.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Check bpy.data.node_groups
|
|
|
|
|
for node_group in bpy.data.node_groups:
|
|
|
|
|
if hasattr(node_group, 'library') and node_group.library:
|
|
|
|
|
all_libraries.add(node_group.library.filepath)
|
|
|
|
|
|
|
|
|
|
# Now try to detect indirect links by parsing the linked files
|
|
|
|
|
# This is a simplified approach - in practice, you'd need to parse .blend files
|
|
|
|
|
indirect_libraries = set()
|
|
|
|
|
|
|
|
|
|
# For now, we'll just note that some libraries might have indirect links
|
|
|
|
|
# In a full implementation, you'd parse each .blend file to find its linked libraries
|
|
|
|
|
|
2025-08-20 17:07:03 -06:00
|
|
|
# Store results in scene properties
|
2025-08-20 17:28:21 -06:00
|
|
|
context.scene.dynamic_link_manager.linked_assets_count = len(all_libraries)
|
|
|
|
|
|
2025-08-21 17:18:54 -06:00
|
|
|
# Create library items for the UI
|
|
|
|
|
for filepath in sorted(all_libraries):
|
|
|
|
|
if filepath:
|
|
|
|
|
lib_item = context.scene.dynamic_link_manager.linked_libraries.add()
|
|
|
|
|
lib_item.filepath = filepath
|
|
|
|
|
lib_item.name = get_library_name(filepath)
|
|
|
|
|
lib_item.is_missing = is_file_missing(filepath)
|
|
|
|
|
|
|
|
|
|
# Add linked datablocks info
|
|
|
|
|
for obj in bpy.data.objects:
|
|
|
|
|
if hasattr(obj, 'library') and obj.library and obj.library.filepath == filepath:
|
|
|
|
|
db_item = lib_item.linked_datablocks.add()
|
|
|
|
|
db_item.name = obj.name
|
|
|
|
|
db_item.type = "OBJECT"
|
|
|
|
|
|
|
|
|
|
if obj.data and hasattr(obj.data, 'library') and obj.data.library and obj.data.library.filepath == filepath:
|
|
|
|
|
db_item = lib_item.linked_datablocks.add()
|
|
|
|
|
db_item.name = f"{obj.name} ({type(obj.data).__name__})"
|
|
|
|
|
db_item.type = "DATA"
|
|
|
|
|
|
|
|
|
|
# Check other data types
|
|
|
|
|
for armature in bpy.data.armatures:
|
|
|
|
|
if hasattr(armature, 'library') and armature.library and armature.library.filepath == filepath:
|
|
|
|
|
db_item = lib_item.linked_datablocks.add()
|
|
|
|
|
db_item.name = armature.name
|
|
|
|
|
db_item.type = "ARMATURE"
|
|
|
|
|
|
|
|
|
|
for mesh in bpy.data.meshes:
|
|
|
|
|
if hasattr(mesh, 'library') and mesh.library and mesh.library.filepath == filepath:
|
|
|
|
|
db_item = lib_item.linked_datablocks.add()
|
|
|
|
|
db_item.name = mesh.name
|
|
|
|
|
db_item.type = "MESH"
|
|
|
|
|
|
|
|
|
|
for material in bpy.data.materials:
|
|
|
|
|
if hasattr(material, 'library') and material.library and material.library.filepath == filepath:
|
|
|
|
|
db_item = lib_item.linked_datablocks.add()
|
|
|
|
|
db_item.name = material.name
|
|
|
|
|
db_item.type = "MATERIAL"
|
|
|
|
|
|
2025-08-20 17:28:21 -06:00
|
|
|
# Show detailed info
|
|
|
|
|
self.report({'INFO'}, f"Found {len(all_libraries)} unique linked library files")
|
|
|
|
|
if all_libraries:
|
|
|
|
|
for lib in sorted(all_libraries):
|
|
|
|
|
self.report({'INFO'}, f"Library: {lib}")
|
2025-08-20 17:07:03 -06:00
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
|
bpy.utils.register_class(DYNAMICLINK_OT_replace_linked_asset)
|
|
|
|
|
bpy.utils.register_class(DYNAMICLINK_OT_scan_linked_assets)
|
|
|
|
|
|
2025-08-21 17:18:54 -06:00
|
|
|
class DYNAMICLINK_OT_open_linked_file(Operator):
|
|
|
|
|
"""Open the linked file in a new Blender instance"""
|
|
|
|
|
bl_idname = "dynamiclink.open_linked_file"
|
|
|
|
|
bl_label = "Open Linked File"
|
|
|
|
|
bl_options = {'REGISTER'}
|
|
|
|
|
|
|
|
|
|
filepath: StringProperty(
|
|
|
|
|
name="File Path",
|
|
|
|
|
description="Path to the linked file",
|
|
|
|
|
default=""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
|
if not self.filepath:
|
|
|
|
|
self.report({'ERROR'}, "No file path specified")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
# Try to open the linked file in a new Blender instance
|
|
|
|
|
try:
|
|
|
|
|
# Use Blender's built-in file browser to open the file
|
|
|
|
|
bpy.ops.wm.path_open(filepath=self.filepath)
|
|
|
|
|
self.report({'INFO'}, f"Opening linked file: {self.filepath}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.report({'ERROR'}, f"Failed to open linked file: {e}")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
|
bpy.utils.register_class(DYNAMICLINK_OT_replace_linked_asset)
|
|
|
|
|
bpy.utils.register_class(DYNAMICLINK_OT_scan_linked_assets)
|
|
|
|
|
bpy.utils.register_class(DYNAMICLINK_OT_open_linked_file)
|
|
|
|
|
|
2025-08-20 17:07:03 -06:00
|
|
|
def unregister():
|
2025-08-21 17:18:54 -06:00
|
|
|
bpy.utils.unregister_class(DYNAMICLINK_OT_open_linked_file)
|
2025-08-20 17:07:03 -06:00
|
|
|
bpy.utils.unregister_class(DYNAMICLINK_OT_scan_linked_assets)
|
|
|
|
|
bpy.utils.unregister_class(DYNAMICLINK_OT_replace_linked_asset)
|