Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston committed Oct 31, 2024
1 parent 499e539 commit 6ae137f
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 146 deletions.
20 changes: 1 addition & 19 deletions molecularnodes/blender/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
from pathlib import Path
from typing import Union
import bpy


def path_resolve(path: Union[str, Path]) -> Path:
if isinstance(path, str):
return Path(bpy.path.abspath(path))
elif isinstance(path, Path):
return Path(bpy.path.abspath(str(path)))
else:
raise ValueError(f"Unable to resolve path: {path}")


def active_object(context: bpy.types.Context = None) -> bpy.types.Object:
if context is None:
return bpy.context.active_object

return context.active_object
from .utils import path_resolve
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from dataclasses import dataclass
from enum import Enum
from typing import Type
from .utils import evaluate_object

import bpy
import numpy as np

from pathlib import Path


def evaluate_object(obj: bpy.types.Object):
"Return an object which has the modifiers evaluated."
obj.update_tag()
return obj.evaluated_get(bpy.context.evaluated_depsgraph_get())


def path_resolve(path: str | Path) -> Path:
if isinstance(path, str):
return Path(bpy.path.abspath(path))
elif isinstance(path, Path):
return Path(bpy.path.abspath(str(path)))
else:
raise ValueError(f"Unable to resolve path: {path}")


@dataclass
class AttributeTypeInfo:
Expand Down Expand Up @@ -161,6 +176,21 @@ def dtype(self) -> Type:
def n_values(self) -> int:
return np.prod(self.shape, dtype=int)

@classmethod
def from_object(
cls,
obj: bpy.types.Object,
name: str,
atype: AttributeType,
domain: DomainType,
):
att = obj.data.get(name)
if att is None:
att = obj.data.attributes.new(
name=name, type=atype.value.type_name, domain=domain.value.name
)
return Attribute(att)

def from_array(self, array: np.ndarray) -> None:
"""
Set the attribute data from a numpy array
Expand Down Expand Up @@ -250,7 +280,7 @@ def store_named_attribute(
)

# the 'foreach_set' requires a 1D array, regardless of the shape of the attribute
# it also requires the order to be 'c' or blender might crash!!
# so we have to flatten it first
attribute.data.foreach_set(atype.value.value_name, data.reshape(-1))

# The updating of data doesn't work 100% of the time (see:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import bpy
import numpy as np
from typing import Union, Optional
from typing import Optional
from .attribute import (
evaluate_object,
AttributeTypes,
AttributeType,
Domains,
Expand Down Expand Up @@ -124,25 +125,22 @@ def create_object(
return obj


def active_object(context: bpy.types.Context = None) -> bpy.types.Object:
if context is None:
return bpy.context.active_object

return context.active_object


class BlenderObject:
"""
A convenience class for working with Blender objects
"""

def __init__(self, object: bpy.types.Object | None):
self._object = object

def store_named_attribute(
self,
data: np.ndarray,
name: str,
atype: str | AttributeType | None = None,
domain: str | DomainType = Domains.POINT,
) -> None:
attribute.store_named_attribute(
self.object, data=data, name=name, atype=atype, domain=domain
)
return self
def __init__(self, obj: bpy.types.Object | None):
if not isinstance(obj, bpy.types.Object):
raise ValueError(f"{obj} must be a Blender object of type bpy.types.Object")
self._object = obj

@property
def object(self) -> bpy.types.Object:
Expand All @@ -157,6 +155,39 @@ def object(self) -> bpy.types.Object:
def object(self, value: bpy.types.Object) -> None:
self._object = value

def store_named_attribute(
self,
data: np.ndarray,
name: str,
atype: str | AttributeType | None = None,
domain: str | DomainType = Domains.POINT,
) -> None:
"""
Parameters
----------
data : np.ndarray
The data to be stored as an attribute.
name : str
The name for the attribute. Will overwrite an already existing attribute.
atype : str or AttributeType or None, optional
The attribute type to store the data as. Either string or selection from the
AttributeTypes enum. None will attempt to infer the attribute type from the
input array.
domain : str or DomainType, optional
The domain to store the attribute on. Defaults to Domains.POINT.
Returns
-------
self
"""
attribute.store_named_attribute(
self.object, data=data, name=name, atype=atype, domain=domain
)
return self

def evaluate(self):
return BlenderObject(evaluate_object(self.object))

def named_attribute(self, name: str, evaluate: bool = False) -> np.ndarray:
return attribute.named_attribute(self.object, name=name, evaluate=evaluate)

Expand Down Expand Up @@ -203,3 +234,6 @@ def selected_positions(self, mask: Optional[np.ndarray] = None) -> np.ndarray:
return self.position[np.logical_and(self.selected, mask)]

return self.position[self.selected]

def __len__(self) -> int:
return len(self.object.data.vertices)
7 changes: 0 additions & 7 deletions molecularnodes/blender/databpy/utils.py

This file was deleted.

20 changes: 5 additions & 15 deletions molecularnodes/blender/mesh.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
from typing import Optional

import bpy
import numpy as np

from . import coll, nodes
from .databpy.attribute import (
Attribute,
AttributeMismatchError,
AttributeTypes,
guess_atype_from_array,
store_named_attribute,
named_attribute,
)
from .databpy.object import ObjectTracker, create_object
from .databpy.utils import evaluate_object
from .bpyd.attribute import AttributeTypes, evaluate_object
from .bpyd.object import ObjectTracker, create_object, BlenderObject


def centre(position: np.ndarray):
Expand Down Expand Up @@ -95,7 +85,7 @@ def create_data_object(array, collection=None, name="DataObject", world_scale=0.
if not collection:
collection = coll.data()

obj = create_object(locations, collection=collection, name=name)
bob = BlenderObject(create_object(locations, collection=collection, name=name))

attributes = [
("rotation", AttributeTypes.QUATERNION),
Expand All @@ -114,6 +104,6 @@ def create_data_object(array, collection=None, name="DataObject", world_scale=0.
if np.issubdtype(data.dtype, str):
data = np.unique(data, return_inverse=True)[1]

store_named_attribute(obj=obj, data=data, name=column, atype=type)
bob.store_named_attribute(data=data, name=column, atype=type)

return obj
return bob.object
3 changes: 2 additions & 1 deletion molecularnodes/blender/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .. import color, utils
from . import mesh
from . import bpyd
import re

NODE_WIDTH = 180
Expand Down Expand Up @@ -725,7 +726,7 @@ def create_assembly_node_tree(
"name": "assembly_id",
"type": "NodeSocketInt",
"min": 1,
"max": max(mesh.named_attribute(data_object, "assembly_id")),
"max": max(bpyd.named_attribute(data_object, "assembly_id")),
"default": 1,
},
)
Expand Down
11 changes: 11 additions & 0 deletions molecularnodes/blender/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import bpy
from pathlib import Path


def path_resolve(path: str | Path) -> Path:
if isinstance(path, str):
return Path(bpy.path.abspath(path))
elif isinstance(path, Path):
return Path(bpy.path.abspath(str(path)))
else:
raise ValueError(f"Unable to resolve path: {path}")
23 changes: 11 additions & 12 deletions molecularnodes/blender_manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ wheels = [
"./wheels/biotite-0.41.2-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/biotite-0.41.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/biotite-0.41.2-cp311-cp311-win_amd64.whl",
"./wheels/colorama-0.4.6-py2.py3-none-any.whl",
"./wheels/contourpy-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl",
"./wheels/contourpy-1.3.0-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/contourpy-1.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/contourpy-1.3.0-cp311-cp311-win_amd64.whl",
"./wheels/cycler-0.12.1-py3-none-any.whl",
"./wheels/fasteners-0.19-py3-none-any.whl",
"./wheels/fonttools-4.54.0-cp311-cp311-macosx_10_9_universal2.whl",
"./wheels/fonttools-4.54.0-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/fonttools-4.54.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/fonttools-4.54.0-cp311-cp311-win_amd64.whl",
"./wheels/fonttools-4.54.1-cp311-cp311-macosx_10_9_universal2.whl",
"./wheels/fonttools-4.54.1-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/fonttools-4.54.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/fonttools-4.54.1-cp311-cp311-win_amd64.whl",
"./wheels/joblib-1.4.2-py3-none-any.whl",
"./wheels/kiwisolver-1.4.7-cp311-cp311-macosx_10_9_x86_64.whl",
"./wheels/kiwisolver-1.4.7-cp311-cp311-macosx_11_0_arm64.whl",
Expand All @@ -62,17 +61,17 @@ wheels = [
"./wheels/msgpack-1.1.0-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/msgpack-1.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/msgpack-1.1.0-cp311-cp311-win_amd64.whl",
"./wheels/networkx-3.3-py3-none-any.whl",
"./wheels/networkx-3.4.2-py3-none-any.whl",
"./wheels/packaging-24.1-py3-none-any.whl",
"./wheels/pandas-2.2.3-cp311-cp311-macosx_10_9_x86_64.whl",
"./wheels/pandas-2.2.3-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/pandas-2.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/pandas-2.2.3-cp311-cp311-win_amd64.whl",
"./wheels/pillow-10.4.0-cp311-cp311-macosx_10_10_x86_64.whl",
"./wheels/pillow-10.4.0-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/pillow-10.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/pillow-10.4.0-cp311-cp311-win_amd64.whl",
"./wheels/pyparsing-3.1.4-py3-none-any.whl",
"./wheels/pillow-11.0.0-cp311-cp311-macosx_10_10_x86_64.whl",
"./wheels/pillow-11.0.0-cp311-cp311-macosx_11_0_arm64.whl",
"./wheels/pillow-11.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"./wheels/pillow-11.0.0-cp311-cp311-win_amd64.whl",
"./wheels/pyparsing-3.2.0-py3-none-any.whl",
"./wheels/python_dateutil-2.9.0.post0-py2.py3-none-any.whl",
"./wheels/pytz-2024.2-py2.py3-none-any.whl",
"./wheels/scipy-1.14.1-cp311-cp311-macosx_10_13_x86_64.whl",
Expand All @@ -82,7 +81,7 @@ wheels = [
"./wheels/six-1.16.0-py2.py3-none-any.whl",
"./wheels/starfile-0.5.6-py3-none-any.whl",
"./wheels/threadpoolctl-3.5.0-py3-none-any.whl",
"./wheels/tqdm-4.66.5-py3-none-any.whl",
"./wheels/tqdm-4.66.6-py3-none-any.whl",
"./wheels/typing_extensions-4.12.2-py3-none-any.whl",
"./wheels/tzdata-2024.2-py2.py3-none-any.whl",
]
Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/ensemble/cellpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .cif import OldCIF
from ..molecule import molecule
from ... import blender as bl
from ...blender.databpy import store_named_attribute, AttributeTypes
from ...blender.bpyd import store_named_attribute, AttributeTypes
from ... import color


Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/ensemble/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PIL import Image

from ... import blender as bl
from ...blender.databpy import AttributeTypes, store_named_attribute, BlenderObject
from ...blender.bpyd import AttributeTypes, store_named_attribute, BlenderObject
from .ensemble import Ensemble


Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bpy
from uuid import uuid1
from .. import blender as bl
from ..blender.databpy import (
from ..blender.bpyd import (
AttributeTypes,
BlenderObject,
)
Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/molecule/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ... import blender as bl
from ... import color, data, utils
from ...blender.databpy import Domains, AttributeTypes, BlenderObject
from ...blender.bpyd import Domains, AttributeTypes, BlenderObject
from ..entity import MolecularEntity


Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/trajectory/dna.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bpy
from ... import color
from ...blender import mesh, coll, nodes
from ...blender.databpy import store_named_attribute, AttributeTypes
from ...blender.bpyd import store_named_attribute, AttributeTypes

bpy.types.Scene.MN_import_oxdna_topology = bpy.props.StringProperty(
name="Toplogy",
Expand Down
2 changes: 1 addition & 1 deletion molecularnodes/entities/trajectory/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ... import data
from ..entity import MolecularEntity
from ...blender import coll, mesh, nodes, path_resolve
from ...blender import databpy as db
from ...blender import bpyd as db
from ...utils import lerp, correct_periodic_positions
from .selections import Selection, TrajectorySelectionItem

Expand Down
Empty file removed molecularnodes/load.py
Empty file.
35 changes: 0 additions & 35 deletions molecularnodes/logger.py

This file was deleted.

Loading

0 comments on commit 6ae137f

Please sign in to comment.