Skip to content

Commit

Permalink
fix centre on selection and add test for that node
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston committed Sep 19, 2024
1 parent fb49c99 commit 447ed6d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Binary file modified molecularnodes/assets/MN_data_file_4.2.blend
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def main():
python = os.path.realpath(sys.executable)

commands = [
f"{python} -m pip install -r requirements.txt",
# f"{python} -m pip install -r requirements.txt",
# test
# f'{python} -m pip uninstall pytest-snapshot'
f"{python} -m pip install pytest pytest-cov syrupy",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_nodes_geometry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np

import molecularnodes as mn
from molecularnodes.blender import mesh, nodes

from .constants import data_dir


def test_centre_on_selection():
mol = mn.entities.fetch("1cd3", cache_dir=data_dir, style=None)

group = nodes.get_mod(mol.object).node_group = nodes.new_group()
link = group.links.new
node_centre = nodes.add_custom(group, "Centre on Selection")
node_chain_id = nodes.add_custom(group, "Chain ID", location=[-100, -100])

link(group.nodes["Group Input"].outputs[0], node_centre.inputs[0])
link(node_chain_id.outputs["chain_id"], node_centre.inputs["Group ID"])
link(node_centre.outputs[0], group.nodes["Group Output"].inputs[0])

old_pos = mol.named_attribute("position", evaluate=False)
new_pos = mol.named_attribute("position", evaluate=True)

assert not np.allclose(old_pos, new_pos)

chain_id = mol.named_attribute("chain_id")
chain_ids = np.unique(chain_id)

old_centres = [mesh.centre(old_pos[chain_id == x]) for x in chain_ids]
new_centres = [mesh.centre(new_pos[chain_id == x]) for x in chain_ids]

assert not np.allclose(old_centres, new_centres)
assert np.allclose(
new_centres, [np.array((0, 0, 0), dtype=float) for x in chain_ids], atol=1e-5
)

0 comments on commit 447ed6d

Please sign in to comment.