BrendanParmer / NodeToPython

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

Stop creating new node group if exist #70

Open fmnijk opened 10 months ago

fmnijk commented 10 months ago
file.write((f"{inner}if {str_to_py_str(nt_name)} in bpy.data.node_groups:\n"))
file.write((f"{inner}\treturn\n"))
file.write((f"{inner}else:\n"))
file.write((f"{inner}\t{nt_var}"
        f"= bpy.data.node_groups.new("
        f"type = \'ShaderNodeTree\', "
        f"name = {str_to_py_str(nt_name)})\n"))
file.write("\n")

Something like this I think.

BrendanParmer commented 10 months ago

I like the idea; I think it's important to have a way to not clutter up your node groups.

I think the trouble is relying solely on the name to tell if a node tree is already in the scene. Let's say we've got a tree "Geometry Nodes" doing x that we convert to a Python add-on. But if a user has a different tree "Geometry Nodes" doing y already in their project before running the add-on, then the add-on won't generate the intended x tree. There'd need to be a check to compare the nodes of the x group with the y.

At some point I'd like to have that node tree comparing function anyways; it'd certainly make testing NodeToPython a lot easier and more robust. Can't say I've looked into it too much yet, but if anyone knows a quick way I'm open to implementing this.

In absence of that comparison function, I'm open to some sort of "Try to reuse node groups" checkbox you could set for the generated add-on's operator, which would do just the name check.

fmnijk commented 10 months ago

Currently it will use exist node group but still add a new node group. So the code I provided is a quick fix.

I think a checkbox is a good idea. Currently I rename node groups before exporting if I don't want it to reuse exist node groups.