Skip to content

Commit

Permalink
importing via UI successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston committed May 9, 2024
1 parent cf41eaf commit 09dbd99
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion molecularnodes/io/parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# from .cif import CIF
from .pdb import PDB
from .cellpack import CellPack
from .star import StarFile
from .star import StarFile, NDJSON
from .sdf import SDF
from .mda import MDAnalysisSession
from .mrc import MRC
78 changes: 60 additions & 18 deletions molecularnodes/io/parse/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bpy
import json


from mathutils import Matrix
from .ensemble import Ensemble
from ... import blender as bl

Expand Down Expand Up @@ -230,22 +230,64 @@ def create_model(self, name='StarFileObject', node_setup=True, world_scale=0.01)
return blender_object


def read_ndjson(file):
with open(file, 'r') as f:
lines = f.readlines()

has_rotation = bool(json.loads(lines[0]).get('xyz_rotation_matrix'))

arr = np.zeros((len(lines), 4, 4), float)

for i, line in enumerate(lines):
matrix = np.identity(4, float)
data = json.loads(line)
pos = [data['location'][axis] for axis in 'xyz']
class NDJSON(Ensemble):
def __init__(self, file_path):
super().__init__(file_path)
self.scale = 10

Check warning on line 236 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L235-L236

Added lines #L235 - L236 were not covered by tests

matrix[:3, 3] = pos
if has_rotation:
matrix[:3, :3] = data['xyz_rotation_matrix']
arr[i] = matrix
@classmethod
def from_ndjson(cls, file_path):
self = cls(file_path)
self.data = self._read()
return self

Check warning on line 242 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L240-L242

Added lines #L240 - L242 were not covered by tests

return arr
def _read(self):
with open(self.file_path, 'r') as f:
lines = f.readlines()

Check warning on line 246 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L245-L246

Added lines #L245 - L246 were not covered by tests

has_rotation = bool(json.loads(lines[0]).get('xyz_rotation_matrix'))

Check warning on line 248 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L248

Added line #L248 was not covered by tests

arr = np.zeros((len(lines), 4, 4), float)

Check warning on line 250 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L250

Added line #L250 was not covered by tests

for i, line in enumerate(lines):
matrix = np.identity(4, float)
data = json.loads(line)
pos = [data['location'][axis] for axis in 'xyz']

Check warning on line 255 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L252-L255

Added lines #L252 - L255 were not covered by tests

matrix[:3, 3] = pos
if has_rotation:
matrix[:3, :3] = data['xyz_rotation_matrix']
arr[i] = matrix

Check warning on line 260 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L257-L260

Added lines #L257 - L260 were not covered by tests

# returns a (n, 4, 4) matrix, where the 4x4 rotation matrix is returned for
# each point from the ndjson file
# this currently doesn't handle where there might be different points or different
# proteins being instanced on those points, at which point we will have to change
# what kind of array we are returning
return arr

Check warning on line 267 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L267

Added line #L267 was not covered by tests

def create_model(self, name='NewInstances', world_scale=0.01, node_setup=True):
n_points = len(self.data)
data = np.zeros((n_points, 7), float)

Check warning on line 271 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L270-L271

Added lines #L270 - L271 were not covered by tests

for i in range(n_points):
translation, rotation, scale = Matrix(self.data[i]).decompose()
data[i, :3] = translation
data[i, 3:] = rotation

Check warning on line 276 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L273-L276

Added lines #L273 - L276 were not covered by tests

# use the positions to create the object
bob = bl.obj.create_object(

Check warning on line 279 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L279

Added line #L279 was not covered by tests
vertices=data[:, :3] * world_scale * self.scale,
collection=bl.coll.mn(),
name=name
)
bob.mn['molecule_type'] = 'ndjson'

Check warning on line 284 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L284

Added line #L284 was not covered by tests

bl.obj.set_attribute(

Check warning on line 286 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L286

Added line #L286 was not covered by tests
object=bob,
name='rotation',
data=data[:, 3:],
type='QUATERNION'
)

self.object = bob

Check warning on line 293 in molecularnodes/io/parse/star.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/star.py#L293

Added line #L293 was not covered by tests
16 changes: 13 additions & 3 deletions molecularnodes/io/star.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import bpy
from pathlib import Path
from . import parse


bpy.types.Scene.MN_import_star_file_path = bpy.props.StringProperty(
name='File',
description='File path for the `.star` file to import.',
Expand All @@ -21,10 +23,18 @@ def load(
node_setup=True,
world_scale=0.01
):
suffix = Path(file_path).suffix
parser = {
'.star': parse.StarFile.from_starfile,
'.ndjson': parse.NDJSON.from_ndjson
}

ensemble = parse.StarFile.from_starfile(file_path)
ensemble.create_model(name=name, node_setup=node_setup,
world_scale=world_scale)
ensemble = parser[suffix](file_path)
ensemble.create_model(
name=name,
node_setup=node_setup,
world_scale=world_scale
)

return ensemble

Expand Down

0 comments on commit 09dbd99

Please sign in to comment.