Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative filepaths #663

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions molecularnodes/bpyd/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, obj: Object | None):
"""
if not isinstance(obj, Object):
raise ValueError(f"{obj} must be a Blender object of type Object")
self._object = obj
self._object_name = obj.name

@property
def object(self) -> Object | None:
Expand All @@ -107,30 +107,8 @@ def object(self) -> Object | None:
Object | None
The Blender object, or None if not found.
"""
# If we don't have connection to an object, attempt to re-stablish to a new
# object in the scene with the same UUID. This helps if duplicating / deleting
# objects in the scene, but sometimes Blender just loses reference to the object
# we are working with because we are manually setting the data on the mesh,
# which can wreak havoc on the object database. To protect against this,
# if we have a broken link we just attempt to find a new suitable object for it
try:
# if the connection is broken then trying to the name will raise a connection
# error. If we are loading from a saved session then the object_ref will be
# None and get an AttributeError
self._object.name
return self._object
except (ReferenceError, AttributeError):
for obj in bpy.data.objects:
if obj.mn.uuid == self.uuid:
print(
Warning(
f"Lost connection to object: {self._object}, now connected to {obj}"
)
)
self._object = obj
return obj

return None
return bpy.data.objects.get(self._object_name)

@object.setter
def object(self, value: Object) -> None:
Expand All @@ -142,7 +120,7 @@ def object(self, value: Object) -> None:
value : Object
The Blender object to set.
"""
self._object = value
self._object_name = value.name

def store_named_attribute(
self,
Expand Down
15 changes: 10 additions & 5 deletions molecularnodes/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from . import molecule, trajectory
from .molecule import CLASSES as MOL_CLASSES
from .trajectory import CLASSES as TRAJ_CLASSES
from .density import MN_OT_Import_Map
from .trajectory.dna import MN_OT_Import_OxDNA_Trajectory
from .ensemble.ui import MN_OT_Import_Cell_Pack, MN_OT_Import_Star_File

from .ensemble.cellpack import CellPack
from .ensemble.star import StarFile
from .ensemble.ui import MN_OT_Import_Cell_Pack, MN_OT_Import_Star_File
from .molecule.pdb import PDB
from .molecule.pdbx import BCIF, CIF
from .molecule.sdf import SDF
from .molecule.ui import fetch, load_local
from .trajectory.trajectory import Trajectory
from .trajectory import Trajectory
from .molecule import Molecule
from .ensemble import Ensemble


CLASSES = (
[
Expand All @@ -17,6 +22,6 @@
MN_OT_Import_OxDNA_Trajectory,
MN_OT_Import_Star_File,
]
+ trajectory.CLASSES
+ molecule.CLASSES
+ TRAJ_CLASSES
+ MOL_CLASSES
)
1 change: 1 addition & 0 deletions molecularnodes/entities/ensemble/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .ui import load_starfile, load_cellpack
from .ensemble import Ensemble
2 changes: 0 additions & 2 deletions molecularnodes/entities/ensemble/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ def __init__(self, file_path: Union[str, Path]):

"""
super().__init__()
self.type: str = "ensemble"
self.file_path: Path = bl.path_resolve(file_path)
self.instances: bpy.types.Collection = None
self.frames: bpy.types.Collection = None
bpy.context.scene.MNSession.ensembles[self.uuid] = self

@classmethod
def create_object(
Expand Down
1 change: 0 additions & 1 deletion molecularnodes/entities/ensemble/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
class StarFile(Ensemble):
def __init__(self, file_path):
super().__init__(file_path)
self.type = "starfile"

@classmethod
def from_starfile(cls, file_path):
Expand Down
24 changes: 22 additions & 2 deletions molecularnodes/entities/entity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABCMeta
import bpy
from uuid import uuid1
from bpy.types import Object
from ..bpyd import (
BlenderObject,
)
Expand All @@ -12,9 +13,28 @@ class MolecularEntity(
):
def __init__(self) -> None:
self.uuid: str = str(uuid1())
self.type: str = ""
self._object: bpy.types.Object | None
bpy.context.scene.MNSession.entities[self.uuid] = self

@property
def bob(self) -> BlenderObject:
return BlenderObject(self.object)

@property
def object(self) -> Object:
try:
return bpy.data.objects[self._object_name]
except KeyError:
# if we can't find a refernce to the object via a name, then we look via the
# unique uuids that were assigned to the object and the entity to match up
for obj in bpy.data.objects:
if obj.mn.uuid == self.uuid:
self._object_name = obj.name
return obj

@object.setter
def object(self, value) -> None:
if not isinstance(value, Object):
raise ValueError(
f"Can only set object to be of type bpy.types.Object, not {type(value)=}"
)
self._object_name = value.name
15 changes: 12 additions & 3 deletions molecularnodes/entities/molecule/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from abc import ABCMeta
from pathlib import Path
from typing import Optional, Tuple, Union
from bpy.types import Collection

import biotite.structure as struc
import bpy
Expand Down Expand Up @@ -71,10 +72,18 @@ def __init__(self, file_path: Union[str, Path, io.BytesIO]):
self._parse_filepath(file_path=file_path)
self.file: str
self.array: np.ndarray
self.frames: bpy.types.Collection | None = None
self.frames_name: str = ""
self._frames_collection_name: str = ""

bpy.context.scene.MNSession.molecules[self.uuid] = self
@property
def frames(self) -> Collection:
return bpy.data.collections.get(self._frames_collection_name)

@frames.setter
def frames(self, value) -> None:
if value is None:
self._frames_collection_name = None
else:
self._frames_collection_name = value.name

@classmethod
def _read(self, file_path: Union[Path, io.BytesIO]):
Expand Down
1 change: 0 additions & 1 deletion molecularnodes/entities/trajectory/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self, universe: mda.Universe, world_scale: float = 0.01):
self.calculations: Dict[str, Callable] = {}
self.world_scale = world_scale
self.frame_mapping: npt.NDArray[np.in64] | None = None
bpy.context.scene.MNSession.trajectories[self.uuid] = self

def selection_from_ui(self, ui_item: TrajectorySelectionItem) -> Selection:
self.selections[ui_item.name] = Selection(
Expand Down
3 changes: 1 addition & 2 deletions molecularnodes/entities/trajectory/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import MDAnalysis as mda

from ... import blender as bl
from ...session import get_session
from .trajectory import Trajectory
from bpy.props import StringProperty

Expand Down Expand Up @@ -63,7 +62,7 @@ class MN_OT_Reload_Trajectory(bpy.types.Operator):
@classmethod
def poll(cls, context):
obj = context.active_object
traj = get_session(context).trajectories.get(obj.mn.uuid)
traj = context.scene.MNSession.trajectories.get(obj.mn.uuid)
return not traj

def execute(self, context):
Expand Down
Loading
Loading