Skip to content

Commit f7e16bd

Browse files
committed
fix: nodes with multiple sockets with the same name no longer cause issues
1 parent 92a5b6d commit f7e16bd

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ Download `node_to_python.py`, and install it to Blender like other add-ons. Then
3232
* Automatically format code to be PEP8 compliant
3333

3434
## Potential Issues
35-
* 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.
36-
* 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!)
35+
* 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!
3736
* As of version 1.0.0, the add-on will not set default values for
3837
* Collections
3938
* Images
4039
* Materials
4140
* Objects
4241
* Textures
4342

44-
as they won't exist in every blend file. In the future, I may have the script automatically recreate these assets, espcially with materials.
43+
as they won't exist in every blend file. In the future, I may have the script automatically recreate these assets, espcially with materials.

node_to_python.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
"""TODO: compositing node tree"""
1313
# https://blender.stackexchange.com/questions/62701/modify-nodes-in-compositing-nodetree-using-python
1414

15-
"""TODO: shader node tree"""
16-
# bpy.data.materials["material name"]
1715
import bpy
1816
import os
1917

@@ -222,14 +220,14 @@ def process_node_group(node_group, level):
222220
process_node_group(node.node_tree, level + 1)
223221
elif node.bl_idname == 'NodeGroupInput':
224222
file.write(f"{inner}#{ng_name} inputs\n")
225-
for input in node.outputs:
223+
for i, input in enumerate(node.outputs):
226224
if input.bl_idname != "NodeSocketVirtual":
227225
file.write(f"{inner}#input {input.name}\n")
228226
file.write((f"{inner}{ng_name}.inputs.new"
229227
f"(\"{input.bl_idname}\", "
230228
f"\"{input.name}\")\n"))
231229
if input.bl_idname in default_sockets:
232-
socket = node_group.inputs[input.name]
230+
socket = node_group.inputs[i]
233231
if input.bl_idname == 'NodeSocketColor':
234232
col = socket.default_value
235233
r, g, b, a = col[0], col[1], col[2], col[3]
@@ -242,17 +240,17 @@ def process_node_group(node_group, level):
242240

243241
#default value
244242
file.write((f"{inner}{ng_name}"
245-
f".inputs[\"{input.name}\"]"
243+
f".inputs[{i}]"
246244
f".default_value = {dv}\n"))
247245
if input.bl_idname in value_sockets:
248246
#min value
249247
file.write((f"{inner}{ng_name}"
250-
f".inputs[\"{input.name}\"]"
248+
f".inputs[{i}]"
251249
f".min_value = "
252250
f"{socket.min_value}\n"))
253251
#max value
254252
file.write((f"{inner}{ng_name}"
255-
f".inputs[\"{input.name}\"]"
253+
f".inputs[{i}]"
256254
f".max_value = "
257255
f"{socket.max_value}\n"))
258256
file.write("\n")
@@ -383,15 +381,33 @@ def process_node_group(node_group, level):
383381
for link in node_group.links:
384382
input_node = link.from_node.name.lower()
385383
input_node = input_node.replace(' ', '_').replace('.', '_')
386-
input_socket = link.from_socket.name
384+
input_socket = link.from_socket
385+
386+
"""
387+
Blender's socket dictionary doesn't guarantee
388+
unique keys, which has caused much wailing and
389+
gnashing of teeth. This is a quick fix that
390+
doesn't run quick
391+
"""
392+
for i, item in enumerate(link.from_node.outputs.items()):
393+
if item[1] == input_socket:
394+
input_idx = i
395+
break
387396

388397
output_node = link.to_node.name.lower()
389398
output_node = output_node.replace(' ', '_').replace('.', '_')
390-
output_socket = link.to_socket.name
399+
output_socket = link.to_socket
400+
401+
for i, item in enumerate(link.to_node.inputs.items()):
402+
if item[1] == output_socket:
403+
output_idx = i
404+
break
391405

406+
file.write((f"{inner}#{input_node}.{input_socket.name} "
407+
f"-> {output_node}.{output_socket.name}\n"))
392408
file.write((f"{inner}{ng_name}.links.new({input_node}"
393-
f".outputs[\"{input_socket}\"], "
394-
f"{output_node}.inputs[\"{output_socket}\"])\n"))
409+
f".outputs[{input_idx}], "
410+
f"{output_node}.inputs[{output_idx}])\n"))
395411

396412
#create node group
397413
file.write("\n")

0 commit comments

Comments
 (0)