Cleanup: Move Blender utility functions from dumper.py to utils.py
				
					
				
			This commit is contained in:
		
							parent
							
								
									fc34669af7
								
							
						
					
					
						commit
						fb1caf9174
					
				
							
								
								
									
										26
									
								
								dumper.py
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								dumper.py
									
									
									
									
									
								
							| @ -8,15 +8,6 @@ import bpy | ||||
| from . import utils | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| def get_default(prop: bpy.types.Property): | ||||
|     """Get the default value of a Blender property""" | ||||
|     if getattr(prop, "is_array", False): | ||||
|         return list(prop.default_array) | ||||
|     elif hasattr(prop, "default"): | ||||
|         return prop.default | ||||
| 
 | ||||
| 
 | ||||
| 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""" | ||||
| 
 | ||||
| @ -53,13 +44,6 @@ def load_nodes(data, node_tree): | ||||
|     Dumper.pointers.clear() | ||||
| 
 | ||||
| 
 | ||||
| def set_attribute(bl_object, attr, value): | ||||
|     try: | ||||
|         setattr(bl_object, attr, value) | ||||
|     except Exception as e: | ||||
|         print(e) | ||||
| 
 | ||||
| 
 | ||||
| class Dumper: | ||||
|     pointers = {} | ||||
|     includes = [] | ||||
| @ -118,7 +102,7 @@ class Dumper: | ||||
|                     value = cls.pointers[value] | ||||
| 
 | ||||
|                 elif value is None: | ||||
|                     set_attribute(bl_object, key, value) | ||||
|                     utils.set_bl_attribute(bl_object, key, value) | ||||
| 
 | ||||
|                 else: | ||||
|                     bl_type = prop.fixed_type.bl_rna.type_recast() | ||||
| @ -137,14 +121,14 @@ class Dumper: | ||||
|                     value = attr | ||||
| 
 | ||||
|                 if not prop.is_readonly: | ||||
|                     set_attribute(bl_object, key, value) | ||||
|                     utils.set_bl_attribute(bl_object, key, value) | ||||
| 
 | ||||
|                 # Some coll needs a manual update like curve mapping | ||||
|                 if hasattr(attr, "update"): | ||||
|                     attr.update() | ||||
| 
 | ||||
|             elif not prop.is_readonly: | ||||
|                 set_attribute(bl_object, key, value) | ||||
|                 utils.set_bl_attribute(bl_object, key, value) | ||||
|                 continue | ||||
| 
 | ||||
|         # return bl_object | ||||
| @ -231,7 +215,7 @@ class PropCollection(Dumper): | ||||
|                 params = value["_new"] | ||||
|             else: | ||||
|                 params = { | ||||
|                     k: value.get(k, get_default(v)) | ||||
|                     k: value.get(k, utils.get_bl_default(v)) | ||||
|                     for k, v in new_func.parameters.items()[:-1] | ||||
|                 } | ||||
| 
 | ||||
| @ -531,7 +515,7 @@ class Points(PropCollection): | ||||
|     @classmethod | ||||
|     def load(cls, values, coll): | ||||
|         new_func = coll.bl_rna.functions["new"] | ||||
|         params = {k: get_default(v) + 1.1 for k, v in new_func.parameters.items()[:-1]} | ||||
|         params = {k: utils.get_bl_default(v) + 1.1 for k, v in new_func.parameters.items()[:-1]} | ||||
| 
 | ||||
|         # Match the same number of elements in collection | ||||
|         if len(values) > len(coll): | ||||
|  | ||||
							
								
								
									
										16
									
								
								utils.py
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								utils.py
									
									
									
									
									
								
							| @ -1,6 +1,22 @@ | ||||
| import bpy | ||||
| 
 | ||||
| 
 | ||||
| def all_subclasses(cls): | ||||
|     return set(cls.__subclasses__()).union( | ||||
|         [s for c in cls.__subclasses__() for s in all_subclasses(c)] | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| def get_bl_default(prop: bpy.types.Property): | ||||
|     """Get the default value of a Blender property""" | ||||
|     if getattr(prop, "is_array", False): | ||||
|         return list(prop.default_array) | ||||
|     elif hasattr(prop, "default"): | ||||
|         return prop.default | ||||
| 
 | ||||
| 
 | ||||
| def set_bl_attribute(bl_object, attr, value): | ||||
|     try: | ||||
|         setattr(bl_object, attr, value) | ||||
|     except Exception as e: | ||||
|         print(e) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user