diff --git a/README.md b/README.md index e55e67e..6e38bad 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,7 @@ Download `node_to_python.py`, and install it to Blender like other add-ons. Then * Automatically format code to be PEP8 compliant ## Potential Issues -* This should work on Unix-like systems (macOS, Linux), but I haven't tested it on Windows yet. If you use Windows, please let me know or create an issue. -* Make sure all your group inputs and outputs have different names, or it won't be able to find the appropriate sockets (this is best practice anyways!) +* This should work on Unix-like systems (macOS, Linux), but I haven't tested it on Windows yet. If you use Windows, please let me know if it does! * As of version 1.0.0, the add-on will not set default values for * Collections * Images @@ -41,4 +40,4 @@ Download `node_to_python.py`, and install it to Blender like other add-ons. Then * Objects * Textures - as they won't exist in every blend file. In the future, I may have the script automatically recreate these assets, espcially with materials. + as they won't exist in every blend file. In the future, I may have the script automatically recreate these assets, espcially with materials. \ No newline at end of file diff --git a/node_to_python.py b/node_to_python.py index b88d9b6..2dce5cb 100644 --- a/node_to_python.py +++ b/node_to_python.py @@ -12,8 +12,6 @@ """TODO: compositing node tree""" # https://blender.stackexchange.com/questions/62701/modify-nodes-in-compositing-nodetree-using-python -"""TODO: shader node tree""" -# bpy.data.materials["material name"] import bpy import os @@ -222,14 +220,14 @@ def process_node_group(node_group, level): process_node_group(node.node_tree, level + 1) elif node.bl_idname == 'NodeGroupInput': file.write(f"{inner}#{ng_name} inputs\n") - for input in node.outputs: + for i, input in enumerate(node.outputs): if input.bl_idname != "NodeSocketVirtual": file.write(f"{inner}#input {input.name}\n") file.write((f"{inner}{ng_name}.inputs.new" f"(\"{input.bl_idname}\", " f"\"{input.name}\")\n")) if input.bl_idname in default_sockets: - socket = node_group.inputs[input.name] + socket = node_group.inputs[i] if input.bl_idname == 'NodeSocketColor': col = socket.default_value r, g, b, a = col[0], col[1], col[2], col[3] @@ -242,17 +240,17 @@ def process_node_group(node_group, level): #default value file.write((f"{inner}{ng_name}" - f".inputs[\"{input.name}\"]" + f".inputs[{i}]" f".default_value = {dv}\n")) if input.bl_idname in value_sockets: #min value file.write((f"{inner}{ng_name}" - f".inputs[\"{input.name}\"]" + f".inputs[{i}]" f".min_value = " f"{socket.min_value}\n")) #max value file.write((f"{inner}{ng_name}" - f".inputs[\"{input.name}\"]" + f".inputs[{i}]" f".max_value = " f"{socket.max_value}\n")) file.write("\n") @@ -383,15 +381,33 @@ def process_node_group(node_group, level): for link in node_group.links: input_node = link.from_node.name.lower() input_node = input_node.replace(' ', '_').replace('.', '_') - input_socket = link.from_socket.name + input_socket = link.from_socket + + """ + Blender's socket dictionary doesn't guarantee + unique keys, which has caused much wailing and + gnashing of teeth. This is a quick fix that + doesn't run quick + """ + for i, item in enumerate(link.from_node.outputs.items()): + if item[1] == input_socket: + input_idx = i + break output_node = link.to_node.name.lower() output_node = output_node.replace(' ', '_').replace('.', '_') - output_socket = link.to_socket.name + output_socket = link.to_socket + + for i, item in enumerate(link.to_node.inputs.items()): + if item[1] == output_socket: + output_idx = i + break + file.write((f"{inner}#{input_node}.{input_socket.name} " + f"-> {output_node}.{output_socket.name}\n")) file.write((f"{inner}{ng_name}.links.new({input_node}" - f".outputs[\"{input_socket}\"], " - f"{output_node}.inputs[\"{output_socket}\"])\n")) + f".outputs[{input_idx}], " + f"{output_node}.inputs[{output_idx}])\n")) #create node group file.write("\n")