Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ 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
* Materials
* 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.
38 changes: 27 additions & 11 deletions node_to_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down