├── .gitattributes ├── .github ├── release-drafter.yml └── workflows │ ├── release-drafter.yml │ └── release.yml ├── .gitignore ├── .prettierrc ├── .vscode └── BioxelNodes.code-workspace ├── LICENSE ├── README.md ├── build.py ├── docs ├── SARS-Cov-2.md ├── advanced_usage.md ├── assets │ ├── 4d-time.gif │ ├── SARS-Cov-2 │ │ ├── image-1.png │ │ ├── image-10.png │ │ ├── image-11.png │ │ ├── image-2.png │ │ ├── image-3.png │ │ ├── image-4.png │ │ ├── image-5.png │ │ ├── image-6.png │ │ ├── image-7.png │ │ ├── image-8.png │ │ ├── image-9.png │ │ └── image.png │ ├── advanced_usage │ │ ├── image-1.png │ │ ├── image-10.png │ │ ├── image-11.png │ │ ├── image-12.png │ │ ├── image-13.png │ │ ├── image-2.png │ │ ├── image-3.png │ │ ├── image-4.png │ │ ├── image-5.png │ │ ├── image-6.png │ │ ├── image-7.png │ │ ├── image-8.png │ │ ├── image-9.png │ │ └── image.png │ ├── cover.png │ ├── eevee.gif │ ├── improve_performance │ │ ├── image-1.png │ │ ├── image-2.png │ │ └── image.png │ ├── installation │ │ └── image.png │ ├── logo.png │ └── step_by_step │ │ ├── image-1.png │ │ ├── image-2.png │ │ ├── image-3.png │ │ ├── image-4.png │ │ ├── image-5.png │ │ ├── image-6.png │ │ ├── image-7.png │ │ ├── image-8.png │ │ ├── image-9.png │ │ └── image.png ├── faq.md ├── improve_performance.md ├── index.md ├── installation.md ├── step_by_step.md ├── stylesheets │ └── extra.css └── support_format.md ├── mkdocs.yml ├── pyproject.toml ├── scipy_ndimage ├── linux-x64 │ └── _nd_image.cpython-311-x86_64-linux-gnu.so ├── macos-arm64 │ └── _nd_image.cpython-311-darwin.so ├── macos-x64 │ └── _nd_image.cpython-311-darwin.so └── windows-x64 │ └── _nd_image.cp311-win_amd64.pyd ├── src └── bioxelnodes │ ├── __init__.py │ ├── assets │ └── Nodes │ │ ├── BioxelNodes_latest.blend │ │ ├── BioxelNodes_v0.2.9.blend │ │ └── BioxelNodes_v0.3.3.blend │ ├── auto_load.py │ ├── bioxel │ ├── __init__.py │ ├── container.py │ ├── io.py │ ├── layer.py │ ├── parse.py │ ├── scipy │ │ ├── __init__.py │ │ ├── _filters.py │ │ ├── _interpolation.py │ │ ├── _ni_support.py │ │ └── _utils.py │ └── skimage │ │ ├── __init__.py │ │ ├── _utils.py │ │ ├── _warps.py │ │ └── dtype.py │ ├── bioxelutils │ ├── __init__.py │ ├── common.py │ ├── container.py │ ├── layer.py │ └── node.py │ ├── blender_manifest.toml │ ├── constants.py │ ├── exceptions.py │ ├── menus.py │ ├── node_menu.py │ ├── operators │ ├── __init__.py │ ├── container.py │ ├── io.py │ ├── layer.py │ ├── misc.py │ └── node.py │ ├── preferences.py │ ├── props.py │ └── utils.py ├── tests └── __init__.py └── uv.lock /.gitattributes: -------------------------------------------------------------------------------- 1 | *.blend filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | # release title (appears, for example, in repo's sidebar) 2 | # NOTE: $RESOLVED_VERSION == $MAJOR.$MINOR.$PATCH 3 | name-template: "v$RESOLVED_VERSION" 4 | 5 | # git tag to be used for the release 6 | tag-template: "v$RESOLVED_VERSION" 7 | 8 | # Release Notes template (keep it as is) 9 | template: | 10 | ## What’s Changed 11 | 12 | $CHANGES 13 | 14 | change-template: '- $TITLE @$AUTHOR (#$NUMBER)' 15 | change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. 16 | 17 | # Define which PR label will cause which kind of 18 | # version bump (following semantic versioning). 19 | # If no labels match, the default "patch". 20 | version-resolver: 21 | major: 22 | labels: 23 | - 'major' 24 | minor: 25 | labels: 26 | - 'minor' 27 | patch: 28 | labels: 29 | - 'patch' 30 | default: patch 31 | 32 | # Define which PR label will be listed in which 33 | # category of changes in the Release Notes. 34 | # If no labels match, the default is to be 35 | # listed on the top, before the sections. 36 | categories: 37 | - title: "🚨 BREAKING CHANGES 🚨" 38 | labels: 39 | - "BREAKING CHANGE" 40 | - title: "🚀 New Features" 41 | labels: 42 | - "feature" 43 | - "enhancement" 44 | - title: "🐛 Bug Fixes" 45 | labels: 46 | - "fix" 47 | - "bug" 48 | - title: "🛠️ Other Changes" 49 | labels: 50 | - "chore" 51 | - "refactor" 52 | - "documentation" 53 | - "style" 54 | - "test" 55 | - "revert" 56 | - "dependencies" 57 | - "ci" -------------------------------------------------------------------------------- /.github/workflows/release-drafter.yml: -------------------------------------------------------------------------------- 1 | name: Release Drafter 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | types: [opened, reopened, synchronize, labeled, unlabeled] 9 | 10 | permissions: 11 | contents: read 12 | 13 | jobs: 14 | update_release_draft: 15 | permissions: 16 | # write permission required to create a github release 17 | contents: write 18 | # write permission required for autolabeler 19 | pull-requests: write 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: release-drafter/release-drafter@v5 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release Version 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' 7 | 8 | jobs: 9 | draft_release: 10 | name: Draft Release 11 | permissions: 12 | contents: write 13 | pull-requests: write 14 | runs-on: ubuntu-latest 15 | outputs: 16 | upload_url: ${{ steps.draft_release.outputs.upload_url }} 17 | version: ${{ steps.set_env.outputs.version }} 18 | steps: 19 | - name: Set Version Env 20 | id: set_env 21 | run: | 22 | ref_name=${{ github.ref_name }} 23 | echo "version=${ref_name#release/}" >> "$GITHUB_OUTPUT" 24 | - name: Draft Release ${{ steps.set_env.outputs.version }} 25 | uses: release-drafter/release-drafter@v5 26 | id: draft_release 27 | with: 28 | name: ${{ steps.set_env.outputs.version }} 29 | tag: ${{ steps.set_env.outputs.version }} 30 | env: 31 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 32 | 33 | build_extension: 34 | name: Build Blender Extension 35 | needs: draft_release 36 | runs-on: ubuntu-latest 37 | strategy: 38 | matrix: 39 | platform: ["windows-x64", "linux-x64", "macos-arm64", "macos-x64"] 40 | steps: 41 | - name: Checkout Code 42 | uses: actions/checkout@v4 43 | with: 44 | lfs: "true" 45 | - name: Set up Python 46 | uses: actions/setup-python@v4 47 | with: 48 | python-version: 3.11 49 | - name: Build Extension 50 | run: | # build.py need tomlkit to edit blender_manifest.toml 51 | pip install tomlkit 52 | python build.py ${{ matrix.platform }} 53 | cd src/bioxelnodes 54 | zip -r ../../package.zip . 55 | - name: Upload Extension 56 | id: upload-release-asset 57 | uses: actions/upload-release-asset@v1 58 | env: 59 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 60 | with: 61 | upload_url: ${{ needs.draft_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 62 | asset_path: ./package.zip 63 | asset_name: BioxelNodes_${{ needs.draft_release.outputs.version }}_${{ matrix.platform }}.zip 64 | asset_content_type: application/zip 65 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | db.sqlite3 61 | db.sqlite3-journal 62 | 63 | # Flask stuff: 64 | instance/ 65 | .webassets-cache 66 | 67 | # Scrapy stuff: 68 | .scrapy 69 | 70 | # Sphinx documentation 71 | docs/_build/ 72 | 73 | # PyBuilder 74 | target/ 75 | 76 | # Jupyter Notebook 77 | .ipynb_checkpoints 78 | 79 | # IPython 80 | profile_default/ 81 | ipython_config.py 82 | 83 | # pyenv 84 | .python-version 85 | 86 | # pipenv 87 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 88 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 89 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 90 | # install all needed dependencies. 91 | #Pipfile.lock 92 | 93 | # celery beat schedule file 94 | celerybeat-schedule 95 | 96 | # SageMath parsed files 97 | *.sage.py 98 | 99 | # Environments 100 | .env 101 | .venv 102 | env/ 103 | venv/ 104 | ENV/ 105 | env.bak/ 106 | venv.bak/ 107 | 108 | # Spyder project settings 109 | .spyderproject 110 | .spyproject 111 | 112 | # Rope project settings 113 | .ropeproject 114 | 115 | # mkdocs documentation 116 | /site 117 | 118 | # mypy 119 | .mypy_cache/ 120 | .dmypy.json 121 | dmypy.json 122 | 123 | # Pyre type checker 124 | .pyre/ 125 | 126 | pythonlib* 127 | 128 | *.blend1 129 | 130 | blendcache_* 131 | 132 | *.ipynb 133 | 134 | .secrets 135 | 136 | .vdb 137 | 138 | !scipy_ndimage/*/** -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "overrides": [ 4 | { 5 | "files": "*.md", 6 | "options": { 7 | "tabWidth": 4 8 | } 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /.vscode/BioxelNodes.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": ".." 5 | }, 6 | { 7 | "path": "../src/bioxelnodes" 8 | } 9 | ], 10 | "settings": {} 11 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [中文文档](https://uj6xfhbzp0.feishu.cn/wiki/LPKEwjooSivxjskWHlCcQznjnNf?from=from_copylink) 2 | 3 | # Bioxel Nodes 4 | 5 | [![For Blender](https://img.shields.io/badge/Blender-orange?style=for-the-badge&logo=blender&logoColor=white&color=black)](https://blender.org/) 6 | ![GitHub License](https://img.shields.io/github/license/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 7 | ![GitHub Release](https://img.shields.io/github/v/release/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 8 | ![GitHub Repo stars](https://img.shields.io/github/stars/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 9 | 10 | [![Discord](https://img.shields.io/discord/1265129134397587457?style=for-the-badge&logo=discord&label=Discord&labelColor=white&color=black)](https://discord.gg/pYkNyq2TjE) 11 | 12 | Bioxel Nodes is a Blender addon for scientific volumetric data visualization. It using Blender's powerful **Geometry Nodes** and **Cycles** to process and render volumetric data. 13 | 14 | ![cover](https://omoolab.github.io/BioxelNodes/latest/assets/cover.png) 15 | 16 | ## Features 17 | 18 | - Realistic rendering result, also support EEVEE NEXT. 19 | - Support many formats:.dcm, .jpg, .tif, .nii, .nrrd, .ome, .mrc... 20 | - Support 4D volumetric data. 21 | - Many kinds of cutters. 22 | - Simple and powerful nodes. 23 | - Based on blender natively, can work without addon. 24 | 25 | | ![4D](https://omoolab.github.io/BioxelNodes/latest/assets/4d-time.gif) | ![EEVEE](https://omoolab.github.io/BioxelNodes/latest/assets/eevee.gif) | 26 | | :--------------------------------------------------------------------: | :---------------------------------------------------------------------: | 27 | | Support 4D volumetric data | Real-time render with eevee | 28 | 29 | **Read the [getting started](https://omoolab.github.io/BioxelNodes/latest/installation) to begin your journey into visualizing volumetric data!** 30 | 31 | Welcome to our [discord server](https://discord.gg/pYkNyq2TjE), if you have any problems with this add-on. 32 | 33 | ## Citation 34 | 35 | If you want to cite this work, you can cite it from Zenodo: 36 | 37 | [![DOI](https://zenodo.org/badge/786623459.svg)](https://zenodo.org/badge/latestdoi/786623459) 38 | 39 | ## Known Limitations 40 | 41 | - Only one cutter supported in EEVEE render 42 | - Shader fail to work when convert to mesh. 43 | - Section surface cannot be generated when convert to mesh (will be supported soon) 44 | 45 | ## Roadmap 46 | 47 | - Better multi-format import experience 48 | - One-click bake model with texture 49 | - AI Segmentation to Generate Labels 50 | -------------------------------------------------------------------------------- /build.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import shutil 3 | import subprocess 4 | import sys 5 | from dataclasses import dataclass 6 | import tomlkit 7 | 8 | 9 | @dataclass 10 | class Platform: 11 | pypi_suffix: str 12 | blender_tag: str 13 | 14 | 15 | required_packages = ["SimpleITK==2.3.1", 16 | "pyometiff==1.0.0", 17 | "mrcfile==1.5.1", 18 | "h5py==3.11.0", 19 | "transforms3d==0.4.2", 20 | "tifffile==2024.7.24"] 21 | 22 | 23 | platforms = {"windows-x64": Platform(pypi_suffix="win_amd64", 24 | blender_tag="windows-x64"), 25 | "linux-x64": Platform(pypi_suffix="manylinux2014_x86_64", 26 | blender_tag="linux-x64"), 27 | "macos-arm64": Platform(pypi_suffix="macosx_12_0_arm64", 28 | blender_tag="macos-arm64"), 29 | "macos-x64": Platform(pypi_suffix="macosx_10_16_x86_64", 30 | blender_tag="macos-x64")} 31 | 32 | packages_to_remove = { 33 | "imagecodecs", 34 | "numpy" 35 | } 36 | 37 | 38 | def run_python(args: str): 39 | python = Path(sys.executable).resolve() 40 | subprocess.run([python] + args.split(" ")) 41 | 42 | 43 | def build_extension(platform: Platform, python_version="3.11") -> None: 44 | wheel_dirpath = Path("./src/bioxelnodes/wheels") 45 | toml_filepath = Path("./src/bioxelnodes/blender_manifest.toml") 46 | scipy_ndimage_dirpath = Path("./scipy_ndimage", platform.blender_tag) 47 | 48 | # download required_packages 49 | run_python( 50 | f"-m pip download {' '.join(required_packages)} --dest {wheel_dirpath.as_posix()} --only-binary=:all: --python-version={python_version} --platform={platform.pypi_suffix}" 51 | ) 52 | 53 | for f in wheel_dirpath.glob('*.whl'): 54 | if any([package in f.name for package in packages_to_remove]): 55 | f.unlink(missing_ok=True) 56 | 57 | elif platform.blender_tag == "macos-arm64" and \ 58 | "lxml" in f.name and "universal2" in f.name: 59 | f.rename(Path(f.parent, 60 | f.name.replace("universal2", "arm64"))) 61 | 62 | for ndimage_filepath in scipy_ndimage_dirpath.iterdir(): 63 | to_filepath = Path("./src/bioxelnodes/bioxel/scipy", ndimage_filepath.name) 64 | shutil.copy(ndimage_filepath, to_filepath) 65 | 66 | # Load the TOML file 67 | with toml_filepath.open("r") as file: 68 | manifest = tomlkit.parse(file.read()) 69 | 70 | manifest["platforms"] = [platform.blender_tag] 71 | manifest["wheels"] = [f"./wheels/{f.name}" 72 | for f in wheel_dirpath.glob('*.whl')] 73 | 74 | # build.append('generated', generated) 75 | # manifest.append('build', build) 76 | 77 | # Write the updated TOML file 78 | with toml_filepath.open("w") as file: 79 | text = tomlkit.dumps(manifest) 80 | file.write(text) 81 | 82 | 83 | def main(): 84 | platform_name = sys.argv[1] 85 | platform = platforms[platform_name] 86 | build_extension(platform) 87 | 88 | 89 | if __name__ == "__main__": 90 | main() 91 | -------------------------------------------------------------------------------- /docs/SARS-Cov-2.md: -------------------------------------------------------------------------------- 1 | # SARS-Cov-2 2 | 3 | ![alt text](assets/SARS-Cov-2/image.png) 4 | 5 | ![alt text](assets/SARS-Cov-2/image-1.png) 6 | 7 | In this tutorial, we'll make a structural visualzation of the SARS-Cov-2 Virus. Please see [step by step](step_by_step.md) to make sure you have a basic understanding of how to use the addon, and make sure the addon version is v1.0.2 or higher! 8 | 9 | Research data is from paper published in September 2020 in Cell Press ([https://www.cell.com/cell/fulltext/S0092-8674(20)31159-4]()) by Li Sai team at Tsinghua University. This research revealed the molecular assembly of the authentic SARS-CoV-2 virus using cryoelectron tomography (cryo-ET) and subtomogram averaging (STA). Here is an official video of this study 10 | 11 | 12 | 13 | "It remains enigmatic how coronaviruses pack the ∼30-kb RNA into the ∼80-nm-diameter viral lumen. Are the RNPs ordered relative to each other to avoid RNA entangling, knotting, or even damage or are they involved in virus assembly?" Three assembly classes were proposed in the paper, we will make a visualzation of the "eggs-in-a-nest"-shaped RNPs assembly, based on real cryo-ET data. 14 | 15 | ## Download and import data 16 | 17 | The data of this research is hosted on EMDB ([https://www.ebi.ac.uk/emdb](https://www.ebi.ac.uk/emdb)), and the website of SARS-Cov-2 virus is [here](https://www.ebi.ac.uk/emdb/EMD-30430). Open the page, find "Download" button and select the first selection "3D Volume (.map.gz)". 18 | 19 | In case you can't download data, you can download here. 20 | 21 | [SARS-Cov-2.map](https://drive.google.com/file/d/1LMybsmTVbwQ38_eqAx6hbZTc5p2fdcjK/view?usp=sharing) 22 | 23 | After downloading, put it into any directory and import the data. In the import options, it is recommended to adjust the Bioxel Size to 5, in order to reduce the shape of the data to increase the speed of calculation and rendering. If you are confident in the performance of your hardware, you can leave the original 2.72 unchanged. After adjusting the Bioxel Size, the shape of the converted data will be calculated below based on the current bioxel size. The amount of data will increase exponentially with larger shape, and may result in OOM (out of memory). 24 | 25 | ## Cutout the Virus 26 | 27 | After importing, you can get the virus by creating and connecting nodes and setting parameters as shown below 28 | 29 | ![alt text](assets/SARS-Cov-2/image-2.png) 30 | 31 | Place the light and the camera in the way as below. The backlight is a area light, color white, intensity 500W, spread 90°, and inside the virus, place a point light, color `FFD08D`, intensity 5W. Camera focal length is 200 mm. the background color is pure black. The Look of the color management is High Contrast. 32 | 33 | ![alt text](assets/SARS-Cov-2/image-3.png) 34 | 35 | Continue editing the node, followed by "Set Properies" and "Set Color by Ramp 4" node, the parameters of these two nodes are set as shown in the figure, where the color part, from top to bottom, is set to `E1D0FC` (1.0), `FFE42D` (0.5), `3793FF` (0.5), `FFFF8EC` (0.1) (alpha value in parentheses). Color alpha also affects density. 36 | 37 | ![alt text](assets/SARS-Cov-2/image-4.png) 38 | 39 | If you feel that rendering is too slow, see [here](improve_performance.md) for suggestions 40 | 41 | ## Color the RNPs alone 42 | 43 | The current rendering is pretty good, but considering that my goal is to make it clear how the RNPs are arranged within the virus, the RNPs should be colored differently than the others. So how to color the RNPs alone? 44 | 45 | First of all, we have to separate the RNPs. The value of RNPs is between the membrane and S protein, so it is very difficult to separate. The good thing is that the virus is spherical, we can separate the RNPs part of the virus from the rest by using a sphere cutter. To do so, in the Geometry Nodes panel menu of the container, click **Bioxel Nodes > Add a Cutter > Sphere Cutter**, and adjust the scale and position of the newly created sphere object named "Sphere_Cutter" appropriately, so that it can just separate the internal RNPs and membrane of the virus. 46 | 47 | ![alt text](assets/SARS-Cov-2/image-5.png) 48 | 49 | The process of changing the position of the "Sphere_Cutter" object can be very laggy, then you can turn off "With Surface" and do it in Slice Viewer mode. Once you are happy with it, you can restore the settings. 50 | 51 | Then we turn on Invert in the "Sphere Cutter" node and you can see the opposite result. This way we've managed to separate the two, next let's color them separately and finally merge them with Join Component. Create and join the nodes as shown below, where the second Cutter node is copied and the color value in Set Color is `FFDDFE` (0.5). If all is well, the rendering should look like below. 52 | 53 | ![alt text](assets/SARS-Cov-2/image-6.png) 54 | 55 | At this point, the rendering of a SARS-Cov-2 virus is complete! 56 | 57 | ## Render RNPs and Membranes 58 | 59 | Realistic rendering is great, but Non-photorealistic rendering (NPR) is better for presenting structural information. So I will render ultrastructure of the RNPs assembly with toon shader. 60 | 61 | ![alt text](assets/SARS-Cov-2/image-7.png) 62 | 63 | The silhouettes of RNPs uses Blender's Line Art feature, but Line Art must be based on a mesh. Select the nodes that are extruding the RNPs (i.e., the "Cut" nodes that are in charge of cutting out the RNPs), and right-click, click **Bioxel Nodes > Extract Mesh**. This gives you a new mesh object of the RNPs. 64 | 65 | Extract the membrane mesh in the same way. 66 | 67 | The membrane has a much higher value than the rest of the virus, so let's create a new "Cutout" node, also after the ReCenter, but with the threshold adjusted to 0.13. After the Cutout, connect to the "To Surface" node and parameterize it as shown in the figure, noting that the Remove Island Threshold is set to 1000 so that any fragments smaller than 1000 pts be eliminated. Then select the "To Surface" node, and perform the operation of extracting the mesh. 68 | 69 | ![alt text](assets/SARS-Cov-2/image-8.png) 70 | 71 | The mesh of the membrane needs to be cut in half, this can be done with the Box Trim brush in sculpt mode or with the Boolean modifier. the mesh of the RNPs may also need some repair work, I cleaned up some of the broken surfaces of the RNPs. all the meshes are now ready to be used. 72 | 73 | ![alt text](assets/SARS-Cov-2/image-9.png) 74 | 75 | For Line Art, please see the video tutorial from Blender Secrets. 76 | 77 | 78 | 79 | Finally, an infographic on the ultrastructure of RNPs is done! 80 | 81 | ![alt text](assets/SARS-Cov-2/image-1.png) 82 | 83 | If you have trouble following the docs, you can download the project files. 84 | 85 | [SARS-Cov-2.zip](https://drive.google.com/file/d/15GpIoIjVAE-Jr98zWo7oupuk1KfRVPmk/view?usp=sharing) 86 | 87 | ## Homework 88 | 89 | Other data relevant to the study are provided in the paper, you could try visualizing them. 90 | 91 | ![alt text](assets/SARS-Cov-2/image-10.png) 92 | 93 | Go to the official EMDB website https://www.ebi.ac.uk/emdb/ and enter the EMD number in the search box to get them. 94 | 95 | The Electron Microscopy Data Bank (EMDB) is a public repository for cryogenic-sample Electron Microscopy (cryoEM) volumes and representative tomograms of macromolecular complexes and subcellular structures. If you want to get data on other virus, just enter their name in the search box. For example, hepatitis B virus, enter it into the search box, then select Virus in the Sample Type on the left side, and download the data you need (it is better to find the corresponding paper to clarify the data information). 96 | 97 | **Try to visualize hepatitis B virus.** 98 | 99 | ![alt text](assets/SARS-Cov-2/image-11.png) 100 | -------------------------------------------------------------------------------- /docs/advanced_usage.md: -------------------------------------------------------------------------------- 1 | # Advanced Usage 2 | 3 | ## Concepts 4 | 5 | In the previous section, I mentioned some key concepts, I'll describe them in detail so that you can better understand the following operations. 6 | 7 | ### Container 8 | 9 | First is the **Container**, it is the easiest to describe, you can think of it as a scene, or a solver container. it is the stage that carries all stuffs. 10 | 11 | ### Layer 12 | 13 | Second is the **Layer**, which represents different fields of data under the same location in container of the same subject. Just as view layers in any map app, one as roads, one as satellite view, while in Bioxel, one as MRI, one as CT, one as electrical signals, etc. Some layers are for material, such as color, density, etc., we call them **Material Layer**. One container contains different layers of one subject each. Any external data imported into the container is converted into the layer. 14 | 15 | ### Component 16 | 17 | Finally, the **Component**, which is a fragment of **Material Layer**. Multiple materials (Matters) may exist within the same area with different meanings of presentation, like objects in blender, each representing a part of the scene. Bioxel Nodes provides a series of nodes and tools that allow the user to create material layers (Components) based on the other layers (serve as source data), In the previous section, we used the CT data (layer) to make the material (matter) of skull, this process known as materialization. 18 | 19 | ## Attache the Muscles to the Skull 20 | 21 | Multiple components can exist in one container, just as multiple objects can exist in one scene. They can be controlled individually or combined with each other to build complex scenes. Next, let's try cutouting muscle from the VHP anatomical images and attaching the muscles to the skull that created in the previous section. The anatomical images are very large, so I've cropped and scaled them down to 1/4 of their original size for ease of use. Download the file and unzip it into a new directory. 22 | 23 | [VHP_M_AnatomicalImages_Head.zip](https://drive.google.com/file/d/164AjQX0tmgUpWZlleJ5i1IKHco4ytDu2/view?usp=drive_link) 24 | 25 | You can also download the data directly from the official website, [https://data.lhncbc.nlm.nih.gov/public/Visible-Human/Male-Images/PNG_format/head/index.html](https://data.lhncbc.nlm.nih.gov/public/Visible-Human/Male-Images/PNG_format/head/index.html) 26 | 27 | First import the CT data as described in [step by step](step_by_step.md). Then import the anatomical images, but unlike before, this time from the Geometry Nodes menu of the existing container (not the top menu), click **Bioxel Nodes > Import Volumetric Data (Init) > as Color**, locate to the unzipped folder, and select any .png file (do not multi-select, and do not select the folder). This way the imported layer will be in the same container as the previous CT data. You can also import by dragging it into the container geometry node interface instead of the 3D Viewport interface. Also note that **as Color** (not as Scalar) is clicked because the anatomical images are RGB data. 28 | 29 | ![alt text](assets/advanced_usage/image.png) 30 | 31 | Let's take more time in the import options this time 🫡, first let's talk about what the **Original Spacing** is in the options, you can simply think of it as the original "pixel size" of a image, since the volume is 3D, its pixels (i.e., voxels) are cuboids with a length, width, and height rather than 2D rectangles. And their length, width, and height size is the **Spacing**. 32 | 33 | Normally, Bioxel Nodes can read the spacing from the data header, and you don't need to fill in the value, but there is no record of that in any PNG image, so you need to input it manually. in the official description of the VHP, it is mentioned that each pixel of the anatomical images corresponds to the size of .33mm, and the thickness of each slice is 1mm, so the Original Spacing of the anatomical images should be set to `(0.33, 0.33, 1)`. 34 | 35 | However, the file I provided is scaled by 1/4 for each image, so the X and Y axes need to be multiplied by 4. So the final Original Spacing should be set to `(1.33, 1.33, 1)`, which should be understandable, right? (If you are importing an official file, then it should still be set to `(0.33, 0.33, 1)`) 36 | Layer Name should be set to "Anatomical Images", other options remain default, click Ok. 37 | 38 | ![alt text](assets/advanced_usage/image-1.png) 39 | 40 | After importing, connect the "Cutout by Hue" node (Add > Bioxel Nodes > Component > Cutout by Hue) after the newly created "Fetch Layer" node, and turn on the Slice Viewer mode preview, as shown below. 41 | 42 | ![alt text](assets/advanced_usage/image-2.png) 43 | 44 | In addition to spacing, the Dicom file also records the subject's position and orientation, but those information is apparently not recorded in PNG format. This results in a mismatch between the CT data and the anatomical images in position. We need to manually align this two layers: First, set the threshold of the "Cutout by Threshold" node to 0.2 so that the full head can be cutouted and displayed. Then add "ReCenter" node before "Cutout by Hue" node and "Cutout by Threshold" node each, then add Locater to the "anatomical images" workflow (see [step by step](step_by_step.md) for details), and finally add "Join Component" node (Add > Bioxel Nodes > Component > Join Component), and connect them as shown below, with the parameters set as shown. 45 | 46 | ![alt text](assets/advanced_usage/image-3.png) 47 | 48 | The "Join Component" node allows you to merge two components together. Move and rotate the Locater object so that the two components are exactly the same in position. If you're having trouble matching them up, here's the exact transform values to fill in the Locater object properties. 49 | 50 | ![alt text](assets/advanced_usage/image-4.png) 51 | 52 | Next let's cutout the muscles individually. 53 | 54 | First let's talk about the "Cutout by Hue" node. Literally, it cutout regions by hue of color. If you select this node and press "M" key to temporarily mute it, you'll see that the data becomes a cube with blue parts in Slice Viewer mode. the default value of Hue in the "Cutout by Hue" node is red, meaning that the node retains areas of the body that are close to red, and strip out the blue, which is on the opposite side of the red in the hue ring, leaving the human body parts. 55 | 56 | If you turn down the value of Sensitivity, you'll see that the visabile part is decreasing, leaving only the part more closer to red, and the muscle happens to be red, so we can easily cutout the muscle. The color of the specimen tissue is not as vibrant as it is in the live state, and we can fine-tune the color to restore the vibrancy by using the "Set Color by Color" node (Add > Bioxel Nodes > Property > Set Color by Color). 57 | 58 | To finally put it all together, connect and set the nodes as shown below, where the two colors in "Set Color by Ramp 2" node are `E7D2C5` and `FFEDEC`. If everything is working correctly, you'll get the rendering below. 59 | 60 | ![alt text](assets/advanced_usage/image-5.png) 61 | 62 | You can also add "Transfrom" and "Cutter" nodes at the end and all components. 63 | 64 | ![alt text](assets/advanced_usage/image-6.png) 65 | 66 | The multimodal data of VHP do not have any deformation or scaling differences, but only positional differences, so all data can be aligned easily. However, in most cases, the differences of different modalities are so great that they need to be registered first, which is not possible with Bioxel Nodes at the moment (but it is in plan). Nevertheless, you can build different components based on the same layer and combine them together. For example, based on the CT data, with the "Cutout by Range" node, you can eliminate the low value regions (fat), eliminate the high value regions (bone), and you can still get the muscle. And then attach it to the skull. 67 | 68 | ![alt text](assets/advanced_usage/image-7.png) 69 | 70 | However, the CT values of brain and muscle are very close to each other, so it is extremely difficult to separate the brain from the muscle by threshold. So how should cutout the brain? 71 | 72 | ## Cutout the Brain 73 | 74 | We can use AI technology to realize the division of brain regions, this process is called Segmentation, and the brain regions are as label. There are many models and methods for medical image segmentation, here we recommend [Total Segmentator](https://github.com/wasserth/TotalSegmentator). 75 | 76 | It is an out-of-the-box CT/MRI medical image segmentation Python library. You can deploy Total Segmentator locally or process your data through the official online site [https://totalsegmentator.com](https://totalsegmentator.com). 77 | 78 | Let me demonstrate the operation, we are still using the CT data from VHP. Click on "Drop DICOM.zip or ..." and select the VHP_M_CT_Head.zip file. Enter "brain" below "Selected task", press Enter to confirm. Finally click "Process data" button. 79 | 80 | ![alt text](assets/advanced_usage/image-8.png) 81 | 82 | Wait for the uploading and processing, after all, you will be redirected to the following page, click "Download results" button, download and unzip segmentations.zip to any folder directory. 83 | 84 | ![alt text](assets/advanced_usage/image-9.png) 85 | 86 | In case you can't get the brain segment (label), you can download it directly (without unzipping it). 87 | 88 | [VHP_M_CT_Head_Brain.nii.gz](https://drive.google.com/file/d/1KGiZ3G11YLXkGszSrvWQ9kTF4TMpxUPd/view?usp=drive_link) 89 | 90 | Next we import the brain segment (label) into the container in the same way as we imported the anatomical imagess(under the same container), but this time selecting **as Label**, with the Layer Name set to "Brain", and leaving the other options as default. Once the import is complete, a new "Fetch Layer" node will be added. Create and set the node as shown below, and you should be able to see the surface of brain. 91 | 92 | ![alt text](assets/advanced_usage/image-10.png) 93 | 94 | Do you see the "step" like shape on surface? this is due to the low precision of the AI segmentation results. So How to addressing the issue? Select the Fetch Layer node of the brain label, right-click to pop up menu, click **Bioxel Nodes > Resample Value**, in the dialog box, change the "Smooth Size" to 3 or higher (the higher it is, the slower the calculation will be), click OK and wait for the calculation to finish. When it is done, a new layer will be added, replace the original one, is it smoother? The following figure lists different levels of smoothing. 95 | 96 | ![alt text](assets/advanced_usage/image-11.png) 97 | 98 | The smoother it is, the more comfortable it looks, but it also means that more details are lost and the calculation is slower. 99 | 100 | But if you cut the brain, you will find that the cross-section is homogeneous, this is because the label type layer is does not contain any modality data itself. Bioxel Nodes provides a function to fill in any layer values by label, if you set the value below brain tissue in the non-brain region of CT data, then any "Cutout" node will only keep the brain region. 101 | 102 | The operation is like this, select the "Fetch Layer" node of the CT data, right-click to pop up the menu, click **Bioxel Nodes > Fill Value by Label**, in the dialog box, Label is set to the newly imported brain label layer, Fill Value is set to -100 (as long as it is less than the Hu value of the brain tissue), click OK. wait for the data processing, a new data layer will be added to the container when it is finished. 103 | 104 | As shown in the following figure, connect the nodes and set the node parameters, in which the two colors of "Set Color by Ramp 2" node are `B26267` and `EBC5B7`, and the "Cutout by Range" and "Set Color by Ramp 2" node are changed to "Value" mode (the default is "Factor" mode), because the maximum and minimum values of the brain are too close to each other and difficult to adjust in "Factor" mode. To make the brain tissue look more transparent, I add the "Set Properties" (Add > Bioxel Nodes > Property > Set Properties) node at the end and changed the "Density" to 0.5. 105 | 106 | ![alt text](assets/advanced_usage/image-12.png) 107 | 108 | (Still feel not smooth enough? It's really hard to get rid of "step" surface completely at the moment) 109 | 110 | Now the brain is done. Finally we combine all together and you get something like the following. 111 | 112 | ![alt text](assets/advanced_usage/image-13.png) 113 | 114 | 🤗 If you've followed the docs up to this point, give yourself a round of applause, you've mastered this addon! If you have trouble following the docs instruction, you can download the project files to understand how the nodes work. 115 | 116 | [VHP.zip](https://drive.google.com/file/d/1EHB7sxNSvJNwmoTfFnpyOSsm2M_2Y0hO/view?usp=sharing) 117 | 118 | AI segmentation is an essential part of volumetric data visualization. Therefore, the future Bioxel Nodes will integrate some commonly used AI segmentation models, so that users can complete the volumetric data visualization in place! 119 | -------------------------------------------------------------------------------- /docs/assets/4d-time.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/4d-time.gif -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-1.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-10.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-11.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-2.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-3.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-4.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-5.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-6.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-7.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-8.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image-9.png -------------------------------------------------------------------------------- /docs/assets/SARS-Cov-2/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/SARS-Cov-2/image.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-1.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-10.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-11.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-12.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-13.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-2.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-3.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-4.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-5.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-6.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-7.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-8.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image-9.png -------------------------------------------------------------------------------- /docs/assets/advanced_usage/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/advanced_usage/image.png -------------------------------------------------------------------------------- /docs/assets/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/cover.png -------------------------------------------------------------------------------- /docs/assets/eevee.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/eevee.gif -------------------------------------------------------------------------------- /docs/assets/improve_performance/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/improve_performance/image-1.png -------------------------------------------------------------------------------- /docs/assets/improve_performance/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/improve_performance/image-2.png -------------------------------------------------------------------------------- /docs/assets/improve_performance/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/improve_performance/image.png -------------------------------------------------------------------------------- /docs/assets/installation/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/installation/image.png -------------------------------------------------------------------------------- /docs/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/logo.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-1.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-2.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-3.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-4.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-5.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-6.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-7.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-8.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image-9.png -------------------------------------------------------------------------------- /docs/assets/step_by_step/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/docs/assets/step_by_step/image.png -------------------------------------------------------------------------------- /docs/faq.md: -------------------------------------------------------------------------------- 1 | # FAQ 2 | 3 | ## Nothing in the scene when rendering? 4 | 5 | If you're using the Cycles renderer with GPU, make sure your GPU supports "Optix", as Bioxel Nodes relies on OSL (Open shader language) for volumetric rendering, otherwise you'll have to use the CPU for rendering. 6 | 7 | If you're using the EEVEE renderer, this issue is due to EEVEE not loading shaders for some unknown reason, save the file and restart Blender to fix it. 8 | 9 | ## After updating the addon, the nodes in the file have turned red? 10 | 11 | This is because the version of the nodes in the file does not match the current version of the addon after updating. 12 | 13 | If you still want to edit the file with Bioxel Nodes, you can only roll back the addon version to the version that corresponds to the nodes. 14 | 15 | If you just want the file to work and you won't rely on Bioxel Nodes any more, then you can just click **Bioxel Nodes > Relink Node Library >** in the top menu and select the corresponding version. Check to see if the nodes are working and rendering properly, and when everything is OK, click **Bioxel Nodes > Save Node Library** and select the save location in the dialog box. 16 | -------------------------------------------------------------------------------- /docs/improve_performance.md: -------------------------------------------------------------------------------- 1 | # Improve Performance 2 | 3 | Volume reconstruction and volume rendering are very consuming computation, I believe that this add-on's experience is terrible on poorly hardware. here are tips to improve the add-on performance. 4 | 5 | ## Use Low-Res Data as Preview 6 | 7 | The addon provides data resample function, the operation is as follows. First, in the container's geometry node, select the layer you need to resample, right-click **Bioxel Nodes > Resample Value**, in the dialog box, change the "Bioxel Size" value to twice or more than the current value, click OK. the addon will create a low-res version of the layer and load it into the container's geometry node. 8 | 9 | ![alt text](assets/improve_performance/image.png) 10 | 11 | You can see that the speed of the reconstruction has been reduced from 240 ms to 37 ms, making it possible to compute in almost real-time, and improving the speed of the feedback of the changing the parameters. Once you are satisfied with the adjustment, you can then connect the original layer to the nodes. This tip can greatly improve the operation experience of the node. 12 | 13 | ## Raise the Step Rate of a Container 14 | 15 | Step rate is a key setting in volume rendering. The higher the step rate, the faster the rendering will be, but at the same time, the thinner the volume will look. If you want to have a very thin effect, or if you don't need to cut through component, you can always set the step rate high, you can adjust step rate in the render settings globaly. However, I recommend you to adjust the step rate of the container separately, so that it doesn't affect the rendering of the other volume in the Blender file, do as follows. 16 | 17 | In the container's geometry nodes panel menu, click **Bioxel Nodes > Change Container Properties**, in the dialog box, raise the "Step Rate" value (up to 100) and click OK. 18 | 19 | ![alt text](assets/improve_performance/image-1.png) 20 | 21 | You can see that the rendering speed is much higher, but at the same time the cuts look blurry and transparent. 22 | 23 | ## Balance rendering settings 24 | 25 | I list some of the settings that affect the volume rendering most, so you can find the most balanced settings for your needs. 26 | 27 | - **Light Paths > Max Bounces > Volume** affects the number of bounces a volume has, the higher the value, the more transparent it will look. 28 | 29 | - **Light Paths > Max Bounces > Transparent** affects the number of times transparent surfaces are transmitted, the higher the value, the more transparent it will look. 30 | 31 | - **Volumes > Step Rate Render | Viewport** affects the volume rendering step, the smaller the value, the more detailed the volume will look 32 | 33 | Bioxel Nodes provides some render setting presets for quick setup. In the top menu, click **Bioxel Nodes > Render Setting Presets**, including Performance (left), Balance (center), and Quality (right). Here is a comparison of them. 34 | 35 | ![alt text](assets/improve_performance/image-2.png) 36 | 37 | ## Rendering with EEVEE 38 | 39 | EEVEE doesn't render volume as well as Cycles, but it does have the advantage of rendering the volumetric data, and is even a better choice if you want to get a clear view of slice. And EEVEE's real-time rendering make it possible to create interactive stuff in Blender. 40 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | [中文文档](https://uj6xfhbzp0.feishu.cn/wiki/LPKEwjooSivxjskWHlCcQznjnNf?from=from_copylink) 2 | 3 | # Bioxel Nodes 4 | 5 | [![For Blender](https://img.shields.io/badge/Blender-orange?style=for-the-badge&logo=blender&logoColor=white&color=black)](https://blender.org/) 6 | ![GitHub License](https://img.shields.io/github/license/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 7 | ![GitHub Release](https://img.shields.io/github/v/release/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 8 | ![GitHub Repo stars](https://img.shields.io/github/stars/OmooLab/BioxelNodes?style=for-the-badge&labelColor=black) 9 | 10 | [![Discord](https://img.shields.io/discord/1265129134397587457?style=for-the-badge&logo=discord&label=Discord&labelColor=white&color=black)](https://discord.gg/pYkNyq2TjE) 11 | 12 | Bioxel Nodes is a Blender addon for scientific volumetric data visualization. It using Blender's powerful **Geometry Nodes** and **Cycles** to process and render volumetric data. 13 | 14 | ![cover](https://omoolab.github.io/BioxelNodes/latest/assets/cover.png) 15 | 16 | - Realistic rendering result, also support EEVEE NEXT. 17 | - Support multiple formats. 18 | - Support 4D volumetric data. 19 | - All kinds of cutters. 20 | - Simple and powerful nodes. 21 | - Based on blender natively, can work without addon. 22 | 23 | **Read the [getting started](https://omoolab.github.io/BioxelNodes/latest/installation) to begin your journey into visualizing volumetric data!** 24 | 25 | Welcome to our [discord server](https://discord.gg/pYkNyq2TjE), if you have any problems with this add-on. 26 | 27 | ## Support Multiple Formats 28 | 29 | | Format | EXT | 30 | | ------ | ---------------------------------------- | 31 | | DICOM | .dcm, .DCM, .DICOM, .ima, .IMA | 32 | | BMP | .bmp, .BMP | 33 | | JPEG | .jpg, .JPG, .jpeg, .JPEG | 34 | | PNG | .png, .PNG | 35 | | TIFF | .tif, .TIF, .tiff, .TIFF | 36 | | Nifti | .nia, .nii, .nii.gz, .hdr, .img, .img.gz | 37 | | Nrrd | .nrrd, .nhdr | 38 | | HDF5 | .hdf, .h4, .hdf4, .he2, .h5, .hdf5, .he5 | 39 | | OME | .ome.tiff, .ome.tif | 40 | | MRC | .mrc, .mrc.gz, .map, .map.gz | 41 | 42 | ## Support 4D volumetric data 43 | 44 | ![4d](https://omoolab.github.io/BioxelNodes/latest/assets/4d-time.gif) 45 | 46 | 🥰 4D volumetric data can also be imported into Blender. 47 | 48 | ## Support EEVEE NEXT 49 | 50 | ![eevee](https://omoolab.github.io/BioxelNodes/latest/assets/eevee.gif) 51 | 52 | 👍 EEVEE NEXT is absolutely AWESOME! Bioxel Nodes is fully support EEVEE NEXT now! However, there are some limitations: 53 | 54 | 1. Only one cutter supported. 55 | 2. EEVEE result is not that great as Cycles does. 56 | 57 | ## Known Limitations 58 | 59 | - Only works with Cycles CPU , Cycles GPU (OptiX), EEVEE 60 | - Section surface cannot be generated when convert to mesh (will be supported soon) 61 | 62 | ## Roadmap 63 | 64 | - Better multi-format import experience 65 | - One-click bake model with texture 66 | - AI Segmentation to Generate Labels 67 | -------------------------------------------------------------------------------- /docs/installation.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | **Currently only support Blender 4.2 or above, make sure you have the correct version of Blender.** 4 | 5 | ## Install in Blender (recommended) 6 | 7 | This is the most recommended way, in the top menu, click **Edit > Preferences**, in "Get Extensions" Section, enter "bio" in the search box, then click Install. due to the add-on size (25MB~50MB) you may need to wait a while. 8 | 9 | ![alt text](assets/installation/image.png) 10 | 11 | ## Blender Official Extensions Website 12 | 13 | Or you can visit the Blender official extensions website at [https://extensions.blender.org/add-ons/bioxelnodes/](https://extensions.blender.org/add-ons/bioxelnodes/) 14 | Click Get Add-on, open Blender, drag in Blender and follow the instructions to install. 15 | 16 | ## Manual Install 17 | 18 | You can also install _BioxelNodes\_{version}.zip_ manually by downloading from [bere](https://github.com/OmooLab/BioxelNodes/releases/latest). 19 | In the top menu, click **Edit > Preferences**, **Add-ons > Install from Disk** and select the Zip file you just downloaded. 20 | -------------------------------------------------------------------------------- /docs/step_by_step.md: -------------------------------------------------------------------------------- 1 | # Step by Step 2 | 3 | ## Download Data 4 | 5 | Here is the open data from [Visible Human Project (VHP)](https://www.nlm.nih.gov/research/visible/visible_human.html) for you to learn how to use this addon. It is a CT scan image of a male head, download and unzip it to a new directry for later usage. 6 | 7 | [VHP_M_CT_Head.zip](https://drive.google.com/file/d/1bBGpt5pQ0evr-0-f4KDNRnKPoUYj2bJ-/view?usp=drive_link) 8 | 9 | VHP original radiology data was shared in the proprietary format that predated DICOM, complicating its use. Now all data has been harmonized to standard DICOM and released in [NCI Imaging Data Commons (IDC)](https://portal.imaging.datacommons.cancer.gov/), you also can download data from IDC by yourself. 10 | 11 | ## Import Data 12 | 13 | In the top menu, click **Bioxel Nodes > Import Volumetric Data (Init) > as Scalar**, locate to the unzipped folder, select any DICOM file (don't select more than one, and don't select the folder either), and click **Import as Scalar**. 14 | 15 | ![alt text](assets/step_by_step/image.png) 16 | 17 | You can also choose to import data by dragging one DICOM file into the 3D viewport (some DICOM files don't have extension suffix and can't be dragged in). 18 | 19 | The process of reading the data may take a while, the bottom right corner of the Blender interface will show you the progress of the reading, and a dialog box will pop up when the reading is successful. Ignore all the options for now and just click OK, I will explain the purpose of these options later. 20 | 21 | The import process involves data conversion, so there is more waiting time, and the progress of the import is displayed in the lower right corner of the interface. After importing, the addon will create a new object, with a new geometry nodes that will serve as a "workbench" for manipulating the data, the new object is called the **Container**. The newly imported data is stored in the Container as a **Layer**, which is loaded into the Container's Geometry node via the "Fetch Layer" node (the red one), and be converted into a **Component** that can be rendered. 22 | 23 | ![alt text](assets/step_by_step/image-1.png) 24 | 25 | Next, let's preview the data in Blender. 26 | 27 | In the container's geometry nodes panel menu, click **Bioxel Nodes > Add a Slicer**. The addon will insert a node named "Slice" between the "Fetch Layer" and the "Output" and create a new plane object named "Slicer". Click the "Slice Viewer" button in the upper right corner of the 3D viewport to enter the preview mode, then move and rotate the "Slicer" object. You will see a slice image of the data, which should be familiar to anyone who has used any DICOM viewing software. 28 | 29 | ![alt text](assets/step_by_step/image-2.png) 30 | 31 | (If the volume disappears when you click the "Slice Viewer" button, save the file and restart Blender, it should be fixed, or you can turn on the Cycles rendering to see the slicer plane as well. The issue is caused by EEVEE's failure of reloading the shaders.) 32 | 33 | The "Slicer" node is used to display the slices of the data, with an external object as the location of the slice plane. This step is not necessary for visualization, but it provides a quick way to preview the data in Blender for user perception. Next, let's turn the volumetric data into a renderable object. 34 | 35 | ## Cutout the Skull 36 | 37 | Bone tends to have much higher CT values than soft tissue, so you can split bone and soft tissue by setting a threshold. In the Geometry Nodes panel menu of the container, click **Add > Bioxel Nodes > Component > Cutout by Threshold** to add a "Cutout" node and connect it between the "Fetch Layer" node and the "Output", and then set the "Threshold" parameter of the "Cutout by Threshold" node to 0.3 and turn on the "With Surface" option. Switch to viewport shading to "Render Preview". The node graph and the render result are shown below. 38 | 39 | ![alt text](assets/step_by_step/image-3.png) 40 | 41 | If you want the render result to be consistent with the above image, you also need to change the default light object type from "Point" to "Area" to increase the brightness, and change the Look in Color Management to "High Contrast". 42 | 43 | You can understand the role of the "Threshold" parameter in shaping the output by changing it. If you feel very laggy while draging the parameter, you can temporarily turn off the "With Surface" option in the "Cutout" node. When you are satisfied, turn it on again. In addition, the volume rendering is very computationally intensive, which is also a major cause of the lag, so you can adjust the parameters in "Slice Viewer" mode first, and then do the Cycles rendering when you are satisfied. 44 | 45 | You may consider using GPU for faster rendering, but please note that Bioxel Nodes only supports Optix GPUs due to its dependency on OSL (Open shader language), and the first time you turn on GPU rendering, you may need to wait for Blender to load the appropriate dependencies (the screen will get stuck), so please be patient. 46 | 47 | Although the output component looks like a mesh object, it retains its internal information through its volume, so when you cut through the component, you should see an inhomogeneous cross-section made up of volume, rather than just an empty shell like any mesh object. Let's cut the skull to see its complex internal structure. 48 | 49 | ## Cut and Color 50 | 51 | In the container's geometry nodes panel menu, click **Bioxel Nodes > Add a Cutter > Plane Cutter**, the addon will insert a "Cut" node and a "Object Cutter" node. Also, it will create a new plane object named "Plane Cutter" to the scene, at this point you should be able to see that the skull has been cut through as expected. 52 | 53 | Just like the "Slicer" object, move and rotate the "Plane Cutter" and the position and direction of the cut will change accordingly. Please adjust the Cutter object to a vertical orientation so that it cuts the skull vertically, as shown below. 54 | 55 | ![alt text](assets/step_by_step/image-4.png) 56 | 57 | The CT value of the skull is inhomogeneous, which reflects the difference in substance density of the bone. We can enhance the display of this difference by coloring. In the Geometry Nodes panel menu of the container, click **Add > Bioxel Nodes > Propetry > Set Color by Ramp 5** to add the "Set Color" node and connect it after any node. Set the node's parameters "From Min" to 0.3 and "From Max" to 0.5, as shown below. 58 | 59 | ![alt text](assets/step_by_step/image-5.png) 60 | 61 | When rendering is turned on, you can clearly see that the calvaria and teeth are colored red, meaning that the bone in these areas is denser. You can try to adjust the parameters of the "Set Color" node to recognize their roles. If you feel laggy when draging the parameters, you can temporarily turn off "With Surface" and switch to "Slice Viewer" mode. 62 | 63 | ## Transformation 64 | 65 | You may find that the position of the skull is a bit off the origin, this is due to the fact that the addon keeps the position information from the original data record during the import process. If you need to change the position, do not move the container (object) directly as you would in 3D viewport; the addon provides dedicated "Transform" node to handle transformations. 66 | 67 | In the Geometry Nodes panel menu of the container, click **Bioxel Nodes > Add a Locator**, the addon will insert the "Transform Parent" node and create a new empty object named "Locator". If you move, rotate, or scale the "Locator", the skull will Transform as well. If you also want the origin of the rotational transformation to be set at the geometric center of the skull, just add a "ReCenter" node (Add > Bioxel Nodes > Transform > ReCenter) in front of the "Transform Parent" node, as shown below. 68 | 69 | ![alt text](assets/step_by_step/image-6.png) 70 | 71 | Being used to moving objects directly in the 3D viewport, you may find it strange to have an extra step to transform component like this. This is in consideration of the fact that there may be multiple components involved in one container with different transform needs, as well as future development plans for resampling mechanism, which I'll explain in more detail later. 72 | 73 | ## Surface Mesh 74 | 75 | A mesh, made up of vertexs and faces, is the "greatest common" in the 3D world. Therefore, in order to be compatible with other 3D workflow, the addon provides the "To Surface" node (Add > Bioxel Nodes > Component > ToSurface), which converts the component into a mesh. Note that the surface mesh is not editable in the container's geometry node, and can only be connected to "Transform" and "Shader" nodes. 76 | 77 | As shown below, you can connect "Slime Shader" node (Add > Bioxel Nodes > Surface > Slime Shader) after the "To Surface" node to give the surface a shader, or connect "Transform" nodes. 78 | 79 | ![alt text](assets/step_by_step/image-7.png) 80 | 81 | If you want to edit the mesh, in the Geometry Nodes panel menu of the container, click **Bioxel Nodes > Extract from Container > Extract Mesh**, the addon will create a new mesh model object prefixed with the container name, and then you can perform the usual 3D operations, such as digitally sculpting, animating, exporting to 3D print format stl, etc. 82 | 83 | ## Deliver Blender File 84 | 85 | The layers cache is stored in a temporary folder, while the addon's custom nodes are linked to the node library file in addon directory. Both of them are exist locally by default, and if you only deliver the Blender file to other devices, the file will not work properly because the resources are missing. 86 | 87 | Therefore, you need to save all temporary files before you deliver the Blender file. The procedure is simple: 88 | 89 | 1. Save the Blender file. 90 | 2. in the top menu, click **Bioxel Nodes > Save Node Library**, and set the relative path. 91 | 3. in the top menu, click **Bioxel Nodes > Save All Layers Cache** and set the relative path. 92 | 93 | Zip the Blender file together with the local node library file and the layer cache files (there may be more than one). 94 | 95 | ![alt text](assets/step_by_step/image-9.png) 96 | 97 | 🤗 If you can follow the documentation up to this point, you're already started! 98 | -------------------------------------------------------------------------------- /docs/stylesheets/extra.css: -------------------------------------------------------------------------------- 1 | [data-md-color-scheme="slate"] { 2 | --md-code-hl-string-color: #8b8b8b; 3 | --md-code-hl-keyword-color: white; 4 | --md-code-hl-number-color: white; 5 | --md-typeset-a-color: white; 6 | } 7 | 8 | [data-md-color-scheme="default"] { 9 | --md-code-hl-string-color: #444444; 10 | --md-code-hl-keyword-color: black; 11 | --md-code-hl-number-color: black; 12 | } 13 | 14 | [data-md-color-scheme=slate][data-md-color-primary=black] { 15 | --md-typeset-a-color: white; 16 | } 17 | 18 | [data-md-color-scheme=default][data-md-color-primary=black] { 19 | --md-typeset-a-color: black; 20 | } -------------------------------------------------------------------------------- /docs/support_format.md: -------------------------------------------------------------------------------- 1 | # Support Format 2 | 3 | Can't wait to play with the addon using your own data? Here's a list of the formats currently supported. 4 | 5 | | Format | EXT | 6 | | ------ | ---------------------------------------- | 7 | | DICOM | .dcm, .DCM, .DICOM, .ima, .IMA | 8 | | BMP | .bmp, .BMP | 9 | | JPEG | .jpg, .JPG, .jpeg, .JPEG | 10 | | PNG | .png, .PNG | 11 | | TIFF | .tif, .TIF, .tiff, .TIFF | 12 | | Nifti | .nia, .nii, .nii.gz, .hdr, .img, .img.gz | 13 | | Nrrd | .nrrd, .nhdr | 14 | | HDF5 | .hdf, .h4, .hdf4, .he2, .h5, .hdf5, .he5 | 15 | | OME | .ome.tiff, .ome.tif | 16 | | MRC | .mrc, .mrc.gz, .map, .map.gz | 17 | 18 | Despite my best efforts, I still can't achieve perfect support for all volumetric data formats, and I will continue to work on this. 19 | 20 | ## Open Databases 21 | 22 | If you don't have volumetric data, then you can download from some open databases. 23 | 24 | **Note that just because they are open and available for download does not mean you can use them for anything! Be sure to look at the description of the available scopes from website.** 25 | 26 | | Source | Object | 27 | | ------------------------------------------------------------------------------------ | ------------------ | 28 | | [MorphoSource](https://www.morphosource.org/) | Open Research Data | 29 | | [Dryad](https://datadryad.org) | Open Research Data | 30 | | [Cell Image Library](http://cellimagelibrary.org/home) | Cells | 31 | | [OpenOrganelle](https://openorganelle.janelia.org/datasets) | Cells | 32 | | [Allen Cell Explorer](https://www.allencell.org/3d-cell-viewer.html) | Cells | 33 | | [EMDB](https://www.ebi.ac.uk/emdb/) | Protein, Viruses | 34 | | [IDC](https://portal.imaging.datacommons.cancer.gov/explore/) | Medical Images | 35 | | [Embodi3D](https://www.embodi3d.com/files/category/37-medical-scans/) | Medical Images | 36 | | [Github](https://github.com/sfikas/medical-imaging-datasets) | Medical Images | 37 | | [NIHR](https://nhsx.github.io/open-source-imaging-data-sets/) | Medical Images | 38 | | [Medical Segmentation Decathlon](http://medicaldecathlon.com/) | Medical Images | 39 | | [Visible Human Project](https://www.nlm.nih.gov/research/visible/visible_human.html) | Medical Images | 40 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Bioxel Nodes Handbook 2 | site_url: https://omoolab.github.io/BioxelNodes/ 3 | site_author: OmooLab 4 | site_description: Bioxel Nodes is a Blender add-on for scientific volumetric data visualization. 5 | 6 | repo_name: OmooLab/BioxelNodes 7 | repo_url: https://github.com/OmooLab/BioxelNodes 8 | 9 | nav: 10 | - index.md 11 | - Getting Started: 12 | - Installation: installation.md 13 | - Step by Step: step_by_step.md 14 | - Advanced Usage: advanced_usage.md 15 | - Support Format: support_format.md 16 | - Improve Performance: improve_performance.md 17 | - FAQ: faq.md 18 | - Tutorials: 19 | - SARS-Cov-2: SARS-Cov-2.md 20 | 21 | theme: 22 | name: material 23 | favicon: assets/logo.png 24 | logo: assets/logo.png 25 | language: en 26 | font: 27 | text: Open Sans 28 | code: Cascadia Code 29 | palette: 30 | # Palette toggle for automatic mode 31 | - media: "(prefers-color-scheme)" 32 | primary: black 33 | toggle: 34 | icon: fontawesome/solid/circle-half-stroke 35 | name: Switch to light mode 36 | 37 | # Palette toggle for light mode 38 | - media: "(prefers-color-scheme: light)" 39 | scheme: default 40 | primary: black 41 | toggle: 42 | icon: fontawesome/regular/moon 43 | name: Switch to dark mode 44 | 45 | # Palette toggle for dark mode 46 | - media: "(prefers-color-scheme: dark)" 47 | scheme: slate 48 | primary: black 49 | toggle: 50 | icon: fontawesome/solid/moon 51 | name: Switch to system preference 52 | features: 53 | - header.autohide 54 | - navigation.footer 55 | - navigation.indexes 56 | - navigation.sections 57 | - navigation.tabs 58 | - navigation.tabs.sticky 59 | - content.code.copy 60 | - content.code.select 61 | 62 | extra: 63 | version: 64 | provider: mike 65 | default: latest 66 | 67 | extra_css: 68 | - stylesheets/extra.css 69 | 70 | copyright: Copyright © 2020 - 2024 Omoolab 71 | 72 | plugins: 73 | - search 74 | - mkdocstrings 75 | 76 | markdown_extensions: 77 | - attr_list 78 | - md_in_html 79 | - mkdocs-click 80 | - admonition 81 | - pymdownx.highlight: 82 | anchor_linenums: true 83 | line_spans: __span 84 | pygments_lang_class: true 85 | - pymdownx.inlinehilite 86 | - pymdownx.snippets 87 | - pymdownx.details 88 | - pymdownx.superfences 89 | - pymdownx.tabbed: 90 | alternate_style: true 91 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "bioxelnodes" 3 | version = "1.0.9" 4 | description = "" 5 | authors = [{ name = "Ma Nan", email = "icrdr2010@outlook.com" }] 6 | requires-python = ">=3.11.0,<3.12.dev0" 7 | readme = "README.md" 8 | license = "MIT" 9 | dependencies = [ 10 | "simpleitk==2.3.1", 11 | "pyometiff==1.0.0", 12 | "mrcfile==1.5.1", 13 | "h5py==3.11.0", 14 | "transforms3d==0.4.2", 15 | ] 16 | 17 | [dependency-groups] 18 | dev = [ 19 | "bpy>=4.2.0,<5", 20 | "pytest>=8.3.2,<9", 21 | "pytest-dependency>=0.6.0,<0.7", 22 | "pytest-cov>=5.0.0,<6", 23 | "mkdocs>=1.6.0,<2", 24 | "mkdocs-material>=9.5.30,<10", 25 | "autopep8>=2.3.1,<3", 26 | "mike>=2.1.2,<3", 27 | "tomlkit>=0.13.0,<0.14", 28 | ] 29 | 30 | [build-system] 31 | requires = ["hatchling"] 32 | build-backend = "hatchling.build" 33 | -------------------------------------------------------------------------------- /scipy_ndimage/linux-x64/_nd_image.cpython-311-x86_64-linux-gnu.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/scipy_ndimage/linux-x64/_nd_image.cpython-311-x86_64-linux-gnu.so -------------------------------------------------------------------------------- /scipy_ndimage/macos-arm64/_nd_image.cpython-311-darwin.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/scipy_ndimage/macos-arm64/_nd_image.cpython-311-darwin.so -------------------------------------------------------------------------------- /scipy_ndimage/macos-x64/_nd_image.cpython-311-darwin.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/scipy_ndimage/macos-x64/_nd_image.cpython-311-darwin.so -------------------------------------------------------------------------------- /scipy_ndimage/windows-x64/_nd_image.cp311-win_amd64.pyd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/scipy_ndimage/windows-x64/_nd_image.cp311-win_amd64.pyd -------------------------------------------------------------------------------- /src/bioxelnodes/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | from .props import BIOXELNODES_LayerListUL 4 | from . import auto_load 5 | from . import menus 6 | 7 | 8 | auto_load.init() 9 | 10 | 11 | def register(): 12 | auto_load.register() 13 | bpy.types.WindowManager.bioxelnodes_progress_factor = bpy.props.FloatProperty( 14 | default=1.0) 15 | bpy.types.WindowManager.bioxelnodes_progress_text = bpy.props.StringProperty() 16 | bpy.types.WindowManager.bioxelnodes_layer_list_UL = bpy.props.PointerProperty( 17 | type=BIOXELNODES_LayerListUL) 18 | menus.add() 19 | 20 | 21 | def unregister(): 22 | menus.remove() 23 | del bpy.types.WindowManager.bioxelnodes_progress_factor 24 | del bpy.types.WindowManager.bioxelnodes_progress_text 25 | del bpy.types.WindowManager.bioxelnodes_layer_list_UL 26 | auto_load.unregister() 27 | -------------------------------------------------------------------------------- /src/bioxelnodes/assets/Nodes/BioxelNodes_latest.blend: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:a76a54a0ec3a2a279e6cc06288975c23ca0cb999dafd0d3fd8842ec3e310fc41 3 | size 9057934 4 | -------------------------------------------------------------------------------- /src/bioxelnodes/assets/Nodes/BioxelNodes_v0.2.9.blend: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b219a6f005718d8223766215a598ebde9b646c3960fb298d72ffc864791f3c3a 3 | size 6691383 4 | -------------------------------------------------------------------------------- /src/bioxelnodes/assets/Nodes/BioxelNodes_v0.3.3.blend: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:87831f56b9d23b8ee3eccfea02c27e8ff305723a079e2c115a7b059fb5e1cb24 3 | size 6803344 4 | -------------------------------------------------------------------------------- /src/bioxelnodes/auto_load.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | import typing 3 | import inspect 4 | import pkgutil 5 | import importlib 6 | from pathlib import Path 7 | 8 | __all__ = ( 9 | "init", 10 | "register", 11 | "unregister", 12 | ) 13 | 14 | blender_version = bpy.app.version 15 | 16 | modules = None 17 | ordered_classes = None 18 | 19 | 20 | def init(): 21 | global modules 22 | global ordered_classes 23 | 24 | modules = get_all_submodules(Path(__file__).parent) 25 | ordered_classes = get_ordered_classes_to_register(modules) 26 | 27 | 28 | def register(): 29 | for cls in ordered_classes: 30 | bpy.utils.register_class(cls) 31 | 32 | for module in modules: 33 | if module.__name__ == __name__: 34 | continue 35 | if hasattr(module, "register"): 36 | module.register() 37 | 38 | 39 | def unregister(): 40 | for cls in reversed(ordered_classes): 41 | bpy.utils.unregister_class(cls) 42 | 43 | for module in modules: 44 | if module.__name__ == __name__: 45 | continue 46 | if hasattr(module, "unregister"): 47 | module.unregister() 48 | 49 | 50 | # Import modules 51 | ################################################# 52 | 53 | 54 | def get_all_submodules(directory): 55 | return list(iter_submodules(directory, __package__)) 56 | 57 | 58 | def iter_submodules(path, package_name): 59 | for name in sorted(iter_submodule_names(path)): 60 | yield importlib.import_module("." + name, package_name) 61 | 62 | 63 | def iter_submodule_names(path, root=""): 64 | for _, module_name, is_package in pkgutil.iter_modules([str(path)]): 65 | if is_package: 66 | sub_path = path / module_name 67 | sub_root = root + module_name + "." 68 | yield from iter_submodule_names(sub_path, sub_root) 69 | else: 70 | yield root + module_name 71 | 72 | 73 | # Find classes to register 74 | ################################################# 75 | 76 | 77 | def get_ordered_classes_to_register(modules): 78 | return toposort(get_register_deps_dict(modules)) 79 | 80 | 81 | def get_register_deps_dict(modules): 82 | my_classes = set(iter_my_classes(modules)) 83 | my_classes_by_idname = {cls.bl_idname: cls for cls in my_classes if hasattr(cls, "bl_idname")} 84 | 85 | deps_dict = {} 86 | for cls in my_classes: 87 | deps_dict[cls] = set(iter_my_register_deps(cls, my_classes, my_classes_by_idname)) 88 | return deps_dict 89 | 90 | 91 | def iter_my_register_deps(cls, my_classes, my_classes_by_idname): 92 | yield from iter_my_deps_from_annotations(cls, my_classes) 93 | yield from iter_my_deps_from_parent_id(cls, my_classes_by_idname) 94 | 95 | 96 | def iter_my_deps_from_annotations(cls, my_classes): 97 | for value in typing.get_type_hints(cls, {}, {}).values(): 98 | dependency = get_dependency_from_annotation(value) 99 | if dependency is not None: 100 | if dependency in my_classes: 101 | yield dependency 102 | 103 | 104 | def get_dependency_from_annotation(value): 105 | if blender_version >= (2, 93): 106 | if isinstance(value, bpy.props._PropertyDeferred): 107 | return value.keywords.get("type") 108 | else: 109 | if isinstance(value, tuple) and len(value) == 2: 110 | if value[0] in (bpy.props.PointerProperty, bpy.props.CollectionProperty): 111 | return value[1]["type"] 112 | return None 113 | 114 | 115 | def iter_my_deps_from_parent_id(cls, my_classes_by_idname): 116 | if issubclass(cls, bpy.types.Panel): 117 | parent_idname = getattr(cls, "bl_parent_id", None) 118 | if parent_idname is not None: 119 | parent_cls = my_classes_by_idname.get(parent_idname) 120 | if parent_cls is not None: 121 | yield parent_cls 122 | 123 | 124 | def iter_my_classes(modules): 125 | base_types = get_register_base_types() 126 | for cls in get_classes_in_modules(modules): 127 | if any(issubclass(cls, base) for base in base_types): 128 | if not getattr(cls, "is_registered", False): 129 | yield cls 130 | 131 | 132 | def get_classes_in_modules(modules): 133 | classes = set() 134 | for module in modules: 135 | for cls in iter_classes_in_module(module): 136 | classes.add(cls) 137 | return classes 138 | 139 | 140 | def iter_classes_in_module(module): 141 | for value in module.__dict__.values(): 142 | if inspect.isclass(value): 143 | yield value 144 | 145 | 146 | def get_register_base_types(): 147 | return set( 148 | getattr(bpy.types, name) 149 | for name in [ 150 | "Panel", 151 | "Operator", 152 | "PropertyGroup", 153 | "AddonPreferences", 154 | "Header", 155 | "Menu", 156 | "Node", 157 | "NodeSocket", 158 | "NodeTree", 159 | "UIList", 160 | "RenderEngine", 161 | "Gizmo", 162 | "GizmoGroup", 163 | "FileHandler" 164 | ] 165 | ) 166 | 167 | 168 | # Find order to register to solve dependencies 169 | ################################################# 170 | 171 | 172 | def toposort(deps_dict): 173 | sorted_list = [] 174 | sorted_values = set() 175 | while len(deps_dict) > 0: 176 | unsorted = [] 177 | sorted_list_sub = [] # helper for additional sorting by bl_order - in panels 178 | for value, deps in deps_dict.items(): 179 | if len(deps) == 0: 180 | sorted_list_sub.append(value) 181 | sorted_values.add(value) 182 | else: 183 | unsorted.append(value) 184 | deps_dict = {value: deps_dict[value] - sorted_values for value in unsorted} 185 | sorted_list_sub.sort(key=lambda cls: getattr(cls, "bl_order", 0)) 186 | sorted_list.extend(sorted_list_sub) 187 | return sorted_list 188 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/src/bioxelnodes/bioxel/__init__.py -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/container.py: -------------------------------------------------------------------------------- 1 | from .layer import Layer 2 | 3 | 4 | class Container(): 5 | def __init__(self, 6 | name, 7 | layers: list[Layer] = []) -> None: 8 | self.name = name 9 | self.layers = layers 10 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/io.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import uuid 3 | 4 | 5 | # 3rd-party 6 | import h5py 7 | 8 | from .container import Container 9 | from .layer import Layer 10 | 11 | 12 | def load_container(load_file: str): 13 | load_path = Path(load_file).resolve() 14 | with h5py.File(load_path, 'r') as file: 15 | layers = [] 16 | for key, layer_dset in file['layers'].items(): 17 | layers.append(Layer(data=layer_dset[:], 18 | name=layer_dset.attrs['name'], 19 | kind=layer_dset.attrs['kind'], 20 | affine=layer_dset.attrs['affine'])) 21 | 22 | container = Container(name=file.attrs['name'], 23 | layers=layers) 24 | 25 | return container 26 | 27 | 28 | def save_container(container: Container, save_file: str, overwrite=False): 29 | save_path = Path(save_file).resolve() 30 | if overwrite: 31 | if save_path.is_file(): 32 | save_path.unlink() 33 | 34 | with h5py.File(save_path, "w") as file: 35 | file.attrs['name'] = container.name 36 | layer_group = file.create_group("layers") 37 | for layer in container.layers: 38 | layer_key = uuid.uuid4().hex[:8] 39 | layer_dset = layer_group.create_dataset(name=layer_key, 40 | data=layer.data) 41 | layer_dset.attrs['name'] = layer.name 42 | layer_dset.attrs['kind'] = layer.kind 43 | layer_dset.attrs['affine'] = layer.affine 44 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/layer.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import numpy as np 3 | 4 | from . import scipy 5 | from . import scipy as ndi 6 | 7 | # 3rd-party 8 | import transforms3d 9 | # TODO: turn to dataclasses 10 | 11 | 12 | class Layer(): 13 | def __init__(self, 14 | data: np.ndarray, 15 | name: str, 16 | kind="scalar", 17 | affine=np.identity(4)) -> None: 18 | if data.ndim != 5: 19 | raise Exception("Data shape order should be TXYZC") 20 | 21 | affine = np.array(affine) 22 | if affine.shape != (4, 4): 23 | raise Exception("affine shape should be (4,4)") 24 | 25 | self.data = data 26 | self.name = name 27 | self.kind = kind 28 | self.affine = affine 29 | 30 | @property 31 | def bioxel_size(self): 32 | t, r, z, s = transforms3d.affines.decompose44(self.affine) 33 | return z.tolist() 34 | 35 | @property 36 | def shape(self): 37 | return self.data.shape[1:4] 38 | 39 | @property 40 | def dtype(self): 41 | return self.data.dtype 42 | 43 | @property 44 | def origin(self): 45 | t, r, z, s = transforms3d.affines.decompose44(self.affine) 46 | return t.tolist() 47 | 48 | @property 49 | def euler(self): 50 | t, r, z, s = transforms3d.affines.decompose44(self.affine) 51 | return list(transforms3d.euler.mat2euler(r)) 52 | 53 | @property 54 | def min(self): 55 | return float(np.min(self.data)) 56 | 57 | @property 58 | def frame_count(self): 59 | return self.data.shape[0] 60 | 61 | @property 62 | def channel_count(self): 63 | return self.data.shape[-1] 64 | 65 | @property 66 | def max(self): 67 | return float(np.max(self.data)) 68 | 69 | def copy(self): 70 | return copy.deepcopy(self) 71 | 72 | def fill(self, value: float, mask: np.ndarray, smooth: int = 0): 73 | mask_frames = () 74 | if mask.ndim == 4: 75 | if mask.shape[0] != self.frame_count: 76 | raise Exception("Mask frame count is not same as ") 77 | for f in range(self.frame_count): 78 | mask_frame = mask[f, :, :, :] 79 | if smooth > 0: 80 | mask_frame = scipy.median_filter(mask_frame.astype(np.float32), 81 | mode="nearest", 82 | size=smooth) 83 | # mask_frame = scipy.median_filter( 84 | # mask_frame.astype(np.float32), size=2) 85 | mask_frames += (mask_frame,) 86 | elif mask.ndim == 3: 87 | for f in range(self.frame_count): 88 | mask_frame = mask[:, :, :] 89 | if smooth > 0: 90 | mask_frame = scipy.median_filter(mask_frame.astype(np.float32), 91 | mode="nearest", 92 | size=smooth) 93 | # mask_frame = scipy.median_filter( 94 | # mask_frame.astype(np.float32), size=2) 95 | mask_frames += (mask_frame,) 96 | else: 97 | raise Exception("Mask shape order should be TXYZ or XYZ") 98 | 99 | _mask = np.stack(mask_frames) 100 | _mask = np.expand_dims(_mask, axis=-1) 101 | self.data = _mask * value + (1-_mask) * self.data 102 | 103 | def resize(self, shape: tuple, smooth: int = 0, progress_callback=None): 104 | if len(shape) != 3: 105 | raise Exception("Shape must be 3 dim") 106 | 107 | data = self.data 108 | 109 | # # TXYZC > TXYZ 110 | # if self.kind in ['label', 'scalar']: 111 | # data = np.amax(data, -1) 112 | 113 | # if self.kind in ['scalar']: 114 | # dtype = data.dtype 115 | # data = data.astype(np.float32) 116 | 117 | data_frames = () 118 | for f in range(self.frame_count): 119 | if progress_callback: 120 | progress_callback(f, self.frame_count) 121 | 122 | frame = data[f, :, :, :, :] 123 | if smooth > 0: 124 | frame = scipy.median_filter(frame.astype(np.float32), 125 | mode="nearest", 126 | size=smooth) 127 | 128 | factors = np.divide(self.shape, shape) 129 | zoom_factors = [1 / f for f in factors] 130 | order = 0 if frame.dtype == bool else 1 131 | frame = ndi.zoom(frame, 132 | zoom_factors+[1.0], 133 | mode="nearest", 134 | grid_mode=False, 135 | order=order) 136 | if smooth > 0: 137 | frame = frame.astype(self.dtype) 138 | data_frames += (frame,) 139 | 140 | data = np.stack(data_frames) 141 | 142 | # if self.kind in ['scalar']: 143 | # data = data.astype(dtype) 144 | 145 | # TXYZ > TXYZC 146 | # if self.kind in ['label', 'scalar']: 147 | # data = np.expand_dims(data, axis=-1) # expend channel 148 | 149 | self.data = data 150 | 151 | mat_scale = transforms3d.zooms.zfdir2aff(factors[0]) 152 | self.affine = np.dot(self.affine, mat_scale) 153 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/parse.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import numpy as np 3 | 4 | # 3rd-party 5 | import SimpleITK as sitk 6 | from pyometiff import OMETIFFReader 7 | import mrcfile 8 | import transforms3d 9 | 10 | 11 | """ 12 | Convert any volumetric data to 3D numpy array with order TXYZC 13 | """ 14 | 15 | SUPPORT_EXTS = ['', '.dcm', '.DCM', '.DICOM', '.ima', '.IMA', 16 | '.ome.tiff', '.ome.tif', 17 | '.tif', '.TIF', '.tiff', '.TIFF', 18 | '.mrc', '.mrc.gz', '.map', '.map.gz', 19 | '.bmp', '.BMP', 20 | '.png', '.PNG', 21 | '.jpg', '.JPG', '.jpeg', '.JPEG', 22 | '.PIC', '.pic', 23 | '.gipl', '.gipl.gz', 24 | '.lsm', '.LSM', 25 | '.mnc', '.MNC', 26 | '.mrc', '.rec', 27 | '.mha', '.mhd', 28 | '.hdf', '.h4', '.hdf4', '.he2', '.h5', '.hdf5', '.he5', 29 | '.nia', '.nii', '.nii.gz', '.hdr', '.img', '.img.gz', 30 | '.nrrd', '.nhdr', 31 | '.vtk', 32 | '.gz'] 33 | 34 | OME_EXTS = ['.ome.tiff', '.ome.tif', 35 | '.tif', '.TIF', '.tiff', '.TIFF'] 36 | 37 | MRC_EXTS = ['.mrc', '.mrc.gz', '.map', '.map.gz'] 38 | 39 | DICOM_EXTS = ['', '.dcm', '.DCM', '.DICOM', '.ima', '.IMA'] 40 | 41 | SEQUENCE_EXTS = ['.bmp', '.BMP', 42 | '.jpg', '.JPG', '.jpeg', '.JPEG', 43 | '.tif', '.TIF', '.tiff', '.TIFF', 44 | '.png', '.PNG', 45 | '.mrc'] 46 | 47 | 48 | def get_ext(filepath: Path) -> str: 49 | if filepath.name.endswith(".nii.gz"): 50 | return ".nii.gz" 51 | elif filepath.name.endswith(".img.gz"): 52 | return ".img.gz" 53 | elif filepath.name.endswith(".gipl.gz"): 54 | return ".gipl.gz" 55 | elif filepath.name.endswith(".ome.tiff"): 56 | return ".ome.tiff" 57 | elif filepath.name.endswith(".ome.tif"): 58 | return ".ome.tif" 59 | elif filepath.name.endswith(".mrc.gz"): 60 | return ".mrc.gz" 61 | elif filepath.name.endswith(".map.gz"): 62 | return ".map.gz" 63 | else: 64 | suffix = filepath.suffix 65 | return "" if len(suffix) > 5 else suffix 66 | 67 | 68 | def get_filename(filepath: Path): 69 | ext = get_ext(filepath) 70 | return filepath.name.removesuffix(ext) 71 | 72 | 73 | def get_filename_parts(filepath: Path) -> str: 74 | def has_digits(s): 75 | return any(char.isdigit() for char in s) 76 | 77 | name = get_filename(filepath) 78 | parts = name.replace(".", " ").replace("_", " ").split(" ") 79 | skip_prefixs = ["CH", "ch", "channel"] 80 | number_part = None 81 | number_part_i = None 82 | 83 | for i, part in enumerate(parts[::-1]): 84 | if has_digits(part): 85 | if not any([part.startswith(prefix) for prefix in skip_prefixs]): 86 | number_part = part 87 | number_part_i = len(parts)-i 88 | break 89 | 90 | if number_part is None: 91 | return name, "", "" 92 | 93 | prefix_parts = parts[:number_part_i-1] 94 | prefix_parts_count = sum([len(part)+1 for part in prefix_parts]) 95 | 96 | digits = "" 97 | suffix = "" 98 | 99 | # Iterate through the characters in reverse order 100 | started = False 101 | for char in number_part[::-1]: 102 | if char.isdigit(): 103 | started = True 104 | # If the character is a digit, add it to the digits string 105 | digits += char 106 | else: 107 | if started: 108 | # If a non-digit character is encountered, stop the loop 109 | break 110 | else: 111 | suffix += char 112 | 113 | digits = digits[::-1] 114 | 115 | prefix_parts_count += len(number_part) - \ 116 | len(digits) - len(suffix) 117 | 118 | # Reverse the digits string to get the correct order 119 | prefix = name[:prefix_parts_count] 120 | suffix = name[prefix_parts_count+len(digits):] 121 | 122 | return prefix, digits, suffix 123 | 124 | 125 | def get_file_no_digits_name(filepath: Path) -> str: 126 | prefix, digits, suffix = get_filename_parts(filepath) 127 | prefix = remove_end_str(prefix, "_") 128 | prefix = remove_end_str(prefix, ".") 129 | prefix = remove_end_str(prefix, "-") 130 | prefix = remove_end_str(prefix, " ") 131 | return prefix + suffix 132 | 133 | 134 | def get_file_index(filepath: Path) -> int: 135 | prefix, digits, suffix = get_filename_parts(filepath) 136 | return int(digits) if digits != "" else 0 137 | 138 | 139 | def collect_sequence(filepath: Path): 140 | file_dict = {} 141 | for f in filepath.parent.iterdir(): 142 | if f.is_file() \ 143 | and get_ext(filepath) == get_ext(f) \ 144 | and get_file_no_digits_name(filepath) == get_file_no_digits_name(f): 145 | index = get_file_index(f) 146 | file_dict[index] = f 147 | 148 | # reomve isolated seq file 149 | for key in file_dict.copy().keys(): 150 | if not file_dict.get(key+1) \ 151 | and not file_dict.get(key-1): 152 | del file_dict[key] 153 | 154 | file_dict = dict(sorted(file_dict.items())) 155 | sequence = [str(f) for f in file_dict.values()] 156 | 157 | if len(sequence) == 0: 158 | sequence = [str(filepath)] 159 | 160 | return sequence 161 | 162 | 163 | def remove_end_str(string: str, end: str): 164 | while string.endswith(end) and len(string) > 0: 165 | string = string.removesuffix(end) 166 | return string 167 | 168 | 169 | def parse_volumetric_data(data_file: str, series_id="", progress_callback=None): 170 | """Parse any volumetric data to numpy with shap (T,X,Y,Z,C) 171 | 172 | Args: 173 | data_file (str): file path 174 | series_id (str, optional): DICOM series id. Defaults to "". 175 | 176 | Returns: 177 | _type_: _description_ 178 | """ 179 | 180 | data_path = Path(data_file).resolve() 181 | ext = get_ext(data_path) 182 | 183 | if progress_callback: 184 | progress_callback(0.0, "Reading the Data...") 185 | 186 | is_sequence = False 187 | if ext in SEQUENCE_EXTS: 188 | sequence = collect_sequence(data_path) 189 | if len(sequence) > 1: 190 | is_sequence = True 191 | 192 | data = None 193 | name = "", 194 | description = "" 195 | affine = np.identity(4) 196 | spacing = (1, 1, 1) 197 | origin = (0, 0, 0) 198 | direction = (1, 0, 0, 0, 1, 0, 0, 0, 1) 199 | 200 | # Parsing with mrcfile 201 | if data is None and ext in MRC_EXTS and not is_sequence: 202 | print("Parsing with mrcfile...") 203 | # TODO: much to do with mrc 204 | with mrcfile.open(data_path, 'r') as mrc: 205 | data = mrc.data 206 | # mrc.print_header() 207 | # print(data.shape) 208 | # print(mrc.voxel_size) 209 | 210 | if mrc.is_single_image(): 211 | data = np.expand_dims(data, axis=0) # expend frame 212 | data = np.expand_dims(data, axis=-1) # expend Z 213 | data = np.expand_dims(data, axis=-1) # expend channel 214 | 215 | elif mrc.is_image_stack(): 216 | data = np.expand_dims(data, axis=-1) # expend Z 217 | data = np.expand_dims(data, axis=-1) # expend channel 218 | 219 | elif mrc.is_volume(): 220 | data = np.expand_dims(data, axis=0) # expend frame 221 | data = np.expand_dims(data, axis=-1) # expend channel 222 | 223 | elif mrc.is_volume_stack(): 224 | data = np.expand_dims(data, axis=-1) # expend channel 225 | 226 | name = get_file_no_digits_name(data_path) 227 | spacing = (mrc.voxel_size.x, 228 | mrc.voxel_size.y, 229 | mrc.voxel_size.z) 230 | 231 | # Parsing with OMETIFFReader 232 | if data is None and ext in OME_EXTS and not is_sequence: 233 | print("Parsing with OMETIFFReader...") 234 | reader = OMETIFFReader(fpath=data_path) 235 | ome_image, metadata, xml_metadata = reader.read() 236 | 237 | # TODO: some old bio-format tiff the header is not the same. 238 | if progress_callback: 239 | progress_callback(0.5, "Transpose to 'TXYZC'...") 240 | 241 | try: 242 | # print(ome_image.shape) 243 | # for key in metadata: 244 | # print(f"{key},{metadata[key]}") 245 | ome_order = metadata['DimOrder BF Array'] 246 | if ome_image.ndim == 2: 247 | ome_order = ome_order.replace("T", "")\ 248 | .replace("C", "").replace("Z", "") 249 | bioxel_order = (ome_order.index('X'), 250 | ome_order.index('Y')) 251 | data = np.transpose(ome_image, bioxel_order) 252 | data = np.expand_dims(data, axis=0) # expend frame 253 | data = np.expand_dims(data, axis=-1) # expend Z 254 | data = np.expand_dims(data, axis=-1) # expend channel 255 | 256 | elif ome_image.ndim == 3: 257 | # -> XYZC 258 | ome_order = ome_order.replace("T", "").replace("C", "") 259 | bioxel_order = (ome_order.index('X'), 260 | ome_order.index('Y'), 261 | ome_order.index('Z')) 262 | data = np.transpose(ome_image, bioxel_order) 263 | data = np.expand_dims(data, axis=0) # expend frame 264 | data = np.expand_dims(data, axis=-1) # expend channel 265 | elif ome_image.ndim == 4: 266 | # -> XYZC 267 | ome_order = ome_order.replace("T", "") 268 | bioxel_order = (ome_order.index('X'), 269 | ome_order.index('Y'), 270 | ome_order.index('Z'), 271 | ome_order.index('C')) 272 | data = np.transpose(ome_image, bioxel_order) 273 | data = np.expand_dims(data, axis=0) # expend frame 274 | elif ome_image.ndim == 5: 275 | # -> TXYZC 276 | bioxel_order = (ome_order.index('T'), 277 | ome_order.index('X'), 278 | ome_order.index('Y'), 279 | ome_order.index('Z'), 280 | ome_order.index('C')) 281 | data = np.transpose(ome_image, bioxel_order) 282 | 283 | try: 284 | spacing = (metadata['PhysicalSizeX'], 285 | metadata['PhysicalSizeY'], 286 | metadata['PhysicalSizeZ']) 287 | except: 288 | ... 289 | 290 | name = get_file_no_digits_name(data_path) 291 | except: 292 | ... 293 | 294 | # Parsing with SimpleITK 295 | if data is None: 296 | print("Parsing with SimpleITK...") 297 | if ext in DICOM_EXTS: 298 | data_dirpath = data_path.parent 299 | reader = sitk.ImageSeriesReader() 300 | reader.MetaDataDictionaryArrayUpdateOn() 301 | reader.LoadPrivateTagsOn() 302 | series_files = reader.GetGDCMSeriesFileNames( 303 | str(data_dirpath), series_id) 304 | reader.SetFileNames(series_files) 305 | 306 | itk_image = reader.Execute() 307 | # for k in reader.GetMetaDataKeys(0): 308 | # v = reader.GetMetaData(0, k) 309 | # print(f'({k}) = = "{v}"') 310 | 311 | def get_meta(key): 312 | try: 313 | stirng = reader.GetMetaData(0, key).removesuffix(" ") 314 | stirng.encode('utf-8') 315 | if stirng in ["No study description", 316 | "No series description", 317 | ""]: 318 | return None 319 | else: 320 | return stirng 321 | except: 322 | return None 323 | 324 | study_description = get_meta("0008|1030") 325 | series_description = get_meta("0008|103e") 326 | series_modality = get_meta("0008|0060") 327 | 328 | name = study_description or data_dirpath.name 329 | if series_description and series_modality: 330 | description = f"{series_description}-{series_modality}" 331 | elif series_description: 332 | description = series_description 333 | elif series_modality: 334 | description = series_modality 335 | else: 336 | description = "" 337 | 338 | name = name.replace(" ", "-") 339 | description = description.replace(" ", "-") 340 | 341 | elif ext in SEQUENCE_EXTS and is_sequence: 342 | itk_image = sitk.ReadImage(sequence) 343 | name = get_file_no_digits_name(data_path) 344 | else: 345 | itk_image = sitk.ReadImage(data_path) 346 | name = get_filename(data_path) 347 | 348 | # for key in itk_image.GetMetaDataKeys(): 349 | # print(f"{key},{itk_image.GetMetaData(key)}") 350 | 351 | if progress_callback: 352 | progress_callback(0.5, "Transpose to 'TXYZC'...") 353 | 354 | if itk_image.GetDimension() == 2: 355 | 356 | data = sitk.GetArrayFromImage(itk_image) 357 | 358 | if data.ndim == 3: 359 | data = np.transpose(data, (1, 0, 2)) 360 | 361 | data = np.expand_dims(data, axis=-2) # expend Z 362 | else: 363 | data = np.transpose(data) 364 | data = np.expand_dims(data, axis=-1) # expend Z 365 | data = np.expand_dims(data, axis=-1) # expend channel 366 | 367 | data = np.expand_dims(data, axis=0) # expend frame 368 | 369 | elif itk_image.GetDimension() == 3: 370 | if ext not in SEQUENCE_EXTS: 371 | itk_image = sitk.DICOMOrient(itk_image, 'RAS') 372 | # After sitk.DICOMOrient(), origin and direction will also orient base on LPS 373 | # so we need to convert them into RAS 374 | # affine = axis_conversion(from_forward='-Z', 375 | # from_up='-Y', 376 | # to_forward='-Z', 377 | # to_up='Y').to_4x4() 378 | 379 | affine = np.array([[-1.0000, 0.0000, 0.0000, 0.0000], 380 | [0.0000, -1.0000, 0.0000, 0.0000], 381 | [0.0000, 0.0000, 1.0000, 0.0000], 382 | [0.0000, 0.0000, 0.0000, 1.0000]]) 383 | 384 | spacing = tuple(itk_image.GetSpacing()) 385 | origin = tuple(itk_image.GetOrigin()) 386 | direction = tuple(itk_image.GetDirection()) 387 | 388 | data = sitk.GetArrayFromImage(itk_image) 389 | # transpose ijk to kji 390 | if data.ndim == 4: 391 | data = np.transpose(data, (2, 1, 0, 3)) 392 | else: 393 | data = np.transpose(data) 394 | data = np.expand_dims(data, axis=-1) # expend channel 395 | 396 | data = np.expand_dims(data, axis=0) # expend frame 397 | 398 | elif itk_image.GetDimension() == 4: 399 | 400 | spacing = tuple(itk_image.GetSpacing()[:3]) 401 | origin = tuple(itk_image.GetOrigin()[:3]) 402 | # FIXME: not sure... 403 | direction = np.array(itk_image.GetDirection()) 404 | direction = direction.reshape(3, 3) if itk_image.GetDimension() == 3 \ 405 | else direction.reshape(4, 4) 406 | 407 | direction = direction[1:, 1:] 408 | direction = tuple(direction.flatten()) 409 | 410 | data = sitk.GetArrayFromImage(itk_image) 411 | 412 | if data.ndim == 5: 413 | data = np.transpose(data, (0, 3, 2, 1, 4)) 414 | else: 415 | data = np.transpose(data, (0, 3, 2, 1)) 416 | data = np.expand_dims(data, axis=-1) 417 | 418 | if itk_image.GetDimension() > 5: 419 | raise Exception 420 | 421 | t = origin 422 | r = np.array(direction).reshape((3, 3)) 423 | affine = np.dot(affine, 424 | transforms3d.affines.compose(t, r, [1, 1, 1])) 425 | 426 | meta = { 427 | "name": name, 428 | "description": description, 429 | "spacing": spacing, 430 | "affine": affine, 431 | "xyz_shape": data.shape[1:4], 432 | "frame_count": data.shape[0], 433 | "channel_count": data.shape[-1], 434 | } 435 | 436 | return data, meta 437 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/scipy/__init__.py: -------------------------------------------------------------------------------- 1 | from ._interpolation import zoom 2 | from ._filters import (gaussian_filter, 3 | median_filter, 4 | maximum_filter, 5 | minimum_filter) 6 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/scipy/_interpolation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import warnings 3 | 4 | from . import _ni_support 5 | from . import _nd_image 6 | from ._utils import normalize_axis_index 7 | 8 | 9 | def _prepad_for_spline_filter(input, mode, cval): 10 | if mode in ['nearest', 'grid-constant']: 11 | npad = 12 12 | if mode == 'grid-constant': 13 | padded = np.pad(input, npad, mode='constant', 14 | constant_values=cval) 15 | elif mode == 'nearest': 16 | padded = np.pad(input, npad, mode='edge') 17 | else: 18 | # other modes have exact boundary conditions implemented so 19 | # no prepadding is needed 20 | npad = 0 21 | padded = input 22 | return padded, npad 23 | 24 | def spline_filter1d(input, order=3, axis=-1, output=np.float64, 25 | mode='mirror'): 26 | """ 27 | Calculate a 1-D spline filter along the given axis. 28 | 29 | The lines of the array along the given axis are filtered by a 30 | spline filter. The order of the spline must be >= 2 and <= 5. 31 | 32 | Parameters 33 | ---------- 34 | %(input)s 35 | order : int, optional 36 | The order of the spline, default is 3. 37 | axis : int, optional 38 | The axis along which the spline filter is applied. Default is the last 39 | axis. 40 | output : ndarray or dtype, optional 41 | The array in which to place the output, or the dtype of the returned 42 | array. Default is ``numpy.float64``. 43 | %(mode_interp_mirror)s 44 | 45 | Returns 46 | ------- 47 | spline_filter1d : ndarray 48 | The filtered input. 49 | 50 | See Also 51 | -------- 52 | spline_filter : Multidimensional spline filter. 53 | 54 | Notes 55 | ----- 56 | All of the interpolation functions in `ndimage` do spline interpolation of 57 | the input image. If using B-splines of `order > 1`, the input image 58 | values have to be converted to B-spline coefficients first, which is 59 | done by applying this 1-D filter sequentially along all 60 | axes of the input. All functions that require B-spline coefficients 61 | will automatically filter their inputs, a behavior controllable with 62 | the `prefilter` keyword argument. For functions that accept a `mode` 63 | parameter, the result will only be correct if it matches the `mode` 64 | used when filtering. 65 | 66 | For complex-valued `input`, this function processes the real and imaginary 67 | components independently. 68 | 69 | .. versionadded:: 1.6.0 70 | Complex-valued support added. 71 | 72 | Examples 73 | -------- 74 | We can filter an image using 1-D spline along the given axis: 75 | 76 | >>> from scipy.ndimage import spline_filter1d 77 | >>> import numpy as np 78 | >>> import matplotlib.pyplot as plt 79 | >>> orig_img = np.eye(20) # create an image 80 | >>> orig_img[10, :] = 1.0 81 | >>> sp_filter_axis_0 = spline_filter1d(orig_img, axis=0) 82 | >>> sp_filter_axis_1 = spline_filter1d(orig_img, axis=1) 83 | >>> f, ax = plt.subplots(1, 3, sharex=True) 84 | >>> for ind, data in enumerate([[orig_img, "original image"], 85 | ... [sp_filter_axis_0, "spline filter (axis=0)"], 86 | ... [sp_filter_axis_1, "spline filter (axis=1)"]]): 87 | ... ax[ind].imshow(data[0], cmap='gray_r') 88 | ... ax[ind].set_title(data[1]) 89 | >>> plt.tight_layout() 90 | >>> plt.show() 91 | 92 | """ 93 | if order < 0 or order > 5: 94 | raise RuntimeError('spline order not supported') 95 | input = np.asarray(input) 96 | complex_output = np.iscomplexobj(input) 97 | output = _ni_support._get_output(output, input, 98 | complex_output=complex_output) 99 | if complex_output: 100 | spline_filter1d(input.real, order, axis, output.real, mode) 101 | spline_filter1d(input.imag, order, axis, output.imag, mode) 102 | return output 103 | if order in [0, 1]: 104 | output[...] = np.array(input) 105 | else: 106 | mode = _ni_support._extend_mode_to_code(mode) 107 | axis = normalize_axis_index(axis, input.ndim) 108 | _nd_image.spline_filter1d(input, order, axis, output, mode) 109 | return output 110 | 111 | 112 | def spline_filter(input, order=3, output=np.float64, mode='mirror'): 113 | """ 114 | Multidimensional spline filter. 115 | 116 | Parameters 117 | ---------- 118 | %(input)s 119 | order : int, optional 120 | The order of the spline, default is 3. 121 | output : ndarray or dtype, optional 122 | The array in which to place the output, or the dtype of the returned 123 | array. Default is ``numpy.float64``. 124 | %(mode_interp_mirror)s 125 | 126 | Returns 127 | ------- 128 | spline_filter : ndarray 129 | Filtered array. Has the same shape as `input`. 130 | 131 | See Also 132 | -------- 133 | spline_filter1d : Calculate a 1-D spline filter along the given axis. 134 | 135 | Notes 136 | ----- 137 | The multidimensional filter is implemented as a sequence of 138 | 1-D spline filters. The intermediate arrays are stored 139 | in the same data type as the output. Therefore, for output types 140 | with a limited precision, the results may be imprecise because 141 | intermediate results may be stored with insufficient precision. 142 | 143 | For complex-valued `input`, this function processes the real and imaginary 144 | components independently. 145 | 146 | .. versionadded:: 1.6.0 147 | Complex-valued support added. 148 | 149 | Examples 150 | -------- 151 | We can filter an image using multidimentional splines: 152 | 153 | >>> from scipy.ndimage import spline_filter 154 | >>> import numpy as np 155 | >>> import matplotlib.pyplot as plt 156 | >>> orig_img = np.eye(20) # create an image 157 | >>> orig_img[10, :] = 1.0 158 | >>> sp_filter = spline_filter(orig_img, order=3) 159 | >>> f, ax = plt.subplots(1, 2, sharex=True) 160 | >>> for ind, data in enumerate([[orig_img, "original image"], 161 | ... [sp_filter, "spline filter"]]): 162 | ... ax[ind].imshow(data[0], cmap='gray_r') 163 | ... ax[ind].set_title(data[1]) 164 | >>> plt.tight_layout() 165 | >>> plt.show() 166 | 167 | """ 168 | if order < 2 or order > 5: 169 | raise RuntimeError('spline order not supported') 170 | input = np.asarray(input) 171 | complex_output = np.iscomplexobj(input) 172 | output = _ni_support._get_output(output, input, 173 | complex_output=complex_output) 174 | if complex_output: 175 | spline_filter(input.real, order, output.real, mode) 176 | spline_filter(input.imag, order, output.imag, mode) 177 | return output 178 | if order not in [0, 1] and input.ndim > 0: 179 | for axis in range(input.ndim): 180 | spline_filter1d(input, order, axis, output=output, mode=mode) 181 | input = output 182 | else: 183 | output[...] = input[...] 184 | return output 185 | 186 | 187 | def zoom(input, zoom, output=None, order=3, mode='constant', cval=0.0, 188 | prefilter=True, *, grid_mode=False): 189 | """ 190 | Zoom an array. 191 | 192 | The array is zoomed using spline interpolation of the requested order. 193 | 194 | Parameters 195 | ---------- 196 | %(input)s 197 | zoom : float or sequence 198 | The zoom factor along the axes. If a float, `zoom` is the same for each 199 | axis. If a sequence, `zoom` should contain one value for each axis. 200 | %(output)s 201 | order : int, optional 202 | The order of the spline interpolation, default is 3. 203 | The order has to be in the range 0-5. 204 | %(mode_interp_constant)s 205 | %(cval)s 206 | %(prefilter)s 207 | grid_mode : bool, optional 208 | If False, the distance from the pixel centers is zoomed. Otherwise, the 209 | distance including the full pixel extent is used. For example, a 1d 210 | signal of length 5 is considered to have length 4 when `grid_mode` is 211 | False, but length 5 when `grid_mode` is True. See the following 212 | visual illustration: 213 | 214 | .. code-block:: text 215 | 216 | | pixel 1 | pixel 2 | pixel 3 | pixel 4 | pixel 5 | 217 | |<-------------------------------------->| 218 | vs. 219 | |<----------------------------------------------->| 220 | 221 | The starting point of the arrow in the diagram above corresponds to 222 | coordinate location 0 in each mode. 223 | 224 | Returns 225 | ------- 226 | zoom : ndarray 227 | The zoomed input. 228 | 229 | Notes 230 | ----- 231 | For complex-valued `input`, this function zooms the real and imaginary 232 | components independently. 233 | 234 | .. versionadded:: 1.6.0 235 | Complex-valued support added. 236 | 237 | Examples 238 | -------- 239 | >>> from scipy import ndimage, datasets 240 | >>> import matplotlib.pyplot as plt 241 | 242 | >>> fig = plt.figure() 243 | >>> ax1 = fig.add_subplot(121) # left side 244 | >>> ax2 = fig.add_subplot(122) # right side 245 | >>> ascent = datasets.ascent() 246 | >>> result = ndimage.zoom(ascent, 3.0) 247 | >>> ax1.imshow(ascent, vmin=0, vmax=255) 248 | >>> ax2.imshow(result, vmin=0, vmax=255) 249 | >>> plt.show() 250 | 251 | >>> print(ascent.shape) 252 | (512, 512) 253 | 254 | >>> print(result.shape) 255 | (1536, 1536) 256 | """ 257 | if order < 0 or order > 5: 258 | raise RuntimeError('spline order not supported') 259 | input = np.asarray(input) 260 | if input.ndim < 1: 261 | raise RuntimeError('input and output rank must be > 0') 262 | zoom = _ni_support._normalize_sequence(zoom, input.ndim) 263 | output_shape = tuple( 264 | [int(round(ii * jj)) for ii, jj in zip(input.shape, zoom)]) 265 | complex_output = np.iscomplexobj(input) 266 | output = _ni_support._get_output(output, input, shape=output_shape, 267 | complex_output=complex_output) 268 | if complex_output: 269 | # import under different name to avoid confusion with zoom parameter 270 | from scipy.ndimage._interpolation import zoom as _zoom 271 | 272 | kwargs = dict(order=order, mode=mode, prefilter=prefilter) 273 | _zoom(input.real, zoom, output=output.real, 274 | cval=np.real(cval), **kwargs) 275 | _zoom(input.imag, zoom, output=output.imag, 276 | cval=np.imag(cval), **kwargs) 277 | return output 278 | if prefilter and order > 1: 279 | padded, npad = _prepad_for_spline_filter(input, mode, cval) 280 | filtered = spline_filter(padded, order, output=np.float64, mode=mode) 281 | else: 282 | npad = 0 283 | filtered = input 284 | if grid_mode: 285 | # warn about modes that may have surprising behavior 286 | suggest_mode = None 287 | if mode == 'constant': 288 | suggest_mode = 'grid-constant' 289 | elif mode == 'wrap': 290 | suggest_mode = 'grid-wrap' 291 | if suggest_mode is not None: 292 | warnings.warn( 293 | (f"It is recommended to use mode = {suggest_mode} instead of {mode} " 294 | f"when grid_mode is True."), 295 | stacklevel=2 296 | ) 297 | mode = _ni_support._extend_mode_to_code(mode) 298 | 299 | zoom_div = np.array(output_shape) 300 | zoom_nominator = np.array(input.shape) 301 | if not grid_mode: 302 | zoom_div -= 1 303 | zoom_nominator -= 1 304 | 305 | # Zooming to infinite values is unpredictable, so just choose 306 | # zoom factor 1 instead 307 | zoom = np.divide(zoom_nominator, zoom_div, 308 | out=np.ones_like(input.shape, dtype=np.float64), 309 | where=zoom_div != 0) 310 | zoom = np.ascontiguousarray(zoom) 311 | _nd_image.zoom_shift(filtered, zoom, None, output, order, mode, cval, npad, 312 | grid_mode) 313 | return output 314 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/scipy/_ni_support.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2003-2005 Peter J. Verveer 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above 11 | # copyright notice, this list of conditions and the following 12 | # disclaimer in the documentation and/or other materials provided 13 | # with the distribution. 14 | # 15 | # 3. The name of the author may not be used to endorse or promote 16 | # products derived from this software without specific prior 17 | # written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 | # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | from collections.abc import Iterable 32 | import operator 33 | import warnings 34 | import numpy as np 35 | 36 | 37 | def _extend_mode_to_code(mode): 38 | """Convert an extension mode to the corresponding integer code. 39 | """ 40 | if mode == 'nearest': 41 | return 0 42 | elif mode == 'wrap': 43 | return 1 44 | elif mode in ['reflect', 'grid-mirror']: 45 | return 2 46 | elif mode == 'mirror': 47 | return 3 48 | elif mode == 'constant': 49 | return 4 50 | elif mode == 'grid-wrap': 51 | return 5 52 | elif mode == 'grid-constant': 53 | return 6 54 | else: 55 | raise RuntimeError('boundary mode not supported') 56 | 57 | 58 | def _normalize_sequence(input, rank): 59 | """If input is a scalar, create a sequence of length equal to the 60 | rank by duplicating the input. If input is a sequence, 61 | check if its length is equal to the length of array. 62 | """ 63 | is_str = isinstance(input, str) 64 | if not is_str and isinstance(input, Iterable): 65 | normalized = list(input) 66 | if len(normalized) != rank: 67 | err = "sequence argument must have length equal to input rank" 68 | raise RuntimeError(err) 69 | else: 70 | normalized = [input] * rank 71 | return normalized 72 | 73 | 74 | def _get_output(output, input, shape=None, complex_output=False): 75 | if shape is None: 76 | shape = input.shape 77 | if output is None: 78 | if not complex_output: 79 | output = np.zeros(shape, dtype=input.dtype.name) 80 | else: 81 | complex_type = np.promote_types(input.dtype, np.complex64) 82 | output = np.zeros(shape, dtype=complex_type) 83 | elif isinstance(output, (type, np.dtype)): 84 | # Classes (like `np.float32`) and dtypes are interpreted as dtype 85 | if complex_output and np.dtype(output).kind != 'c': 86 | warnings.warn("promoting specified output dtype to complex", stacklevel=3) 87 | output = np.promote_types(output, np.complex64) 88 | output = np.zeros(shape, dtype=output) 89 | elif isinstance(output, str): 90 | output = np.dtype(output) 91 | if complex_output and output.kind != 'c': 92 | raise RuntimeError("output must have complex dtype") 93 | elif not issubclass(output.type, np.number): 94 | raise RuntimeError("output must have numeric dtype") 95 | output = np.zeros(shape, dtype=output) 96 | elif output.shape != shape: 97 | raise RuntimeError("output shape not correct") 98 | elif complex_output and output.dtype.kind != 'c': 99 | raise RuntimeError("output must have complex dtype") 100 | return output 101 | 102 | 103 | def _check_axes(axes, ndim): 104 | if axes is None: 105 | return tuple(range(ndim)) 106 | elif np.isscalar(axes): 107 | axes = (operator.index(axes),) 108 | elif isinstance(axes, Iterable): 109 | for ax in axes: 110 | axes = tuple(operator.index(ax) for ax in axes) 111 | if ax < -ndim or ax > ndim - 1: 112 | raise ValueError(f"specified axis: {ax} is out of range") 113 | axes = tuple(ax % ndim if ax < 0 else ax for ax in axes) 114 | else: 115 | message = "axes must be an integer, iterable of integers, or None" 116 | raise ValueError(message) 117 | if len(tuple(set(axes))) != len(axes): 118 | raise ValueError("axes must be unique") 119 | return axes 120 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/scipy/_utils.py: -------------------------------------------------------------------------------- 1 | 2 | def normalize_axis_index(axis, ndim): 3 | # Check if `axis` is in the correct range and normalize it 4 | if axis < -ndim or axis >= ndim: 5 | msg = f"axis {axis} is out of bounds for array of dimension {ndim}" 6 | raise Exception(msg) 7 | 8 | if axis < 0: 9 | axis = axis + ndim 10 | return axis -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/skimage/__init__.py: -------------------------------------------------------------------------------- 1 | from ._warps import resize 2 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/skimage/_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def convert_to_float(image, preserve_range): 5 | """Convert input image to float image with the appropriate range. 6 | 7 | Parameters 8 | ---------- 9 | image : ndarray 10 | Input image. 11 | preserve_range : bool 12 | Determines if the range of the image should be kept or transformed 13 | using img_as_float. Also see 14 | https://scikit-image.org/docs/dev/user_guide/data_types.html 15 | 16 | Notes 17 | ----- 18 | * Input images with `float32` data type are not upcast. 19 | 20 | Returns 21 | ------- 22 | image : ndarray 23 | Transformed version of the input. 24 | 25 | """ 26 | if image.dtype == np.float16: 27 | return image.astype(np.float32) 28 | if preserve_range: 29 | # Convert image to double only if it is not single or double 30 | # precision float 31 | if image.dtype.char not in 'df': 32 | image = image.astype(float) 33 | else: 34 | from .dtype import img_as_float 35 | 36 | image = img_as_float(image) 37 | return image 38 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxel/skimage/_warps.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | import numpy as np 3 | from .. import scipy as ndi 4 | from ._utils import convert_to_float 5 | 6 | def _clip_warp_output(input_image, output_image, mode, cval, clip): 7 | """Clip output image to range of values of input image. 8 | 9 | Note that this function modifies the values of `output_image` in-place 10 | and it is only modified if ``clip=True``. 11 | 12 | Parameters 13 | ---------- 14 | input_image : ndarray 15 | Input image. 16 | output_image : ndarray 17 | Output image, which is modified in-place. 18 | 19 | Other parameters 20 | ---------------- 21 | mode : {'constant', 'edge', 'symmetric', 'reflect', 'wrap'} 22 | Points outside the boundaries of the input are filled according 23 | to the given mode. Modes match the behaviour of `numpy.pad`. 24 | cval : float 25 | Used in conjunction with mode 'constant', the value outside 26 | the image boundaries. 27 | clip : bool 28 | Whether to clip the output to the range of values of the input image. 29 | This is enabled by default, since higher order interpolation may 30 | produce values outside the given input range. 31 | 32 | """ 33 | if clip: 34 | min_val = np.min(input_image) 35 | if np.isnan(min_val): 36 | # NaNs detected, use NaN-safe min/max 37 | min_func = np.nanmin 38 | max_func = np.nanmax 39 | min_val = min_func(input_image) 40 | else: 41 | min_func = np.min 42 | max_func = np.max 43 | max_val = max_func(input_image) 44 | 45 | # Check if cval has been used such that it expands the effective input 46 | # range 47 | preserve_cval = ( 48 | mode == 'constant' 49 | and not min_val <= cval <= max_val 50 | and min_func(output_image) <= cval <= max_func(output_image) 51 | ) 52 | 53 | # expand min/max range to account for cval 54 | if preserve_cval: 55 | # cast cval to the same dtype as the input image 56 | cval = input_image.dtype.type(cval) 57 | min_val = min(min_val, cval) 58 | max_val = max(max_val, cval) 59 | 60 | # Convert array-like types to ndarrays (gh-7159) 61 | min_val, max_val = np.asarray(min_val), np.asarray(max_val) 62 | np.clip(output_image, min_val, max_val, out=output_image) 63 | 64 | 65 | def _validate_interpolation_order(image_dtype, order): 66 | """Validate and return spline interpolation's order. 67 | 68 | Parameters 69 | ---------- 70 | image_dtype : dtype 71 | Image dtype. 72 | order : int, optional 73 | The order of the spline interpolation. The order has to be in 74 | the range 0-5. See `skimage.transform.warp` for detail. 75 | 76 | Returns 77 | ------- 78 | order : int 79 | if input order is None, returns 0 if image_dtype is bool and 1 80 | otherwise. Otherwise, image_dtype is checked and input order 81 | is validated accordingly (order > 0 is not supported for bool 82 | image dtype) 83 | 84 | """ 85 | 86 | if order is None: 87 | return 0 if image_dtype == bool else 1 88 | 89 | if order < 0 or order > 5: 90 | raise ValueError( 91 | "Spline interpolation order has to be in the " "range 0-5.") 92 | 93 | if image_dtype == bool and order != 0: 94 | raise ValueError( 95 | "Input image dtype is bool. Interpolation is not defined " 96 | "with bool data type. Please set order to 0 or explicitly " 97 | "cast input image to another data type." 98 | ) 99 | 100 | return order 101 | 102 | 103 | def _preprocess_resize_output_shape(image, output_shape): 104 | """Validate resize output shape according to input image. 105 | 106 | Parameters 107 | ---------- 108 | image: ndarray 109 | Image to be resized. 110 | output_shape: iterable 111 | Size of the generated output image `(rows, cols[, ...][, dim])`. If 112 | `dim` is not provided, the number of channels is preserved. 113 | 114 | Returns 115 | ------- 116 | image: ndarray 117 | The input image, but with additional singleton dimensions appended in 118 | the case where ``len(output_shape) > input.ndim``. 119 | output_shape: tuple 120 | The output image converted to tuple. 121 | 122 | Raises 123 | ------ 124 | ValueError: 125 | If output_shape length is smaller than the image number of 126 | dimensions 127 | 128 | Notes 129 | ----- 130 | The input image is reshaped if its number of dimensions is not 131 | equal to output_shape_length. 132 | 133 | """ 134 | output_shape = tuple(output_shape) 135 | output_ndim = len(output_shape) 136 | input_shape = image.shape 137 | if output_ndim > image.ndim: 138 | # append dimensions to input_shape 139 | input_shape += (1,) * (output_ndim - image.ndim) 140 | image = np.reshape(image, input_shape) 141 | elif output_ndim == image.ndim - 1: 142 | # multichannel case: append shape of last axis 143 | output_shape = output_shape + (image.shape[-1],) 144 | elif output_ndim < image.ndim: 145 | raise ValueError( 146 | "output_shape length cannot be smaller than the " 147 | "image number of dimensions" 148 | ) 149 | 150 | return image, output_shape 151 | 152 | 153 | def _to_ndimage_mode(mode): 154 | """Convert from `numpy.pad` mode name to the corresponding ndimage mode.""" 155 | mode_translation_dict = dict( 156 | constant='constant', 157 | edge='nearest', 158 | symmetric='reflect', 159 | reflect='mirror', 160 | wrap='wrap', 161 | ) 162 | if mode not in mode_translation_dict: 163 | raise ValueError( 164 | f"Unknown mode: '{mode}', or cannot translate mode. The " 165 | f"mode should be one of 'constant', 'edge', 'symmetric', " 166 | f"'reflect', or 'wrap'. See the documentation of numpy.pad for " 167 | f"more info." 168 | ) 169 | return _fix_ndimage_mode(mode_translation_dict[mode]) 170 | 171 | 172 | def _fix_ndimage_mode(mode): 173 | # SciPy 1.6.0 introduced grid variants of constant and wrap which 174 | # have less surprising behavior for images. Use these when available 175 | grid_modes = {'constant': 'grid-constant', 'wrap': 'grid-wrap'} 176 | return grid_modes.get(mode, mode) 177 | 178 | 179 | def resize( 180 | image, 181 | output_shape, 182 | order=None, 183 | mode='reflect', 184 | cval=0, 185 | clip=True, 186 | preserve_range=False, 187 | anti_aliasing=None, 188 | anti_aliasing_sigma=None, 189 | ): 190 | """Resize image to match a certain size. 191 | 192 | Performs interpolation to up-size or down-size N-dimensional images. Note 193 | that anti-aliasing should be enabled when down-sizing images to avoid 194 | aliasing artifacts. For downsampling with an integer factor also see 195 | `skimage.transform.downscale_local_mean`. 196 | 197 | Parameters 198 | ---------- 199 | image : ndarray 200 | Input image. 201 | output_shape : iterable 202 | Size of the generated output image `(rows, cols[, ...][, dim])`. If 203 | `dim` is not provided, the number of channels is preserved. In case the 204 | number of input channels does not equal the number of output channels a 205 | n-dimensional interpolation is applied. 206 | 207 | Returns 208 | ------- 209 | resized : ndarray 210 | Resized version of the input. 211 | 212 | Other parameters 213 | ---------------- 214 | order : int, optional 215 | The order of the spline interpolation, default is 0 if 216 | image.dtype is bool and 1 otherwise. The order has to be in 217 | the range 0-5. See `skimage.transform.warp` for detail. 218 | mode : {'constant', 'edge', 'symmetric', 'reflect', 'wrap'}, optional 219 | Points outside the boundaries of the input are filled according 220 | to the given mode. Modes match the behaviour of `numpy.pad`. 221 | cval : float, optional 222 | Used in conjunction with mode 'constant', the value outside 223 | the image boundaries. 224 | clip : bool, optional 225 | Whether to clip the output to the range of values of the input image. 226 | This is enabled by default, since higher order interpolation may 227 | produce values outside the given input range. 228 | preserve_range : bool, optional 229 | Whether to keep the original range of values. Otherwise, the input 230 | image is converted according to the conventions of `img_as_float`. 231 | Also see https://scikit-image.org/docs/dev/user_guide/data_types.html 232 | anti_aliasing : bool, optional 233 | Whether to apply a Gaussian filter to smooth the image prior 234 | to downsampling. It is crucial to filter when downsampling 235 | the image to avoid aliasing artifacts. If not specified, it is set to 236 | True when downsampling an image whose data type is not bool. 237 | It is also set to False when using nearest neighbor interpolation 238 | (``order`` == 0) with integer input data type. 239 | anti_aliasing_sigma : {float, tuple of floats}, optional 240 | Standard deviation for Gaussian filtering used when anti-aliasing. 241 | By default, this value is chosen as (s - 1) / 2 where s is the 242 | downsampling factor, where s > 1. For the up-size case, s < 1, no 243 | anti-aliasing is performed prior to rescaling. 244 | 245 | Notes 246 | ----- 247 | Modes 'reflect' and 'symmetric' are similar, but differ in whether the edge 248 | pixels are duplicated during the reflection. As an example, if an array 249 | has values [0, 1, 2] and was padded to the right by four values using 250 | symmetric, the result would be [0, 1, 2, 2, 1, 0, 0], while for reflect it 251 | would be [0, 1, 2, 1, 0, 1, 2]. 252 | 253 | Examples 254 | -------- 255 | >>> from skimage import data 256 | >>> from skimage.transform import resize 257 | >>> image = data.camera() 258 | >>> resize(image, (100, 100)).shape 259 | (100, 100) 260 | 261 | """ 262 | 263 | image, output_shape = _preprocess_resize_output_shape(image, output_shape) 264 | input_shape = image.shape 265 | input_type = image.dtype 266 | 267 | if input_type == np.float16: 268 | image = image.astype(np.float32) 269 | 270 | if anti_aliasing is None: 271 | anti_aliasing = ( 272 | not input_type == bool 273 | and not (np.issubdtype(input_type, np.integer) and order == 0) 274 | and any(x < y for x, y in zip(output_shape, input_shape)) 275 | ) 276 | 277 | if input_type == bool and anti_aliasing: 278 | raise ValueError("anti_aliasing must be False for boolean images") 279 | 280 | factors = np.divide(input_shape, output_shape) 281 | order = _validate_interpolation_order(input_type, order) 282 | if order > 0: 283 | image = convert_to_float(image, preserve_range) 284 | 285 | # Translate modes used by np.pad to those used by scipy.ndimage 286 | ndi_mode = _to_ndimage_mode(mode) 287 | if anti_aliasing: 288 | if anti_aliasing_sigma is None: 289 | anti_aliasing_sigma = np.maximum(0, (factors - 1) / 2) 290 | else: 291 | anti_aliasing_sigma = np.atleast_1d(anti_aliasing_sigma) * np.ones_like( 292 | factors 293 | ) 294 | if np.any(anti_aliasing_sigma < 0): 295 | raise ValueError( 296 | "Anti-aliasing standard deviation must be " 297 | "greater than or equal to zero" 298 | ) 299 | elif np.any((anti_aliasing_sigma > 0) & (factors <= 1)): 300 | warnings.warn( 301 | "Anti-aliasing standard deviation greater than zero but " 302 | "not down-sampling along all axes", 303 | stacklevel=2 304 | ) 305 | filtered = ndi.gaussian_filter( 306 | image, anti_aliasing_sigma, cval=cval, mode=ndi_mode 307 | ) 308 | else: 309 | filtered = image 310 | 311 | zoom_factors = [1 / f for f in factors] 312 | out = ndi.zoom( 313 | filtered, zoom_factors, order=order, mode=ndi_mode, cval=cval, grid_mode=True 314 | ) 315 | 316 | _clip_warp_output(image, out, mode, cval, clip) 317 | 318 | return out 319 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxelutils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/src/bioxelnodes/bioxelutils/__init__.py -------------------------------------------------------------------------------- /src/bioxelnodes/bioxelutils/common.py: -------------------------------------------------------------------------------- 1 | from ast import literal_eval 2 | from pathlib import Path 3 | import bpy 4 | 5 | from ..constants import LATEST_NODE_LIB_PATH, NODE_LIB_DIRPATH, VERSIONS 6 | from ..utils import get_cache_dir 7 | 8 | 9 | def move_node_to_node(node, target_node, offset=(0, 0)): 10 | node.location.x = target_node.location.x + offset[0] 11 | node.location.y = target_node.location.y + offset[1] 12 | 13 | 14 | def move_node_between_nodes(node, target_nodes, offset=(0, 0)): 15 | xs = [] 16 | ys = [] 17 | for target_node in target_nodes: 18 | xs.append(target_node.location.x) 19 | ys.append(target_node.location.y) 20 | 21 | node.location.x = sum(xs) / len(xs) + offset[0] 22 | node.location.y = sum(ys) / len(ys) + offset[1] 23 | 24 | 25 | def get_node_type(node): 26 | node_type = type(node).__name__ 27 | if node_type == "GeometryNodeGroup": 28 | node_type = node.node_tree.name 29 | 30 | return node_type 31 | 32 | 33 | def get_nodes_by_type(node_group, type_name: str): 34 | return [node for node in node_group.nodes if get_node_type(node) == type_name] 35 | 36 | 37 | def get_container_objs_from_selection(): 38 | container_objs = [] 39 | for obj in bpy.context.selected_objects: 40 | if get_container_obj(obj): 41 | container_objs.append(obj) 42 | 43 | return list(set(container_objs)) 44 | 45 | 46 | def get_container_obj(current_obj): 47 | if current_obj: 48 | if current_obj.get('bioxel_container'): 49 | return current_obj 50 | elif current_obj.get('bioxel_layer'): 51 | parent = current_obj.parent 52 | return parent if parent.get('bioxel_container') else None 53 | return None 54 | 55 | 56 | def get_layer_prop_value(layer_obj: bpy.types.Object, prop_name: str): 57 | node_group = layer_obj.modifiers[0].node_group 58 | layer_node = get_nodes_by_type(node_group, "BioxelNodes__Layer")[0] 59 | prop = layer_node.inputs.get(prop_name) 60 | if prop is None: 61 | return None 62 | 63 | value = prop.default_value 64 | 65 | if type(value).__name__ == "bpy_prop_array": 66 | value = tuple(value) 67 | return tuple([int(v) for v in value]) \ 68 | if prop in ["shape"] else value 69 | elif type(value).__name__ == "str": 70 | return str(value) 71 | elif type(value).__name__ == "float": 72 | value = float(value) 73 | return round(value, 2) \ 74 | if prop in ["bioxel_size"] else value 75 | elif type(value).__name__ == "int": 76 | value = int(value) 77 | return value 78 | else: 79 | return value 80 | 81 | 82 | def get_layer_name(layer_obj): 83 | return get_layer_prop_value(layer_obj, "name") 84 | 85 | 86 | def get_layer_kind(layer_obj): 87 | return get_layer_prop_value(layer_obj, "kind") 88 | 89 | 90 | def get_layer_label(layer_obj): 91 | name = get_layer_name(layer_obj) 92 | # kind = get_layer_kind(layer_obj) 93 | 94 | label = f"{name}" 95 | 96 | if is_missing_layer(layer_obj): 97 | return "**MISSING**" + label 98 | elif is_temp_layer(layer_obj): 99 | return "* " + label 100 | else: 101 | return label 102 | 103 | 104 | def is_missing_layer(layer_obj): 105 | cache_filepath = Path(bpy.path.abspath( 106 | layer_obj.data.filepath)).resolve() 107 | return not cache_filepath.is_file() 108 | 109 | 110 | def is_temp_layer(layer_obj): 111 | cache_filepath = Path(bpy.path.abspath( 112 | layer_obj.data.filepath)).resolve() 113 | cache_dirpath = Path(get_cache_dir()) 114 | return cache_dirpath in cache_filepath.parents 115 | 116 | 117 | def set_layer_prop_value(layer_obj: bpy.types.Object, prop: str, value): 118 | node_group = layer_obj.modifiers[0].node_group 119 | layer_node = get_nodes_by_type(node_group, "BioxelNodes__Layer")[0] 120 | layer_node.inputs[prop].default_value = value 121 | 122 | 123 | def get_layer_obj(current_obj: bpy.types.Object): 124 | if current_obj: 125 | if current_obj.get('bioxel_layer') and current_obj.parent: 126 | if current_obj.parent.get('bioxel_container'): 127 | return current_obj 128 | return None 129 | 130 | 131 | def get_container_layer_objs(container_obj: bpy.types.Object): 132 | layer_objs = [] 133 | for obj in bpy.data.objects: 134 | if obj.parent == container_obj and get_layer_obj(obj): 135 | layer_objs.append(obj) 136 | 137 | return layer_objs 138 | 139 | 140 | def get_all_layer_objs(): 141 | layer_objs = [] 142 | for obj in bpy.data.objects: 143 | if get_layer_obj(obj): 144 | layer_objs.append(obj) 145 | 146 | return layer_objs 147 | 148 | 149 | def add_driver(target, target_prop, var_sources, expression): 150 | driver = target.driver_add(target_prop) 151 | is_vector = isinstance(driver, list) 152 | drivers = driver if is_vector else [driver] 153 | 154 | for i, driver in enumerate(drivers): 155 | for j, var_source in enumerate(var_sources): 156 | 157 | source = var_source['source'] 158 | prop = var_source['prop'] 159 | 160 | var = driver.driver.variables.new() 161 | var.name = f"var{j}" 162 | 163 | var.targets[0].id_type = source.id_type 164 | var.targets[0].id = source 165 | var.targets[0].data_path = f'["{prop}"][{i}]'\ 166 | if is_vector else f'["{prop}"]' 167 | 168 | driver.driver.expression = expression 169 | 170 | 171 | def add_direct_driver(target, target_prop, source, source_prop): 172 | drivers = [ 173 | { 174 | "source": source, 175 | "prop": source_prop 176 | } 177 | ] 178 | expression = "var0" 179 | add_driver(target, target_prop, drivers, expression) 180 | 181 | 182 | def read_file_prop(content: str): 183 | props = {} 184 | for line in content.split("\n"): 185 | line = line.replace(" ", "") 186 | p = line.split("=")[0] 187 | if p != "": 188 | v = line.split("=")[-1] 189 | props[p] = v 190 | return props 191 | 192 | 193 | def write_file_prop(props: dict): 194 | lines = [] 195 | for p, v in props.items(): 196 | lines.append(f"{p} = {v}") 197 | return "\n".join(lines) 198 | 199 | 200 | def set_file_prop(prop, value): 201 | if bpy.data.texts.get("BioxelNodes") is None: 202 | bpy.data.texts.new("BioxelNodes") 203 | 204 | props = read_file_prop(bpy.data.texts["BioxelNodes"].as_string()) 205 | props[prop] = value 206 | bpy.data.texts["BioxelNodes"].clear() 207 | bpy.data.texts["BioxelNodes"].write(write_file_prop(props)) 208 | 209 | 210 | def get_file_prop(prop): 211 | if bpy.data.texts.get("BioxelNodes") is None: 212 | bpy.data.texts.new("BioxelNodes") 213 | 214 | props = read_file_prop(bpy.data.texts["BioxelNodes"].as_string()) 215 | return props.get(prop) 216 | 217 | 218 | def get_node_version(): 219 | node_version = get_file_prop("node_version") 220 | return literal_eval(node_version) if node_version else None 221 | 222 | 223 | def is_incompatible(): 224 | node_version = get_node_version() 225 | if node_version is None: 226 | for node_group in bpy.data.node_groups: 227 | if node_group.name.startswith("BioxelNodes"): 228 | return True 229 | else: 230 | addon_version = VERSIONS[0]["node_version"] 231 | if node_version[0] != addon_version[0]\ 232 | or node_version[1] != addon_version[1]: 233 | return True 234 | 235 | return False 236 | 237 | 238 | def get_node_lib_path(node_version): 239 | version_str = "v"+".".join([str(i) for i in list(node_version)]) 240 | lib_filename = f"BioxelNodes_{version_str}.blend" 241 | return Path(NODE_LIB_DIRPATH, 242 | lib_filename).resolve() 243 | 244 | 245 | def local_lib_not_updated(): 246 | addon_version = VERSIONS[0]["node_version"] 247 | addon_lib_path = LATEST_NODE_LIB_PATH 248 | 249 | use_local = False 250 | for node_group in bpy.data.node_groups: 251 | if node_group.name.startswith("BioxelNodes"): 252 | lib = node_group.library 253 | if lib: 254 | lib_path = Path(bpy.path.abspath(lib.filepath)).resolve() 255 | if lib_path != addon_lib_path: 256 | use_local = True 257 | break 258 | 259 | not_update = get_node_version() != addon_version 260 | return use_local and not_update 261 | 262 | 263 | def get_output_node(node_group): 264 | try: 265 | output_node = get_nodes_by_type(node_group, 266 | 'NodeGroupOutput')[0] 267 | except: 268 | output_node = node_group.nodes.new("NodeGroupOutput") 269 | 270 | return output_node 271 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxelutils/container.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | from ..bioxel.container import Container 4 | from bpy_extras.io_utils import axis_conversion 5 | from mathutils import Matrix, Vector 6 | 7 | 8 | from .layer import Layer, layer_to_obj, obj_to_layer 9 | from .common import (get_container_layer_objs, 10 | get_layer_prop_value, 11 | get_nodes_by_type, get_output_node, 12 | move_node_to_node) 13 | from .node import add_node_to_graph 14 | from ..utils import get_use_link 15 | 16 | NODE_TYPE = { 17 | "label": "BioxelNodes_MaskByLabel", 18 | "scalar": "BioxelNodes_MaskByThreshold" 19 | } 20 | 21 | 22 | def calc_bbox_verts(origin: tuple, size: tuple): 23 | bbox_origin = Vector( 24 | (origin[0], origin[1], origin[2])) 25 | bbox_size = Vector( 26 | (size[0], size[1], size[2])) 27 | bbox_verts = [ 28 | ( 29 | bbox_origin[0] + 0, 30 | bbox_origin[1] + 0, 31 | bbox_origin[2] + 0 32 | ), 33 | ( 34 | bbox_origin[0] + 0, 35 | bbox_origin[1] + 0, 36 | bbox_origin[2] + bbox_size[2] 37 | ), 38 | ( 39 | bbox_origin[0] + 0, 40 | bbox_origin[1] + bbox_size[1], 41 | bbox_origin[2] + 0 42 | ), 43 | ( 44 | bbox_origin[0] + 0, 45 | bbox_origin[1] + bbox_size[1], 46 | bbox_origin[2] + bbox_size[2] 47 | ), 48 | ( 49 | bbox_origin[0] + bbox_size[0], 50 | bbox_origin[1] + 0, 51 | bbox_origin[2] + 0 52 | ), 53 | ( 54 | bbox_origin[0] + bbox_size[0], 55 | bbox_origin[1] + 0, 56 | bbox_origin[2] + bbox_size[2], 57 | ), 58 | ( 59 | bbox_origin[0] + bbox_size[0], 60 | bbox_origin[1] + bbox_size[1], 61 | bbox_origin[2] + 0, 62 | ), 63 | ( 64 | bbox_origin[0] + bbox_size[0], 65 | bbox_origin[1] + bbox_size[1], 66 | bbox_origin[2] + bbox_size[2], 67 | ), 68 | ] 69 | return bbox_verts 70 | 71 | 72 | def obj_to_container(container_obj: bpy.types.Object): 73 | layer_objs = get_container_layer_objs(container_obj) 74 | layers = [obj_to_layer(obj) for obj in layer_objs] 75 | container = Container(name=container_obj.name, 76 | layers=layers) 77 | return container 78 | 79 | 80 | def add_layers(layers: list[Layer], 81 | container_obj: bpy.types.Object, 82 | cache_dir: str): 83 | 84 | node_group = container_obj.modifiers[0].node_group 85 | output_node = get_output_node(node_group) 86 | 87 | for i, layer in enumerate(layers): 88 | layer_obj = layer_to_obj(layer, container_obj, cache_dir) 89 | fetch_node = add_node_to_graph("FetchLayer", 90 | node_group, 91 | use_link=get_use_link()) 92 | fetch_node.label = get_layer_prop_value(layer_obj, "name") 93 | fetch_node.inputs[0].default_value = layer_obj 94 | 95 | if len(output_node.inputs[0].links) == 0: 96 | node_group.links.new(fetch_node.outputs[0], 97 | output_node.inputs[0]) 98 | move_node_to_node(fetch_node, output_node, (-900, 0)) 99 | else: 100 | move_node_to_node(fetch_node, output_node, (0, -100 * (i+1))) 101 | 102 | return container_obj 103 | 104 | 105 | def container_to_obj(container: Container, 106 | scene_scale: float, 107 | step_size: float, 108 | cache_dir: str): 109 | # Wrapper a Container 110 | 111 | # Make transformation 112 | # (S)uperior -Z -> Y 113 | # (A)osterior Y -> Z 114 | mat_ras2blender = axis_conversion(from_forward='-Z', 115 | from_up='Y', 116 | to_forward='Y', 117 | to_up='Z').to_4x4() 118 | 119 | mat_scene_scale = Matrix.Scale(scene_scale, 120 | 4) 121 | 122 | bpy.ops.mesh.primitive_cube_add(enter_editmode=False, 123 | align='WORLD', 124 | location=(0, 0, 0), 125 | scale=(1, 1, 1)) 126 | 127 | container_obj = bpy.context.active_object 128 | 129 | bbox_verts = calc_bbox_verts((0, 0, 0), container.layers[0].shape) 130 | for i, vert in enumerate(container_obj.data.vertices): 131 | transform = Matrix(container.layers[0].affine) 132 | vert.co = transform @ Vector(bbox_verts[i]) 133 | 134 | container_obj.matrix_world = mat_ras2blender @ mat_scene_scale 135 | container_obj.name = container.name 136 | container_obj.data.name = container.name 137 | container_obj.show_in_front = True 138 | 139 | container_obj.lock_location[0] = True 140 | container_obj.lock_location[1] = True 141 | container_obj.lock_location[2] = True 142 | 143 | container_obj.lock_rotation[0] = True 144 | container_obj.lock_rotation[1] = True 145 | container_obj.lock_rotation[2] = True 146 | 147 | container_obj.lock_scale[0] = True 148 | container_obj.lock_scale[1] = True 149 | container_obj.lock_scale[2] = True 150 | 151 | container_obj['bioxel_container'] = True 152 | container_obj["scene_scale"] = scene_scale 153 | container_obj["step_size"] = step_size 154 | 155 | modifier = container_obj.modifiers.new("GeometryNodes", 'NODES') 156 | node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree') 157 | node_group.interface.new_socket(name="Output", 158 | in_out="OUTPUT", 159 | socket_type="NodeSocketGeometry") 160 | modifier.node_group = node_group 161 | node_group.nodes.new("NodeGroupOutput") 162 | 163 | container_obj = add_layers(container.layers, 164 | container_obj=container_obj, 165 | cache_dir=cache_dir) 166 | 167 | try: 168 | layer_node = get_nodes_by_type( 169 | node_group, "BioxelNodes_FetchLayer")[0] 170 | center_node = add_node_to_graph("ReCenter", 171 | node_group, 172 | use_link=get_use_link()) 173 | 174 | output_node = get_output_node(node_group) 175 | 176 | node_group.links.new(layer_node.outputs[0], 177 | center_node.inputs[0]) 178 | node_group.links.new(center_node.outputs[0], 179 | output_node.inputs[0]) 180 | 181 | move_node_to_node(center_node, layer_node, (300, 0)) 182 | bpy.ops.bioxelnodes.add_slicer('EXEC_DEFAULT') 183 | except: 184 | pass 185 | 186 | return container_obj 187 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxelutils/layer.py: -------------------------------------------------------------------------------- 1 | import random 2 | import re 3 | import bpy 4 | import numpy as np 5 | 6 | try: 7 | import pyopenvdb as vdb 8 | except: 9 | import openvdb as vdb 10 | 11 | from pathlib import Path 12 | from uuid import uuid4 13 | 14 | from ..bioxel.layer import Layer 15 | from ..utils import get_use_link 16 | from .node import add_node_to_graph 17 | from .common import (get_layer_prop_value, 18 | move_node_between_nodes) 19 | 20 | 21 | def obj_to_layer(layer_obj: bpy.types.Object): 22 | cache_filepath = Path(bpy.path.abspath(layer_obj.data.filepath)).resolve() 23 | is_sequence = re.search(r'\.\d{4}\.', 24 | cache_filepath.name) is not None 25 | if is_sequence: 26 | cache_path = cache_filepath.parent 27 | data_frames = () 28 | for f in cache_path.iterdir(): 29 | if not f.is_file() or f.suffix != ".vdb": 30 | continue 31 | grids, base_metadata = vdb.readAll(str(f)) 32 | grid = grids[0] 33 | metadata = grid.metadata 34 | if grid["layer_kind"] in ['label', 'scalar']: 35 | data_shape = grid["data_shape"] 36 | else: 37 | data_shape = tuple(list(grid["data_shape"]) + [3]) 38 | data_frame = np.ndarray(data_shape, np.float32) 39 | grid.copyToArray(data_frame) 40 | data_frames += (data_frame,) 41 | data = np.stack(data_frames) 42 | else: 43 | grids, base_metadata = vdb.readAll(str(cache_filepath)) 44 | grid = grids[0] 45 | metadata = grid.metadata 46 | if grid["layer_kind"] in ['label', 'scalar']: 47 | data_shape = grid["data_shape"] 48 | else: 49 | data_shape = tuple(list(grid["data_shape"]) + [3]) 50 | data = np.ndarray(data_shape, np.float32) 51 | grid.copyToArray(data) 52 | data = np.expand_dims(data, axis=0) # expend frame 53 | 54 | name = get_layer_prop_value(layer_obj, "name") \ 55 | or metadata["layer_name"] 56 | kind = get_layer_prop_value(layer_obj, "kind") \ 57 | or metadata["layer_kind"] 58 | affine = metadata["layer_affine"] 59 | dtype = get_layer_prop_value(layer_obj, "dtype") \ 60 | or metadata.get("data_dtype") or "float32" 61 | offset = get_layer_prop_value(layer_obj, "offset") \ 62 | or metadata.get("data_offset") or 0 63 | 64 | data = data - np.full_like(data, offset) 65 | data = data.astype(dtype) 66 | 67 | if kind in ["scalar", "label"]: 68 | data = np.expand_dims(data, axis=-1) # expend channel 69 | 70 | layer = Layer(data=data, 71 | name=name, 72 | kind=kind, 73 | affine=affine) 74 | 75 | return layer 76 | 77 | 78 | def layer_to_obj(layer: Layer, 79 | container_obj: bpy.types.Object, 80 | cache_dir: str): 81 | 82 | data = layer.data 83 | 84 | # TXYZC > TXYZ 85 | if layer.kind in ['label', 'scalar']: 86 | data = np.amax(data, -1) 87 | 88 | offset = 0 89 | if layer.kind in ['scalar']: 90 | data = data.astype(np.float32) 91 | orig_min = float(np.min(data)) 92 | if orig_min < 0: 93 | offset = -orig_min 94 | 95 | data = data + np.full_like(data, offset) 96 | 97 | metadata = { 98 | "layer_name": layer.name, 99 | "layer_kind": layer.kind, 100 | "layer_affine": layer.affine.tolist(), 101 | "data_shape": layer.shape, 102 | "data_dtype": layer.data.dtype.str, 103 | "data_offset": offset 104 | } 105 | 106 | layer_display_name = f"{container_obj.name}_{layer.name}" 107 | if layer.frame_count > 1: 108 | print(f"Saving the Cache of {layer.name}...") 109 | vdb_name = str(uuid4()) 110 | sequence_path = Path(cache_dir, vdb_name) 111 | sequence_path.mkdir(parents=True, exist_ok=True) 112 | 113 | cache_filepaths = [] 114 | for f in range(layer.frame_count): 115 | if layer.kind in ['label', 'scalar']: 116 | grid = vdb.FloatGrid() 117 | grid.copyFromArray(data[f, :, :, :].copy().astype(np.float32)) 118 | else: 119 | # color 120 | grid = vdb.Vec3SGrid() 121 | grid.copyFromArray( 122 | data[f, :, :, :, :].copy().astype(np.float32)) 123 | grid.transform = vdb.createLinearTransform( 124 | layer.affine.transpose()) 125 | grid.metadata = metadata 126 | grid.name = layer.kind 127 | 128 | cache_filepath = Path(sequence_path, 129 | f"{vdb_name}.{str(f+1).zfill(4)}.vdb") 130 | vdb.write(str(cache_filepath), grids=[grid]) 131 | cache_filepaths.append(cache_filepath) 132 | 133 | else: 134 | if layer.kind in ['label', 'scalar']: 135 | grid = vdb.FloatGrid() 136 | grid.copyFromArray(data[0, :, :, :].copy().astype(np.float32)) 137 | else: 138 | # color 139 | grid = vdb.Vec3SGrid() 140 | grid.copyFromArray(data[0, :, :, :, :].copy().astype(np.float32)) 141 | grid.transform = vdb.createLinearTransform( 142 | layer.affine.transpose()) 143 | grid.metadata = metadata 144 | grid.name = layer.kind 145 | 146 | print(f"Saving the Cache of {layer.name}...") 147 | cache_filepath = Path(cache_dir, f"{uuid4()}.vdb") 148 | vdb.write(str(cache_filepath), grids=[grid]) 149 | cache_filepaths = [cache_filepath] 150 | 151 | layer_data = bpy.data.volumes.new(layer_display_name) 152 | layer_data.render.space = 'WORLD' 153 | scene_scale = container_obj.get("scene_scale") or 0.01 154 | step_size = container_obj.get("step_size") or 1 155 | layer_data.render.step_size = scene_scale * step_size 156 | layer_data.sequence_mode = 'REPEAT' 157 | layer_data.filepath = str(cache_filepaths[0]) 158 | 159 | if layer.frame_count > 1: 160 | layer_data.is_sequence = True 161 | layer_data.frame_duration = layer.frame_count 162 | else: 163 | layer_data.is_sequence = False 164 | 165 | layer_obj = bpy.data.objects.new(layer_display_name, layer_data) 166 | layer_obj['bioxel_layer'] = True 167 | 168 | print(f"Creating Node for {layer.name}...") 169 | modifier = layer_obj.modifiers.new("GeometryNodes", 'NODES') 170 | node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree') 171 | node_group.interface.new_socket(name="Cache", 172 | in_out="INPUT", 173 | socket_type="NodeSocketGeometry") 174 | node_group.interface.new_socket(name="Layer", 175 | in_out="OUTPUT", 176 | socket_type="NodeSocketGeometry") 177 | modifier.node_group = node_group 178 | 179 | layer_node = add_node_to_graph("_Layer", 180 | node_group, 181 | use_link=get_use_link()) 182 | 183 | layer_node.inputs['name'].default_value = layer.name 184 | layer_node.inputs['shape'].default_value = layer.shape 185 | layer_node.inputs['kind'].default_value = layer.kind 186 | 187 | for i in range(layer.affine.shape[1]): 188 | for j in range(layer.affine.shape[0]): 189 | affine_key = f"affine{i}{j}" 190 | layer_node.inputs[affine_key].default_value = layer.affine[j, i] 191 | 192 | layer_node.inputs['unique'].default_value = random.uniform(0, 1) 193 | layer_node.inputs['bioxel_size'].default_value = layer.bioxel_size[0] 194 | layer_node.inputs['dtype'].default_value = layer.dtype.str 195 | layer_node.inputs['dtype_num'].default_value = layer.dtype.num 196 | layer_node.inputs['frame_count'].default_value = layer.frame_count 197 | layer_node.inputs['channel_count'].default_value = layer.channel_count 198 | layer_node.inputs['offset'].default_value = max(0, -layer.min) 199 | layer_node.inputs['min'].default_value = layer.min 200 | layer_node.inputs['max'].default_value = layer.max 201 | 202 | input_node = node_group.nodes.new("NodeGroupInput") 203 | output_node = node_group.nodes.new("NodeGroupOutput") 204 | 205 | node_group.links.new(input_node.outputs[0], 206 | layer_node.inputs[0]) 207 | node_group.links.new(layer_node.outputs[0], 208 | output_node.inputs[0]) 209 | 210 | move_node_between_nodes( 211 | layer_node, [input_node, output_node]) 212 | 213 | layer_obj.parent = container_obj 214 | 215 | return layer_obj 216 | -------------------------------------------------------------------------------- /src/bioxelnodes/bioxelutils/node.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import bpy 3 | 4 | from .common import get_file_prop, set_file_prop 5 | from ..exceptions import NoFound 6 | 7 | from ..constants import LATEST_NODE_LIB_PATH, VERSIONS 8 | 9 | 10 | def get_node_tree(node_type: str, use_link=True): 11 | # unannotate below for local debug in node lib file. 12 | # node_group = bpy.data.node_groups[node_type] 13 | # return node_group 14 | 15 | # added node is always from latest node version 16 | addon_version = VERSIONS[0]["node_version"] 17 | addon_lib_path = LATEST_NODE_LIB_PATH 18 | 19 | if get_file_prop("node_version") is None: 20 | set_file_prop("node_version", addon_version) 21 | 22 | local_lib = None 23 | for node_group in bpy.data.node_groups: 24 | if node_group.name.startswith("BioxelNodes"): 25 | lib = node_group.library 26 | if lib: 27 | lib_path = Path(bpy.path.abspath(lib.filepath)).resolve() 28 | if lib_path != addon_lib_path: 29 | local_lib = lib.filepath 30 | break 31 | 32 | # local lib first 33 | lib_path_str = local_lib or str(addon_lib_path) 34 | 35 | with bpy.data.libraries.load(lib_path_str, 36 | link=use_link, 37 | relative=True) as (data_from, data_to): 38 | data_to.node_groups = [n for n in data_from.node_groups 39 | if n == node_type] 40 | 41 | node_tree = data_to.node_groups[0] 42 | 43 | if node_tree is None: 44 | raise NoFound('No custom node found') 45 | 46 | return node_tree 47 | 48 | 49 | def assign_node_tree(node, node_tree): 50 | node.node_tree = node_tree 51 | node.width = 200.0 52 | node.name = node_tree.name 53 | return node 54 | 55 | 56 | def add_node_to_graph(node_name: str, node_group, node_label=None, use_link=True): 57 | node_type = f"BioxelNodes_{node_name}" 58 | node_label = node_label or node_name 59 | 60 | # Deselect all nodes first 61 | for node in node_group.nodes: 62 | if node.select: 63 | node.select = False 64 | 65 | node_tree = get_node_tree(node_type, use_link) 66 | node = node_group.nodes.new("GeometryNodeGroup") 67 | assign_node_tree(node, node_tree) 68 | 69 | node.label = node_label 70 | node.show_options = False 71 | return node 72 | -------------------------------------------------------------------------------- /src/bioxelnodes/blender_manifest.toml: -------------------------------------------------------------------------------- 1 | schema_version = "1.0.0" 2 | 3 | id = "bioxelnodes" 4 | version = "1.0.9" 5 | name = "Bioxel Nodes" 6 | tagline = "For scientific volumetric data visualization in Blender" 7 | maintainer = "Ma Nan " 8 | type = "add-on" 9 | website = "https://omoolab.github.io/BioxelNodes/latest" 10 | 11 | tags = ["Geometry Nodes", "Render", "Import-Export"] 12 | blender_version_min = "4.2.0" 13 | license = ['SPDX:GPL-3.0-or-later'] 14 | copyright = ["2024 OmooLab"] 15 | platforms = ["windows-x64"] 16 | 17 | wheels = ["./wheels/h5py-3.11.0-cp311-cp311-win_amd64.whl", "./wheels/lxml-5.3.2-cp311-cp311-win_amd64.whl", "./wheels/mrcfile-1.5.1-py2.py3-none-any.whl", "./wheels/pyometiff-1.0.0-py3-none-any.whl", "./wheels/SimpleITK-2.3.1-cp311-cp311-win_amd64.whl", "./wheels/tifffile-2024.7.24-py3-none-any.whl", "./wheels/transforms3d-0.4.2-py3-none-any.whl"] 18 | 19 | [permissions] 20 | files = "Import/export volume data from/to disk" -------------------------------------------------------------------------------- /src/bioxelnodes/constants.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | VERSIONS = [{"label": "Latest", "node_version": (1, 0, 7)}, 4 | {"label": "v0.3.x", "node_version": (0, 3, 3)}, 5 | {"label": "v0.2.x", "node_version": (0, 2, 9)}] 6 | 7 | NODE_LIB_DIRPATH = Path(Path(__file__).parent, 8 | "assets/Nodes").resolve() 9 | 10 | LATEST_NODE_LIB_PATH = lib_path = Path(NODE_LIB_DIRPATH, 11 | "BioxelNodes_latest.blend").resolve() 12 | 13 | COMPONENT_OUTPUT_NODES = [ 14 | "CutoutByThreshold", 15 | "CutoutByRange", 16 | "CutoutByHue", 17 | "JoinComponent", 18 | "SetProperties", 19 | "SetColor", 20 | "SetColorByColor", 21 | "SetColorByColor", 22 | "SetColorByRamp2", 23 | "SetColorByRamp3", 24 | "SetColorByRamp4", 25 | "SetColorByRamp5", 26 | "Cut", 27 | ] 28 | 29 | MENU_ITEMS = [ 30 | { 31 | 'label': 'Component', 32 | 'icon': 'OUTLINER_DATA_VOLUME', 33 | 'items': [ 34 | { 35 | 'label': 'Cutout by Threshold', 36 | 'icon': 'EMPTY_SINGLE_ARROW', 37 | 'name': 'CutoutByThreshold', 38 | 'description': '' 39 | }, 40 | { 41 | 'label': 'Cutout by Range', 42 | 'icon': 'IPO_CONSTANT', 43 | 'name': 'CutoutByRange', 44 | 'description': '' 45 | }, 46 | { 47 | 'label': 'Cutout by Hue', 48 | 'icon': 'COLOR', 49 | 'name': 'CutoutByHue', 50 | 'description': '' 51 | }, 52 | "separator", 53 | { 54 | 'label': 'To Surface', 55 | 'icon': 'MESH_DATA', 56 | 'name': 'ToSurface', 57 | 'description': '' 58 | }, 59 | { 60 | 'label': 'Join Component', 61 | 'icon': 'CONSTRAINT_BONE', 62 | 'name': 'JoinComponent', 63 | 'description': '' 64 | }, 65 | { 66 | 'label': 'Slice', 67 | 'icon': 'TEXTURE', 68 | 'name': 'Slice', 69 | 'description': '' 70 | } 71 | ] 72 | }, 73 | { 74 | 'label': 'Property', 75 | 'icon': 'PROPERTIES', 76 | 'items': [ 77 | { 78 | 'label': 'Set Properties', 79 | 'icon': 'PROPERTIES', 80 | 'name': 'SetProperties', 81 | 'description': '' 82 | }, 83 | "separator", 84 | { 85 | 'label': 'Set Color', 86 | 'icon': 'IPO_SINE', 87 | 'name': 'SetColor', 88 | 'description': '' 89 | }, 90 | { 91 | 'label': 'Set Color by Color', 92 | 'icon': 'IPO_QUINT', 93 | 'name': 'SetColorByColor', 94 | 'description': '' 95 | }, 96 | { 97 | 'label': 'Set Color by Ramp 2', 98 | 'icon': 'IPO_QUAD', 99 | 'name': 'SetColorByRamp2', 100 | 'description': '' 101 | }, 102 | { 103 | 'label': 'Set Color by Ramp 3', 104 | 'icon': 'IPO_CUBIC', 105 | 'name': 'SetColorByRamp3', 106 | 'description': '' 107 | }, 108 | { 109 | 'label': 'Set Color by Ramp 4', 110 | 'icon': 'IPO_QUART', 111 | 'name': 'SetColorByRamp4', 112 | 'description': '' 113 | }, 114 | { 115 | 'label': 'Set Color by Ramp 5', 116 | 'icon': 'IPO_QUINT', 117 | 'name': 'SetColorByRamp5', 118 | 'description': '' 119 | } 120 | ] 121 | }, 122 | { 123 | 'label': 'Surface', 124 | 'icon': 'MESH_DATA', 125 | 'items': [ 126 | { 127 | 'label': 'Membrane Shader', 128 | 'icon': 'NODE_MATERIAL', 129 | 'name': 'AssignMembraneShader', 130 | 'description': '' 131 | }, 132 | { 133 | 'label': 'Solid Shader', 134 | 'icon': 'SHADING_SOLID', 135 | 'name': 'AssignSolidShader', 136 | 'description': '' 137 | }, 138 | { 139 | 'label': 'Slime Shader', 140 | 'icon': 'OUTLINER_DATA_META', 141 | 'name': 'AssignSlimeShader', 142 | 'description': '' 143 | }, 144 | 145 | ] 146 | }, 147 | { 148 | 'label': 'Transform', 149 | 'icon': 'EMPTY_AXIS', 150 | 'items': [ 151 | { 152 | 'label': 'Transform', 153 | 'icon': 'EMPTY_AXIS', 154 | 'name': 'Transform', 155 | 'description': '' 156 | }, 157 | { 158 | 'label': 'Transform Parent', 159 | 'icon': 'ORIENTATION_PARENT', 160 | 'name': 'TransformParent', 161 | 'description': '' 162 | }, 163 | { 164 | 'label': 'ReCenter', 165 | 'icon': 'PROP_CON', 166 | 'name': 'ReCenter', 167 | 'description': '' 168 | }, 169 | { 170 | 'label': 'Copy Transform', 171 | 'icon': 'EMPTY_AXIS', 172 | 'name': 'CopyTransform', 173 | 'description': '' 174 | }, 175 | { 176 | 'label': 'Extract Transform', 177 | 'icon': 'EMPTY_AXIS', 178 | 'name': 'ExtractTransform', 179 | 'description': '' 180 | }, 181 | ] 182 | }, 183 | { 184 | 'label': 'Cut', 185 | 'icon': 'MOD_BEVEL', 186 | 'items': [ 187 | { 188 | 'label': 'Cut', 189 | 'icon': 'MOD_BEVEL', 190 | 'name': 'Cut', 191 | 'description': '' 192 | }, 193 | "separator", 194 | { 195 | 'label': 'Primitive Cutter', 196 | 'icon': 'MOD_LINEART', 197 | 'name': 'PrimitiveCutter', 198 | 'description': '' 199 | }, 200 | { 201 | 'label': 'Object Cutter', 202 | 'icon': 'MESH_PLANE', 203 | 'name': 'ObjectCutter', 204 | 'description': '' 205 | } 206 | ] 207 | }, 208 | { 209 | 'label': 'Extra', 210 | 'icon': 'MODIFIER', 211 | 'items': [ 212 | { 213 | 'label': 'Fetch Mesh', 214 | 'icon': 'OUTLINER_OB_MESH', 215 | 'name': 'FetchMesh', 216 | 'description': '' 217 | }, 218 | { 219 | 'label': 'Fetch Volume', 220 | 'icon': 'OUTLINER_OB_VOLUME', 221 | 'name': 'FetchVolume', 222 | 'description': '' 223 | }, 224 | { 225 | 'label': 'Fetch Shape Wire', 226 | 'icon': 'FILE_VOLUME', 227 | 'name': 'FetchShapeWire', 228 | 'description': '' 229 | }, 230 | { 231 | 'label': 'Fetch Bbox Wire', 232 | 'icon': 'MESH_CUBE', 233 | 'name': 'FetchBboxWire', 234 | 'description': '' 235 | }, 236 | "separator", 237 | { 238 | 'label': 'Inflate', 239 | 'icon': 'OUTLINER_OB_META', 240 | 'name': 'Inflate', 241 | 'description': '' 242 | }, 243 | { 244 | 'label': 'Smooth', 245 | 'icon': 'MOD_SMOOTH', 246 | 'name': 'Smooth', 247 | 'description': '' 248 | }, 249 | { 250 | 'label': 'Remove Small Island', 251 | 'icon': 'FORCE_LENNARDJONES', 252 | 'name': 'RemoveSmallIsland', 253 | 'description': '' 254 | } 255 | ] 256 | } 257 | ] 258 | -------------------------------------------------------------------------------- /src/bioxelnodes/exceptions.py: -------------------------------------------------------------------------------- 1 | class CancelledByUser(Exception): 2 | def __init__(self): 3 | message = 'Cancelled by user' 4 | super().__init__(message) 5 | 6 | 7 | class NoContent(Exception): 8 | def __init__(self, message): 9 | super().__init__(message) 10 | self.message = message 11 | 12 | 13 | class NoFound(Exception): 14 | def __init__(self, message): 15 | super().__init__(message) 16 | self.message = message 17 | 18 | 19 | class Incompatible(Exception): 20 | def __init__(self, message): 21 | super().__init__(message) 22 | self.message = message 23 | -------------------------------------------------------------------------------- /src/bioxelnodes/menus.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | from .constants import MENU_ITEMS, VERSIONS 4 | from .node_menu import NodeMenu 5 | 6 | from .bioxelutils.common import (get_container_obj, 7 | get_container_layer_objs, get_layer_label, 8 | get_layer_prop_value, get_node_version, is_incompatible) 9 | from .operators.layer import (FetchLayer, RelocateLayer, RetimeLayer, RenameLayer, 10 | RemoveSelectedLayers, SaveSelectedLayersCache, 11 | ResampleLayer, SignScalar, CombineLabels, 12 | FillByLabel, FillByThreshold, FillByRange) 13 | from .operators.container import (AddLocator, AddSlicer, ContainerProps, 14 | SaveContainerLayersCache, RemoveContainerMissingLayers, 15 | SaveContainer, LoadContainer, 16 | AddPieCutter, AddPlaneCutter, 17 | AddCylinderCutter, AddCubeCutter, AddSphereCutter, 18 | ExtractBboxWire, ExtractMesh, ExtractShapeWire, 19 | ExtractNodeMesh, ExtractNodeBboxWire, ExtractNodeShapeWire) 20 | 21 | from .operators.io import (ImportAsLabel, ImportAsScalar, ImportAsColor) 22 | from .operators.misc import (AddEeveeEnv, CleanTemp, Help, 23 | ReLinkNodeLib, RemoveAllMissingLayers, 24 | RenderSettingPreset, SaveAllLayersCache, 25 | SaveNodeLib, SliceViewer) 26 | 27 | 28 | class IncompatibleMenu(bpy.types.Menu): 29 | bl_idname = "BIOXELNODES_MT_INCOMPATIBLE" 30 | bl_label = "Bioxel Nodes" 31 | 32 | def draw(self, context): 33 | tip_text = "please downgrade addon version." 34 | node_version = get_node_version() 35 | if node_version: 36 | version_str = "v"+".".join([str(i) for i in list(node_version)]) 37 | tip_text = f"please downgrade addon version to {version_str}." 38 | 39 | layout = self.layout 40 | layout.label(text="Incompatible node version detected.") 41 | layout.separator() 42 | layout.label( 43 | text="If you still want to edit this file with bioxel nodes, ") 44 | layout.label(text=tip_text) 45 | layout.separator() 46 | layout.label(text="If this file is archived, " 47 | "please relink node library, ") 48 | layout.label(text="check if it still works, " 49 | "then save node library.") 50 | layout.separator() 51 | layout.menu(ReLinkNodeLibMenu.bl_idname) 52 | layout.operator(SaveNodeLib.bl_idname) 53 | 54 | layout.separator() 55 | layout.menu(DangerZoneMenu.bl_idname) 56 | 57 | layout.separator() 58 | layout.menu(RenderSettingMenu.bl_idname) 59 | 60 | layout.separator() 61 | layout.operator(Help.bl_idname, 62 | icon=Help.bl_icon) 63 | 64 | 65 | class FetchLayerMenu(bpy.types.Menu): 66 | bl_idname = "BIOXELNODES_MT_ADD_LAYER" 67 | bl_label = "Fetch Layer" 68 | 69 | def draw(self, context): 70 | container_obj = get_container_obj(bpy.context.active_object) 71 | layer_objs = get_container_layer_objs(container_obj) 72 | layout = self.layout 73 | 74 | for layer_obj in layer_objs: 75 | op = layout.operator(FetchLayer.bl_idname, 76 | text=get_layer_label(layer_obj)) 77 | op.layer_obj_name = layer_obj.name 78 | 79 | 80 | class ExtractFromContainerMenu(bpy.types.Menu): 81 | bl_idname = "BIOXELNODES_MT_PICK" 82 | bl_label = "Extract from Container" 83 | 84 | def draw(self, context): 85 | layout = self.layout 86 | layout.operator(ExtractMesh.bl_idname) 87 | layout.operator(ExtractShapeWire.bl_idname) 88 | layout.operator(ExtractBboxWire.bl_idname) 89 | 90 | 91 | class AddCutterMenu(bpy.types.Menu): 92 | bl_idname = "BIOXELNODES_MT_CUTTERS" 93 | bl_label = "Add a Cutter" 94 | 95 | def draw(self, context): 96 | layout = self.layout 97 | layout.operator(AddPlaneCutter.bl_idname, 98 | icon=AddPlaneCutter.bl_icon, text="Plane Cutter") 99 | layout.operator(AddCylinderCutter.bl_idname, 100 | icon=AddCylinderCutter.bl_icon, text="Cylinder Cutter") 101 | layout.operator(AddCubeCutter.bl_idname, 102 | icon=AddCubeCutter.bl_icon, text="Cube Cutter") 103 | layout.operator(AddSphereCutter.bl_idname, 104 | icon=AddSphereCutter.bl_icon, text="Sphere Cutter") 105 | layout.operator(AddPieCutter.bl_idname, 106 | icon=AddPieCutter.bl_icon, text="Pie Cutter") 107 | 108 | 109 | class ImportLayerMenu(bpy.types.Menu): 110 | bl_idname = "BIOXELNODES_MT_IMPORTLAYER" 111 | bl_label = "Import Volumetric Data (Init)" 112 | bl_icon = "FILE_NEW" 113 | 114 | def draw(self, context): 115 | layout = self.layout 116 | layout.operator(ImportAsScalar.bl_idname, 117 | text="as Scalar") 118 | layout.operator(ImportAsLabel.bl_idname, 119 | text="as Label") 120 | layout.operator(ImportAsColor.bl_idname, 121 | text="as Color") 122 | 123 | 124 | class AddLayerMenu(bpy.types.Menu): 125 | bl_idname = "BIOXELNODES_MT_ADDLAYER" 126 | bl_label = "Import Volumetric Data (Add)" 127 | bl_icon = "FILE_NEW" 128 | 129 | def draw(self, context): 130 | layout = self.layout 131 | layout.operator(ImportAsScalar.bl_idname, 132 | text="as Scalar") 133 | layout.operator(ImportAsLabel.bl_idname, 134 | text="as Label") 135 | layout.operator(ImportAsColor.bl_idname, 136 | text="as Color") 137 | 138 | 139 | class ModifyLayerMenu(bpy.types.Menu): 140 | bl_idname = "BIOXELNODES_MT_MODIFYLAYER" 141 | bl_label = "Modify Layer" 142 | bl_icon = "FILE_NEW" 143 | 144 | def draw(self, context): 145 | layout = self.layout 146 | layout.operator(SignScalar.bl_idname, 147 | icon=SignScalar.bl_icon) 148 | layout.operator(FillByThreshold.bl_idname, 149 | icon=FillByThreshold.bl_icon) 150 | layout.operator(FillByRange.bl_idname, 151 | icon=FillByRange.bl_icon) 152 | layout.operator(FillByLabel.bl_idname, 153 | icon=FillByLabel.bl_icon) 154 | layout.operator(CombineLabels.bl_idname, 155 | icon=CombineLabels.bl_icon) 156 | 157 | 158 | class ReLinkNodeLibMenu(bpy.types.Menu): 159 | bl_idname = "BIOXELNODES_MT_RELINK" 160 | bl_label = "Relink Node Library" 161 | 162 | def draw(self, context): 163 | layout = self.layout 164 | 165 | for index, version in enumerate(VERSIONS): 166 | op = layout.operator(ReLinkNodeLib.bl_idname, 167 | text=version["label"]) 168 | op.index = index 169 | 170 | 171 | class RenderSettingMenu(bpy.types.Menu): 172 | bl_idname = "BIOXELNODES_MT_RENDER" 173 | bl_label = "Render Setting Presets" 174 | 175 | def draw(self, context): 176 | layout = self.layout 177 | for k, v in RenderSettingPreset.PRESETS.items(): 178 | op = layout.operator(RenderSettingPreset.bl_idname, 179 | text=v) 180 | op.preset = k 181 | 182 | 183 | class DangerZoneMenu(bpy.types.Menu): 184 | bl_idname = "BIOXELNODES_MT_DANGER" 185 | bl_label = "Danger Zone" 186 | 187 | def draw(self, context): 188 | layout = self.layout 189 | layout.operator(CleanTemp.bl_idname) 190 | 191 | 192 | class BioxelNodesTopbarMenu(bpy.types.Menu): 193 | bl_idname = "BIOXELNODES_MT_TOPBAR" 194 | bl_label = "Bioxel Nodes" 195 | 196 | def draw(self, context): 197 | layout = self.layout 198 | 199 | layout.menu(ImportLayerMenu.bl_idname, 200 | icon=ImportLayerMenu.bl_icon) 201 | layout.operator(LoadContainer.bl_idname) 202 | 203 | layout.separator() 204 | layout.operator(SaveAllLayersCache.bl_idname) 205 | layout.operator(RemoveAllMissingLayers.bl_idname) 206 | 207 | layout.separator() 208 | layout.menu(ReLinkNodeLibMenu.bl_idname) 209 | layout.operator(SaveNodeLib.bl_idname) 210 | 211 | layout.separator() 212 | layout.menu(DangerZoneMenu.bl_idname) 213 | 214 | layout.separator() 215 | layout.operator(AddEeveeEnv.bl_idname) 216 | layout.menu(RenderSettingMenu.bl_idname) 217 | 218 | layout.separator() 219 | layout.operator(Help.bl_idname, 220 | icon=Help.bl_icon) 221 | 222 | 223 | def TOPBAR(self, context): 224 | layout = self.layout 225 | 226 | if is_incompatible(): 227 | layout.menu(IncompatibleMenu.bl_idname) 228 | else: 229 | layout.menu(BioxelNodesTopbarMenu.bl_idname) 230 | 231 | 232 | class NodeHeadMenu(bpy.types.Menu): 233 | bl_idname = "BIOXELNODES_MT_NODE_HEAD" 234 | bl_label = "Bioxel Nodes" 235 | bl_icon = "FILE_VOLUME" 236 | 237 | def draw(self, context): 238 | 239 | layout = self.layout 240 | layout.menu(AddLayerMenu.bl_idname, 241 | icon=AddLayerMenu.bl_icon) 242 | layout.operator(SaveContainer.bl_idname) 243 | 244 | layout.separator() 245 | layout.operator(ContainerProps.bl_idname) 246 | layout.menu(ExtractFromContainerMenu.bl_idname) 247 | 248 | layout.separator() 249 | layout.operator(SaveContainerLayersCache.bl_idname, 250 | icon=SaveContainerLayersCache.bl_icon) 251 | layout.operator(RemoveContainerMissingLayers.bl_idname) 252 | 253 | layout.separator() 254 | layout.operator(AddLocator.bl_idname, 255 | icon=AddLocator.bl_icon) 256 | layout.operator(AddSlicer.bl_idname, 257 | icon=AddSlicer.bl_icon) 258 | layout.menu(AddCutterMenu.bl_idname) 259 | 260 | layout.separator() 261 | layout.menu(FetchLayerMenu.bl_idname) 262 | 263 | 264 | class NodeContextMenu(bpy.types.Menu): 265 | bl_idname = "BIOXELNODES_MT_NODE_CONTEXT" 266 | bl_label = "Bioxel Nodes" 267 | bl_icon = "FILE_VOLUME" 268 | 269 | def draw(self, context): 270 | layout = self.layout 271 | layout.operator(ExtractNodeMesh.bl_idname) 272 | layout.operator(ExtractNodeShapeWire.bl_idname) 273 | layout.operator(ExtractNodeBboxWire.bl_idname) 274 | 275 | layout.separator() 276 | layout.operator(SaveSelectedLayersCache.bl_idname, 277 | icon=SaveSelectedLayersCache.bl_icon) 278 | layout.operator(RemoveSelectedLayers.bl_idname) 279 | 280 | layout.separator() 281 | layout.operator(RenameLayer.bl_idname, 282 | icon=RenameLayer.bl_icon) 283 | layout.operator(RetimeLayer.bl_idname) 284 | layout.operator(RelocateLayer.bl_idname) 285 | 286 | layout.separator() 287 | layout.operator(ResampleLayer.bl_idname, 288 | icon=ResampleLayer.bl_icon) 289 | layout.operator(SignScalar.bl_idname) 290 | layout.operator(FillByThreshold.bl_idname) 291 | layout.operator(FillByRange.bl_idname) 292 | layout.operator(FillByLabel.bl_idname) 293 | layout.operator(CombineLabels.bl_idname) 294 | 295 | 296 | def NODE_CONTEXT(self, context): 297 | if is_incompatible(): 298 | return 299 | 300 | container_obj = context.object 301 | is_geo_nodes = context.area.ui_type == "GeometryNodeTree" 302 | is_container = get_container_obj(container_obj) 303 | 304 | if not is_geo_nodes or not is_container: 305 | return 306 | 307 | layout = self.layout 308 | layout.separator() 309 | layout.menu(NodeContextMenu.bl_idname) 310 | 311 | 312 | def NODE_HEAD(self, context): 313 | if is_incompatible(): 314 | return 315 | 316 | container_obj = context.object 317 | is_geo_nodes = context.area.ui_type == "GeometryNodeTree" 318 | is_container = get_container_obj(container_obj) 319 | 320 | if not is_geo_nodes or not is_container: 321 | return 322 | 323 | layout = self.layout 324 | layout.separator() 325 | layout.menu(NodeHeadMenu.bl_idname) 326 | 327 | 328 | def NODE_PROP(self, context): 329 | if is_incompatible(): 330 | return 331 | 332 | container_obj = context.object 333 | is_geo_nodes = context.area.ui_type == "GeometryNodeTree" 334 | is_container = get_container_obj(container_obj) 335 | self.bl_label = "Group" 336 | 337 | if not is_geo_nodes or not is_container: 338 | return 339 | 340 | if container_obj.modifiers[0].node_group != context.space_data.edit_tree: 341 | return 342 | 343 | self.bl_label = "Bioxel Nodes" 344 | 345 | layer_list_UL = bpy.context.window_manager.bioxelnodes_layer_list_UL 346 | layer_list = layer_list_UL.layer_list 347 | layer_list.clear() 348 | 349 | for layer_obj in get_container_layer_objs(container_obj): 350 | layer_item = layer_list.add() 351 | layer_item.label = get_layer_label(layer_obj) 352 | layer_item.obj_name = layer_obj.name 353 | layer_item.info_text = "\n".join([f"{prop}: {get_layer_prop_value(layer_obj, prop)}" 354 | for prop in ["kind", 355 | "bioxel_size", 356 | "shape", 357 | "frame_count", 358 | "channel_count", 359 | "min", "max"]]) 360 | 361 | layout = self.layout 362 | layout.label(text="Layer List") 363 | split = layout.row() 364 | split.template_list(listtype_name="BIOXELNODES_UL_layer_list", 365 | list_id="layer_list", 366 | dataptr=layer_list_UL, 367 | propname="layer_list", 368 | active_dataptr=layer_list_UL, 369 | active_propname="layer_list_active", 370 | item_dyntip_propname="info_text", 371 | rows=20) 372 | 373 | # sidebar = split.column(align=True) 374 | # sidebar.menu(AddLayerMenu.bl_idname, 375 | # icon=AddLayerMenu.bl_icon, text="") 376 | 377 | # sidebar.operator(SaveContainerLayersCache.bl_idname, 378 | # icon=SaveContainerLayersCache.bl_icon, text="") 379 | # sidebar.operator(RemoveContainerMissingLayers.bl_idname, 380 | # icon=RemoveContainerMissingLayers.bl_icon, text="") 381 | 382 | # sidebar.separator() 383 | # sidebar.operator(SaveSelectedLayersCache.bl_idname, 384 | # icon=SaveSelectedLayersCache.bl_icon, text="") 385 | # sidebar.operator(RemoveSelectedLayers.bl_idname, 386 | # icon=RemoveSelectedLayers.bl_icon, text="") 387 | 388 | # sidebar.separator() 389 | # sidebar.operator(RenameLayer.bl_idname, 390 | # icon=RenameLayer.bl_icon, text="") 391 | # sidebar.operator(ResampleLayer.bl_idname, 392 | # icon=ResampleLayer.bl_icon, text="") 393 | # sidebar.operator(RetimeLayer.bl_idname, 394 | # icon=RetimeLayer.bl_icon, text="") 395 | 396 | # sidebar.separator() 397 | # sidebar.operator(SignScalar.bl_idname, 398 | # icon=SignScalar.bl_icon, text="") 399 | # sidebar.operator(FillByThreshold.bl_idname, 400 | # icon=FillByThreshold.bl_icon, text="") 401 | # sidebar.operator(FillByRange.bl_idname, 402 | # icon=FillByRange.bl_icon, text="") 403 | # sidebar.operator(FillByLabel.bl_idname, 404 | # icon=FillByLabel.bl_icon, text="") 405 | 406 | # sidebar.separator() 407 | # layout.separator() 408 | 409 | 410 | def VIEW3D_TOPBAR(self, context): 411 | layout = self.layout 412 | layout.operator(SliceViewer.bl_idname) 413 | 414 | 415 | node_menu = NodeMenu( 416 | menu_items=MENU_ITEMS 417 | ) 418 | 419 | 420 | def add(): 421 | node_menu.register() 422 | # bpy.types.VIEW3D_HT_header.append(VIEW3D_TOPBAR) 423 | bpy.types.TOPBAR_MT_editor_menus.append(TOPBAR) 424 | bpy.types.NODE_PT_node_tree_properties.prepend(NODE_PROP) 425 | bpy.types.NODE_MT_editor_menus.append(NODE_HEAD) 426 | bpy.types.NODE_MT_context_menu.append(NODE_CONTEXT) 427 | 428 | 429 | def remove(): 430 | # bpy.types.VIEW3D_HT_header.remove(VIEW3D_TOPBAR) 431 | bpy.types.TOPBAR_MT_editor_menus.remove(TOPBAR) 432 | bpy.types.NODE_PT_node_tree_properties.remove(NODE_PROP) 433 | bpy.types.NODE_MT_editor_menus.remove(NODE_HEAD) 434 | bpy.types.NODE_MT_context_menu.remove(NODE_CONTEXT) 435 | node_menu.unregister() 436 | -------------------------------------------------------------------------------- /src/bioxelnodes/node_menu.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from .operators.node import AddNode 3 | 4 | 5 | class NodeMenu(): 6 | def __init__( 7 | self, 8 | menu_items, 9 | ) -> None: 10 | 11 | self.menu_items = menu_items 12 | self.class_prefix = f"BIOXELNODES_MT" 13 | root_label = "Bioxel Nodes" 14 | menu_classes = [] 15 | self._create_menu_class( 16 | items=menu_items, 17 | label=root_label, 18 | menu_classes=menu_classes 19 | ) 20 | self.menu_classes = menu_classes 21 | 22 | idname = f"{self.class_prefix}_{root_label.replace(' ', '').upper()}" 23 | icon = "FILE_VOLUME" 24 | 25 | def drew_menu(self, context): 26 | if (bpy.context.area.spaces[0].tree_type == 'GeometryNodeTree'): 27 | layout = self.layout 28 | layout.separator() 29 | layout.menu(idname, 30 | icon=icon) 31 | self.drew_menu = drew_menu 32 | 33 | def _create_menu_class(self, menu_classes, items, label='CustomNodes', icon=0, idname_namespace=None): 34 | idname_namespace = idname_namespace or self.class_prefix 35 | idname = f"{idname_namespace}_{label.replace(' ', '').upper()}" 36 | 37 | # create submenu class if item is menu. 38 | for item in items: 39 | item_items = item.get('items') if item != 'separator' else None 40 | if item_items: 41 | menu_class = self._create_menu_class( 42 | menu_classes=menu_classes, 43 | items=item_items, 44 | label=item.get('label') or 'CustomNodes', 45 | icon=item.get('icon') or 0, 46 | idname_namespace=idname 47 | ) 48 | item['menu_class'] = menu_class 49 | 50 | # create menu class 51 | class Menu(bpy.types.Menu): 52 | bl_idname = idname 53 | bl_label = label 54 | 55 | def draw(self, context): 56 | layout = self.layout 57 | for item in items: 58 | if item == "separator": 59 | layout.separator() 60 | elif item.get('menu_class'): 61 | layout.menu( 62 | item.get('menu_class').bl_idname, 63 | icon=item.get('icon') or 'NONE' 64 | ) 65 | else: 66 | op = layout.operator( 67 | AddNode.bl_idname, 68 | text=item.get('label'), 69 | icon=item.get('icon') or 'NONE' 70 | ) 71 | op.node_name = item['name'] 72 | op.node_label = item.get('label') or "" 73 | op.node_description = item.get( 74 | 'node_description') or "" 75 | 76 | menu_classes.append(Menu) 77 | return Menu 78 | 79 | def register(self): 80 | for cls in self.menu_classes: 81 | bpy.utils.register_class(cls) 82 | bpy.types.NODE_MT_add.append(self.drew_menu) 83 | 84 | def unregister(self): 85 | bpy.types.NODE_MT_add.remove(self.drew_menu) 86 | for cls in reversed(self.menu_classes): 87 | bpy.utils.unregister_class(cls) 88 | -------------------------------------------------------------------------------- /src/bioxelnodes/operators/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/src/bioxelnodes/operators/__init__.py -------------------------------------------------------------------------------- /src/bioxelnodes/operators/misc.py: -------------------------------------------------------------------------------- 1 | import webbrowser 2 | import bpy 3 | from pathlib import Path 4 | import shutil 5 | 6 | from ..bioxelutils.common import get_all_layer_objs, get_node_lib_path, get_node_version, is_missing_layer, set_file_prop 7 | from .layer import RemoveLayers, SaveLayersCache 8 | 9 | from ..constants import LATEST_NODE_LIB_PATH, VERSIONS 10 | from ..utils import get_cache_dir 11 | 12 | 13 | class ReLinkNodeLib(bpy.types.Operator): 14 | bl_idname = "bioxelnodes.relink_node_lib" 15 | bl_label = "Relink Node Library" 16 | bl_description = "Relink all nodes to addon library source" 17 | bl_options = {'UNDO'} 18 | 19 | index: bpy.props.IntProperty() # type: ignore 20 | 21 | def execute(self, context): 22 | node_version = VERSIONS[self.index]['node_version'] 23 | lib_path = get_node_lib_path(node_version) \ 24 | if self.index != 0 else LATEST_NODE_LIB_PATH 25 | 26 | node_libs = [] 27 | for node_group in bpy.data.node_groups: 28 | if node_group.name.startswith("BioxelNodes"): 29 | node_lib = node_group.library 30 | if node_lib: 31 | node_libs.append(node_lib) 32 | 33 | node_libs = list(set(node_libs)) 34 | 35 | for node_lib in node_libs: 36 | node_lib.filepath = str(lib_path) 37 | # FIXME: may cause crash 38 | node_lib.reload() 39 | 40 | set_file_prop("node_version", node_version) 41 | 42 | self.report({"INFO"}, f"Successfully relinked.") 43 | 44 | return {'FINISHED'} 45 | 46 | 47 | class SaveNodeLib(bpy.types.Operator): 48 | bl_idname = "bioxelnodes.save_node_lib" 49 | bl_label = "Save Node Library" 50 | bl_description = "Save node library file to local" 51 | bl_options = {'UNDO'} 52 | 53 | lib_dir: bpy.props.StringProperty( 54 | name="Library Directory", 55 | subtype='DIR_PATH', 56 | default="//" 57 | ) # type: ignore 58 | 59 | def execute(self, context): 60 | node_version = get_node_version() 61 | 62 | if node_version is None: 63 | node_version = VERSIONS[0]["node_version"] 64 | else: 65 | if node_version not in [v["node_version"] for v in VERSIONS]: 66 | node_version = VERSIONS[0]["node_version"] 67 | 68 | if node_version == VERSIONS[0]["node_version"]: 69 | lib_path = LATEST_NODE_LIB_PATH 70 | else: 71 | lib_path = get_node_lib_path(node_version) 72 | 73 | version_str = "v"+".".join([str(i) for i in list(node_version)]) 74 | 75 | lib_dir = bpy.path.abspath(self.lib_dir) 76 | local_lib_path: Path = Path(lib_dir, 77 | f"BioxelNodes_{version_str}.blend").resolve() 78 | node_lib_path: Path = lib_path 79 | blend_path = Path(bpy.path.abspath("//")).resolve() 80 | 81 | if local_lib_path != node_lib_path: 82 | shutil.copy(node_lib_path, local_lib_path) 83 | 84 | libs = [] 85 | for node_group in bpy.data.node_groups: 86 | if node_group.name.startswith("BioxelNodes"): 87 | if node_group.library: 88 | libs.append(node_group.library) 89 | 90 | libs = list(set(libs)) 91 | for lib in libs: 92 | lib.filepath = bpy.path.relpath(str(local_lib_path), 93 | start=str(blend_path)) 94 | 95 | return {'FINISHED'} 96 | 97 | def invoke(self, context, event): 98 | context.window_manager.invoke_props_dialog(self) 99 | return {'RUNNING_MODAL'} 100 | 101 | @classmethod 102 | def poll(cls, context): 103 | return bpy.data.is_saved 104 | 105 | 106 | class CleanTemp(bpy.types.Operator): 107 | bl_idname = "bioxelnodes.clear_temp" 108 | bl_label = "Clean Temp" 109 | bl_description = "Clean all cache in temp (include other project cache)" 110 | 111 | def execute(self, context): 112 | cache_dir = get_cache_dir() 113 | try: 114 | shutil.rmtree(cache_dir) 115 | self.report({"INFO"}, f"Successfully cleaned temp.") 116 | return {'FINISHED'} 117 | except: 118 | self.report({"WARNING"}, 119 | "Fail to clean temp, you may do it manually.") 120 | return {'CANCELLED'} 121 | 122 | def invoke(self, context, event): 123 | context.window_manager.invoke_confirm(self, 124 | event, 125 | message="All temp files will be removed, include other project cache, do you still want to clean?") 126 | return {'RUNNING_MODAL'} 127 | 128 | 129 | class RenderSettingPreset(bpy.types.Operator): 130 | bl_idname = "bioxelnodes.render_setting_preset" 131 | bl_label = "Render Setting Presets" 132 | bl_description = "Render setting presets for bioxel" 133 | bl_options = {'UNDO'} 134 | 135 | PRESETS = { 136 | "performance": "Performance", 137 | "balance": "Balance", 138 | "quality": "Quality", 139 | } 140 | 141 | preset: bpy.props.EnumProperty(name="Preset", 142 | default="balance", 143 | items=[(k, v, "") 144 | for k, v in PRESETS.items()]) # type: ignore 145 | 146 | def execute(self, context): 147 | if self.preset == "performance": 148 | # EEVEE 149 | # bpy.context.scene.eevee.use_taa_reprojection = False 150 | bpy.context.scene.eevee.use_shadow_jitter_viewport = True 151 | bpy.context.scene.eevee.volumetric_tile_size = '2' 152 | bpy.context.scene.eevee.volumetric_shadow_samples = 32 153 | bpy.context.scene.eevee.volumetric_samples = 64 154 | bpy.context.scene.eevee.volumetric_ray_depth = 1 155 | bpy.context.scene.eevee.use_volumetric_shadows = True 156 | 157 | # Cycles 158 | bpy.context.scene.cycles.volume_bounces = 0 159 | bpy.context.scene.cycles.transparent_max_bounces = 4 160 | bpy.context.scene.cycles.volume_preview_step_rate = 4 161 | bpy.context.scene.cycles.volume_step_rate = 4 162 | 163 | elif self.preset == "balance": 164 | # EEVEE 165 | # bpy.context.scene.eevee.use_taa_reprojection = False 166 | bpy.context.scene.eevee.use_shadow_jitter_viewport = True 167 | bpy.context.scene.eevee.volumetric_tile_size = '2' 168 | bpy.context.scene.eevee.volumetric_shadow_samples = 64 169 | bpy.context.scene.eevee.volumetric_samples = 128 170 | bpy.context.scene.eevee.volumetric_ray_depth = 8 171 | bpy.context.scene.eevee.use_volumetric_shadows = True 172 | 173 | # Cycles 174 | bpy.context.scene.cycles.volume_bounces = 4 175 | bpy.context.scene.cycles.transparent_max_bounces = 8 176 | bpy.context.scene.cycles.volume_preview_step_rate = 1 177 | bpy.context.scene.cycles.volume_step_rate = 1 178 | 179 | elif self.preset == "quality": 180 | # EEVEE 181 | # bpy.context.scene.eevee.use_taa_reprojection = False 182 | bpy.context.scene.eevee.use_shadow_jitter_viewport = True 183 | bpy.context.scene.eevee.volumetric_tile_size = '2' 184 | bpy.context.scene.eevee.volumetric_shadow_samples = 128 185 | bpy.context.scene.eevee.volumetric_samples = 256 186 | bpy.context.scene.eevee.volumetric_ray_depth = 16 187 | bpy.context.scene.eevee.use_volumetric_shadows = True 188 | 189 | # Cycles 190 | bpy.context.scene.cycles.volume_bounces = 8 191 | bpy.context.scene.cycles.transparent_max_bounces = 16 192 | bpy.context.scene.cycles.volume_preview_step_rate = 0.5 193 | bpy.context.scene.cycles.volume_step_rate = 0.5 194 | 195 | return {'FINISHED'} 196 | 197 | 198 | class SliceViewer(bpy.types.Operator): 199 | bl_idname = "bioxelnodes.slice_viewer" 200 | bl_label = "Slice Viewer" 201 | bl_description = "A preview scene setting for viewing slicers" 202 | bl_icon = "FILE_VOLUME" 203 | 204 | def execute(self, context): 205 | # bpy.context.scene.eevee.use_taa_reprojection = False 206 | bpy.context.scene.eevee.volumetric_tile_size = '2' 207 | bpy.context.scene.eevee.volumetric_shadow_samples = 128 208 | bpy.context.scene.eevee.volumetric_samples = 128 209 | bpy.context.scene.eevee.volumetric_ray_depth = 1 210 | 211 | bpy.context.scene.eevee.use_shadows = False 212 | bpy.context.scene.eevee.use_volumetric_shadows = False 213 | 214 | view_3d = None 215 | if context.area.type == 'VIEW_3D': 216 | view_3d = context.area 217 | else: 218 | for area in context.screen.areas: 219 | if area.type == 'VIEW_3D': 220 | view_3d = area 221 | break 222 | if view_3d: 223 | view_3d.spaces[0].shading.type = 'MATERIAL' 224 | view_3d.spaces[0].shading.studio_light = 'studio.exr' 225 | view_3d.spaces[0].shading.studiolight_intensity = 1.5 226 | view_3d.spaces[0].shading.use_scene_lights = False 227 | view_3d.spaces[0].shading.use_scene_world = False 228 | 229 | return {'FINISHED'} 230 | 231 | 232 | def get_all_layers(layer_filter=None): 233 | def _layer_filter(layer_obj): 234 | return True 235 | 236 | layer_filter = layer_filter or _layer_filter 237 | layer_objs = get_all_layer_objs() 238 | return [obj for obj in layer_objs if layer_filter(obj)] 239 | 240 | 241 | class SaveAllLayersCache(bpy.types.Operator, SaveLayersCache): 242 | bl_idname = "bioxelnodes.save_all_layers_cache" 243 | bl_label = "Save All Layers Cache" 244 | bl_description = "Save all cache of this file" 245 | bl_icon = "FILE_TICK" 246 | 247 | success_msg = "Successfully saved all layers." 248 | 249 | def get_layers(self, context): 250 | def is_not_missing(layer_obj): 251 | return not is_missing_layer(layer_obj) 252 | return get_all_layers(is_not_missing) 253 | 254 | 255 | class RemoveAllMissingLayers(bpy.types.Operator, RemoveLayers): 256 | bl_idname = "bioxelnodes.remove_all_missing_layers" 257 | bl_label = "Remove All Missing Layers" 258 | bl_description = "Remove all current container missing layers" 259 | bl_icon = "BRUSH_DATA" 260 | 261 | success_msg = "Successfully removed all missing layers." 262 | 263 | def get_layers(self, context): 264 | def is_missing(layer_obj): 265 | return is_missing_layer(layer_obj) 266 | return get_all_layers(is_missing) 267 | 268 | def invoke(self, context, event): 269 | context.window_manager.invoke_confirm(self, 270 | event, 271 | message=f"Are you sure to remove all **Missing** layers?") 272 | return {'RUNNING_MODAL'} 273 | 274 | 275 | class Help(bpy.types.Operator): 276 | bl_idname = "bioxelnodes.help" 277 | bl_label = "Need Help?" 278 | bl_description = "Online Manual for Beginner" 279 | bl_icon = "HELP" 280 | 281 | def execute(self, context): 282 | webbrowser.open( 283 | 'https://omoolab.github.io/BioxelNodes/latest/', new=2) 284 | 285 | return {'FINISHED'} 286 | 287 | 288 | class AddEeveeEnv(bpy.types.Operator): 289 | bl_idname = "bioxelnodes.add_eevee_env" 290 | bl_label = "Add EEVEE Env Light" 291 | bl_description = "To make volume shadow less dark" 292 | 293 | def execute(self, context): 294 | 295 | bpy.ops.wm.append( 296 | directory=f"{str(LATEST_NODE_LIB_PATH)}/Object", 297 | filename="EeveeEnv" 298 | ) 299 | 300 | bpy.context.scene.eevee.use_shadows = True 301 | bpy.context.scene.eevee.use_volumetric_shadows = True 302 | 303 | return {'FINISHED'} 304 | -------------------------------------------------------------------------------- /src/bioxelnodes/operators/node.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | from ..bioxelutils.common import is_incompatible, local_lib_not_updated 4 | from ..bioxelutils.node import assign_node_tree, get_node_tree 5 | from ..utils import get_use_link 6 | 7 | 8 | class AddNode(bpy.types.Operator): 9 | bl_idname = "bioxelnodes.add_node" 10 | bl_label = "Add Node" 11 | bl_options = {"REGISTER", "UNDO"} 12 | 13 | node_name: bpy.props.StringProperty( 14 | default='', 15 | ) # type: ignore 16 | 17 | node_label: bpy.props.StringProperty( 18 | default='' 19 | ) # type: ignore 20 | 21 | node_description: bpy.props.StringProperty( 22 | default="", 23 | ) # type: ignore 24 | 25 | @property 26 | def node_type(self): 27 | return f"BioxelNodes_{self.node_name}" 28 | 29 | def execute(self, context): 30 | space = context.space_data 31 | if space.type != "NODE_EDITOR": 32 | self.report({"ERROR"}, "Not in node editor.") 33 | return {'CANCELLED'} 34 | 35 | if not space.edit_tree.is_editable: 36 | self.report({"ERROR"}, "Not editable.") 37 | return {'CANCELLED'} 38 | 39 | if is_incompatible(): 40 | self.report({"ERROR"}, 41 | "Current addon verison is not compatible to this file. If you insist on editing this file please keep the same addon version.") 42 | return {'CANCELLED'} 43 | 44 | node_tree = get_node_tree(self.node_type, get_use_link()) 45 | bpy.ops.node.add_node( 46 | 'INVOKE_DEFAULT', 47 | type='GeometryNodeGroup', 48 | use_transform=True 49 | ) 50 | node = bpy.context.active_node 51 | assign_node_tree(node, node_tree) 52 | node.label = self.node_label 53 | 54 | node.show_options = False 55 | 56 | if local_lib_not_updated(): 57 | self.report({"WARNING"}, 58 | "Local node library version does not match the current addon version, which may cause problems, please save the node library again.") 59 | 60 | return {"FINISHED"} 61 | -------------------------------------------------------------------------------- /src/bioxelnodes/preferences.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from pathlib import Path 3 | 4 | 5 | class BioxelNodesPreferences(bpy.types.AddonPreferences): 6 | bl_idname = __package__ 7 | 8 | cache_dir: bpy.props.StringProperty( 9 | name="Set Cache Directory", 10 | subtype='DIR_PATH', 11 | default=str(Path(Path.home(), '.bioxelnodes')) 12 | ) # type: ignore 13 | 14 | change_render_setting: bpy.props.BoolProperty( 15 | name="Change Render Setting on First Import", 16 | default=True, 17 | ) # type: ignore 18 | 19 | node_import_method: bpy.props.EnumProperty(name="Node Import Method", 20 | default="LINK", 21 | items=[("LINK", "Link", ""), 22 | ("APPEND", "Append (Reuse Data)", "")]) # type: ignore 23 | 24 | def draw(self, context): 25 | layout = self.layout 26 | layout.label(text="Configuration") 27 | layout.prop(self, 'cache_dir') 28 | layout.prop(self, "change_render_setting") 29 | layout.prop(self, "node_import_method") 30 | -------------------------------------------------------------------------------- /src/bioxelnodes/props.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | from .bioxelutils.common import get_node_type 4 | 5 | 6 | class BIOXELNODES_UL_layer_list(bpy.types.UIList): 7 | def draw_item(self, context, layout, data, item, icon, active_data, active_propname): 8 | layout.label(text=item.label, translate=False, icon_value=icon) 9 | 10 | 11 | class BIOXELNODES_Series(bpy.types.PropertyGroup): 12 | id: bpy.props.StringProperty() # type: ignore 13 | label: bpy.props.StringProperty() # type: ignore 14 | 15 | 16 | def select_layer(self, context): 17 | layer_list_UL = bpy.context.window_manager.bioxelnodes_layer_list_UL 18 | layer_list = layer_list_UL.layer_list 19 | layer_list_active = layer_list_UL.layer_list_active 20 | 21 | if len(layer_list) > 0 and layer_list_active != -1 and layer_list_active < len(layer_list): 22 | layer_obj = bpy.data.objects[layer_list[layer_list_active].obj_name] 23 | node_group = context.space_data.edit_tree 24 | for node in node_group.nodes: 25 | node.select = False 26 | if get_node_type(node) == "BioxelNodes_FetchLayer": 27 | if node.inputs[0].default_value == layer_obj: 28 | node.select = True 29 | 30 | layer_list_UL.layer_list_active = -1 31 | 32 | 33 | class BIOXELNODES_Layer(bpy.types.PropertyGroup): 34 | obj_name: bpy.props.StringProperty() # type: ignore 35 | label: bpy.props.StringProperty() # type: ignore 36 | info_text: bpy.props.StringProperty() # type: ignore 37 | 38 | 39 | class BIOXELNODES_LayerListUL(bpy.types.PropertyGroup): 40 | layer_list: bpy.props.CollectionProperty( 41 | type=BIOXELNODES_Layer) # type: ignore 42 | layer_list_active: bpy.props.IntProperty(default=-1, 43 | update=select_layer, 44 | options=set()) # type: ignore 45 | -------------------------------------------------------------------------------- /src/bioxelnodes/utils.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import shutil 3 | import bpy 4 | 5 | 6 | def copy_to_dir(source_path, dir_path, new_name=None, exist_ok=True): 7 | source = Path(source_path) 8 | target = Path(dir_path) 9 | 10 | # Check if the source exists 11 | if not source.exists(): 12 | raise FileNotFoundError 13 | 14 | # Check if the target exists 15 | if not target.exists(): 16 | target.mkdir(parents=True, exist_ok=True) 17 | 18 | target_path = target / new_name if new_name else target / source.name 19 | # If source is a file, copy it to the target directory 20 | if source.is_file(): 21 | try: 22 | shutil.copy(source, target_path) 23 | except shutil.SameFileError: 24 | if exist_ok: 25 | pass 26 | else: 27 | raise shutil.SameFileError 28 | # If source is a directory, copy its contents to the target directory 29 | elif source.is_dir(): 30 | shutil.copytree(source, target_path, dirs_exist_ok=exist_ok) 31 | 32 | if not target_path.exists(): 33 | raise Exception 34 | 35 | 36 | def select_object(target_obj): 37 | for obj in bpy.data.objects: 38 | obj.select_set(False) 39 | 40 | target_obj.select_set(True) 41 | bpy.context.view_layer.objects.active = target_obj 42 | 43 | 44 | def progress_bar(self, context): 45 | row = self.layout.row() 46 | row.progress( 47 | factor=context.window_manager.bioxelnodes_progress_factor, 48 | type="BAR", 49 | text=context.window_manager.bioxelnodes_progress_text 50 | ) 51 | row.scale_x = 2 52 | 53 | 54 | def progress_update(context, factor, text=""): 55 | context.window_manager.bioxelnodes_progress_factor = factor 56 | context.window_manager.bioxelnodes_progress_text = text 57 | 58 | 59 | def get_preferences(): 60 | return bpy.context.preferences.addons[__package__].preferences 61 | 62 | 63 | def get_cache_dir(): 64 | preferences = get_preferences() 65 | cache_path = Path(preferences.cache_dir, 'VDBs') 66 | cache_path.mkdir(parents=True, exist_ok=True) 67 | return str(cache_path) 68 | 69 | 70 | def get_use_link(): 71 | preferences = get_preferences() 72 | return preferences.node_import_method == "LINK" 73 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OmooLab/BioxelNodes/afae532a2c91c87068458035d244e237cbe71a50/tests/__init__.py --------------------------------------------------------------------------------