MRO based Dumper resolution system
This commit is contained in:
		
							parent
							
								
									e4ca202608
								
							
						
					
					
						commit
						94627debc6
					
				@ -1,3 +1,5 @@
 | 
			
		||||
from __future__ import annotations
 | 
			
		||||
 | 
			
		||||
import json
 | 
			
		||||
from copy import copy
 | 
			
		||||
from os.path import abspath
 | 
			
		||||
@ -5,6 +7,8 @@ from pprint import pprint
 | 
			
		||||
 | 
			
		||||
import bpy
 | 
			
		||||
 | 
			
		||||
from .. import utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
format_token = "#FMT:NODE_KIT#"
 | 
			
		||||
 | 
			
		||||
@ -29,14 +33,16 @@ def get_default(prop: bpy.types.Property):
 | 
			
		||||
        return prop.default
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_dumper(bl_object: bpy.types.bpy_struct):
 | 
			
		||||
    """Get the corresponding dumper for a given Blender object, or its closest base type"""
 | 
			
		||||
    for dp in dumpers:
 | 
			
		||||
        if isinstance(bl_object, dp.bl_type):  # TODO: Cascade system
 | 
			
		||||
            return dp
 | 
			
		||||
def get_dumper(bl_object: bpy.types.bpy_struct) -> type[Dumper]:
 | 
			
		||||
    """Get the corresponding dumper for a given Blender object, or its closest base type using its MRO"""
 | 
			
		||||
 | 
			
		||||
    return Dumper or None
 | 
			
		||||
    for cls in bl_object.__class__.mro():
 | 
			
		||||
        dumper_map = DumperRegistry().dumper_map
 | 
			
		||||
        if cls in dumper_map:
 | 
			
		||||
            return dumper_map[cls]
 | 
			
		||||
 | 
			
		||||
    # Fallback to base Dumper if no matches are found
 | 
			
		||||
    return Dumper
 | 
			
		||||
 | 
			
		||||
def get_current_node_tree(data):
 | 
			
		||||
    if data.get("_new", {}).get("type") == "GeometryNodeTree":
 | 
			
		||||
@ -780,31 +786,19 @@ class ViewLayers(PropCollection):
 | 
			
		||||
            Dumper.load(value, view_layer)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
dumpers = [
 | 
			
		||||
    CompositorNodeRLayers,
 | 
			
		||||
    CompositorNodeGlare,
 | 
			
		||||
    Node,
 | 
			
		||||
    NodeSocket,
 | 
			
		||||
    NodeTree,
 | 
			
		||||
    NodeLink,
 | 
			
		||||
    NodeTreeInterface,
 | 
			
		||||
    NodeTreeInterfaceSocket,
 | 
			
		||||
    NodeGeometryRepeatOutputItems,
 | 
			
		||||
    Image,
 | 
			
		||||
    Material,
 | 
			
		||||
    Object,
 | 
			
		||||
    Scene,
 | 
			
		||||
    Collection,
 | 
			
		||||
    ViewLayer,
 | 
			
		||||
    CurveMapPoints,
 | 
			
		||||
    ColorRampElements,
 | 
			
		||||
    NodeInputs,
 | 
			
		||||
    NodeOutputs,
 | 
			
		||||
    Nodes,
 | 
			
		||||
    ViewLayers,
 | 
			
		||||
    PropCollection,
 | 
			
		||||
    AOVs,
 | 
			
		||||
    PropArray,
 | 
			
		||||
    CompositorNodeOutputFileLayerSlots,
 | 
			
		||||
    CompositorNodeOutputFileFileSlots,
 | 
			
		||||
]
 | 
			
		||||
class DumperRegistry:
 | 
			
		||||
    """Singleton-like class that holds a map of all parsers, constructed on first instantiation"""
 | 
			
		||||
    dumper_map = None
 | 
			
		||||
    
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        if self.dumper_map is None:
 | 
			
		||||
            self.construct_dumper_map()
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def construct_dumper_map(cls):
 | 
			
		||||
        cls.dumper_map = {}
 | 
			
		||||
 | 
			
		||||
        for subclass in utils.all_subclasses(Dumper):
 | 
			
		||||
            assert hasattr(subclass, "bl_type")
 | 
			
		||||
            cls.dumper_map[subclass.bl_type] = subclass
 | 
			
		||||
            print(cls.dumper_map)
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user