refactor: clean material

This commit is contained in:
Swann
2020-03-26 18:51:35 +01:00
parent cab4a8876b
commit d7e47e5c14
2 changed files with 13 additions and 43 deletions

View File

@ -25,55 +25,25 @@ from ..libs import dump_anything
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def clean_color_ramp(target_ramp):
# clear existing
try:
for key in target_ramp.elements:
target_ramp.elements.remove(key)
except:
pass
def load_mapping(target_apping, source_mapping): def load_node(node_data, node_tree):
# clear existing curves """ Load a node into a node_tree from a dict
for curve in target_apping.curves:
for point in curve.points:
try:
curve.remove(point)
except:
continue
# Load curves :arg node_data: dumped node data
for curve in source_mapping['curves']: :type node_data: dict
for point in source_mapping['curves'][curve]['points']: :arg node_tree: target node_tree
pos = source_mapping['curves'][curve]['points'][point]['location'] :type node_tree: bpy.types.NodeTree
target_apping.curves[curve].points.new(pos[0],pos[1]) """
target_node = node_tree.nodes.new(type=node_data["bl_idname"])
dump_anything.load(target_node, node_data)
def load_node(target_node_tree, source):
target_node = target_node_tree.nodes.get(source["name"])
if target_node is None: for input in node_data["inputs"]:
node_type = source["bl_idname"]
target_node = target_node_tree.nodes.new(type=node_type)
# Clean color ramp before loading it
if source['type'] == 'VALTORGB':
clean_color_ramp(target_node.color_ramp)
if source['type'] == 'CURVE_RGB':
load_mapping(target_node.mapping, source['mapping'])
dump_anything.load(
target_node,
source)
if source['type'] == 'TEX_IMAGE':
target_node.image = bpy.data.images[source['image']]
for input in source["inputs"]:
if hasattr(target_node.inputs[input], "default_value"): if hasattr(target_node.inputs[input], "default_value"):
try: try:
target_node.inputs[input].default_value = source["inputs"][input]["default_value"] target_node.inputs[input].default_value = node_data["inputs"][input]["default_value"]
except: except:
logger.error("{} not supported, skipping".format(input)) logger.error("{} not supported, skipping".format(input))
@ -144,7 +114,7 @@ class BlMaterial(BlDatablock):
# Load nodes # Load nodes
for node in data["node_tree"]["nodes"]: for node in data["node_tree"]["nodes"]:
load_node(target.node_tree, data["node_tree"]["nodes"][node]) load_node(data["node_tree"]["nodes"][node], target.node_tree)
# Load nodes links # Load nodes links
target.node_tree.links.clear() target.node_tree.links.clear()

View File

@ -43,7 +43,7 @@ class BlWorld(BlDatablock):
target.node_tree.nodes.clear() target.node_tree.nodes.clear()
for node in data["node_tree"]["nodes"]: for node in data["node_tree"]["nodes"]:
load_node(target.node_tree, data["node_tree"]["nodes"][node]) load_node(data["node_tree"]["nodes"][node], target.node_tree)
# Load nodes links # Load nodes links
target.node_tree.links.clear() target.node_tree.links.clear()