Skip to content

Commit

Permalink
Separate out all UI / add-on related code (#755)
Browse files Browse the repository at this point in the history
* initial refactor moving everything UI to it's own module

* more refactoring

* more refactoring

* more refactoring

* fix repeated tests of test_micrograph_loading

* re-export template

* fix doc building with assets

* fix final asset references

* fix add menu eppearing

* fix reloading trajectories
  • Loading branch information
BradyAJohnston authored Feb 27, 2025
1 parent 4840da3 commit 0276802
Show file tree
Hide file tree
Showing 44 changed files with 1,877 additions and 1,940 deletions.
11 changes: 6 additions & 5 deletions docs/generate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import bpy
import pathlib

import molecularnodes as mn
import bpy
import nodepad

import molecularnodes as mn

DOCS_FOLDER = pathlib.Path(__file__).resolve().parent

# load the data file which contains all of the nodes to build docs for
bpy.ops.wm.open_mainfile(filepath=mn.utils.MN_DATA_FILE)
bpy.ops.wm.open_mainfile(filepath=str(mn.assets.MN_DATA_FILE))


header = """---
Expand Down Expand Up @@ -56,14 +57,14 @@
file.write(
"### Residue Names\n\n" "| Name | Integer |\n" "|----------:|:------------|\n"
)
for name, res in mn.data.residues.items():
for name, res in mn.assets.data.residues.items():
file.write(f"| {name} | `{res['res_name_num']}::Int` |\n")
file.write("\n")
file.write("\n")

file.write(
"### Atom Names\n\n" "| Name | Integer |\n" "|----------:|:------------|\n"
)
for name, value in mn.data.atom_names.items():
for name, value in mn.assets.data.atom_names.items():
file.write(f"| {name} | `{value}::Int` |\n")
file.write("\n")
5 changes: 3 additions & 2 deletions molecularnodes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .addon import register, unregister
from . import assets, blender, color, session, ui
from .assets import template
from .entities import fetch, parse
from . import color, blender
from .ui.addon import register, unregister

try:
from .scene import Canvas
Expand Down
8 changes: 8 additions & 0 deletions molecularnodes/assets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path

from .template import install, uninstall
from . import data

ASSET_DIR = Path(__file__).resolve().parent
ADDON_DIR = ASSET_DIR.parent
MN_DATA_FILE = ASSET_DIR / "MN_data_file_4.2.blend"
File renamed without changes.
42 changes: 42 additions & 0 deletions molecularnodes/assets/template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import shutil
from pathlib import Path
import bpy


SUBFOLDER = "Molecular Nodes"


def is_installed():
base_path = Path(
bpy.utils.user_resource(
"SCRIPTS", path=str(Path("startup") / "bl_app_templates_user"), create=False
)
)
molecular_nodes_path = base_path / "Molecular Nodes"
return molecular_nodes_path.exists()


def install():
base_path = Path(
bpy.utils.user_resource(
"SCRIPTS", path=str(Path("startup") / "bl_app_templates_user"), create=True
)
)

base_path.mkdir(parents=True, exist_ok=True)
path_app_templates = base_path / SUBFOLDER
path_app_templates.mkdir(parents=True, exist_ok=True)
startup_file = Path(__file__).parent / "template/startup.blend"
shutil.copy(startup_file, path_app_templates)
bpy.utils.refresh_script_paths()


def uninstall():
for folder in bpy.utils.app_template_paths():
path = Path(folder).absolute() / SUBFOLDER
if "Molecular Nodes" not in str(path):
continue

if path.exists():
shutil.rmtree(path)
bpy.utils.refresh_script_paths()
4 changes: 2 additions & 2 deletions molecularnodes/blender/material.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from bpy.types import Material
from databpy.material import append_from_blend
from ..utils import MN_DATA_FILE
from ..assets import MN_DATA_FILE

MATERIAL_NAMES = [
"MN Default",
Expand All @@ -13,7 +13,7 @@

def append(name: str) -> Material:
"Append a material from the MN_DATA_FILE."
return append_from_blend(name, MN_DATA_FILE)
return append_from_blend(name, str(MN_DATA_FILE))


def add_all_materials() -> None:
Expand Down
8 changes: 4 additions & 4 deletions molecularnodes/blender/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)

from .. import color, utils
from ..utils import MN_DATA_FILE
from ..assets import MN_DATA_FILE
from . import material, mesh

NODE_WIDTH = 180
Expand Down Expand Up @@ -214,8 +214,8 @@ def swap(node: bpy.types.GeometryNode, tree: str | bpy.types.GeometryNodeTree) -

def append(name: str, link: bool = False) -> bpy.types.GeometryNodeTree:
"Append a GN node from the MN data file"
GN_TREES_PATH = os.path.join(MN_DATA_FILE, "NodeTree")
return append_from_blend(name, filepath=GN_TREES_PATH, link=link)
GN_TREES_PATH = MN_DATA_FILE / "NodeTree"
return append_from_blend(name, filepath=str(GN_TREES_PATH), link=link)


def MN_micrograph_material():
Expand Down Expand Up @@ -360,7 +360,7 @@ def create_starting_node_tree(
coll_frames: bpy.types.Collection | None = None,
style: str = "spheres",
name: str | None = None,
color: str | None = "common",
color: str | None = "common",
material: str = "MN Default",
is_modifier: bool = True,
):
Expand Down
16 changes: 1 addition & 15 deletions molecularnodes/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
from . import molecule, trajectory
from .base import EntityType, MolecularEntity
from .density import MN_OT_Import_Map
from .ensemble import CellPack, Ensemble, StarFile
from .ensemble.ui import MN_OT_Import_Cell_Pack, MN_OT_Import_Star_File
from .molecule import BCIF, CIF, PDB, SDF, Molecule
from .molecule.pdb import PDB
from .molecule.pdbx import BCIF, CIF
from .molecule.sdf import SDF
from .molecule.ui import fetch, load_local, parse
from .molecule.io import fetch, load_local, parse
from .trajectory import OXDNA, Trajectory
from .trajectory.dna import MN_OT_Import_OxDNA_Trajectory

CLASSES = (
[
MN_OT_Import_Cell_Pack,
MN_OT_Import_Map,
MN_OT_Import_OxDNA_Trajectory,
MN_OT_Import_Star_File,
]
+ trajectory.CLASSES
+ molecule.CLASSES
)
2 changes: 1 addition & 1 deletion molecularnodes/entities/density/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .ui import MN_OT_Import_Map, load
from .io import load
from .mrc import MRC
from .base import Density
20 changes: 20 additions & 0 deletions molecularnodes/entities/density/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path
from .mrc import MRC


def load(
file_path: str,
name: str = "NewDensity",
invert: bool = False,
setup_nodes: bool = True,
style: str = "density_surface",
center: bool = False,
overwrite: bool = False,
):
density = MRC(
file_path=file_path, center=center, invert=invert, overwrite=overwrite
)
density.create_object(
name=Path(file_path).name, setup_nodes=setup_nodes, style=style
)
return density
71 changes: 0 additions & 71 deletions molecularnodes/entities/density/ui.py

This file was deleted.

2 changes: 1 addition & 1 deletion molecularnodes/entities/ensemble/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base import Ensemble
from .cellpack import CellPack
from .star import StarFile
from .ui import load_cellpack, load_starfile
from .io import load_cellpack, load_starfile
27 changes: 27 additions & 0 deletions molecularnodes/entities/ensemble/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from .star import StarFile
from pathlib import Path
from .cellpack import CellPack


def load_starfile(file_path, node_setup=True, world_scale=0.01):
ensemble = StarFile.from_starfile(file_path)
ensemble.create_object(
name=Path(file_path).name, node_setup=node_setup, world_scale=world_scale
)

return ensemble


def load_cellpack(
file_path,
name="NewCellPackModel",
node_setup=True,
world_scale=0.01,
fraction: float = 1,
):
ensemble = CellPack(file_path)
ensemble.create_object(
name=name, node_setup=node_setup, world_scale=world_scale, fraction=fraction
)

return ensemble
Loading

0 comments on commit 0276802

Please sign in to comment.