├── .github ├── scripts │ └── setup_addon.py └── workflows │ ├── CD.yml │ └── Manual.yml ├── .gitignore ├── .gitmodules ├── LICENSE.md ├── README.md ├── VERSION ├── __init__.py ├── build_mtree.bat ├── build_mtree.osx ├── build_mtree.sh ├── m_tree ├── CMakeLists.txt ├── LICENSE.md ├── install.py ├── python_bindings │ ├── CMakeLists.txt │ └── main.cpp ├── source │ ├── CMakeLists.txt │ ├── mesh │ │ ├── Attribute.hpp │ │ ├── Mesh.cpp │ │ ├── Mesh.hpp │ │ ├── VertexGroup.cpp │ │ └── VertexGroup.hpp │ ├── meshers │ │ ├── base_types │ │ │ ├── TreeMesher.cpp │ │ │ └── TreeMesher.hpp │ │ ├── manifold_mesher │ │ │ ├── ManifoldMesher.cpp │ │ │ ├── ManifoldMesher.hpp │ │ │ ├── smoothing.cpp │ │ │ └── smoothing.hpp │ │ └── splines_mesher │ │ │ ├── BasicMesher.cpp │ │ │ └── BasicMesher.hpp │ ├── tree │ │ ├── GrowthInfo.cpp │ │ ├── GrowthInfo.hpp │ │ ├── Node.cpp │ │ ├── Node.hpp │ │ ├── Tree.cpp │ │ └── Tree.hpp │ ├── tree_functions │ │ ├── BranchFunction.cpp │ │ ├── BranchFunction.hpp │ │ ├── GrowthFunction.cpp │ │ ├── GrowthFunction.hpp │ │ ├── LeavesFunction.cpp │ │ ├── LeavesFunction.hpp │ │ ├── PipeRadiusFunction.cpp │ │ ├── PipeRadiusFunction.hpp │ │ ├── TrunkFunction.cpp │ │ ├── TrunkFunction.hpp │ │ └── base_types │ │ │ ├── Property.hpp │ │ │ ├── TreeFunction.cpp │ │ │ └── TreeFunction.hpp │ └── utilities │ │ ├── GeometryUtilities.cpp │ │ ├── GeometryUtilities.hpp │ │ ├── NodeUtilities.cpp │ │ ├── NodeUtilities.hpp │ │ ├── RandomGenerator.cpp │ │ └── RandomGenerator.hpp ├── test.py └── tests │ ├── CMakeLists.txt │ └── main.cpp ├── python_classes ├── __init__.py ├── nodes │ ├── __init__.py │ ├── base_types │ │ ├── __init__.py │ │ ├── node.py │ │ ├── node_tree.py │ │ └── socket.py │ ├── node_categories.py │ ├── properties │ │ ├── __init__.py │ │ ├── ramp_property.py │ │ └── random_property.py │ ├── sockets │ │ ├── __init__.py │ │ ├── float_socket.py │ │ ├── int_socket.py │ │ ├── property_socket.py │ │ └── tree_socket.py │ └── tree_function_nodes │ │ ├── __init__.py │ │ ├── branch_node.py │ │ ├── pipe_radius_node.py │ │ ├── tree_mesher_node.py │ │ └── trunk_node.py ├── operators.py └── resources │ ├── __init__.py │ ├── node_groups.py │ └── resource_utils.py └── resources └── geo_node ├── geo_nodes_2_93.blend └── geo_nodes_3_2.blend /.github/scripts/setup_addon.py: -------------------------------------------------------------------------------- 1 | from genericpath import exists 2 | import os 3 | import zipfile 4 | from pathlib import Path 5 | import shutil 6 | import platform 7 | 8 | 9 | TMP_DIRPATH = r"./tmp" 10 | ADDON_SOURCE_DIRNAME = "python_classes" 11 | RESOURCES_DIRNAME = "resources" 12 | VERSION_FILEPATH = os.path.join(Path(__file__).parent.parent.parent, "VERSION") 13 | 14 | def setup_addon_directory(): 15 | plateform_name = "windows" if platform.system() == "Windows" else "linux" if platform.system() == "Linux" else "macOS" 16 | version = read_version().rstrip('\n') 17 | addon_dirpath = os.path.join(TMP_DIRPATH, f"modular_tree_{version}_{plateform_name}") 18 | root = os.path.join(addon_dirpath, "modular_tree") 19 | Path(root).mkdir(exist_ok=True, parents=True) 20 | 21 | all_files = os.listdir(".") 22 | 23 | 24 | if not [i for i in all_files if i.endswith(".pyd") or i.endswith(".so")]: 25 | list_files(".") 26 | raise Exception("no libraries were output") 27 | for f in all_files: 28 | if f.endswith(".py") or f.endswith(".pyd") or f.endswith(".so"): 29 | shutil.copy2(os.path.join(".",f), root) 30 | elif f == ADDON_SOURCE_DIRNAME or f == RESOURCES_DIRNAME: 31 | shutil.copytree(os.path.join(".",f), os.path.join(root, f)) 32 | 33 | return addon_dirpath 34 | 35 | def create_zip(input_dir, output_dir): 36 | basename = os.path.join(output_dir, Path(input_dir).stem) 37 | filepath = shutil.make_archive(basename, "zip", input_dir) 38 | return filepath 39 | 40 | 41 | def list_files(root_directory): 42 | excluded_directories = {"dependencies", "build", "__pycache__", ".github", ".git"} 43 | for root, _, files in os.walk(root_directory): 44 | should_skip = False 45 | for exclusion in excluded_directories: 46 | if exclusion in root: 47 | should_skip = True 48 | break 49 | if should_skip: 50 | continue 51 | 52 | 53 | level = root.replace(root_directory, '').count(os.sep) 54 | indent = ' ' * 4 * (level) 55 | print('{}{}/'.format(indent, os.path.basename(root))) 56 | subindent = ' ' * 4 * (level + 1) 57 | for f in files: 58 | print('{}{}'.format(subindent, f)) 59 | 60 | def read_version(): 61 | with open(VERSION_FILEPATH, "r") as f: 62 | return f.read() 63 | 64 | if __name__ == "__main__": 65 | addon_dirpath = setup_addon_directory() 66 | archive_filepath = create_zip(addon_dirpath, TMP_DIRPATH) 67 | -------------------------------------------------------------------------------- /.github/workflows/CD.yml: -------------------------------------------------------------------------------- 1 | name: test_github_actions 2 | on: [push] 3 | jobs: 4 | build: 5 | strategy: 6 | matrix: 7 | include: 8 | - os: windows-latest 9 | extension: bat 10 | name: windows 11 | - os: ubuntu-latest 12 | extension: sh 13 | name: linux 14 | - os: macos-latest 15 | extension: osx 16 | name: macOS 17 | runs-on: ${{ matrix.os }} 18 | steps: 19 | - name: checkout 20 | uses: actions/checkout@v2 21 | with: 22 | submodules: true 23 | - name: setup python 24 | uses: actions/setup-python@v2 25 | with: 26 | python-version: 3.10 27 | - name: build 28 | run: ./build_mtree.${{ matrix.extension }} 29 | - name: package addon 30 | run: python .github/scripts/setup_addon.py 31 | - name: upload zipped addon 32 | uses: actions/upload-artifact@v2 33 | with: 34 | name: modular_tree_${{ matrix.name }} 35 | path: tmp/*.zip 36 | 37 | release: 38 | needs: build 39 | runs-on: ubuntu-latest 40 | env: 41 | STATUS: ${{ !endsWith(github.ref, 'master') && 'alpha' || '' }} 42 | steps: 43 | - name: checkout 44 | uses: actions/checkout@v2 45 | - name: Download build artifacts 46 | uses: actions/download-artifact@v2 47 | - name: Display structure of downloaded files 48 | run: ls -R 49 | - name: Get version 50 | id: vars 51 | run: echo ::set-output name=version::$(cat VERSION) 52 | - name: Update release 53 | uses: johnwbyrd/update-release@v1.0.0 54 | with: 55 | token: ${{ secrets.GITHUB_TOKEN }} 56 | files: ./modular_tree_windows/modular_tree_${{ steps.vars.outputs.version }}_windows.zip ./modular_tree_linux/modular_tree_${{ steps.vars.outputs.version }}_linux.zip ./modular_tree_macOS/modular_tree_${{ steps.vars.outputs.version }}_macOS.zip 57 | release: Release V${{ steps.vars.outputs.version }}${{ env.STATUS }} 58 | tag: ${{ steps.vars.outputs.version }}${{ env.STATUS }} 59 | message: V${{ steps.vars.outputs.version }} 60 | body: '' 61 | 62 | -------------------------------------------------------------------------------- /.github/workflows/Manual.yml: -------------------------------------------------------------------------------- 1 | name: test_github_actions_manual 2 | on: [workflow_dispatch] 3 | jobs: 4 | build: 5 | strategy: 6 | matrix: 7 | include: 8 | - os: windows-latest 9 | extension: bat 10 | name: windows 11 | - os: ubuntu-latest 12 | extension: sh 13 | name: linux 14 | - os: macos-latest 15 | extension: osx 16 | name: macOS 17 | runs-on: ${{ matrix.os }} 18 | steps: 19 | - name: checkout 20 | uses: actions/checkout@v3 21 | with: 22 | submodules: true 23 | - name: setup python 24 | uses: actions/setup-python@v4 25 | with: 26 | python-version: '3.10' 27 | - name: build 28 | run: ./build_mtree.${{ matrix.extension }} 29 | - name: package addon 30 | run: python .github/scripts/setup_addon.py 31 | - name: upload zipped addon 32 | uses: actions/upload-artifact@v3 33 | with: 34 | name: modular_tree_${{ matrix.name }} 35 | path: tmp/*.zip 36 | 37 | release: 38 | needs: build 39 | runs-on: ubuntu-latest 40 | env: 41 | STATUS: ${{ !endsWith(github.ref, 'master') && 'alpha' || '' }} 42 | steps: 43 | - name: checkout 44 | uses: actions/checkout@v3 45 | - name: Download build artifacts 46 | uses: actions/download-artifact@v3 47 | - name: Display structure of downloaded files 48 | run: ls -R 49 | - name: Get version 50 | id: vars 51 | run: echo ::set-output name=version::$(cat VERSION) 52 | 53 | # Create release 54 | - name: Create release 55 | id: create-release 56 | uses: actions/create-release@v1 57 | env: 58 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 59 | with: 60 | tag_name: ${{ steps.vars.outputs.version }}${{ env.STATUS }} 61 | release_name: Release V${{ steps.vars.outputs.version }}${{ env.STATUS }} 62 | draft: false 63 | prerelease: false 64 | 65 | # Upload assets to release 66 | - name: Upload assets to release - Windows 67 | uses: actions/upload-release-asset@v1 68 | env: 69 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 70 | with: 71 | upload_url: ${{ steps.create-release.outputs.upload_url }} 72 | asset_path: ./modular_tree_windows/modular_tree_${{ steps.vars.outputs.version }}_windows.zip 73 | asset_name: modular_tree_${{ steps.vars.outputs.version }}_windows.zip 74 | asset_content_type: application/zip 75 | 76 | - name: Upload assets to release - Linux 77 | uses: actions/upload-release-asset@v1 78 | env: 79 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 80 | with: 81 | upload_url: ${{ steps.create-release.outputs.upload_url }} 82 | asset_path: ./modular_tree_linux/modular_tree_${{ steps.vars.outputs.version }}_linux.zip 83 | asset_name: modular_tree_${{ steps.vars.outputs.version }}_linux.zip 84 | asset_content_type: application/zip 85 | 86 | - name: Upload assets to release - OSX 87 | uses: actions/upload-release-asset@v1 88 | env: 89 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 90 | with: 91 | upload_url: ${{ steps.create-release.outputs.upload_url }} 92 | asset_path: ./modular_tree_macOS/modular_tree_${{ steps.vars.outputs.version }}_macOS.zip 93 | asset_name: modular_tree_${{ steps.vars.outputs.version }}_macOS_universal.zip 94 | asset_content_type: application/zip 95 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | */build/ 2 | *__pycache__/ 3 | *Debug/ 4 | m_tree/m_tree.egg-info/ 5 | 6 | 7 | # Compiled Object files 8 | *.slo 9 | *.lo 10 | *.o 11 | *.obj 12 | 13 | # Precompiled Headers 14 | *.gch 15 | *.pch 16 | 17 | # Compiled Dynamic libraries 18 | *.so 19 | *.dylib 20 | *.dll 21 | 22 | # Fortran module files 23 | *.mod 24 | *.smod 25 | 26 | # Compiled Static libraries 27 | *.lai 28 | *.la 29 | *.a 30 | *.lib 31 | 32 | # Executables 33 | *.exe 34 | *.out 35 | *.app 36 | *.pyd 37 | *.pdb 38 | **/.vscode/ 39 | m_tree.egg-info/ 40 | *.exp 41 | */binaries/ 42 | tmp/* 43 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "m_tree/dependencies/vcpkg"] 2 | path = m_tree/dependencies/vcpkg 3 | url = https://github.com/microsoft/vcpkg.git 4 | [submodule "m_tree/dependencies/pybind11"] 5 | path = m_tree/dependencies/pybind11 6 | url = https://github.com/pybind/pybind11.git 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mtree fork 2 | 3 | This is a fork of the modular_tree Blender add-on by Maxime Herpin. This fork was created to fix compatibility issues with recent versions of Blender (3.1 & 3.2) as activity in the original repo appears to have stalled out for a bit. 4 | 5 | # Mtree 6 | 7 | Mtree (previously Modular Tree) is a library for making 3d trees. It comes as an addon for blender but the c++ library can be used separately. 8 | 9 | ## Table of contents 10 | - [Mtree](#mtree) 11 | - [Table of contents](#table-of-contents) 12 | - [Installation (blender addon)](#installation-blender-addon) 13 | - [Development](#development) 14 | - [Dependencies](#dependencies) 15 | - [Installation](#installation) 16 | - [Usage](#usage) 17 | - [License](#license) 18 | 19 | 20 | ## Installation (blender addon) 21 | Go to the [latest release]. Under `Assets`, select the version corresponding to your os.\ 22 | Follow the [blender documentation][blender addon doc] to install the downloaded addon. 23 | 24 | ## Development 25 | ### Dependencies 26 | - [Cmake] 27 | - Blender 2.93 or higher (if you want to to develop the blender addon) 28 | 29 | ### Installation 30 | 1. Clone the repository reccursively `git clone --recursive https://github.com/MaximeHerpin/modular_tree` 31 | 2. Execute the `build_mtree` bash script corresponding to your platform. 32 | 3. If all went well, a cmake project has been generated under `mtree/build`. 33 | 4. You can bundle the blender addon by calling the [addon bundling script]. 34 | 35 | ### Usage 36 | A `Tree` is generated by executing a succession of `TreeFunction`. When being executed, a `TreeFunction` modifies the structure of the tree, and then calls children functions recursively.\ 37 | For example, a basic tree has a trunk and branches on the trunk. Such a tree can be generated as such: 38 | ```c++ 39 | auto trunk = std::make_shared(); 40 | auto branches = std::make_shared(); 41 | trunk->add_child(branches); // branches are added on top of the trunk 42 | Tree tree(trunk); 43 | tree.execute_functions(); // The tree structure is generated 44 | ManifoldMesher mesher; // A mesher is responsible of converting a tree into a 3d mesh. The ManifoldMesher ensures a smooth topology 45 | mesher.radial_resolution = 32; 46 | Mesh tree_mesh = mesher.mesh_tree(tree); // the resulting mesh contains the geometry of the tree in the form of vertices and triangles 47 | ``` 48 | A second layer of branches can be grown on top of the branches by adding another branch function as a child of the first branch function: 49 | ```c++ 50 | auto branches_primary = std::make_shared(); 51 | auto branches_secondary = std::make_shared(); 52 | branches_primary.add_child(branches_secondary); // the secondray branches will be distributed on top of the primary branches 53 | ``` 54 | Some trees have healthy branches as well as a layer of thin dead branches along the trunk. This can be achieved by adding to branch functions with different parameters on the trunk: 55 | ```c++ 56 | auto trunk = std::make_shared(); 57 | auto branches_healthy = std::make_shared(); 58 | auto branches_dead = std::make_shared(); 59 | branches_dead.length = RandomProperty{.1f,1f}; // dead branches will have a length between 10cm and 1m. 60 | branches_dead.start_radius = ConstantProperty{.05f}; // dead branches will have a radius equal to 5% of the parent nodes 61 | 62 | trunk->add_child(branches_healthy); // both sets of branches are grown on top of the trunk 63 | trunk->add_child(branches_dead); 64 | Tree tree(trunk); 65 | ``` 66 | ## License 67 | Blender being under the GPL license, the blender addon (all files under `python_classes` as well as `__init__.py`) is under the [GPLv3] license.\ 68 | The Mtree library is under the [MIT] license. 69 | 70 | 71 | [latest release]: https://github.com/MaximeHerpin/modular_tree/releases 72 | [blender addon doc]: https://docs.blender.org/manual/en/latest/editors/preferences/addons.html#installing-add-ons 73 | [Cmake]: https://cmake.org/ 74 | [addon bundling script]: ./.github/scripts/setup_addon.py 75 | [GPLv3]: https://www.gnu.org/licenses/gpl-3.0.en.html 76 | [MIT]: https://choosealicense.com/licenses/mit/\ 77 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 4_0_2_1a 2 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # This program is free software; you can redistribute it and/or modify 2 | # it under the terms of the GNU General Public License as published by 3 | # the Free Software Foundation; either version 3 of the License, or 4 | # (at your option) any later version. 5 | # 6 | # This program is distributed in the hope that it will be useful, but 7 | # WITHOUT ANY WARRANTY; without even the implied warranty of 8 | # MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 9 | # General Public License for more details. 10 | # 11 | # You should have received a copy of the GNU General Public License 12 | # along with this program. If not, see . 13 | 14 | import os 15 | from pathlib import Path 16 | from . import python_classes 17 | 18 | 19 | bl_info = { 20 | "name" : "Modular Tree", 21 | "author" : "Maxime", 22 | "description" : "create trees", 23 | "blender" : (2, 83, 0), 24 | "version" : (4, 0, 2, 1), 25 | "location" : "", 26 | "warning" : "", 27 | "category" : "Generic" 28 | } 29 | 30 | 31 | # auto_load.init() 32 | 33 | def register(): 34 | python_classes.register() 35 | # auto_load.register() 36 | 37 | def unregister(): 38 | python_classes.unregister() 39 | # auto_load.unregister() 40 | -------------------------------------------------------------------------------- /build_mtree.bat: -------------------------------------------------------------------------------- 1 | py -3.10 m_tree/install.py 2 | 3 | COPY ".\m_tree\binaries\Release\m_tree.cp310-win_amd64.pyd" "./m_tree.cp310-win_amd64.pyd" 4 | 5 | PAUSE 6 | 7 | 8 | -------------------------------------------------------------------------------- /build_mtree.osx: -------------------------------------------------------------------------------- 1 | python3 m_tree/install.py 2 | cp ./m_tree/binaries/m_tree.cpython-310-darwin.so ./m_tree.cpython-310-darwin.so 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build_mtree.sh: -------------------------------------------------------------------------------- 1 | python3 m_tree/install.py 2 | cp ./m_tree/binaries/m_tree.cpython-310-x86_64-linux-gnu.so ./m_tree.cpython-310-x86_64-linux-gnu.so 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /m_tree/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | 3 | set(CMAKE_CXX_STANDARD 17) 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 5 | set(CMAKE_CXX_EXTENSIONS OFF) 6 | 7 | 8 | set(CMAKE_TOOLCHAIN_FILE ./dependencies/vcpkg/scripts/buildsystems/vcpkg.cmake) 9 | 10 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/binaries) 11 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/binaries) 12 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/binaries) 13 | 14 | project(m_tree) 15 | 16 | 17 | add_subdirectory(./source) 18 | add_subdirectory(./python_bindings) 19 | add_subdirectory(./tests) 20 | 21 | -------------------------------------------------------------------------------- /m_tree/LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 MaximeHerpin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /m_tree/install.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import sys 4 | import platform 5 | import subprocess 6 | 7 | from distutils.version import LooseVersion 8 | 9 | 10 | VCPKG_PATH = os.path.join(os.path.dirname(__file__), "dependencies", "vcpkg") 11 | 12 | PACKAGES = ["eigen3"] 13 | 14 | 15 | def install(): 16 | try: 17 | out = subprocess.check_output(['cmake', '--version']) 18 | except OSError: 19 | raise RuntimeError("CMake must be installed") 20 | 21 | if platform.system() == "Windows": 22 | cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1)) 23 | if cmake_version < '3.1.0': 24 | raise RuntimeError("CMake >= 3.1.0 is required on Windows") 25 | 26 | install_vcpkg_dependencies() 27 | build() 28 | 29 | 30 | def install_vcpkg_dependencies(): 31 | print(f"system is {platform.system()}") 32 | if platform.system() == "Windows": 33 | subprocess.run(f"bootstrap-vcpkg.bat", cwd=VCPKG_PATH, shell=True) 34 | else: 35 | subprocess.run(os.path.join(VCPKG_PATH, "bootstrap-vcpkg.sh")) 36 | for package in PACKAGES: 37 | print(f"installing {package}") 38 | if platform.system() == "Windows": 39 | triplet = ":x64-windows" 40 | elif platform.system() == "Linux": 41 | triplet = ":x64-linux" 42 | else: 43 | triplet = ":x64-osx" 44 | subprocess.check_call([os.path.join(VCPKG_PATH, "vcpkg"), "install", package+triplet]) 45 | 46 | def build(): 47 | build_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "build")) 48 | if not os.path.exists(build_dir): 49 | os.makedirs(build_dir) 50 | 51 | subprocess.check_call(['cmake', "../", "-DPYBIND11_PYTHON_VERSION=3.10", "-DCMAKE_OSX_ARCHITECTURES='x86_64;arm64'"], cwd=build_dir) 52 | subprocess.check_call(['cmake', '--build', '.', "--config", "Release"], cwd=build_dir) 53 | 54 | print([i for i in os.listdir(os.path.join(os.path.dirname(__file__), "binaries"))]) 55 | 56 | 57 | if __name__ == "__main__": 58 | install() 59 | -------------------------------------------------------------------------------- /m_tree/python_bindings/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | 3 | set(CMAKE_CXX_STANDARD 17) 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 5 | set(CMAKE_CXX_EXTENSIONS OFF) 6 | 7 | set(CMAKE_TOOLCHAIN_FILE ../dependencies/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "") 8 | 9 | add_subdirectory(../dependencies/pybind11 [EXCLUDE_FROM_ALL]) 10 | 11 | file(GLOB_RECURSE sources 12 | "./*.hpp" 13 | "./*.cpp" 14 | ) 15 | 16 | pybind11_add_module(m_tree ${sources}) 17 | target_link_libraries(m_tree PRIVATE m_tree-lib) -------------------------------------------------------------------------------- /m_tree/python_bindings/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "source/mesh/Mesh.hpp" 10 | #include "source/tree/Tree.hpp" 11 | #include "source/tree_functions/base_types/Property.hpp" 12 | #include "source/tree_functions/TrunkFunction.hpp" 13 | #include "source/tree_functions/BranchFunction.hpp" 14 | #include "source/tree_functions/GrowthFunction.hpp" 15 | #include "source/tree_functions/PipeRadiusFunction.hpp" 16 | #include "source/meshers/splines_mesher/BasicMesher.hpp" 17 | #include "source/meshers/manifold_mesher/ManifoldMesher.hpp" 18 | 19 | 20 | using namespace Mtree; 21 | namespace py = pybind11; 22 | 23 | 24 | PYBIND11_MODULE(m_tree, m) { 25 | 26 | py::class_>(m, "TreeFunction") 27 | .def_readwrite("seed", &TreeFunction::seed) 28 | .def("add_child", &TreeFunction::add_child); 29 | 30 | py::class_>(m, "ConstantProperty") 31 | .def(py::init<>()) 32 | .def(py::init()) 33 | .def_readwrite("value", &ConstantProperty::value) 34 | ; 35 | 36 | py::class_>(m, "RandomProperty") 37 | .def(py::init<>()) 38 | .def_readwrite("min", &RandomProperty::min_value) 39 | .def_readwrite("max", &RandomProperty::max_value) 40 | ; 41 | 42 | py::class_>(m, "SimpleCurveProperty") 43 | .def(py::init<>()) 44 | .def_readwrite("x_min", &SimpleCurveProperty::x_min) 45 | .def_readwrite("y_min", &SimpleCurveProperty::y_min) 46 | .def_readwrite("x_max", &SimpleCurveProperty::x_max) 47 | .def_readwrite("y_max", &SimpleCurveProperty::y_max) 48 | .def_readwrite("power", &SimpleCurveProperty::power) 49 | ; 50 | 51 | py::class_>(m, "PropertyWrapper") 52 | .def(py::init<>()) 53 | .def(py::init()) 54 | .def(py::init()) 55 | .def(py::init()) 56 | .def("set_constant_property", &PropertyWrapper::set_property) 57 | .def("set_random_property", &PropertyWrapper::set_property) 58 | .def("set_simple_curve_property", &PropertyWrapper::set_property) 59 | ; 60 | 61 | py::class_, TreeFunction>(m, "TrunkFunction") 62 | .def(py::init<>()) 63 | .def_readwrite("length", &TrunkFunction::length) 64 | .def_readwrite("resolution", &TrunkFunction::resolution) 65 | .def_readwrite("start_radius", &TrunkFunction::start_radius) 66 | .def_readwrite("end_radius", &TrunkFunction::end_radius) 67 | .def_readwrite("shape", &TrunkFunction::shape) 68 | .def_readwrite("up_attraction", &TrunkFunction::up_attraction) 69 | .def_readwrite("randomness", &TrunkFunction::randomness) 70 | ; 71 | 72 | py::class_, TreeFunction>(m, "PipeRadiusFunction") 73 | .def(py::init<>()) 74 | .def_readwrite("end_radius", &PipeRadiusFunction::end_radius) 75 | .def_readwrite("constant_growth", &PipeRadiusFunction::constant_growth) 76 | .def_readwrite("power", &PipeRadiusFunction::power) 77 | ; 78 | 79 | py::class_, TreeFunction>(m, "BranchFunction") 80 | .def(py::init<>()) 81 | .def_readwrite("length", &BranchFunction::length) 82 | .def_readwrite("start", &BranchFunction::start) 83 | .def_readwrite("end", &BranchFunction::end) 84 | .def_readwrite("branches_density", &BranchFunction::branches_density) 85 | .def_readwrite("resolution", &BranchFunction::resolution) 86 | .def_readwrite("break_chance", &BranchFunction::break_chance) 87 | .def_readwrite("start_radius", &BranchFunction::start_radius) 88 | .def_readwrite("end_radius", &BranchFunction::end_radius) 89 | .def_readwrite("randomness", &BranchFunction::randomness) 90 | .def_readwrite("flatness", &BranchFunction::flatness) 91 | .def_readwrite("gravity_strength", &BranchFunction::gravity_strength) 92 | .def_readwrite("stiffness", &BranchFunction::stiffness) 93 | .def_readwrite("up_attraction", &BranchFunction::up_attraction) 94 | .def_readwrite("phillotaxis", &BranchFunction::phillotaxis) 95 | .def_readwrite("split_radius", &BranchFunction::split_radius) 96 | .def_readwrite("start_angle", &BranchFunction::start_angle) 97 | .def_readwrite("split_angle", &BranchFunction::split_angle) 98 | .def_readwrite("split_proba", &BranchFunction::split_proba) 99 | ; 100 | 101 | py::class_, TreeFunction>(m, "GrowthFunction") 102 | .def(py::init<>()) 103 | .def_readwrite("iterations", &GrowthFunction::iterations) 104 | .def_readwrite("apical_dominance", &GrowthFunction::apical_dominance) 105 | .def_readwrite("grow_threshold", &GrowthFunction::grow_threshold) 106 | .def_readwrite("cut_threshold", &GrowthFunction::cut_threshold) 107 | .def_readwrite("split_threshold", &GrowthFunction::split_threshold) 108 | .def_readwrite("split_angle", &GrowthFunction::split_angle) 109 | .def_readwrite("branch_length", &GrowthFunction::branch_length) 110 | .def_readwrite("gravitropism", &GrowthFunction::gravitropism) 111 | .def_readwrite("randomness", &GrowthFunction::randomness) 112 | .def_readwrite("gravity_strength", &GrowthFunction::gravity_strength) 113 | ; 114 | 115 | 116 | py::class_(m, "Tree") 117 | .def(py::init<>()) 118 | .def("set_trunk_function", &Tree::set_first_function) 119 | .def("get_trunk_function", &Tree::get_first_function) 120 | .def("execute_functions", &Tree::execute_functions); 121 | 122 | py::class_(m, "Mesh") 123 | .def("get_vertices", [](const Mesh& mesh) 124 | { 125 | py::array_t result(mesh.vertices.size() * 3); 126 | py::buffer_info buff = result.request(); 127 | 128 | float* ptr = (float*)buff.ptr; 129 | for (int i = 0; i < mesh.vertices.size(); i++) 130 | { 131 | for (size_t j = 0; j < 3; j++) 132 | { 133 | ptr[i*3+j] = mesh.vertices[i][j]; 134 | } 135 | } 136 | 137 | return result; 138 | }) 139 | .def("get_float_attribute", [](const Mesh& mesh, std::string name) 140 | { 141 | if (mesh.attributes.count(name) == 0) 142 | { 143 | throw std::invalid_argument("attribute " + name + " doesn't exist"); 144 | } 145 | auto& attribute = *static_cast*>(mesh.attributes.at(name).get()); 146 | py::array_t result(mesh.vertices.size()); 147 | py::buffer_info buff = result.request(); 148 | 149 | float* ptr = (float*)buff.ptr; 150 | for (int i = 0; i < mesh.vertices.size(); i++) 151 | { 152 | ptr[i] = attribute.data[i]; 153 | } 154 | return result; 155 | }) 156 | .def("get_vector3_attribute", [](const Mesh& mesh, std::string name) 157 | { 158 | if (mesh.attributes.count(name) == 0) 159 | { 160 | throw std::invalid_argument("attribute " + name + " doesn't exist"); 161 | } 162 | auto& attribute = *static_cast*>(mesh.attributes.at(name).get()); 163 | py::array_t result(mesh.vertices.size() * 3); 164 | py::buffer_info buff = result.request(); 165 | 166 | float* ptr = (float*)buff.ptr; 167 | for (int i = 0; i < mesh.vertices.size(); i++) 168 | { 169 | ptr[i*3] = attribute.data[i][0]; 170 | ptr[i*3 + 1] = attribute.data[i][1]; 171 | ptr[i*3 + 2] = attribute.data[i][2]; 172 | } 173 | return result; 174 | }) 175 | .def("get_polygons", [](const Mesh& mesh) 176 | { 177 | py::array_t result(mesh.polygons.size() * 4); 178 | py::buffer_info buff = result.request(); 179 | 180 | int* ptr = (int*)buff.ptr; 181 | for (int i = 0; i < mesh.polygons.size(); i++) 182 | { 183 | for (int j = 0; j < 4; j++) 184 | { 185 | ptr[i * 4 + j] = mesh.polygons[i][j]; 186 | } 187 | } 188 | return result; 189 | }) 190 | .def("get_uvs", [](const Mesh& mesh) 191 | { 192 | py::array_t result(mesh.uvs.size() * 2); 193 | py::buffer_info buff = result.request(); 194 | 195 | float* ptr = (float*)buff.ptr; 196 | for (int i = 0; i < mesh.uvs.size(); i++) 197 | { 198 | for (size_t j = 0; j < 2; j++) 199 | { 200 | ptr[i * 2 + j] = mesh.uvs[i][j]; 201 | } 202 | } 203 | 204 | return result; 205 | }) 206 | .def("get_uv_loops", [](const Mesh& mesh) 207 | { 208 | py::array_t result(mesh.uv_loops.size() * 4); 209 | py::buffer_info buff = result.request(); 210 | 211 | int* ptr = (int*)buff.ptr; 212 | for (int i = 0; i < mesh.uv_loops.size(); i++) 213 | { 214 | for (int j = 0; j < 4; j++) 215 | { 216 | ptr[i * 4 + j] = mesh.uv_loops[i][j]; 217 | } 218 | } 219 | return result; 220 | }); 221 | 222 | 223 | py::class_(m, "TreeMesher"); 224 | 225 | py::class_(m, "BasicMesher") 226 | .def(py::init<>()) 227 | .def("mesh_tree", &BasicMesher::mesh_tree); 228 | 229 | py::class_(m, "ManifoldMesher") 230 | .def(py::init<>()) 231 | .def_readwrite("radial_n_points", &ManifoldMesher::radial_resolution) 232 | .def_readwrite("smooth_iterations", &ManifoldMesher::smooth_iterations) 233 | .def("mesh_tree", &ManifoldMesher::mesh_tree); 234 | 235 | 236 | #ifdef VERSION_INFO 237 | m.attr("__version__") = VERSION_INFO; 238 | #else 239 | m.attr("__version__") = "dev"; 240 | #endif 241 | } 242 | -------------------------------------------------------------------------------- /m_tree/source/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | 3 | set(CMAKE_CXX_STANDARD 17) 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 5 | set(CMAKE_CXX_EXTENSIONS OFF) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -O3 -fPIC") 7 | 8 | find_package(Eigen3 CONFIG REQUIRED) 9 | 10 | file(GLOB_RECURSE sources 11 | "./*.hpp" 12 | "./*.cpp" 13 | ) 14 | 15 | add_library(m_tree-lib STATIC ${sources}) 16 | target_include_directories(m_tree-lib PUBLIC ${PROJECT_SOURCE_DIR}) 17 | target_link_libraries(m_tree-lib PUBLIC Eigen3::Eigen) 18 | 19 | source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${sources}) -------------------------------------------------------------------------------- /m_tree/source/mesh/Attribute.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | namespace Mtree 8 | { 9 | 10 | struct AbstractAttribute 11 | { 12 | virtual void add_data() = 0; 13 | }; 14 | 15 | 16 | template 17 | struct Attribute : AbstractAttribute 18 | { 19 | std::string name; 20 | std::vector data; 21 | 22 | Attribute(std::string name) : name{ name } {}; 23 | 24 | virtual void add_data() 25 | { 26 | data.emplace_back(); 27 | }; 28 | }; 29 | } -------------------------------------------------------------------------------- /m_tree/source/mesh/Mesh.cpp: -------------------------------------------------------------------------------- 1 | #include "Mesh.hpp" 2 | 3 | namespace Mtree 4 | { 5 | std::vector> Mesh::get_vertices() 6 | { 7 | auto result = std::vector>(); 8 | for (Vector3& vert : this->vertices) 9 | { 10 | result.push_back(std::vector{vert[0], vert[1], vert[2]}); 11 | } 12 | return result; 13 | } 14 | 15 | int Mesh::add_vertex(const Vector3& position) 16 | { 17 | vertices.push_back(position); 18 | for (auto& attribute : attributes) 19 | { 20 | attribute.second->add_data(); 21 | } 22 | return (int)vertices.size() - 1; 23 | } 24 | int Mesh::add_polygon() 25 | { 26 | polygons.emplace_back(); 27 | uv_loops.emplace_back(); 28 | return (int)polygons.size() - 1; 29 | } 30 | } -------------------------------------------------------------------------------- /m_tree/source/mesh/Mesh.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "Attribute.hpp" 8 | 9 | // #include 10 | // #include 11 | 12 | namespace Mtree 13 | { 14 | using Vector3 = Eigen::Vector3f; 15 | using Vector2 = Eigen::Vector2f; 16 | 17 | // namespace py = pybind11; 18 | 19 | class Mesh 20 | { 21 | public: 22 | std::vector vertices; 23 | std::vector normals; 24 | std::vector uvs; 25 | std::vector> polygons; 26 | std::vector> uv_loops; 27 | std::map> attributes; 28 | 29 | Mesh() {}; 30 | Mesh(std::vector&& vertices) { this->vertices = std::move(vertices); } 31 | std::vector> get_vertices(); 32 | std::vector> get_polygons() { return this->polygons; }; 33 | int add_vertex(const Vector3& position); 34 | int add_polygon(); 35 | template 36 | Attribute& add_attribute(std::string name) 37 | { 38 | auto attribute = std::make_shared>(name); 39 | attributes[name] = attribute; 40 | return *attribute; 41 | }; 42 | }; 43 | } -------------------------------------------------------------------------------- /m_tree/source/mesh/VertexGroup.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/mesh/VertexGroup.cpp -------------------------------------------------------------------------------- /m_tree/source/mesh/VertexGroup.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace Mtree 4 | { 5 | 6 | } -------------------------------------------------------------------------------- /m_tree/source/meshers/base_types/TreeMesher.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/meshers/base_types/TreeMesher.cpp -------------------------------------------------------------------------------- /m_tree/source/meshers/base_types/TreeMesher.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "source/mesh/Mesh.hpp" 3 | #include "source/tree/Tree.hpp" 4 | 5 | namespace Mtree 6 | { 7 | class TreeMesher 8 | { 9 | public: 10 | virtual Mesh mesh_tree(Tree& tree) = 0; 11 | }; 12 | } 13 | -------------------------------------------------------------------------------- /m_tree/source/meshers/manifold_mesher/ManifoldMesher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "source/utilities/GeometryUtilities.hpp" 4 | #include "source/utilities/NodeUtilities.hpp" 5 | #include "ManifoldMesher.hpp" 6 | #include "smoothing.hpp" 7 | 8 | using namespace Mtree; 9 | using namespace Mtree::NodeUtilities; 10 | 11 | using AttributeNames = ManifoldMesher::AttributeNames; 12 | 13 | namespace 14 | { 15 | struct CircleDesignator 16 | { 17 | int vertex_index; 18 | int uv_index; 19 | int radial_n; 20 | }; 21 | struct IndexRange 22 | { 23 | int min_index; 24 | int max_index; 25 | }; 26 | 27 | float get_smooth_amount(const float radius, const float node_length) 28 | { 29 | return std::min(1.f, radius / node_length); 30 | } 31 | 32 | CircleDesignator add_circle(const Vector3& node_position, const Node& node, float factor, const int radial_n_points, Mesh& mesh, const float uv_y) 33 | { 34 | const Vector3& right = node.tangent; 35 | int vertex_index = mesh.vertices.size(); 36 | int uv_index = mesh.uvs.size(); 37 | Vector3 up = node.tangent.cross(node.direction); 38 | Vector3 circle_position = node_position + node.length * factor * node.direction; 39 | float radius = node.is_leaf() ? node.radius : Geometry::lerp(node.radius, node.children[0]->node.radius, factor); 40 | auto& smooth_attr = *static_cast*> (mesh.attributes[AttributeNames::smooth_amount].get()); 41 | auto& radius_attr = *static_cast*> (mesh.attributes[AttributeNames::radius].get()); 42 | float smooth_amount = get_smooth_amount(radius, node.length); 43 | auto& direction_attr = *static_cast*> (mesh.attributes[AttributeNames::direction].get()); 44 | for (size_t i = 0; i < radial_n_points; i++) 45 | { 46 | float angle = (float)i / radial_n_points * 2 * M_PI; 47 | Vector3 point = cos(angle) * right + sin(angle) * up; 48 | point = point * radius + circle_position; 49 | int index = mesh.add_vertex(point); 50 | smooth_attr.data[index] = smooth_amount; 51 | radius_attr.data[index] = radius; 52 | direction_attr.data[index] = node.direction; 53 | mesh.uvs.emplace_back((float)i / radial_n_points, uv_y); 54 | } 55 | mesh.uvs.emplace_back(1, uv_y); 56 | return CircleDesignator{vertex_index, uv_index, radial_n_points}; 57 | } 58 | 59 | bool is_index_in_branch_mask(const std::vector& mask, const int index, const int radial_n_points) 60 | { 61 | int offset = radial_n_points / 2; 62 | for (auto range : mask) 63 | { 64 | int i = index; 65 | if (range.max_index < range.min_index) 66 | { 67 | i = (i + offset) % radial_n_points; 68 | range.min_index = (range.min_index + offset) % radial_n_points; 69 | range.max_index = (range.max_index + offset) % radial_n_points; 70 | } 71 | if (i >= range.min_index && i < range.max_index) 72 | { 73 | return true; 74 | } 75 | } 76 | return false; 77 | } 78 | 79 | void bridge_circles(const CircleDesignator& first_circle, const CircleDesignator& second_circle, const int radial_n_points, Mesh& mesh, std::vector* mask = nullptr) 80 | { 81 | for (int i = 0; i < radial_n_points; i++) 82 | { 83 | if (mask != nullptr && is_index_in_branch_mask(*mask, i, radial_n_points)) 84 | { 85 | continue; 86 | } 87 | int polygon_index = mesh.add_polygon(); 88 | mesh.polygons[polygon_index] = 89 | { 90 | first_circle.vertex_index + i, 91 | first_circle.vertex_index + (i + 1) % radial_n_points, 92 | second_circle.vertex_index + (i + 1) % radial_n_points, 93 | second_circle.vertex_index + i 94 | }; 95 | mesh.uv_loops[polygon_index] = 96 | { // no need for modulo since a circle with n points has n differnt 3d coordinates but n+1 different uv coordinates 97 | first_circle.uv_index + i, 98 | first_circle.uv_index + (i + 1), 99 | second_circle.uv_index + (i + 1), 100 | second_circle.uv_index + i 101 | }; 102 | } 103 | } 104 | 105 | float get_branch_angle_around_parent(const Node& parent, const Node& branch) 106 | { 107 | Vector3 projected_branch_dir = Geometry::projected_on_plane(branch.direction, parent.direction).normalized(); 108 | auto& right = parent.tangent; 109 | Vector3 up = right.cross(parent.direction); 110 | float cos_angle = projected_branch_dir.dot(right); 111 | float sin_angle = projected_branch_dir.dot(up); 112 | return std::fmod(std::atan2(sin_angle, cos_angle) + 2*M_PI, 2*M_PI); 113 | } 114 | 115 | IndexRange get_branch_indices_on_circle(const int radial_n_points, const float circle_radius, const float branch_radius, const float branch_angle) 116 | { 117 | float angle_delta = std::asin(std::clamp(branch_radius / circle_radius, -1.f, 1.f)); 118 | float increment = 2 * M_PI / radial_n_points; 119 | int min_index = (int)(std::fmod(branch_angle - angle_delta + 2*M_PI, 2 * M_PI) / increment); 120 | int max_index = (int)(std::fmod(branch_angle + angle_delta + increment + 2*M_PI, 2 * M_PI) / increment); 121 | return IndexRange{ min_index, max_index }; 122 | } 123 | 124 | std::vector get_children_ranges(const Node& node, const int radial_n_points) 125 | { 126 | std::vector ranges; 127 | for (size_t i = 1; i < node.children.size(); i++) 128 | { 129 | auto& child = node.children[i]; 130 | float angle = get_branch_angle_around_parent(node, child->node); 131 | IndexRange range = get_branch_indices_on_circle(radial_n_points, node.radius, child->node.radius, angle); 132 | 133 | ranges.push_back(range); 134 | } 135 | return ranges; 136 | } 137 | 138 | std::vector get_child_index_order(const CircleDesignator& parent_base, const int child_radial_n, const IndexRange child_range, const NodeChild& child, const Node& parent, const Mesh& mesh) 139 | { 140 | int start = child_range.min_index + parent_base.vertex_index; 141 | std::vector child_base_indices; 142 | child_base_indices.resize((size_t)child_radial_n); 143 | 144 | for (int i = 0; i < child_radial_n / 2; i++) 145 | { 146 | int lower_index = (child_range.min_index + i) % parent_base.radial_n + parent_base.vertex_index; 147 | int upper_index = lower_index + parent_base.radial_n; 148 | int vertex_index = start + (i % (child_radial_n / 2)); 149 | 150 | child_base_indices[i] = lower_index; 151 | child_base_indices[(size_t)child_radial_n - i - 1] = upper_index; 152 | } 153 | return child_base_indices; 154 | } 155 | 156 | void add_child_base_geometry(const std::vector& child_base_indices, const CircleDesignator& child_base, const float child_radius, const Vector3& child_pos, const int offset, const float smooth_amount, Mesh& mesh) 157 | { 158 | auto& smooth_attr = *static_cast*> (mesh.attributes[ManifoldMesher::AttributeNames::smooth_amount].get()); 159 | auto& radius_attr = *static_cast*> (mesh.attributes[ManifoldMesher::AttributeNames::radius].get()); 160 | auto& direction_attr = *static_cast*> (mesh.attributes[ManifoldMesher::AttributeNames::direction].get()); 161 | 162 | Vector3 direction = (mesh.vertices[child_base_indices[2]] - mesh.vertices[child_base_indices[0]]).cross(mesh.vertices[child_base_indices[1]] - mesh.vertices[child_base_indices[0]]).normalized(); 163 | 164 | Vector3 child_base_center{ 0,0,0 }; 165 | for (auto& i : child_base_indices) 166 | child_base_center += mesh.vertices[(size_t)i]; 167 | child_base_center /= child_base_indices.size(); 168 | 169 | for (int i = 0; i < child_base.radial_n; i++) 170 | { 171 | int index = (i + offset) % child_base.radial_n; 172 | Vector3 vertex = mesh.vertices[child_base_indices[(size_t)index]]; 173 | vertex = (vertex - child_base_center).normalized() * child_radius + child_pos; 174 | int added_vertex_index = mesh.add_vertex(vertex); 175 | smooth_attr.data[added_vertex_index] = smooth_amount; 176 | radius_attr.data[added_vertex_index] = child_radius; 177 | direction_attr.data[added_vertex_index] = direction; 178 | 179 | int polygon_index = mesh.add_polygon(); 180 | mesh.polygons[polygon_index] = 181 | { 182 | child_base_indices[index], 183 | child_base_indices[(index + 1) % child_base.radial_n], 184 | child_base.vertex_index + (i + 1) % child_base.radial_n, 185 | child_base.vertex_index + i 186 | }; 187 | int uv_start = child_base.uv_index - child_base.radial_n*2; 188 | mesh.uv_loops[polygon_index] = 189 | { 190 | uv_start + index, 191 | uv_start + (index + 1)%child_base.radial_n, 192 | uv_start + child_base.radial_n + (index + 1)% child_base.radial_n, 193 | uv_start + child_base.radial_n + index 194 | }; 195 | } 196 | } 197 | 198 | float get_child_twist(const Node& child, const Node& parent) 199 | { 200 | Vector3 projected_parent_dir = Geometry::projected_on_plane(parent.direction, child.direction).normalized(); 201 | auto& right = projected_parent_dir; 202 | Vector3 up = right.cross(child.direction); 203 | float cos_angle = child.tangent.dot(right); 204 | float sin_angle = child.tangent.dot(up); 205 | return std::fmod(std::atan2(sin_angle, cos_angle) + 2 * M_PI, 2 * M_PI); 206 | } 207 | 208 | int add_child_base_uvs(float parent_uv_y, const Node& parent, const NodeChild& child, const IndexRange child_range, const int child_radial_n, const int parent_radial_n, Mesh& mesh) 209 | { 210 | float uv_growth = parent.length / (parent.radius + .001f) / (2 * M_PI); 211 | for (size_t i = 0; i < 2; i++) // recreating outer uvs (but without continuous (no looping back to x=0) 212 | { 213 | float uv_y = parent_uv_y + i * uv_growth; 214 | float x_start = child_range.min_index + i * (child_radial_n / 2.f - 1); 215 | float step = i == 0 ? 1 : -1; 216 | for (size_t j = 0; j < child_radial_n / 2; j++) 217 | { 218 | float uv_x = (x_start + j*step) / parent_radial_n; 219 | mesh.uvs.emplace_back(uv_x, uv_y); 220 | } 221 | } 222 | 223 | Vector2 uv_circle_center{ (child_range.min_index + (child_radial_n / 4.f - .5f))/parent_radial_n, parent_uv_y + uv_growth / 2 }; 224 | float uv_circle_radius = std::min((float)child_radial_n / parent_radial_n, uv_growth/2) * .6f; 225 | for (size_t i = 0; i < child_radial_n; i++) // inner uvs 226 | { 227 | float angle = (float)i / (child_radial_n -1) * 2 * M_PI + M_PI; 228 | Vector2 uv_position = Vector2{ cos(angle), sin(angle) } *uv_circle_radius + uv_circle_center; 229 | mesh.uvs.push_back(uv_position); 230 | } 231 | int circle_uv_start_index = mesh.uvs.size(); 232 | 233 | for (int i = 0; i < child_radial_n; i++) 234 | { 235 | mesh.uvs.emplace_back((float)i / child_radial_n, parent_uv_y); 236 | } 237 | mesh.uvs.emplace_back(1, parent_uv_y); 238 | 239 | return circle_uv_start_index; 240 | } 241 | 242 | CircleDesignator add_child_circle(const Node& parent, const NodeChild& child, const Vector3& child_pos, const Vector3& parent_pos, const CircleDesignator& parent_base, const IndexRange child_range, const float uv_y, Mesh& mesh) 243 | { 244 | float smooth_amount = get_smooth_amount(child.node.radius, parent.length); 245 | 246 | int child_radial_n = 2 * ((child_range.max_index - child_range.min_index + parent_base.radial_n) % parent_base.radial_n + 1); // number of vertices in child circle 247 | std::vector child_base_indices = get_child_index_order(parent_base, child_radial_n, child_range, child, parent, mesh); 248 | 249 | float child_twist = get_child_twist(child.node, parent); 250 | int offset = (int)(child_twist / (2 * M_PI) * child_radial_n - child_radial_n / 4 + child_radial_n) % child_radial_n; 251 | 252 | CircleDesignator child_base{ (int)mesh.vertices.size(), (int)mesh.uvs.size(), child_radial_n }; 253 | child_base.uv_index = add_child_base_uvs(uv_y, parent, child, child_range, child_radial_n, parent_base.radial_n, mesh); 254 | add_child_base_geometry(child_base_indices, child_base, child.node.radius, child_pos, offset, smooth_amount, mesh); 255 | return child_base; 256 | } 257 | 258 | Vector3 get_side_child_position(const Node& parent, const NodeChild& child, const Vector3& node_position) 259 | { 260 | Vector3 tangent = Geometry::projected_on_plane(child.node.direction, parent.direction).normalized(); 261 | return node_position + parent.direction * parent.length * child.position_in_parent + tangent * parent.radius; 262 | } 263 | 264 | bool has_side_branches(const Node& node) 265 | { 266 | if (node.children.size() < 2) 267 | return false; 268 | 269 | for (int i = 1; i < node.children.size(); i++) 270 | { 271 | if (node.children[i]->node.children.size() > 0) 272 | return true; 273 | } 274 | } 275 | 276 | void mesh_node_rec(const Node& node, const Vector3& node_position, const CircleDesignator& base, Mesh& mesh, const float uv_y) 277 | { 278 | if (node.children.size() < 2) 279 | { 280 | float uv_growth = node.length / (node.radius+.001f) / (2*M_PI); 281 | auto child_circle = add_circle(node_position, node, 1 , base.radial_n, mesh, uv_y + uv_growth); 282 | bridge_circles(base, child_circle, base.radial_n, mesh); 283 | Vector3 child_pos = NodeUtilities::get_position_in_node(node_position, node, 1); 284 | 285 | if (!node.is_leaf()) 286 | { 287 | mesh_node_rec(node.children[0]->node, child_pos, child_circle, mesh, uv_y + uv_growth); 288 | } 289 | } 290 | else 291 | { 292 | float uv_growth = node.length / (node.radius + .001f) / (2*M_PI); 293 | auto end_circle = add_circle(node_position, node, 1, base.radial_n, mesh, uv_y + uv_growth); 294 | std::vector children_ranges = get_children_ranges(node, base.radial_n); 295 | bridge_circles(base, end_circle, base.radial_n, mesh, &children_ranges); 296 | for (int i = 0; i < node.children.size(); i++) 297 | { 298 | if (i == 0) // first child is the continuity of the branch 299 | { 300 | Vector3 child_pos = NodeUtilities::get_position_in_node(node_position, node, 1); 301 | if (node.children.size() > 0) 302 | { 303 | mesh_node_rec(node.children[0]->node, child_pos, end_circle, mesh, uv_y + uv_growth); 304 | } 305 | } 306 | else 307 | { 308 | auto& child = *node.children[i]; 309 | Vector3 child_pos = get_side_child_position(node, child, node_position); 310 | 311 | auto child_base = add_child_circle(node, child, child_pos, node_position, base, children_ranges[i - 1], uv_y, mesh); 312 | mesh_node_rec(node.children[i]->node, child_pos, child_base, mesh, uv_y + uv_growth); 313 | } 314 | } 315 | } 316 | } 317 | } 318 | 319 | 320 | namespace Mtree 321 | { 322 | 323 | Mesh ManifoldMesher::mesh_tree(Tree& tree) 324 | { 325 | Mesh mesh; 326 | auto& smooth_attr = mesh.add_attribute(AttributeNames::smooth_amount); 327 | auto& radius_attr = mesh.add_attribute(AttributeNames::radius); 328 | auto& direction_attr = mesh.add_attribute(AttributeNames::direction); 329 | for (auto& stem : tree.get_stems()) 330 | { 331 | //__debugbreak(); 332 | 333 | if (stem.node.children.size() == 0) 334 | continue; 335 | CircleDesignator start_circle{ (int)mesh.vertices.size(), (int)mesh.uvs.size(), radial_resolution }; 336 | add_circle(stem.position, stem.node, 0, radial_resolution, mesh, 0); 337 | mesh_node_rec(stem.node, stem.position, start_circle, mesh, 0); 338 | } 339 | if (smooth_iterations > 0) 340 | MeshProcessing::Smoothing::smooth_mesh(mesh, smooth_iterations, 1, &smooth_attr.data); 341 | return mesh; 342 | } 343 | 344 | } -------------------------------------------------------------------------------- /m_tree/source/meshers/manifold_mesher/ManifoldMesher.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../base_types/TreeMesher.hpp" 4 | 5 | namespace Mtree 6 | { 7 | 8 | class ManifoldMesher : public TreeMesher 9 | { 10 | public: 11 | struct AttributeNames 12 | { 13 | inline static std::string smooth_amount = "smooth_amount"; 14 | inline static std::string radius = "radius"; 15 | inline static std::string direction = "direction"; 16 | }; 17 | 18 | int radial_resolution = 8; 19 | int smooth_iterations = 4; 20 | Mesh mesh_tree(Tree& tree) override; 21 | }; 22 | 23 | 24 | } -------------------------------------------------------------------------------- /m_tree/source/meshers/manifold_mesher/smoothing.cpp: -------------------------------------------------------------------------------- 1 | #include "smoothing.hpp" 2 | #include "source/utilities/GeometryUtilities.hpp" 3 | 4 | namespace Mtree::MeshProcessing::Smoothing 5 | { 6 | void add_index_no_duplicates(std::vector& indices, const int index) 7 | { 8 | for (auto i : indices) 9 | { 10 | if (index == i) 11 | { 12 | return; 13 | } 14 | } 15 | indices.push_back(index); 16 | } 17 | 18 | 19 | std::vector> get_neighbourhoods(Mtree::Mesh& mesh) 20 | { 21 | std::vector> vertex_neighbourhood; 22 | vertex_neighbourhood.resize(mesh.vertices.size()); 23 | for (auto& polygon : mesh.polygons) 24 | { 25 | for (int i = 1; i < polygon.size(); i+=1) 26 | { 27 | int i1 = polygon[i]; 28 | int i2 = polygon[(i + 1) % polygon.size()]; 29 | add_index_no_duplicates(vertex_neighbourhood[i1], i2); 30 | add_index_no_duplicates(vertex_neighbourhood[i2], i1); 31 | } 32 | } 33 | return vertex_neighbourhood; 34 | } 35 | 36 | void smooth_mesh_once(std::vector* result, const std::vector* previous_iteration, const std::vector>& neighbourhoods, float factor, std::vector* weights=nullptr) 37 | { 38 | for (size_t i = 0; i < result->size(); i++) 39 | { 40 | if (neighbourhoods[i].size() <= 1) 41 | { 42 | continue; 43 | } 44 | Vector3 barycenter{ 0,0,0 }; 45 | for (auto neighbour : neighbourhoods[i]) 46 | { 47 | barycenter += previous_iteration->at(neighbour); 48 | } 49 | barycenter /= neighbourhoods[i].size(); 50 | float true_factor = factor; 51 | if (weights != nullptr) 52 | { 53 | true_factor *= (*weights)[i]; 54 | } 55 | result->at(i) = Geometry::lerp(previous_iteration->at(i), barycenter, true_factor); 56 | } 57 | } 58 | 59 | void smooth_mesh(Mesh& mesh, const int iterations, const float factor, std::vector* weights) 60 | { 61 | auto neighbourhoods = get_neighbourhoods(mesh); 62 | std::vector* previous_iteration = &mesh.vertices; 63 | std::vector buffer = mesh.vertices; 64 | std::vector* result = &buffer; 65 | 66 | for (size_t i = 0; i < iterations; i++) 67 | { 68 | smooth_mesh_once(result, previous_iteration, neighbourhoods, factor, weights); 69 | auto tmp = result; 70 | result = previous_iteration; 71 | previous_iteration = tmp; 72 | } 73 | if (result != &mesh.vertices) 74 | { 75 | mesh.vertices = std::move(buffer); 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /m_tree/source/meshers/manifold_mesher/smoothing.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "source/mesh/Mesh.hpp" 3 | 4 | namespace Mtree::MeshProcessing::Smoothing 5 | { 6 | void smooth_mesh(Mesh& mesh, const int iterations, const float factor, std::vector* weights = nullptr); 7 | } -------------------------------------------------------------------------------- /m_tree/source/meshers/splines_mesher/BasicMesher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BasicMesher.hpp" 4 | #include "source/utilities/GeometryUtilities.hpp" 5 | 6 | 7 | namespace Mtree 8 | { 9 | std::vector> BasicMesher::get_splines(std::vector& stems) 10 | { 11 | std::vector> splines; 12 | 13 | for (Stem& stem : stems) 14 | { 15 | Node* stem_node = &stem.node; 16 | Vector3 stem_position = stem.position; 17 | splines.push_back(std::vector{}); 18 | get_splines_rec(splines, stem_node, stem_position); 19 | } 20 | return splines; 21 | } 22 | 23 | void BasicMesher::get_splines_rec(std::vector>& splines, Node* current_node, Vector3 current_position) 24 | { 25 | splines.back().push_back(SplinePoint{ current_position, current_node->direction, current_node->radius }); 26 | if (current_node->children.size() == 0) 27 | { 28 | splines.back().push_back(SplinePoint{ current_position + current_node->direction * current_node->length, current_node->direction, current_node->radius }); 29 | return; 30 | } 31 | for (size_t i = 0; i < current_node->children.size(); i++) 32 | { 33 | NodeChild& child = *current_node->children[i]; 34 | Vector3 child_position = current_position + current_node->direction * current_node->length * child.position_in_parent; 35 | Node* child_node = &child.node; 36 | if (i > 0) 37 | splines.push_back(std::vector{}); 38 | get_splines_rec(splines, child_node, child_position); 39 | } 40 | } 41 | 42 | void BasicMesher::mesh_spline(Mesh& mesh, std::vector& spline) 43 | { 44 | for (SplinePoint& spline_point : spline) 45 | { 46 | int n = mesh.vertices.size(); 47 | Geometry::add_circle(mesh.vertices, spline_point.position, spline_point.direction, spline_point.radius, radial_resolution); 48 | if (&spline_point == &spline.back()) 49 | continue; 50 | for (int i = 0; i < radial_resolution; i++) 51 | { 52 | int polygon_index = mesh.add_polygon(); 53 | mesh.polygons[polygon_index] = { 54 | n + i, 55 | n + radial_resolution + i, 56 | n + radial_resolution + (i + 1) % radial_resolution, 57 | n + (i + 1) % radial_resolution }; 58 | } 59 | } 60 | } 61 | 62 | Mesh BasicMesher::mesh_tree(Tree& tree) 63 | { 64 | std::vector& tree_stems = tree.get_stems(); 65 | 66 | std::vector> splines = get_splines(tree_stems); 67 | 68 | Mesh mesh; 69 | for (std::vector& spline : splines) 70 | { 71 | mesh_spline(mesh, spline); 72 | } 73 | 74 | return mesh; 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /m_tree/source/meshers/splines_mesher/BasicMesher.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../base_types/TreeMesher.hpp" 3 | 4 | namespace Mtree 5 | { 6 | 7 | 8 | class BasicMesher : public TreeMesher 9 | { 10 | private: 11 | struct SplinePoint 12 | { 13 | Vector3 position; 14 | Vector3 direction; 15 | float radius; 16 | }; 17 | std::vector> get_splines(std::vector& stems); 18 | void get_splines_rec(std::vector>& splines, Node* current_node, Vector3 current_position); 19 | void mesh_spline(Mesh& mesh, std::vector& spline); 20 | 21 | public: 22 | int radial_resolution = 8; 23 | Mesh mesh_tree(Tree& tree) override; 24 | }; 25 | 26 | 27 | } -------------------------------------------------------------------------------- /m_tree/source/tree/GrowthInfo.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/tree/GrowthInfo.cpp -------------------------------------------------------------------------------- /m_tree/source/tree/GrowthInfo.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace Mtree 4 | { 5 | class GrowthInfo 6 | { 7 | public: 8 | virtual ~GrowthInfo() {} 9 | }; 10 | } -------------------------------------------------------------------------------- /m_tree/source/tree/Node.cpp: -------------------------------------------------------------------------------- 1 | #include "Node.hpp" 2 | #include "source/utilities/GeometryUtilities.hpp" 3 | 4 | 5 | bool Mtree::Node::is_leaf() const 6 | { 7 | return children.size() == 0; 8 | } 9 | 10 | Mtree::Node::Node(Vector3 direction, Vector3 parent_tangent, float length, float radius, int creator_id) 11 | { 12 | this->direction = direction; 13 | this->tangent = Geometry::projected_on_plane(parent_tangent, direction).normalized(); 14 | this->length = length; 15 | this->radius = radius; 16 | this->creator_id = creator_id; 17 | } 18 | -------------------------------------------------------------------------------- /m_tree/source/tree/Node.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include "GrowthInfo.hpp" 6 | 7 | namespace Mtree 8 | { 9 | using Vector3 = Eigen::Vector3f; 10 | 11 | struct NodeChild; 12 | 13 | class Node 14 | { 15 | public: 16 | std::vector> children; 17 | Vector3 direction; 18 | Vector3 tangent; 19 | float length; 20 | float radius; 21 | int creator_id = 0; 22 | std::unique_ptr growthInfo = nullptr; 23 | 24 | bool is_leaf() const; 25 | 26 | Node(Vector3 direction, Vector3 parent_tangent, float length, float radius, int creator_id); 27 | }; 28 | 29 | struct NodeChild 30 | { 31 | Node node; 32 | float position_in_parent; 33 | }; 34 | 35 | struct Stem 36 | { 37 | Node node; 38 | Vector3 position; 39 | }; 40 | } -------------------------------------------------------------------------------- /m_tree/source/tree/Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Tree.hpp" 4 | #include "Node.hpp" 5 | 6 | namespace Mtree 7 | { 8 | Tree::Tree(std::shared_ptr trunkFunction) 9 | { 10 | firstFunction = trunkFunction; 11 | } 12 | void Tree::set_first_function(std::shared_ptr function) 13 | { 14 | firstFunction = function; 15 | } 16 | void Tree::execute_functions() 17 | { 18 | firstFunction->execute(stems); 19 | } 20 | 21 | void Tree::print_tree() 22 | { 23 | std::cout << "tree " << "stems:" << stems.size() << std::endl; 24 | int count = 0; 25 | Node* current_node = &stems[0].node; 26 | while (true) 27 | { 28 | count++; 29 | if (current_node->children.size() == 0) 30 | break; 31 | current_node = ¤t_node->children[0]->node; 32 | } 33 | } 34 | TreeFunction& Tree::get_first_function() 35 | { 36 | return *firstFunction; 37 | } 38 | 39 | std::vector& Tree::get_stems() 40 | { 41 | return stems; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /m_tree/source/tree/Tree.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Node.hpp" 4 | #include "source/tree_functions/base_types/TreeFunction.hpp" 5 | 6 | namespace Mtree 7 | { 8 | class Tree 9 | { 10 | private: 11 | std::vector stems; 12 | std::shared_ptr firstFunction; 13 | public: 14 | Tree(std::shared_ptr trunkFunction); 15 | Tree() { firstFunction = nullptr; }; 16 | void set_first_function(std::shared_ptr function); 17 | void execute_functions(); 18 | void print_tree(); 19 | TreeFunction& get_first_function(); 20 | std::vector& get_stems(); 21 | }; 22 | } -------------------------------------------------------------------------------- /m_tree/source/tree_functions/BranchFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "BranchFunction.hpp" 5 | #include "source/utilities/NodeUtilities.hpp" 6 | #include "source/utilities/GeometryUtilities.hpp" 7 | 8 | 9 | using namespace Mtree; 10 | 11 | namespace 12 | { 13 | void update_positions_rec(Node& node, const Vector3& position) 14 | { 15 | auto& info = static_cast(*node.growthInfo); 16 | info.position = position; 17 | 18 | for (auto& child : node.children) 19 | { 20 | Vector3 child_position = position + node.direction * node.length * child->position_in_parent; 21 | update_positions_rec(child->node, child_position); 22 | } 23 | } 24 | 25 | bool avoid_floor(const Vector3& node_position, Vector3& node_direction, float parent_length) // return true if branch should be terminated 26 | { 27 | if (node_direction.z() < 0) 28 | { 29 | node_direction[2] -= node_direction[2] * 2 / (2 + node_position.z()); 30 | } 31 | return (node_position + node_direction).z() * parent_length * 4 < 0; // is node heading to floor too fast 32 | } 33 | 34 | Vector3 get_main_child_direction(Node& parent, const Vector3& parent_position, const float up_attraction, const float flatness, const float randomness, const float resolution, bool& should_terminate) 35 | { 36 | Vector3 random_dir = Geometry::random_vec(flatness).normalized() + Vector3{ 0,0,1 } * up_attraction; 37 | Vector3 child_direction = parent.direction + random_dir * randomness / resolution; 38 | should_terminate = avoid_floor(parent_position, child_direction, parent.length); 39 | child_direction.normalize(); 40 | return child_direction; 41 | } 42 | 43 | Vector3 get_split_direction(const Node& parent, const Vector3& parent_position, const float up_attraction, const float flatness, const float resolution, const float angle) 44 | { 45 | Vector3 child_direction = Geometry::random_vec(); 46 | child_direction = child_direction.cross(parent.direction) + Vector3{ 0,0,1 } *up_attraction * flatness; 47 | Vector3 flat_normal = Vector3{ 0,0,1 }.cross(parent.direction).cross(parent.direction).normalized(); 48 | child_direction -= child_direction.dot(flat_normal) * flatness * flat_normal; 49 | avoid_floor(parent_position, child_direction, parent.length); 50 | child_direction = Geometry::lerp(parent.direction, child_direction, angle / 90); // TODO use slerp for correct angle 51 | child_direction.normalize(); 52 | return child_direction; 53 | } 54 | 55 | void mark_inactive(Node& node) 56 | { 57 | auto& info = static_cast(*node.growthInfo); 58 | info.inactive = true; 59 | } 60 | 61 | bool propagate_inactive_rec(Node& node) 62 | { 63 | auto* info = dynamic_cast(node.growthInfo.get()); 64 | 65 | if (node.children.size() == 0 || info->inactive) 66 | return info->inactive; 67 | 68 | bool inactive = false; 69 | for (size_t i = 0; i < node.children.size(); i++) 70 | { 71 | if (propagate_inactive_rec(node.children[i]->node)) 72 | inactive = true; 73 | } 74 | info->inactive = inactive; 75 | return inactive; 76 | } 77 | } 78 | 79 | namespace Mtree 80 | { 81 | void BranchFunction::apply_gravity_to_branch(Node& branch_origin) 82 | { 83 | propagate_inactive_rec(branch_origin); 84 | update_weight_rec(branch_origin); 85 | apply_gravity_rec(branch_origin, Eigen::AngleAxisf::Identity()); 86 | BranchGrowthInfo& info = static_cast(*branch_origin.growthInfo); 87 | update_positions_rec(branch_origin, info.position); 88 | } 89 | 90 | void BranchFunction::apply_gravity_rec(Node& node, Eigen::AngleAxisf curent_rotation) 91 | { 92 | BranchGrowthInfo& info = static_cast(*node.growthInfo); 93 | if (!info.inactive || true) 94 | { 95 | float horizontality = 1 - abs(node.direction.z()); 96 | info.age += 1 / resolution; 97 | float displacement = horizontality * std::pow(info.cumulated_weight, .5f) * gravity_strength / resolution / resolution / 1000 / (1 + info.age); 98 | displacement *= std::exp(-std::abs(info.deviation_from_rest_pose / resolution * stiffness)); 99 | info.deviation_from_rest_pose += displacement; 100 | 101 | Vector3 tangent = node.direction.cross(Vector3{ 0,0,-1 }).normalized(); 102 | Eigen::AngleAxisf rot{ displacement, tangent }; 103 | curent_rotation = rot * curent_rotation; 104 | 105 | node.direction = curent_rotation * node.direction; 106 | } 107 | 108 | for (auto& child : node.children) 109 | { 110 | apply_gravity_rec(child->node, curent_rotation); 111 | } 112 | } 113 | 114 | void BranchFunction::update_weight_rec(Node& node) 115 | { 116 | float node_weight = node.length; 117 | for (auto& child : node.children) 118 | { 119 | update_weight_rec(child->node); 120 | BranchGrowthInfo& child_info = dynamic_cast(*child->node.growthInfo); 121 | node_weight += child_info.cumulated_weight; 122 | } 123 | 124 | BranchGrowthInfo* info = dynamic_cast(node.growthInfo.get()); 125 | info->cumulated_weight = node_weight; 126 | } 127 | 128 | // grow extremity by one level (add one or more children) 129 | void BranchFunction::grow_node_once(Node& node, const int id, std::queue>& results) 130 | { 131 | bool break_branch = rand_gen.get_0_1() * resolution < break_chance; 132 | if (break_branch) 133 | { 134 | mark_inactive(node); 135 | return; 136 | } 137 | 138 | BranchGrowthInfo& info = static_cast(*node.growthInfo); 139 | float factor_in_branch = info.current_length / info.desired_length; 140 | 141 | float child_radius = Geometry::lerp(info.origin_radius, info.origin_radius * end_radius, factor_in_branch); 142 | float child_length = std::min(1/resolution, info.desired_length - info.current_length); 143 | bool should_terminate; 144 | Vector3 child_direction = get_main_child_direction(node, info.position, up_attraction, flatness, randomness.execute(factor_in_branch), resolution, should_terminate); 145 | 146 | if (should_terminate) 147 | { 148 | mark_inactive(node); 149 | return; 150 | } 151 | 152 | NodeChild child{ Node{child_direction, node.tangent, child_length, child_radius, id}, 1 }; 153 | node.children.push_back(std::make_shared(std::move(child))); 154 | auto& child_node = node.children.back()->node; 155 | 156 | float current_length = info.current_length + child_length; 157 | Vector3 child_position = info.position + child_direction * child_length; 158 | BranchGrowthInfo child_info{info.desired_length, info.origin_radius, child_position, current_length}; 159 | child_node.growthInfo = std::make_unique(child_info); 160 | if (current_length < info.desired_length) 161 | { 162 | results.push(std::ref(child_node)); 163 | } 164 | 165 | bool split = rand_gen.get_0_1() * resolution < split_proba; // should the node split into two children 166 | if (split) 167 | { 168 | Vector3 split_child_direction = get_split_direction(node, info.position, up_attraction, flatness, resolution, split_angle); 169 | float split_child_radius = node.radius * split_radius; 170 | 171 | NodeChild child{ Node{split_child_direction, node.tangent, child_length, split_child_radius, id}, rand_gen.get_0_1() }; 172 | node.children.push_back(std::make_shared(std::move(child))); 173 | auto& child_node = node.children.back()->node; 174 | 175 | Vector3 split_child_position = info.position + split_child_direction * child_length; 176 | BranchGrowthInfo child_info{ info.desired_length, info.origin_radius * split_radius, split_child_position, current_length}; 177 | child_node.growthInfo = std::make_unique(child_info); 178 | if (current_length < info.desired_length) 179 | { 180 | results.push(std::ref(child_node)); 181 | } 182 | } 183 | } 184 | 185 | void BranchFunction::grow_origins(std::vector>& origins, const int id) 186 | { 187 | std::queue> extremities; 188 | for (auto& node_ref : origins) 189 | { 190 | extremities.push(node_ref); 191 | } 192 | int batch_size = extremities.size(); 193 | while (!extremities.empty()) 194 | { 195 | if (batch_size == 0) 196 | { 197 | batch_size = extremities.size(); 198 | for (auto& node_ref : origins) 199 | { 200 | apply_gravity_to_branch(node_ref.get()); 201 | } 202 | } 203 | auto& node = extremities.front().get(); 204 | extremities.pop(); 205 | grow_node_once(node, id, extremities); 206 | batch_size --; 207 | } 208 | } 209 | 210 | 211 | // get the origins of the branches that will be created. 212 | // origins are created from the nodes made by the parent TreeFunction 213 | std::vector> BranchFunction::get_origins(std::vector& stems, const int id, const int parent_id) 214 | { 215 | // get all nodes created by the parent TreeFunction, organised by branch 216 | NodeUtilities::BranchSelection selection = NodeUtilities::select_from_tree(stems, parent_id); 217 | std::vector> origins; 218 | 219 | float origins_dist = 1 / (branches_density + .001); // distance between two consecutive origins 220 | 221 | for (auto& branch : selection) // parent branches 222 | { 223 | if (branch.size() == 0) 224 | { 225 | continue; 226 | } 227 | 228 | float branch_length = NodeUtilities::get_branch_length(*branch[0].node); 229 | float absolute_start = start * branch_length; // the length at which we can start adding new branch origins 230 | float absolute_end = end * branch_length; // the length at which we stop adding new branch origins 231 | float current_length = 0; 232 | float dist_to_next_origin = absolute_start; 233 | Vector3 tangent = Geometry::get_orthogonal_vector(branch[0].node->direction); 234 | 235 | for (size_t node_index = 0; node_index node.length) 245 | { 246 | dist_to_next_origin -= node.length; 247 | current_length += node.length; 248 | } 249 | else 250 | { 251 | float remaining_node_length = node.length - dist_to_next_origin; 252 | current_length += dist_to_next_origin; 253 | int origins_to_create = remaining_node_length / origins_dist + 1; // number of origins to create on the node 254 | float position_in_parent = dist_to_next_origin / node.length; // position of the first origin within the node 255 | float position_in_parent_step = origins_dist / node.length; // relative distance between origins within the node 256 | 257 | for (int i = 0; i < origins_to_create; i++) 258 | { 259 | if (current_length > absolute_end) 260 | { 261 | break; 262 | } 263 | float factor = (current_length - absolute_start) / std::max(0.001f, absolute_end - absolute_start); 264 | tangent = rot * tangent; 265 | Geometry::project_on_plane(tangent, node.direction); 266 | tangent.normalize(); 267 | Vector3 child_direction = Geometry::lerp(node.direction, tangent, start_angle.execute(factor) / 90); 268 | child_direction.normalize(); 269 | float child_radius = node.radius * start_radius.execute(factor); 270 | float branch_length = length.execute(factor); 271 | float node_length = std::min(branch_length, 1 / (resolution + 0.001f)); 272 | NodeChild child{Node{child_direction, node.tangent, node_length, child_radius, id}, position_in_parent}; 273 | node.children.push_back(std::make_shared(std::move(child))); 274 | auto& child_node = node.children.back()->node; 275 | Vector3 child_position = node_position + node.direction * node.length * position_in_parent; 276 | child_node.growthInfo = std::make_unique(branch_length - node_length, child_radius, child_position, child_node.length, 0); 277 | 278 | if (branch_length - node_length > 1e-3) 279 | origins.push_back(std::ref(child_node)); 280 | position_in_parent += position_in_parent_step; 281 | if (i > 0) 282 | { 283 | current_length += origins_dist; 284 | } 285 | } 286 | remaining_node_length = (remaining_node_length - (origins_to_create - 1) * origins_dist); 287 | dist_to_next_origin = origins_dist - remaining_node_length; 288 | } 289 | } 290 | } 291 | return origins; 292 | } 293 | 294 | void BranchFunction::execute(std::vector& stems, int id, int parent_id) 295 | { 296 | rand_gen.set_seed(seed); 297 | auto origins = get_origins(stems, id, parent_id); 298 | grow_origins(origins, id); 299 | execute_children(stems, id); 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/BranchFunction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "./base_types/TreeFunction.hpp" 5 | #include "source/utilities/NodeUtilities.hpp" 6 | #include "source/utilities/GeometryUtilities.hpp" 7 | #include "source/tree_functions/base_types/Property.hpp" 8 | 9 | 10 | namespace Mtree 11 | { 12 | class BranchFunction : public TreeFunction 13 | { 14 | public: 15 | float start = .1; 16 | float end = 1; 17 | float branches_density = 2; // 0 < x 18 | PropertyWrapper length{ConstantProperty(9)}; // x > 0 19 | PropertyWrapper start_radius { ConstantProperty(.4) }; // 0 > x > 1 20 | float end_radius = .05; 21 | float break_chance = .01; // 0 < x 22 | float resolution = 3; // 0 < x 23 | PropertyWrapper randomness { ConstantProperty(.4) }; 24 | float phillotaxis = 137.5f; 25 | float gravity_strength = 10; 26 | float stiffness = .1; 27 | float up_attraction = .25; 28 | float flatness = .5; // 0 < x < 1 29 | float split_radius = .9f; // 0 < x < 1 30 | PropertyWrapper start_angle{ ConstantProperty(45) }; // -180 < x < 180 31 | float split_angle = 45.0f; 32 | float split_proba = .5f; // 0 < x 33 | 34 | void execute(std::vector& stems, int id, int parent_id) override; 35 | 36 | class BranchGrowthInfo :public GrowthInfo 37 | { 38 | public: 39 | float desired_length; 40 | float current_length; 41 | float origin_radius; 42 | float cumulated_weight = 0; 43 | float deviation_from_rest_pose; 44 | float age = 0; 45 | bool inactive = false; 46 | Vector3 position; 47 | BranchGrowthInfo(float desired_length, float origin_radius, Vector3 position, float current_length = 0, float deviation = 0) : 48 | desired_length(desired_length), origin_radius(origin_radius), 49 | current_length(current_length), deviation_from_rest_pose(deviation), 50 | position(position) {}; 51 | }; 52 | 53 | private: 54 | 55 | std::vector> get_origins(std::vector& stems, const int id, const int parent_id); 56 | 57 | void grow_origins(std::vector>&, const int id); 58 | 59 | void grow_node_once(Node& node, const int id, std::queue>& results); 60 | 61 | void apply_gravity_to_branch(Node& node); 62 | 63 | void apply_gravity_rec(Node& node, Eigen::AngleAxisf previous_rotations); 64 | 65 | void update_weight_rec(Node& node); 66 | 67 | }; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/GrowthFunction.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "source/utilities/GeometryUtilities.hpp" 7 | #include "./base_types/TreeFunction.hpp" 8 | #include "GrowthFunction.hpp" 9 | 10 | namespace Mtree 11 | { 12 | void setup_growth_information_rec(Node& node) 13 | { 14 | node.growthInfo = std::make_unique(node.children.size() == 0 ? BioNodeInfo::NodeType::Meristem : BioNodeInfo::NodeType::Ignored); 15 | for (auto& child : node.children) 16 | setup_growth_information_rec(child->node); 17 | } 18 | 19 | // get total amount of energy from the node and its descendance, and assign for each node the realtive amount of energy it receive 20 | float GrowthFunction::update_vigor_ratio_rec(Node& node) 21 | { 22 | BioNodeInfo& info = static_cast(*node.growthInfo); 23 | if (info.type == BioNodeInfo::NodeType::Meristem) 24 | { 25 | return 1; 26 | } 27 | else if (info.type == BioNodeInfo::NodeType::Branch || info.type == BioNodeInfo::NodeType::Ignored) 28 | { 29 | float light_flux = update_vigor_ratio_rec(node.children[0]->node); 30 | float vigor_ratio = 1; 31 | for (size_t i = 1; i < node.children.size(); i++) 32 | { 33 | float child_flux = update_vigor_ratio_rec(node.children[i]->node); 34 | float t = apical_dominance; 35 | vigor_ratio = (t * light_flux) / (t * light_flux + (1 - t) * child_flux + .001f); 36 | static_cast(node.children[i]->node.growthInfo.get())->vigor_ratio = 1 - vigor_ratio; 37 | light_flux += child_flux; 38 | } 39 | static_cast(node.children[0]->node.growthInfo.get())->vigor_ratio = vigor_ratio; 40 | return light_flux; 41 | } 42 | else 43 | { 44 | info.vigor_ratio = 0; 45 | return 0; 46 | } 47 | } 48 | 49 | // update the amount of energy available to a node 50 | void GrowthFunction::update_vigor_rec(Node& node, float vigor) 51 | { 52 | BioNodeInfo& info = static_cast(*node.growthInfo); 53 | info.vigor = vigor; 54 | for (auto& child : node.children) 55 | { 56 | float child_vigor = static_cast(child->node.growthInfo.get())->vigor_ratio* vigor; 57 | update_vigor_rec(child->node, child_vigor); 58 | } 59 | } 60 | 61 | // apply rules on the node based on the energy available to it 62 | void GrowthFunction::simulate_growth_rec(Node& node, int id) 63 | { 64 | BioNodeInfo& info = static_cast(*node.growthInfo); 65 | bool primary_growth = info.type == BioNodeInfo::NodeType::Meristem && info.vigor > grow_threshold; 66 | bool secondary_growth = info.vigor > grow_threshold && info.type != BioNodeInfo::NodeType::Ignored; // Todo : should be another parameter 67 | bool split = info.type == BioNodeInfo::NodeType::Meristem && info.vigor > split_threshold; 68 | bool cut = info.type == BioNodeInfo::NodeType::Meristem && info.vigor < cut_threshold; 69 | int child_count = node.children.size(); 70 | if (cut && false) 71 | { 72 | info.type = BioNodeInfo::NodeType::Cut; 73 | return; 74 | } 75 | info.age++; 76 | if (secondary_growth) 77 | { 78 | node.radius = (1 - std::exp(-info.age * .01f) + .01f) * .5; 79 | } 80 | if (primary_growth) 81 | { 82 | Vector3 child_direction = node.direction + Vector3{0,0,1} * gravitropism + Geometry::random_vec() * randomness; 83 | child_direction.normalize(); 84 | float child_radius = node.radius; 85 | float child_length = branch_length * (info.vigor + .1f); 86 | NodeChild child = NodeChild{ Node{child_direction, node.tangent, branch_length, child_radius, id}, 1 }; 87 | float child_angle = split ? info.philotaxis_angle + philotaxis_angle : info.philotaxis_angle; 88 | child.node.growthInfo = std::make_unique(BioNodeInfo::NodeType::Meristem, 0, child_angle); 89 | node.children.push_back(std::make_shared(std::move(child))); 90 | info.type = BioNodeInfo::NodeType::Branch; 91 | } 92 | if (split) 93 | { 94 | info.philotaxis_angle += philotaxis_angle; 95 | Vector3 tangent{ std::cos(info.philotaxis_angle), std::sin(info.philotaxis_angle), 0 }; 96 | tangent = Geometry::get_look_at_rot(node.direction) * tangent; 97 | Vector3 child_direction = Geometry::lerp(node.direction, tangent, split_angle / 90); 98 | child_direction.normalize(); 99 | float child_radius = node.radius; 100 | float child_length = branch_length * (info.vigor + .1f); 101 | NodeChild child = NodeChild{ Node{child_direction, node.tangent, branch_length, child_radius, id}, 1 }; 102 | child.node.growthInfo = std::make_unique(BioNodeInfo::NodeType::Meristem); 103 | node.children.push_back(std::make_shared(std::move(child))); 104 | info.type = BioNodeInfo::NodeType::Branch; 105 | } 106 | for (size_t i = 0; i < child_count; i++) 107 | { 108 | simulate_growth_rec(node.children[i]->node, id); 109 | } 110 | } 111 | 112 | void GrowthFunction::get_weight_rec(Node& node) 113 | { 114 | BioNodeInfo& info = static_cast(*node.growthInfo); 115 | for (auto& child : node.children) 116 | { 117 | get_weight_rec(child->node); 118 | } 119 | float segment_weight = node.length * node.radius * node.radius; 120 | Vector3 center_of_mass = (info.absolute_position + node.direction * node.length / 2) * segment_weight; 121 | float total_weight = segment_weight; 122 | for (auto& child : node.children) 123 | { 124 | BioNodeInfo& child_info = static_cast(*child->node.growthInfo); 125 | center_of_mass += child_info.center_of_mass * child_info.branch_weight; 126 | total_weight += child_info.branch_weight; 127 | } 128 | center_of_mass /= total_weight; 129 | info.center_of_mass = center_of_mass; 130 | info.branch_weight = total_weight; 131 | } 132 | 133 | void GrowthFunction::apply_gravity_rec(Node& node, Eigen::Matrix3f curent_rotation) 134 | { 135 | BioNodeInfo& info = static_cast(*node.growthInfo); 136 | Vector3 offset = (info.center_of_mass - info.absolute_position); 137 | offset[2] = 0; 138 | float lever_arm = offset.norm(); 139 | float torque = info.branch_weight * lever_arm; 140 | float bendiness = std::exp(-(info.age/2 + info.vigor)); 141 | float angle = torque * bendiness * gravity_strength * 50; 142 | Vector3 tangent = node.direction.cross(Vector3{ 0,0,-1 }); 143 | Eigen::Matrix3f rot; 144 | rot = Eigen::AngleAxis(angle, tangent); 145 | curent_rotation = curent_rotation * rot; 146 | node.direction = curent_rotation * node.direction; 147 | 148 | for (auto& child : node.children) 149 | { 150 | apply_gravity_rec(child->node, curent_rotation); 151 | } 152 | } 153 | 154 | void GrowthFunction::update_absolute_position_rec(Node& node, const Vector3& node_position) 155 | { 156 | static_cast(node.growthInfo.get())->absolute_position = node_position; 157 | for (auto& child : node.children) 158 | { 159 | Vector3 child_position = node_position + node.direction * child->position_in_parent * node.length; 160 | update_absolute_position_rec(child->node, child_position); 161 | } 162 | } 163 | 164 | void GrowthFunction::execute(std::vector& stems, int id, int parent_id) 165 | { 166 | rand_gen.set_seed(seed); 167 | 168 | for (Stem& stem : stems) 169 | { 170 | setup_growth_information_rec(stem.node); 171 | } 172 | 173 | for (size_t i = 0; i < iterations; i++) // an iteration can be seen as a year of growth 174 | { 175 | for (Stem& stem : stems) // the energy is not shared between stems 176 | { 177 | float target_light_flux = 1 + std::pow((float)i, 1.5); 178 | float light_flux = update_vigor_ratio_rec(stem.node); // get total available energy 179 | 180 | if (target_light_flux > light_flux) 181 | { 182 | cut_threshold -= .1f; 183 | //grow_threshold -= .1f 184 | } 185 | else if (target_light_flux < light_flux) 186 | { 187 | cut_threshold += .1f; 188 | } 189 | //cut_threshold = (light_flux / target_light_flux) / 2; 190 | 191 | update_vigor_rec(stem.node, target_light_flux); // distribute the energy in each node 192 | simulate_growth_rec(stem.node, id); // apply rules to the tree 193 | update_absolute_position_rec(stem.node, stem.position); 194 | get_weight_rec(stem.node); 195 | Eigen::Matrix3f rot; 196 | rot = rot.Identity(); 197 | apply_gravity_rec(stem.node, rot); 198 | } 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/GrowthFunction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "source/tree_functions/base_types/TreeFunction.hpp" 4 | 5 | namespace Mtree 6 | { 7 | class GrowthFunction : public TreeFunction 8 | { 9 | private: 10 | float update_vigor_ratio_rec(Node& node); 11 | void update_vigor_rec(Node& node, float vigor); 12 | void simulate_growth_rec(Node& node, int id); 13 | void get_weight_rec(Node& node); 14 | void apply_gravity_rec(Node& node, Eigen::Matrix3f curent_rotation); 15 | void update_absolute_position_rec(Node& node, const Vector3& node_position); 16 | 17 | public: 18 | int iterations = 5; 19 | float apical_dominance = .7f; 20 | float grow_threshold = .5f; 21 | float split_angle = 60; 22 | float branch_length = 1; 23 | float gravitropism = .1f; 24 | float randomness = .1f; 25 | float cut_threshold = .2f; 26 | float split_threshold = .7f; 27 | float gravity_strength = 1; 28 | 29 | float apical_control = .7f; 30 | float codominant_proba = .1f; 31 | int codominant_count = 2; 32 | float branch_angle = 60; 33 | float philotaxis_angle = 2.399f; 34 | float flower_threshold = .5f; 35 | 36 | float growth_delta = .1f; 37 | float flowering_delta = .1f; 38 | 39 | float root_flux = 5; 40 | 41 | 42 | void execute(std::vector& stems, int id, int parent_id) override; 43 | }; 44 | 45 | class BioNodeInfo : public GrowthInfo 46 | { 47 | public: 48 | enum class NodeType {Meristem, Branch, Cut, Ignored} type; 49 | float branch_weight = 0; 50 | Vector3 center_of_mass; 51 | Vector3 absolute_position; 52 | float vigor_ratio = 1; 53 | float vigor = 0; 54 | int age = 0; 55 | float philotaxis_angle = 0; 56 | 57 | BioNodeInfo(NodeType type, int age = 0, float philotaxis_angle = 0) { this->type = type; this->age = age; this->philotaxis_angle = philotaxis_angle; }; 58 | }; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/LeavesFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/tree_functions/LeavesFunction.cpp -------------------------------------------------------------------------------- /m_tree/source/tree_functions/LeavesFunction.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/tree_functions/LeavesFunction.hpp -------------------------------------------------------------------------------- /m_tree/source/tree_functions/PipeRadiusFunction.cpp: -------------------------------------------------------------------------------- 1 | #include "PipeRadiusFunction.hpp" 2 | #include "source/utilities/GeometryUtilities.hpp" 3 | namespace Mtree 4 | { 5 | void PipeRadiusFunction::update_radius_rec(Node& node) 6 | { 7 | if (node.children.size() == 0) 8 | { 9 | node.radius = end_radius; 10 | return; 11 | } 12 | 13 | float total_children_radius = 0; 14 | for (auto& child : node.children) 15 | { 16 | update_radius_rec(child->node); 17 | total_children_radius += pow(child->node.radius, power); 18 | } 19 | node.radius = pow(total_children_radius, 1 / power) + constant_growth * node.length / 100; 20 | } 21 | void PipeRadiusFunction::execute(std::vector& stems, int id, int parent_id) 22 | { 23 | rand_gen.set_seed(seed); 24 | 25 | for (auto& stem : stems) 26 | { 27 | update_radius_rec(stem.node); 28 | } 29 | execute_children(stems, id); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/PipeRadiusFunction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | // #include 4 | #include "./base_types/TreeFunction.hpp" 5 | // #include "source/utilities/NodeUtilities.hpp" 6 | // #include "source/utilities/GeometryUtilities.hpp" 7 | #include "source/tree_functions/base_types/Property.hpp" 8 | 9 | 10 | namespace Mtree 11 | { 12 | class PipeRadiusFunction : public TreeFunction 13 | { 14 | private: 15 | 16 | void update_radius_rec(Node& node); 17 | 18 | public: 19 | float power = 2.f; 20 | float end_radius = .01f; 21 | float constant_growth = .01f; 22 | void execute(std::vector& stems, int id, int parent_id) override; 23 | }; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/TrunkFunction.cpp: -------------------------------------------------------------------------------- 1 | #include "TrunkFunction.hpp" 2 | #include "source/utilities/GeometryUtilities.hpp" 3 | namespace Mtree 4 | { 5 | void TrunkFunction::execute(std::vector& stems, int id, int parent_id) 6 | { 7 | rand_gen.set_seed(seed); 8 | 9 | float segment_length = 1 / (resolution + .001); 10 | Vector3 stem_direction = Vector3{ 0,0,1 }; 11 | Node firstNode{ stem_direction, Geometry::get_orthogonal_vector(stem_direction), segment_length, start_radius, id }; 12 | Node* current_node = &firstNode; 13 | int node_count = resolution * length; 14 | for (size_t i = 0; i < node_count; i++) 15 | { 16 | if (i == node_count - 1) 17 | segment_length = length - i*segment_length; 18 | 19 | float factor = std::pow((float)i / (node_count), shape); 20 | float radius = Geometry::lerp(start_radius, end_radius, factor); 21 | Vector3 direction = current_node->direction + Geometry::random_vec() * (randomness / (resolution+.001f)); 22 | direction += Vector3(0,0,up_attraction / (resolution + .001f)); 23 | direction.normalize(); 24 | float position_in_parent = 1; 25 | NodeChild child{ Node{direction, firstNode.tangent, segment_length, radius, id}, position_in_parent }; 26 | current_node->children.push_back(std::make_shared(std::move(child))); 27 | current_node = ¤t_node->children.back()->node; 28 | } 29 | 30 | Vector3 position{ 0,0,0 }; 31 | Stem stem{ std::move(firstNode), position }; 32 | stems.push_back(std::move(stem)); 33 | execute_children(stems, id); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/TrunkFunction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "./base_types/TreeFunction.hpp" 4 | 5 | namespace Mtree 6 | { 7 | class TrunkFunction : public TreeFunction 8 | { 9 | public: 10 | float length = 10; 11 | float start_radius = .3f; 12 | float end_radius = .05; 13 | float shape = .5; 14 | float resolution = 3.f; 15 | float randomness = .1f; 16 | float up_attraction = .6f; 17 | 18 | void execute(std::vector& stems, int id, int parent_id) override; 19 | }; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /m_tree/source/tree_functions/base_types/Property.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "source/utilities/RandomGenerator.hpp" 4 | #include "source/utilities/GeometryUtilities.hpp" 5 | 6 | 7 | namespace Mtree 8 | { 9 | 10 | 11 | struct Property 12 | { 13 | virtual float execute(float x) = 0; 14 | }; 15 | 16 | 17 | struct ConstantProperty : Property 18 | { 19 | float value; 20 | 21 | ConstantProperty(float value=1) : value(value) {}; 22 | 23 | float execute(float x) override 24 | { 25 | return value; 26 | } 27 | }; 28 | 29 | struct RandomProperty : Property 30 | { 31 | RandomGenerator rand_gen; 32 | float min_value; 33 | float max_value; 34 | 35 | RandomProperty(float min=0, float max=1) : min_value(min), max_value(max) {}; 36 | 37 | float execute(float x) override 38 | { 39 | return Geometry::lerp(min_value, max_value, rand_gen.get_0_1()); 40 | } 41 | }; 42 | 43 | struct SimpleCurveProperty : Property 44 | { 45 | float x_min; 46 | float x_max; 47 | float y_min; 48 | float y_max; 49 | float power; 50 | 51 | SimpleCurveProperty(float x_min=0, float x_max=1, float y_min=0, float y_max=1, float power = 1) : 52 | x_min(x_min), x_max(x_max), y_min(y_min), y_max(y_max), power(power) {}; 53 | 54 | float execute(float x) override\ 55 | { 56 | float factor = std::clamp((x - x_min) / std::max(0.001f, (x_max - x_min)), 0.f, 1.f); 57 | if (power > 0 && power != 1) 58 | { 59 | factor = std::pow(factor, power); 60 | } 61 | return Geometry::lerp(y_min, y_max, factor); 62 | } 63 | }; 64 | 65 | struct PropertyWrapper 66 | { 67 | std::shared_ptr property; 68 | 69 | PropertyWrapper() { property = std::make_shared(1); }; 70 | 71 | template 72 | PropertyWrapper(T& property) 73 | { 74 | this->property = std::make_shared(property); 75 | }; 76 | 77 | template 78 | PropertyWrapper(T&& property) 79 | { 80 | this->property = std::make_shared(property); 81 | }; 82 | 83 | template 84 | void set_property(T& property) 85 | { 86 | this->property = std::make_shared(property); 87 | } 88 | 89 | float execute(float x) 90 | { 91 | return property->execute(x); 92 | }; 93 | }; 94 | } -------------------------------------------------------------------------------- /m_tree/source/tree_functions/base_types/TreeFunction.cpp: -------------------------------------------------------------------------------- 1 | #include "TreeFunction.hpp" 2 | 3 | namespace Mtree 4 | { 5 | void TreeFunction::execute_children(std::vector& stems, int id) 6 | { 7 | int child_id = id; 8 | for (std::shared_ptr& child : children) 9 | { 10 | child_id++; 11 | child->execute(stems, child_id, id); 12 | } 13 | } 14 | void TreeFunction::add_child(std::shared_ptr child) 15 | { 16 | children.push_back(child); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /m_tree/source/tree_functions/base_types/TreeFunction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "source/tree/Node.hpp" 4 | #include "source/utilities/RandomGenerator.hpp" 5 | 6 | namespace Mtree 7 | { 8 | class TreeFunction 9 | { 10 | protected: 11 | RandomGenerator rand_gen; 12 | std::vector> children; 13 | void execute_children(std::vector& stems, int id); 14 | public: 15 | int seed = 42; 16 | 17 | virtual void execute(std::vector& stems, int id=0, int parent_id = 0) = 0; 18 | void add_child(std::shared_ptr child); 19 | }; 20 | } -------------------------------------------------------------------------------- /m_tree/source/utilities/GeometryUtilities.cpp: -------------------------------------------------------------------------------- 1 | #define _USE_MATH_DEFINES 2 | 3 | #include 4 | #include 5 | #include 6 | #include "GeometryUtilities.hpp" 7 | 8 | namespace Mtree { 9 | namespace Geometry { 10 | 11 | void add_circle(std::vector& points, Vector3 position, Vector3 direction, float radius, int n_points) 12 | { 13 | Eigen::Matrix3f rot; 14 | // rot = Eigen::AngleAxis( angle, axis ); 15 | rot = get_look_at_rot(direction); 16 | 17 | for (size_t i = 0; i < n_points; i++) 18 | { 19 | float circle_angle = M_PI * (float)i / n_points * 2; 20 | Vector3 position_in_circle = Vector3{ std::cos(circle_angle), std::sin(circle_angle),0 } *radius; 21 | position_in_circle = position + rot * position_in_circle; 22 | points.push_back(position_in_circle); 23 | } 24 | } 25 | 26 | Eigen::Matrix3f get_look_at_rot(Vector3 direction) 27 | { 28 | Vector3 up{ 0,0,1 }; 29 | Vector3 axis = up.cross(direction); 30 | float sin = axis.norm(); 31 | float cos = up.dot(direction); 32 | float angle = std::atan2(sin, cos); 33 | if (angle < .01) 34 | axis = up; 35 | else 36 | axis /= sin; 37 | Eigen::Matrix3f rot; 38 | rot = Eigen::AngleAxis(angle, axis); 39 | return rot; 40 | } 41 | 42 | Vector3 random_vec_on_unit_sphere() 43 | { 44 | auto vec = Vector3{}; 45 | vec.setRandom(); 46 | vec.normalize(); 47 | return vec; 48 | } 49 | 50 | Vector3 random_vec(float flatness) 51 | { 52 | auto vec = Vector3{}; 53 | vec.setRandom(); 54 | vec.z() *= (1 - flatness); 55 | return vec; 56 | } 57 | 58 | Vector3 lerp(Vector3 a, Vector3 b, float t) 59 | { 60 | return t * b + (1 - t) * a; 61 | } 62 | 63 | float lerp(float a, float b, float t) 64 | { 65 | t = std::clamp(t, 0.f, 1.f); 66 | return t * b + (1 - t) * a; 67 | } 68 | 69 | 70 | Vector3 get_orthogonal_vector(const Vector3& v) 71 | { 72 | Vector3 tmp; 73 | if (abs(v.z()) < .95) 74 | { 75 | tmp = Vector3{1,0,0}; 76 | } 77 | else 78 | { 79 | tmp = Vector3{0,1,0}; 80 | } 81 | return tmp.cross(v).normalized(); 82 | } 83 | 84 | void project_on_plane(Vector3& v, const Vector3& plane_normal) 85 | { 86 | Vector3 offset = v.dot(plane_normal) * plane_normal; 87 | v -= offset; 88 | } 89 | 90 | Vector3 projected_on_plane(const Vector3& v, const Vector3& plane_normal) 91 | { 92 | auto result = v; 93 | project_on_plane(result, plane_normal); 94 | return result; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /m_tree/source/utilities/GeometryUtilities.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #ifndef M_PI 8 | #define M_PI 3.14159265358979323846 9 | #endif 10 | 11 | 12 | namespace Mtree { namespace Geometry 13 | { 14 | using Vector3 = Eigen::Vector3f; 15 | 16 | void add_circle(std::vector& points, Vector3 position, Vector3 direction, float radius, int n_points); 17 | 18 | Eigen::Matrix3f get_look_at_rot(Vector3 direction); 19 | 20 | Vector3 random_vec_on_unit_sphere(); 21 | 22 | Vector3 random_vec(float flatness=0); 23 | 24 | Vector3 get_orthogonal_vector(const Vector3& v); 25 | 26 | void project_on_plane(Vector3& v, const Vector3& plane_normal); 27 | 28 | Vector3 projected_on_plane(const Vector3& v, const Vector3& plane_normal); 29 | 30 | Vector3 lerp(Vector3 a, Vector3 b, float t); 31 | 32 | float lerp(float a, float b, float t); 33 | }} -------------------------------------------------------------------------------- /m_tree/source/utilities/NodeUtilities.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "NodeUtilities.hpp" 4 | 5 | namespace Mtree 6 | { 7 | namespace NodeUtilities 8 | { 9 | float get_branch_length(Node& branch_origin) 10 | { 11 | float length = 0; 12 | Node* extremity = &branch_origin; 13 | while (extremity->children.size() > 0) 14 | { 15 | length += extremity->length; 16 | extremity = & extremity->children[0]->node; 17 | } 18 | length += extremity->length; 19 | return length; 20 | } 21 | 22 | void select_from_tree_rec(BranchSelection& selection, Node& node, const Vector3& node_position, int id) 23 | { 24 | if (node.creator_id == id) 25 | { 26 | selection[selection.size() - 1].push_back(NodeSelectionElement{node, node_position}); 27 | } 28 | bool first_child = true; 29 | for (auto& child : node.children) 30 | { 31 | if (!first_child) 32 | { 33 | selection.emplace_back(); 34 | } 35 | first_child = false; 36 | Vector3 child_position = node_position + child->node.direction * child->position_in_parent * child->node.length; 37 | select_from_tree_rec(selection, child->node, child_position, id); 38 | } 39 | } 40 | 41 | BranchSelection select_from_tree(std::vector& stems, int id) 42 | { 43 | BranchSelection selection; 44 | selection.emplace_back(); 45 | for (Stem& stem : stems) 46 | { 47 | select_from_tree_rec(selection, stem.node, stem.position, id); 48 | } 49 | return selection; 50 | } 51 | 52 | 53 | Vector3 get_position_in_node(const Vector3& node_position, const Node& node, const float factor) 54 | { 55 | return node_position + node.direction * node.length; 56 | }; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /m_tree/source/utilities/NodeUtilities.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../tree/Node.hpp" 3 | 4 | 5 | namespace Mtree { 6 | namespace NodeUtilities { 7 | 8 | struct NodeSelectionElement 9 | { 10 | Node* node; 11 | Vector3 node_position; 12 | NodeSelectionElement(Node& node, const Vector3& position) : node(&node), node_position(position) {}; 13 | }; 14 | 15 | using NodeSelection = std::vector; 16 | using BranchSelection = std::vector; 17 | 18 | 19 | float get_branch_length(Node& branch_origin); 20 | BranchSelection select_from_tree(std::vector& stems, int id); 21 | Vector3 get_position_in_node(const Vector3& node_position, const Node& node, const float factor); 22 | 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /m_tree/source/utilities/RandomGenerator.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/m_tree/source/utilities/RandomGenerator.cpp -------------------------------------------------------------------------------- /m_tree/source/utilities/RandomGenerator.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | namespace Mtree 4 | { 5 | class RandomGenerator 6 | { 7 | private: 8 | std::mt19937 engine; 9 | std::uniform_real_distribution uniform; 10 | public: 11 | RandomGenerator() { uniform = std::uniform_real_distribution{ 0.0f,1.0f }; }; 12 | void set_seed(int seed) { engine = std::mt19937{ (unsigned int) seed }; srand(seed); }; 13 | float get_0_1() { return uniform(engine); }; 14 | }; 15 | } -------------------------------------------------------------------------------- /m_tree/test.py: -------------------------------------------------------------------------------- 1 | import m_tree 2 | # import pickle 3 | 4 | 5 | 6 | tree = m_tree.Tree() 7 | trunk_function = m_tree.TrunkFunction() 8 | tree.set_trunk_function(trunk_function) 9 | 10 | -------------------------------------------------------------------------------- /m_tree/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | 3 | set(CMAKE_CXX_STANDARD 17) 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 5 | set(CMAKE_CXX_EXTENSIONS OFF) 6 | 7 | set(CMAKE_TOOLCHAIN_FILE ../dependencies/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "") 8 | 9 | 10 | file(GLOB_RECURSE sources 11 | "./*.hpp" 12 | "./*.cpp" 13 | ) 14 | 15 | add_executable(m_tree_tests ${sources}) 16 | 17 | add_test(NAME m_tree_tests COMMAND m_tree_tests) 18 | 19 | target_link_libraries(m_tree_tests PRIVATE m_tree-lib) -------------------------------------------------------------------------------- /m_tree/tests/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "source/mesh/Mesh.hpp" 4 | #include "source/tree/Tree.hpp" 5 | #include "source/tree_functions/TrunkFunction.hpp" 6 | #include "source/tree_functions/BranchFunction.hpp" 7 | #include "source/tree_functions/GrowthFunction.hpp" 8 | #include "source/meshers/splines_mesher/BasicMesher.hpp" 9 | #include "source/meshers/manifold_mesher/ManifoldMesher.hpp" 10 | 11 | 12 | using namespace Mtree; 13 | 14 | int main() 15 | { 16 | std::cout<<"hello world"<(); 19 | auto branch = std::make_shared(); 20 | trunk->add_child(branch); 21 | branch->start_radius = ConstantProperty{ 1.5 }; 22 | //trunk->length = 0.001; 23 | Tree tree(trunk); 24 | tree.execute_functions(); 25 | ManifoldMesher mesher; 26 | mesher.radial_resolution = 32; 27 | mesher.mesh_tree(tree); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /python_classes/__init__.py: -------------------------------------------------------------------------------- 1 | from . import operators 2 | from . import nodes 3 | 4 | def register(): 5 | operators.register() 6 | nodes.register() 7 | 8 | 9 | def unregister(): 10 | operators.unregister() 11 | nodes.unregister() -------------------------------------------------------------------------------- /python_classes/nodes/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from bpy.utils import register_class, unregister_class 3 | import nodeitems_utils 4 | 5 | from . import base_types 6 | from . import sockets 7 | from . import node_categories 8 | from . import tree_function_nodes 9 | from . import properties 10 | 11 | def register(): 12 | base_types.register() 13 | sockets.register() 14 | tree_function_nodes.register() 15 | properties.register() 16 | node_categories.register() 17 | 18 | def unregister(): 19 | node_categories.unregister() 20 | base_types.unregister() 21 | sockets.unregister() 22 | properties.unregister() 23 | tree_function_nodes.unregister() 24 | -------------------------------------------------------------------------------- /python_classes/nodes/base_types/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from bpy.utils import register_class, unregister_class 3 | from .node_tree import MtreeNodeTree 4 | 5 | classes = [MtreeNodeTree] 6 | 7 | def register(): 8 | for cls in classes: 9 | register_class(cls) 10 | 11 | def unregister(): 12 | for cls in reversed(classes): 13 | unregister_class(cls) -------------------------------------------------------------------------------- /python_classes/nodes/base_types/node.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | 4 | 5 | class MtreeNode: 6 | @classmethod 7 | def poll(cls, nodeTree): 8 | return nodeTree.bl_idname == "mt_MtreeNodeTree" 9 | 10 | # utility functions ---------------------------- 11 | 12 | def get_node_tree(self): 13 | return self.id_data 14 | 15 | def get_child_nodes(self): 16 | child_nodes = [] 17 | for socket in self.outputs: 18 | for link in socket.links: 19 | child_nodes.append(link.to_node) 20 | return child_nodes 21 | 22 | def get_neighbours(self): 23 | neighbours = [] 24 | for socket in self.inputs: 25 | for link in socket.links: 26 | neighbours.append(link.from_node) 27 | for socket in self.outputs: 28 | for link in socket.links: 29 | neighbours.append(link.to_node) 30 | return neighbours 31 | 32 | def add_input(self, socket_type, name, **kwargs): 33 | socket = self.inputs.new(socket_type, name) 34 | for key, value in kwargs.items(): 35 | setattr(socket, key, value) 36 | 37 | def add_output(self, socket_type, name, **kwargs): 38 | socket = self.outputs.new(socket_type, name) 39 | for key, value in kwargs.items(): 40 | setattr(socket, key, value) 41 | 42 | 43 | def get_mesher_rec(self, seen_nodes): 44 | if self.bl_idname == 'mt_MesherNode': 45 | return self 46 | 47 | seen_nodes.add(self.name) 48 | 49 | for neighbour in self.get_neighbours(): 50 | if neighbour.name in seen_nodes: 51 | continue 52 | mesher = neighbour.get_mesher_rec(seen_nodes) 53 | if mesher is not None: 54 | return mesher 55 | return None 56 | 57 | 58 | def get_mesher(self): 59 | seen_nodes = set() 60 | return self.get_mesher_rec(seen_nodes) 61 | 62 | # Node events, don't override ---------------- 63 | 64 | def draw_buttons(self, context, layout): 65 | self.draw(context, layout) 66 | 67 | def draw_buttons_ext(self, context, layout): 68 | self.draw_inspector(context, layout) 69 | 70 | 71 | 72 | # Functions that can be overriden ------------- 73 | 74 | def draw(self, context, layout): 75 | pass 76 | 77 | def draw_inspector(self, context, layout): 78 | pass 79 | 80 | 81 | 82 | class MtreeFunctionNode(MtreeNode): 83 | exposed_parameters = [] # List defined for each sub class 84 | advanced_parameters = [] 85 | tree_function = None # tree function type, as defined in m_tree. Should be overriden 86 | 87 | def draw(self, context, layout): 88 | for parameter in self.exposed_parameters: 89 | layout.prop(self, parameter) 90 | 91 | def draw_inspector(self, context, layout): 92 | for parameter in self.exposed_parameters + self.advanced_parameters: 93 | layout.prop(self, parameter) 94 | 95 | def construct_function(self): 96 | function_instance = self.tree_function() 97 | for parameter in self.exposed_parameters: 98 | setattr(function_instance, parameter, getattr(self, parameter)) 99 | 100 | for input_socket in self.inputs: 101 | if input_socket.is_property: 102 | if input_socket.bl_idname == "mt_PropertySocket": 103 | property = input_socket.get_property() 104 | setattr(function_instance, input_socket.property_name, property) 105 | else: 106 | setattr(function_instance, input_socket.property_name, input_socket.property_value) 107 | 108 | for child in self.get_child_nodes(): 109 | if isinstance(child, MtreeFunctionNode): 110 | child_function = child.construct_function() 111 | function_instance.add_child(child_function) 112 | 113 | return function_instance 114 | 115 | 116 | class MtreePropertyNode(MtreeNode): 117 | property_type = None # tree Property type, as defined in m_tree. Should be overriden 118 | 119 | def get_property(self): 120 | property = self.property_type() 121 | for input_socket in self.inputs: 122 | if input_socket.is_property: 123 | setattr(property, input_socket.property_name, input_socket.property_value) 124 | return property 125 | 126 | -------------------------------------------------------------------------------- /python_classes/nodes/base_types/node_tree.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | 4 | class MtreeNodeTree(bpy.types.NodeTree): 5 | bl_idname = "mt_MtreeNodeTree" 6 | bl_label = "Mtree" 7 | bl_icon = "ONIONSKIN_ON" -------------------------------------------------------------------------------- /python_classes/nodes/base_types/socket.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | 3 | 4 | class MtreeSocket: 5 | 6 | is_property : bpy.props.BoolProperty(default=True) 7 | property_name : bpy.props.StringProperty() 8 | property_value : None 9 | color : (.5,.5,.5,1) 10 | 11 | def draw_color(self, context, node): 12 | return self.color 13 | -------------------------------------------------------------------------------- /python_classes/nodes/node_categories.py: -------------------------------------------------------------------------------- 1 | import nodeitems_utils 2 | from nodeitems_utils import NodeCategory, NodeItem 3 | 4 | class MTreeNodeCategory(NodeCategory): 5 | @classmethod 6 | def poll(cls, context): 7 | return context.space_data.tree_type == 'mt_MtreeNodeTree' 8 | 9 | node_categories = [ 10 | # identifier, label, items list 11 | MTreeNodeCategory('Mesher', "Mesher", items=[ 12 | NodeItem("mt_MesherNode"), 13 | ]), 14 | MTreeNodeCategory('Trunk', "Trunk", items=[ 15 | NodeItem("mt_TrunkNode"), 16 | ]), 17 | MTreeNodeCategory('Branch', "Branch", items=[ 18 | NodeItem("mt_BranchNode"), 19 | ]), 20 | MTreeNodeCategory('Modifiers', "Modifiers", items=[ 21 | NodeItem("mt_PipeRadiusNode"), 22 | ]), 23 | MTreeNodeCategory('Property', "Property", items=[ 24 | NodeItem("mt_RandomPropertyNode"), 25 | NodeItem("mt_RampPropertyNode"), 26 | ]), 27 | ] 28 | 29 | def register(): 30 | nodeitems_utils.register_node_categories('MTREE_NODES', node_categories) 31 | 32 | 33 | def unregister(): 34 | nodeitems_utils.unregister_node_categories('MTREE_NODES') -------------------------------------------------------------------------------- /python_classes/nodes/properties/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from bpy.utils import register_class, unregister_class 3 | import nodeitems_utils 4 | 5 | from .random_property import RandomPropertyNode 6 | from .ramp_property import RampPropertyNode 7 | 8 | classes = [RandomPropertyNode, RampPropertyNode] 9 | 10 | def register(): 11 | for cls in classes: 12 | register_class(cls) 13 | 14 | def unregister(): 15 | for cls in classes: 16 | unregister_class(cls) 17 | -------------------------------------------------------------------------------- /python_classes/nodes/properties/ramp_property.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.node import MtreePropertyNode 3 | from ....m_tree import SimpleCurveProperty, PropertyWrapper 4 | 5 | class RampPropertyNode(bpy.types.Node, MtreePropertyNode): 6 | bl_idname = "mt_RampPropertyNode" 7 | bl_label = "Ramp" 8 | 9 | property_type = SimpleCurveProperty 10 | 11 | def init(self, context): 12 | self.add_input("mt_PropertySocket", "start", property_name="y_min", property_value=.01) 13 | self.add_input("mt_PropertySocket", "end", property_name="y_max", property_value=1) 14 | self.add_input("mt_PropertySocket", "power", property_name="power", property_value=1) 15 | self.add_output("mt_PropertySocket", "value", is_property=False) 16 | -------------------------------------------------------------------------------- /python_classes/nodes/properties/random_property.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.node import MtreePropertyNode 3 | from ....m_tree import RandomProperty, PropertyWrapper 4 | 5 | class RandomPropertyNode(bpy.types.Node, MtreePropertyNode): 6 | bl_idname = "mt_RandomPropertyNode" 7 | bl_label = "Random Value" 8 | 9 | property_type = RandomProperty 10 | 11 | def init(self, context): 12 | self.add_input("mt_PropertySocket", "min", property_name="min", property_value=.01) 13 | self.add_input("mt_PropertySocket", "max", property_name="max", property_value=1) 14 | self.add_output("mt_PropertySocket", "value", is_property=False) 15 | -------------------------------------------------------------------------------- /python_classes/nodes/sockets/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from bpy.utils import register_class, unregister_class 3 | from .float_socket import MtreeFloatSocket 4 | from .tree_socket import TreeSocket 5 | from .int_socket import MtreeIntSocket 6 | from .property_socket import MtreePropertySocket 7 | 8 | classes = [MtreeFloatSocket, TreeSocket, MtreeIntSocket, MtreePropertySocket] 9 | 10 | def register(): 11 | for cls in classes: 12 | register_class(cls) 13 | 14 | def unregister(): 15 | for cls in reversed(classes): 16 | unregister_class(cls) -------------------------------------------------------------------------------- /python_classes/nodes/sockets/float_socket.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.socket import MtreeSocket 3 | 4 | class MtreeFloatSocket(bpy.types.NodeSocket, MtreeSocket): 5 | bl_idname = 'mt_FloatSocket' 6 | bl_label = "Mtree Float Socket" 7 | 8 | color = (1.0, 0.4, 0.216, 0.5) 9 | 10 | 11 | min_value : bpy.props.FloatProperty(default = -float('inf')) 12 | max_value : bpy.props.FloatProperty(default = float('inf')) 13 | def update_value(self, context): 14 | self["property_value"] = max(self.min_value, min(self.max_value, self.property_value)) 15 | mesher = self.node.get_mesher() 16 | if mesher is not None: 17 | mesher.build_tree() 18 | property_value : bpy.props.FloatProperty(default = 0, update=update_value) 19 | 20 | 21 | def draw(self, context, layout, node, text): 22 | if self.is_output or self.is_linked: 23 | layout.label(text=text) 24 | else: 25 | layout.prop(self, "property_value", text=text) 26 | 27 | -------------------------------------------------------------------------------- /python_classes/nodes/sockets/int_socket.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.socket import MtreeSocket 3 | 4 | class MtreeIntSocket(bpy.types.NodeSocket, MtreeSocket): 5 | bl_idname = 'mt_IntSocket' 6 | bl_label = "Mtree Int Socket" 7 | 8 | color = (1.0, 0.4, 0.216, 0.5) 9 | 10 | 11 | min_value : bpy.props.IntProperty(default = -10**9) 12 | max_value : bpy.props.IntProperty(default = 10**9) 13 | def update_value(self, context): 14 | self["property_value"] = max(self.min_value, min(self.max_value, self.property_value)) 15 | mesher = self.node.get_mesher() 16 | if mesher is not None: 17 | mesher.build_tree() 18 | property_value : bpy.props.IntProperty(default = 0, update=update_value) 19 | 20 | 21 | def draw(self, context, layout, node, text): 22 | if self.is_output or self.is_linked: 23 | layout.label(text=text) 24 | else: 25 | layout.prop(self, "property_value", text=text) 26 | 27 | -------------------------------------------------------------------------------- /python_classes/nodes/sockets/property_socket.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.socket import MtreeSocket 3 | from ....m_tree import ConstantProperty, RandomProperty, PropertyWrapper 4 | 5 | class MtreePropertySocket(bpy.types.NodeSocket, MtreeSocket): 6 | bl_idname = 'mt_PropertySocket' 7 | bl_label = "Mtree Property Socket" 8 | 9 | color = (.8, 0.8, 0.8, 0.5) 10 | 11 | min_value : bpy.props.FloatProperty(default = -float('inf')) 12 | max_value : bpy.props.FloatProperty(default = float('inf')) 13 | def update_value(self, context): 14 | self["property_value"] = max(self.min_value, min(self.max_value, self.property_value)) 15 | mesher = self.node.get_mesher() 16 | if mesher is not None: 17 | mesher.build_tree() 18 | 19 | property_value : bpy.props.FloatProperty(default = 0, update=update_value) 20 | 21 | def get_property(self): 22 | if self.is_linked: 23 | property = self.links[0].from_node.get_property() 24 | return PropertyWrapper(property) 25 | else: 26 | property = ConstantProperty(float(self.property_value)) 27 | wrapper = PropertyWrapper() 28 | wrapper.set_constant_property(property) 29 | return wrapper 30 | 31 | def draw(self, context, layout, node, text): 32 | if self.is_output or self.is_linked: 33 | layout.label(text=text) 34 | else: 35 | layout.prop(self, "property_value", text=text) 36 | 37 | -------------------------------------------------------------------------------- /python_classes/nodes/sockets/tree_socket.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.socket import MtreeSocket 3 | 4 | class TreeSocket(bpy.types.NodeSocket, MtreeSocket): 5 | bl_idname = 'mt_TreeSocket' 6 | bl_label = "Tree Socket" 7 | 8 | color = (.2,.7,.2,1) 9 | 10 | def update_value(self, context): 11 | mesher = self.node.get_mesher_rec() 12 | if mesher is not None: 13 | mesher.build_tree() 14 | 15 | def draw(self, context, layout, node, text): 16 | layout.label(text=text) 17 | 18 | -------------------------------------------------------------------------------- /python_classes/nodes/tree_function_nodes/__init__.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from bpy.utils import register_class, unregister_class 3 | import nodeitems_utils 4 | 5 | from .branch_node import BranchNode 6 | from .tree_mesher_node import TreeMesherNode 7 | from .trunk_node import TrunkNode 8 | from .pipe_radius_node import PipeRadiusNode 9 | 10 | classes = [BranchNode, TreeMesherNode, TrunkNode, PipeRadiusNode] 11 | 12 | def register(): 13 | for cls in classes: 14 | register_class(cls) 15 | 16 | def unregister(): 17 | for cls in classes: 18 | unregister_class(cls) 19 | -------------------------------------------------------------------------------- /python_classes/nodes/tree_function_nodes/branch_node.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | import bpy 3 | from ..base_types.node import MtreeFunctionNode 4 | from ....m_tree import BranchFunction 5 | 6 | class BranchNode(bpy.types.Node, MtreeFunctionNode): 7 | bl_idname = "mt_BranchNode" 8 | bl_label = "Branches" 9 | 10 | tree_function = BranchFunction 11 | 12 | def init(self, context): 13 | self.add_input("mt_TreeSocket", "Tree", is_property=False) 14 | self.add_input("mt_IntSocket", "Seed", min_value=0, property_name="seed", property_value=randint(0,1000)) 15 | self.add_input("mt_FloatSocket", "Start", min_value=0, max_value=1, property_name="start", property_value=.1) 16 | self.add_input("mt_FloatSocket", "End", min_value=0, max_value=1, property_name="end", property_value=.95) 17 | self.add_input("mt_PropertySocket", "Length", min_value=0, property_name="length", property_value=9) 18 | self.add_input("mt_FloatSocket", "Branches density", min_value=0.0001, property_name="branches_density", property_value=2) 19 | self.add_input("mt_PropertySocket", "Start angle", min_value=0, max_value=180, property_name="start_angle", property_value=45) 20 | self.add_input("mt_PropertySocket", "Randomness", min_value=0.0001, property_name="randomness", property_value=.5) 21 | self.add_input("mt_FloatSocket", "Break Chance", min_value=0, property_name="break_chance", property_value=.02) 22 | self.add_input("mt_FloatSocket", "Resolution", min_value=0.0001, property_name="resolution", property_value=3) 23 | self.add_input("mt_PropertySocket", "Start Radius", min_value=0.0001, property_name="start_radius", property_value=.4) 24 | self.add_input("mt_FloatSocket", "Flatness", min_value=0, max_value=1, property_name="flatness", property_value=0.2) 25 | self.add_input("mt_FloatSocket", "Up Attraction", property_name="up_attraction", property_value=.25) 26 | self.add_input("mt_FloatSocket", "Gravity Strength", property_name="gravity_strength", property_value=10) 27 | self.add_input("mt_FloatSocket", "Stiffness", property_name="stiffness", property_value=.1) 28 | self.add_input("mt_FloatSocket", "Split proba", min_value=0, max_value=1, property_name="split_proba", property_value=.5) 29 | self.add_input("mt_FloatSocket", "Phillotaxis", min_value=0, max_value = 360, property_name="phillotaxis", property_value=137.5) 30 | self.add_input("mt_FloatSocket", "Split radius", min_value=0.0001, property_name="split_radius", property_value=.8) 31 | self.add_input("mt_FloatSocket", "Split angle", min_value=0, max_value=180, property_name="split_angle", property_value=35.) 32 | 33 | self.add_output("mt_TreeSocket", "Tree", is_property=False) 34 | 35 | -------------------------------------------------------------------------------- /python_classes/nodes/tree_function_nodes/pipe_radius_node.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | from ..base_types.node import MtreeFunctionNode 3 | from ....m_tree import PipeRadiusFunction 4 | 5 | class PipeRadiusNode(bpy.types.Node, MtreeFunctionNode): 6 | bl_idname = "mt_PipeRadiusNode" 7 | bl_label = "Radius Override" 8 | 9 | 10 | tree_function = PipeRadiusFunction 11 | 12 | def init(self, context): 13 | self.add_input("mt_TreeSocket", "Tree", is_property=False) 14 | 15 | self.add_input("mt_FloatSocket", "End Radius", min_value=0.0001, property_name="end_radius", property_value=.005) 16 | self.add_input("mt_FloatSocket", "Constant Growth", min_value=0, property_name="constant_growth", property_value=.2) 17 | self.add_input("mt_FloatSocket", "Accumulation Power", min_value=0.1, property_name="power", property_value=2.5) 18 | 19 | self.add_output("mt_TreeSocket", "Tree", is_property=False) 20 | -------------------------------------------------------------------------------- /python_classes/nodes/tree_function_nodes/tree_mesher_node.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | import bpy 4 | from .... import m_tree 5 | from ..base_types.node import MtreeNode 6 | 7 | def on_update_prop(node, context): 8 | node.build_tree() 9 | 10 | class TreeMesherNode(bpy.types.Node, MtreeNode): 11 | bl_idname = "mt_MesherNode" 12 | bl_label = "Tree Mesher" 13 | 14 | radial_resolution : bpy.props.IntProperty(name="Radial Resolution", default=32, min=3, update=on_update_prop) 15 | smoothness : bpy.props.IntProperty(name="smoothness", default=4, min=0, update=on_update_prop) 16 | tree_object : bpy.props.StringProperty(default="") 17 | 18 | def init(self, context): 19 | self.add_output("mt_TreeSocket", "Tree", is_property=False) 20 | 21 | def draw_generate(self, container): 22 | properties = container.operator("mtree.node_function", text="Generate Tree") 23 | properties.node_tree_name = self.get_node_tree().name 24 | properties.node_name = self.name 25 | properties.function_name = "build_tree" 26 | 27 | 28 | def has_valid_tree_object(self): 29 | return bpy.context.scene.objects.get(self.tree_object, None) is not None 30 | 31 | 32 | def draw_properties(self, container): 33 | container.prop(self, "radial_resolution") 34 | container.prop(self, "smoothness") 35 | 36 | def draw_distribute_leaves(self, container): 37 | if self.has_valid_tree_object(): 38 | properties = container.operator("mtree.add_leaves", text="Add leaves") 39 | properties.object_id = self.get_current_tree_object().name 40 | 41 | def draw_current_tree_object(self, container, context): 42 | container.prop_search(self, property="tree_object", search_data=context.scene, search_property="objects", text="") 43 | 44 | def draw(self, context, layout): 45 | valid_tree = self.get_tree_validity() 46 | generate_row = layout.row() 47 | generate_row.enabled = valid_tree 48 | self.draw_generate(generate_row) 49 | self.draw_current_tree_object(layout, context) 50 | self.draw_properties(layout) 51 | leaves_row = layout.row() 52 | leaves_row.enabled = valid_tree 53 | self.draw_distribute_leaves(leaves_row) 54 | 55 | def build_tree(self): 56 | if not self.get_tree_validity(): 57 | return 58 | tree = m_tree.Tree() 59 | trunk_function = self.outputs[0].links[0].to_node.construct_function() 60 | tree.set_trunk_function(trunk_function) 61 | tree.execute_functions() 62 | cpp_mesh = self.mesh_tree(tree) 63 | self.output_object(cpp_mesh) 64 | 65 | def mesh_tree(self, tree): 66 | mesher = m_tree.ManifoldMesher() 67 | mesher.radial_n_points = self.radial_resolution 68 | mesher.smooth_iterations = self.smoothness 69 | mesh_data = mesher.mesh_tree(tree) 70 | return mesh_data 71 | 72 | def get_current_tree_object(self): 73 | tree_obj = bpy.context.scene.objects.get(self.tree_object, None) 74 | if tree_obj is None: 75 | tree_mesh = bpy.data.meshes.new('tree') 76 | tree_obj = bpy.data.objects.new("tree", tree_mesh) 77 | bpy.context.collection.objects.link(tree_obj) 78 | self.tree_object = tree_obj.name 79 | bpy.context.view_layer.objects.active = tree_obj 80 | tree_obj.select_set(True) 81 | return tree_obj 82 | 83 | def output_object(self, cp_mesh): 84 | tree_obj = self.get_current_tree_object() 85 | tree_mesh = tree_obj.data 86 | tree_mesh.clear_geometry() 87 | bpy.context.view_layer.objects.active = tree_obj 88 | self.fill_blender_mesh(tree_mesh, cp_mesh) 89 | 90 | def fill_blender_mesh(self, mesh, cpp_mesh): 91 | verts = cpp_mesh.get_vertices() 92 | faces = np.copy(cpp_mesh.get_polygons()[::-1]) # reverse faces to flip normals 93 | radii = cpp_mesh.get_float_attribute("radius") 94 | directions = cpp_mesh.get_vector3_attribute("direction") 95 | 96 | mesh.vertices.add(len(verts)//3) 97 | mesh.vertices.foreach_set("co", verts) 98 | mesh.attributes.new(name='radius', type='FLOAT', domain='POINT') 99 | mesh.attributes['radius'].data.foreach_set('value', radii) 100 | mesh.attributes.new(name='direction', type='FLOAT_VECTOR', domain='POINT') 101 | mesh.attributes['direction'].data.foreach_set('vector', directions) 102 | 103 | mesh.loops.add(len(faces)) 104 | mesh.loops.foreach_set("vertex_index", faces) 105 | 106 | loop_start = np.arange(0, len(faces), 4, dtype=np.int) 107 | loop_total = np.ones(len(faces)//4, dtype = np.int)*4 108 | mesh.polygons.add(len(faces)//4) 109 | mesh.polygons.foreach_set("loop_start", loop_start) 110 | mesh.polygons.foreach_set("loop_total", loop_total) 111 | mesh.polygons.foreach_set('use_smooth', np.ones(len(faces)//4, dtype=np.bool)) 112 | 113 | 114 | uv_data = cpp_mesh.get_uvs() 115 | uv_data.shape = (len(uv_data)//2, 2) 116 | uv_loops = np.copy(cpp_mesh.get_uv_loops()[::-1]) # need to be reversed since faces are reversed 117 | uvs = uv_data[uv_loops].flatten() 118 | uv_layer = mesh.uv_layers.new() if len(mesh.uv_layers) == 0 else mesh.uv_layers[0] 119 | uv_layer.data.foreach_set("uv", uvs) 120 | 121 | mesh.update(calc_edges=True) 122 | 123 | def get_tree_validity(self): 124 | has_valid_child = len(self.outputs[0].links) == 1 125 | loops_detected = self.detect_loop_rec(self) 126 | return has_valid_child and not loops_detected 127 | 128 | def detect_loop_rec(self, node = None, seen_nodes = None): 129 | if node is None: 130 | node = self 131 | if seen_nodes is None: 132 | seen_nodes = set() 133 | for output in node.outputs: 134 | for link in output.links: 135 | destination_node = link.to_node 136 | if destination_node in seen_nodes: 137 | return True 138 | seen_nodes.add(destination_node) 139 | self.detect_loop_rec(destination_node, seen_nodes) 140 | return False 141 | 142 | -------------------------------------------------------------------------------- /python_classes/nodes/tree_function_nodes/trunk_node.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | import bpy 3 | from ..base_types.node import MtreeFunctionNode 4 | from ....m_tree import TrunkFunction 5 | 6 | class TrunkNode(bpy.types.Node, MtreeFunctionNode): 7 | bl_idname = "mt_TrunkNode" 8 | bl_label = "Trunk" 9 | 10 | 11 | tree_function = TrunkFunction 12 | 13 | def init(self, context): 14 | self.add_input("mt_TreeSocket", "Tree", is_property=False) 15 | self.add_input("mt_IntSocket", "Seed", min_value=0, property_name="seed", property_value=randint(0,1000)) 16 | self.add_input("mt_FloatSocket", "Length", min_value=0, property_name="length", property_value=14) 17 | self.add_input("mt_FloatSocket", "Start Radius", min_value=0.0001, property_name="start_radius", property_value=.3) 18 | self.add_input("mt_FloatSocket", "End Radius", min_value=0.0001, property_name="end_radius", property_value=.05) 19 | self.add_input("mt_FloatSocket", "Shape", min_value=0.0001, property_name="shape", property_value=.7) 20 | self.add_input("mt_FloatSocket", "Up Attraction", property_name="up_attraction", property_value=.6) 21 | self.add_input("mt_FloatSocket", "Resolution", min_value=0.0001, property_name="resolution", property_value=3) 22 | self.add_input("mt_FloatSocket", "Randomness", property_name="randomness", property_value=1) 23 | 24 | self.add_output("mt_TreeSocket", "Tree", is_property=False) 25 | -------------------------------------------------------------------------------- /python_classes/operators.py: -------------------------------------------------------------------------------- 1 | from bpy.utils import register_class, unregister_class 2 | from gpu_extras.batch import batch_for_shader 3 | import time 4 | import bpy 5 | import bmesh 6 | import gpu 7 | import numpy as np 8 | 9 | from .. import m_tree 10 | from .resources.node_groups import distribute_leaves 11 | 12 | 13 | class ExecuteNodeFunction(bpy.types.Operator): 14 | bl_idname = "mtree.node_function" 15 | bl_label = "Node Function callback" 16 | bl_options = {'REGISTER', 'UNDO'} 17 | 18 | node_tree_name: bpy.props.StringProperty() 19 | node_name: bpy.props.StringProperty() 20 | function_name : bpy.props.StringProperty() 21 | 22 | def execute(self, context): 23 | node = bpy.data.node_groups[self.node_tree_name].nodes[self.node_name] 24 | getattr(node, self.function_name)() 25 | return {'FINISHED'} 26 | 27 | 28 | class AddLeavesModifier(bpy.types.Operator): 29 | bl_idname = "mtree.add_leaves" 30 | bl_label = "Add leaves distribution modifier to tree" 31 | bl_options = {'REGISTER', 'UNDO'} 32 | 33 | object_id: bpy.props.StringProperty() 34 | 35 | def execute(self, context): 36 | ob = bpy.data.objects.get(self.object_id) 37 | if ob is not None: 38 | distribute_leaves(ob) 39 | return {'FINISHED'} 40 | 41 | 42 | def register(): 43 | register_class(ExecuteNodeFunction) 44 | register_class(AddLeavesModifier) 45 | 46 | def unregister(): 47 | unregister_class(ExecuteNodeFunction) 48 | unregister_class(AddLeavesModifier) 49 | -------------------------------------------------------------------------------- /python_classes/resources/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/python_classes/resources/__init__.py -------------------------------------------------------------------------------- /python_classes/resources/node_groups.py: -------------------------------------------------------------------------------- 1 | import os 2 | import bpy 3 | 4 | from .resource_utils import ResourceUtils 5 | 6 | 7 | def distribute_leaves(ob): 8 | if ob.modifiers.get("leaves") is not None: 9 | return 10 | modifier = ob.modifiers.new("leaves", 'NODES') 11 | modifier.node_group = ResourceUtils.append_geo_node("leaves_distribution") -------------------------------------------------------------------------------- /python_classes/resources/resource_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | from pathlib import Path 3 | import bpy 4 | 5 | MTREE_RESOURCE_DIR = (Path(__file__).parent.parent.parent / "resources").resolve() 6 | 7 | class ResourceUtils: 8 | 9 | geo_nodes_dir = os.path.join(MTREE_RESOURCE_DIR, "geo_node") 10 | 11 | @classmethod 12 | def append_geo_node(cls, name:str): 13 | group = bpy.data.node_groups.get(name, None) 14 | 15 | if group is None: 16 | if bpy.app.version < (3, 2, 0): 17 | directory = os.path.join(cls.geo_nodes_dir, "geo_nodes_2_93.blend", "NodeTree") 18 | else: 19 | directory = os.path.join(cls.geo_nodes_dir, "geo_nodes_3_2.blend", "NodeTree") 20 | 21 | filepath = os.path.join(directory, name) 22 | bpy.ops.wm.append(filepath=filepath,filename=name,directory=directory, autoselect=False) 23 | group = bpy.data.node_groups[name] 24 | 25 | return group 26 | -------------------------------------------------------------------------------- /resources/geo_node/geo_nodes_2_93.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/resources/geo_node/geo_nodes_2_93.blend -------------------------------------------------------------------------------- /resources/geo_node/geo_nodes_3_2.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steven-ray/modular_tree/87dea8b4402d97255a6076b4e3f706ec712497e9/resources/geo_node/geo_nodes_3_2.blend --------------------------------------------------------------------------------