Skip to content

Commit

Permalink
fix and add test (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston authored Jan 8, 2025
1 parent 25ccf42 commit efa2cc8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions molecularnodes/blender/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ def create_starting_nodes_density(object, threshold=0.8, style="density_surface"
mod = get_mod(object)
node_name = f"MN_density_{object.name}"

try:
tree = bpy.data.node_groups[node_name]
mod.node_group = tree
return
except KeyError:
pass

# create a new GN node group, specific to this particular molecule
group = new_tree(node_name, fallback=False)
link = group.links.new
Expand Down Expand Up @@ -380,8 +387,14 @@ def create_starting_node_tree(
if not name:
name = f"MN_{object.name}"

# create a new GN node group, specific to this particular molecule
mod = get_mod(object)
# check if the node tree already exists and use that instead
try:
tree = bpy.data.node_groups[name]
mod.node_group = tree
return
except KeyError:
pass

tree = new_tree(name)
tree.is_modifier = is_modifier
link = tree.links.new
Expand Down
13 changes: 13 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,16 @@ def test_is_modifier():
assert not tree.is_modifier
mol = mn.entities.fetch("4ozs")
assert mol.tree.is_modifier


def test_reuse_node_group():
mol = mn.entities.fetch("4ozs")
tree = bpy.data.node_groups["MN_4ozs"]
n_nodes = len(tree.nodes)
bpy.data.objects.remove(mol.object)
del mol

assert n_nodes == len(tree.nodes)

mol = mn.fetch("4ozs")
assert n_nodes == len(tree.nodes)

0 comments on commit efa2cc8

Please sign in to comment.