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
11 changes: 1 addition & 10 deletions geo_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
'NodeSocketInt',
'NodeSocketVector'}

#node input sockets that are messy to set default values for
dont_set_defaults = {'NodeSocketCollection',
'NodeSocketGeometry',
'NodeSocketMaterial',
'NodeSocketObject',
'NodeSocketTexture',
'NodeSocketVirtual'}

geo_node_settings = {
#attribute
"GeometryNodeAttributeStatistic" : ["data_type", "domain"],
Expand Down Expand Up @@ -317,8 +309,7 @@ def process_geo_nodes_group(node_tree, level):
elif node.bl_idname in curve_nodes:
curve_node_settings(node, file, inner, node_var)

set_input_defaults(node, dont_set_defaults, file, inner,
node_var, addon_dir)
set_input_defaults(node, file, inner, node_var, addon_dir)

set_parents(node_tree, file, inner, node_vars)
set_locations(node_tree, file, inner, node_vars)
Expand Down
12 changes: 1 addition & 11 deletions materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@

from .utils import *

#node input sockets that are messy to set default values for
dont_set_defaults = {'NodeSocketCollection',
'NodeSocketGeometry',
'NodeSocketMaterial',
'NodeSocketObject',
'NodeSocketShader',
'NodeSocketTexture',
'NodeSocketVirtual'}

node_settings = {
#input
"ShaderNodeAmbientOcclusion" : ["samples", "inside", "only_local"],
Expand Down Expand Up @@ -188,8 +179,7 @@ def process_mat_node_group(node_tree, level):
elif node.bl_idname in curve_nodes:
curve_node_settings(node, file, inner, node_var)

set_input_defaults(node, dont_set_defaults, file, inner,
node_var, addon_dir)
set_input_defaults(node, file, inner, node_var, addon_dir)
set_parents(node_tree, file, inner, node_vars)
set_locations(node_tree, file, inner, node_vars)
set_dimensions(node_tree, file, inner, node_vars)
Expand Down
57 changes: 53 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

image_dir_name = "imgs"

#node input sockets that are messy to set default values for
dont_set_defaults = {'NodeSocketGeometry',
'NodeSocketShader',
'NodeSocketVirtual'}

def clean_string(string: str, lower: bool = True) -> str:
"""
Cleans up a string for use as a variable or file name
Expand Down Expand Up @@ -325,15 +330,13 @@ def curve_node_settings(node, file: TextIO, inner: str, node_var: str):
file.write(f"{inner}#update curve after changes\n")
file.write(f"{mapping}.update()\n")

def set_input_defaults(node, dont_set_defaults: set, file: TextIO, inner: str,
node_var: str, addon_dir: str):
def set_input_defaults(node, file: TextIO, inner: str, node_var: str,
addon_dir: str):
"""
Sets defaults for input sockets

Parameters:
node (bpy.types.Node): node we're setting inputs for
dont_set_defaults (set): set of sockets we shouldn't attempt to set
default values for
file (TextIO): file we're generating the add-on into
inner (str): indentation
node_var (str): variable name we're using for the copied node
Expand All @@ -346,19 +349,48 @@ def set_input_defaults(node, dont_set_defaults: set, file: TextIO, inner: str,
for i, input in enumerate(node.inputs):
if input.bl_idname not in dont_set_defaults and not input.is_linked:
socket_var = f"{node_var}.inputs[{i}]"

#colors
if input.bl_idname == 'NodeSocketColor':
default_val = vec4_to_py_str(input.default_value)

#vector types
elif "Vector" in input.bl_idname:
default_val = vec3_to_py_str(input.default_value)

#strings
elif input.bl_idname == 'NodeSocketString':
default_val = str_to_py_str(input.default_value)

#images
elif input.bl_idname == 'NodeSocketImage':
print("Input is linked: ", input.is_linked)
img = input.default_value
if img is not None:
save_image(img, addon_dir)
load_image(img, file, inner, f"{socket_var}.default_value")
default_val = None

#materials
elif input.bl_idname == 'NodeSocketMaterial':
in_file_inputs(input, file, inner, socket_var, "materials")
default_val = None

#collections
elif input.bl_idname == 'NodeSocketCollection':
in_file_inputs(input, file, inner, socket_var, "collections")
default_val = None

#objects
elif input.bl_idname == 'NodeSocketObject':
in_file_inputs(input, file, inner, socket_var, "objects")
default_val = None

#textures
elif input.bl_idname == 'NodeSocketTexture':
in_file_inputs(input, file, inner, socket_var, "textures")
default_val = None

else:
default_val = input.default_value
if default_val is not None:
Expand All @@ -367,6 +399,23 @@ def set_input_defaults(node, dont_set_defaults: set, file: TextIO, inner: str,
f" = {default_val}\n"))
file.write("\n")

def in_file_inputs(input, file: TextIO, inner: str, socket_var: str, type: str):
"""
Sets inputs for a node input if one already exists in the blend file

Parameters:
input (bpy.types.NodeSocket): input socket we're setting the value for
file (TextIO): file we're writing the add-on into
inner (str): indentation string
socket_var (str): variable name we're using for the socket
type (str): from what section of bpy.data to pull the default value from
"""

name = str_to_py_str(input.default_value.name)
file.write(f"{inner}if {name} in bpy.data.{type}:\n")
file.write((f"{inner}\t{socket_var}.default_value = "
f"bpy.data.{type}[{name}]\n"))

def set_parents(node_tree, file: TextIO, inner: str, node_vars: dict):
"""
Sets parents for all nodes, mostly used to put nodes in frames
Expand Down