BrendanParmer / NodeToPython

Convert Blender node groups to a Python add-on
MIT License
236 stars 21 forks source link

How to Organize Generated Node to Python Script to Run #111

Closed gabe-broida closed 2 months ago

gabe-broida commented 3 months ago

Main Tree Geo Node Nested Geo Node Tree

I'm still new to coding but I'm trying to convert a test project to python code so that way I can generate a nested geonode tree within an existing geonode.

Test: Create a new geometry node tree, add another node, (any node will do), then group that node to create a nested node tree within the original geometry node named Main Geo Tree. I'm grateful that I found a way to generated the python script so I understand each line of code better, but I'm trying to figure out how to organize the code given below to allow the script to run. I'm trying to generate this within python to help my workflow a bit more rather than having to rebuild an entire geometry node tree from scratch.

Images above are of the main and sub node tree layouts. The code below is what NodeToPython generated for me based on my test nodes. If anyone can help me understand this or help solve this issue I'd greatly appreciate the help. Thank you in advance!

initialize joined_geo node group

def joined_geo_node_group(): joined_geo = bpy.data.node_groups.new(type = 'GeometryNodeTree', name = "Joined Geo")

#initialize joined_geo nodes
#joined_geo interface
#Socket Geometry
geometry_socket = joined_geo.interface.new_socket(name = "Geometry", in_out='OUTPUT', socket_type = 'NodeSocketGeometry')
geometry_socket.attribute_domain = 'POINT'

#Socket Geometry
geometry_socket_1 = joined_geo.interface.new_socket(name = "Geometry", in_out='INPUT', socket_type = 'NodeSocketGeometry')
geometry_socket_1.attribute_domain = 'POINT'

#node Group Output
group_output = joined_geo.nodes.new("NodeGroupOutput")
group_output.name = "Group Output"
group_output.is_active_output = True

#node Group Input
group_input = joined_geo.nodes.new("NodeGroupInput")
group_input.name = "Group Input"

#node Curve Line
curve_line = joined_geo.nodes.new("GeometryNodeCurvePrimitiveLine")
curve_line.name = "Curve Line"
curve_line.mode = 'POINTS'
#Start
curve_line.inputs[0].default_value = (0.0, 0.0, 0.0)
#End
curve_line.inputs[1].default_value = (0.0, 0.0, 1.0)
#Direction
curve_line.inputs[2].default_value = (0.0, 0.0, 1.0)
#Length
curve_line.inputs[3].default_value = 1.0

#node Join Geometry
join_geometry = joined_geo.nodes.new("GeometryNodeJoinGeometry")
join_geometry.name = "Join Geometry"

#Set locations
group_output.location = (297.0329895019531, 0.0)
group_input.location = (-307.03302001953125, 0.0)
curve_line.location = (-107.03300476074219, 51.27969741821289)
join_geometry.location = (107.03300476074219, -51.27969741821289)

#Set dimensions
group_output.width, group_output.height = 140.0, 100.0
group_input.width, group_input.height = 140.0, 100.0
curve_line.width, curve_line.height = 140.0, 100.0
join_geometry.width, join_geometry.height = 140.0, 100.0

#initialize joined_geo links
#group_input.Geometry -> join_geometry.Geometry
joined_geo.links.new(group_input.outputs[0], join_geometry.inputs[0])
#join_geometry.Geometry -> group_output.Geometry
joined_geo.links.new(join_geometry.outputs[0], group_output.inputs[0])
#curve_line.Curve -> join_geometry.Geometry
joined_geo.links.new(curve_line.outputs[0], join_geometry.inputs[0])
return joined_geo

joined_geo = joined_geo_node_group()

initialize main_geo_tree node group

def main_geo_tree_node_group(): main_geo_tree = bpy.data.node_groups.new(type = 'GeometryNodeTree', name = "Main Geo Tree")

main_geo_tree.is_modifier = True

#initialize main_geo_tree nodes
#main_geo_tree interface
#Socket Geometry
geometry_socket_2 = main_geo_tree.interface.new_socket(name = "Geometry", in_out='OUTPUT', socket_type = 'NodeSocketGeometry')
geometry_socket_2.attribute_domain = 'POINT'

#Socket Geometry
geometry_socket_3 = main_geo_tree.interface.new_socket(name = "Geometry", in_out='INPUT', socket_type = 'NodeSocketGeometry')
geometry_socket_3.attribute_domain = 'POINT'

#node Group Input
group_input_1 = main_geo_tree.nodes.new("NodeGroupInput")
group_input_1.name = "Group Input"

#node Group Output
group_output_1 = main_geo_tree.nodes.new("NodeGroupOutput")
group_output_1.name = "Group Output"
group_output_1.is_active_output = True

#node Group
group = main_geo_tree.nodes.new("GeometryNodeGroup")
group.name = "Group"
group.node_tree = joined_geo

#Set locations
group_input_1.location = (-340.0, 0.0)
group_output_1.location = (405.37823486328125, -59.09478759765625)
group.location = (39.848114013671875, -8.60012435913086)

#Set dimensions
group_input_1.width, group_input_1.height = 140.0, 100.0
group_output_1.width, group_output_1.height = 140.0, 100.0
group.width, group.height = 140.0, 100.0

#initialize main_geo_tree links
#group.Geometry -> group_output_1.Geometry
main_geo_tree.links.new(group.outputs[0], group_output_1.inputs[0])
#group_input_1.Geometry -> group.Geometry
main_geo_tree.links.new(group_input_1.outputs[0], group.inputs[0])
return main_geo_tree

main_geo_tree = main_geo_tree_node_group()

gabe-broida commented 3 months ago

I figured it out, nevermind! I love this addon so much! Thank you!!