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

Pythonify #5

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
32 changes: 18 additions & 14 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""BlenderPhotonics - a Blender addon for 3-D mesh generation and Monte Carlo simulation

* Authors: (c) 2021-2022 Qianqian Fang <q.fang at neu.edu>
(c) 2021 Yuxuan Zhang <zhang.yuxuan1 at northeastern.edu>
(c) 2021 Yuxuan Zhang <zhang.yuxuan1 at northeastern.edu>
* License: GNU General Public License V3 or later (GPLv3)
* Version: v2022 (v0.6.0)
* Website: http://mcx.space/bp
Expand All @@ -22,7 +22,7 @@
users to create sophisticated optical simulations needed for a wide range of
biophotonics applications.

Installing this module via Blender menu "Edit\Preference\Add-ons\Install..."
Installing this module via Blender menu "Edit\\Preference\\Add-ons\\Install..."
enables the BlenderPhotonics panel. The BlenderPhotonics panel contains
the following 4 submodules:

Expand All @@ -38,7 +38,8 @@

@article{BlenderPhotonics2022,
author = {Yuxuan Zhang and Qianqian Fang},
title = {{BlenderPhotonics: an integrated open-source software environment for three-dimensional meshing and photon simulations in complex tissues}},
title = {{BlenderPhotonics: an integrated open-source software environment for three-dimensional meshing and photon
simulations in complex tissues}},
volume = {27},
journal = {Journal of Biomedical Optics},
number = {8},
Expand All @@ -54,27 +55,31 @@

"""

import bpy
from .ui import BlenderPhotonics_UI
from .blender2mesh import scene2mesh
from .mesh2blender import mesh2scene
from .obj2surf import object2surf
from .runmmc import runmmc
from .niifile import niifile
from .nii2mesh import nii2mesh
from bpy.props import PointerProperty


bl_info = {
"name": "BlenderPhotonics",
"author": "(c) 2021 Yuxuan (Victor) Zhang, (c) 2021 Qianqian Fang",
"version": (1, 0), # min plug-in version
"blender": (2, 82, 0), # min blender version
"location": "Layout,UI",
"description": "An integrated 3D mesh generation and Monte Carlo photon transport simulation environment",
"warning": "This plug-in requires the preinstallation of Iso2Mesh (http://iso2mesh.sf.net) and MMCLAB (http://mcx.space)",
"warning": "This plug-in requires the preinstallation of Iso2Mesh (http://iso2mesh.sf.net) and "
"MMCLAB (http://mcx.space)",
"doc_url": "https://github.com/COTILab/BlenderPhotonics",
"tracker_url": "https://github.com/COTILab/BlenderPhotonics/issues",
"category": "User Interface",
}
import bpy
from .ui import BlenderPhotonics_UI
from .blender2mesh import scene2mesh
from .mesh2blender import mesh2scene
from .obj2surf import object2surf
from .runmmc import runmmc
from .niifile import niifile
from .nii2mesh import nii2mesh
from bpy.props import PointerProperty


def register():
print("Registering BlenderPhotonics")
Expand All @@ -98,4 +103,3 @@ def unregister():
bpy.utils.unregister_class(runmmc)
bpy.utils.unregister_class(BlenderPhotonics_UI)
del bpy.types.Scene.blender_photonics

56 changes: 56 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from abc import ABC, abstractmethod, abstractproperty
from collections import namedtuple
from pathlib import Path


PackageBrief = namedtuple('PackageBrief', ['name', 'probe_fun'])


M_SCRIPTS = Path(__file__).parent / 'script'
OUTPUT_DIR = Path(__file__).parent / 'out'
GATEWAY_DIR = Path(__file__).parent / 'gateway'
PACKAGES = [PackageBrief(name='jsonlab', probe_fun='loadjson'),
PackageBrief(name='iso2mesh', probe_fun='s2m')]


class BackendABC(ABC):
@abstractmethod
def __init__(self, engine_type: str):
pass

@

class OctaveBackend(BackendABC):
def __init__(self, engine_type: str):
self.engine_type = engine_type.lower()
if self.engine_type == 'octave':
self.EngineError2 = self.EngineError1 = __import__('oct2py.utils', globals(), locals(), ['Oct2PyError'], 0)
self.engine = __import__('oct2py', globals(), locals(), ['Oct2Py'], 0)
else:
matlab_stuff = __import__('matlab.engine', globals(), locals(), ['MatlabEngine',
'MatlabExecutionError',
'RejectedExecutionError'], 0)
self.EngineError1 = matlab_stuff.MatlabExecutionError
self.EngineError1 = matlab_stuff.RejectedExecutionError
self.engine = matlab_stuff.MatlabEngine

def __getattr__(self, item):
def inner(*args, nout: int = 0):
with self.engine() as engine:
# import
engine.addpath(M_SCRIPTS)
try:
packages_to_load = filter(lambda p: p['name'] in PACKAGES, engine.pkg('list').tolist()[0])
for package in packages_to_load:
engine.addpath(str(Path(package['dir'])))
except IndexError:
raise ImportError('No local packages installed for Octave') # TODO: another exception type
try:
return engine.feval(item, *args, nargout=nout)
except self.EngineError1:
return engine.feval(item, *args, nout=nout)
return inner


def get_backend(engine_type='octave'):
return Backend(engine_type)
Loading