Debian Codegraph @

src/bonsai/docs/guides/development/hello_world.rst

raw
1 Hello, world!
2 =============
3
4 Bonsai takes a unique approach to authoring BIM data. Traditional BIM authoring
5 apps create features that are tailored for a single discipline's paradigm, such
6 as a 3D environment, or a spreadsheet view, and store their data structure in a
7 schema that is unique to their application. In order to interoperate with
8 others, there is an export or import process that translates between their
9 bespoke schema to and from open data standards. The most famous ISO standard
10 for BIM is IFC. After this translation, they then serialise it typically into a
11 format, which may be saved to disk.
12
13 Bonsai does things differently.
14
15 Bonsai does not have its own bespoke data structure and does not import or
16 export. Bonsai uses ISO open data standards directly in memory. Most commonly,
17 this is IFC data. We will place a focus on IFC on this guide, but the reader
18 should be aware that Bonsai also takes the same approach to dealing with other
19 open data standards, like Brickschema or BCF. The same concepts will apply. We
20 can call this Native OpenBIM authoring, which is a paradigm shift from
21 traditional BIM which relies on translated IFC data.
22
23 .. image:: images/native-openbim.png
24
25 Every user operation reads or writes this data structure in memory, and the IFC
26 data becomes the source of truth for all data. There is no such thing as an
27 import or export. The data is always represented in IFC. When a BIM model is
28 opened or saved, it is simply a serialisation or deserialisation operation. This
29 also means that you are using Blender simply as an interface to interact with
30 IFC, and the ``.blend`` container is largely unnecessary, as nothing of
31 significance is stored in the Blender system, it is simply a snapshot of your
32 working session.
33
34 Due to this significant difference, hacking on Bonsai requires knowledge not
35 just about how Blender works, but also how open data standards like IFC works.
36
37 Just show me the code!
38 ----------------------
39
40 Sometimes, the best way to learn how to hack on a project is to just start
41 hacking away. First, download the code. To keep things simple, you can download
42 the source as a zip file for now, but keep in mind that sooner or later you'll
43 need to :ref:`use Git to collaborate <submitting-code-to-git>`.
44
45 .. container:: blockbutton
46
47 `Download Source
48 <https://github.com/IfcOpenShell/IfcOpenShell/archive/refs/heads/v0.8.0.zip>`__
49
50 BIM authoring is a really big topic. As a result, the Bonsai code is separated
51 into modules. Each module focuses on a particular topic of BIM. Most modules
52 are self-contained, but sometimes they connect to one another, just like how
53 BIM works.
54
55 .. image:: images/module-architecture.png
56
57 Modules are not arbitrary divisions. They tend to reflect how portions of BIM
58 data are segregated in the IFC international standard. This allows us to
59 minimise the overlap between modules, so that developers can work on a single
60 portion of the code with relative certainty that their actions will not affects
61 other developers.
62
63 - `Bonsai modules <https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.8.0/src/bonsai/bonsai/bim/module>`__
64 - `IFC modules <https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.8.0/src/ifcopenshell-python/ifcopenshell/api>`__
65
66 Bonsai comes with a secret **demo module** which is basically a hello world
67 coding tutorial which teaches you about all the moving parts. It's far more
68 interesting to read this code rather than 15 pages of abstract software
69 architecture flow charts and diagrams. The code and its comments will guide you
70 through the process.
71
72 Before playing with the demo module, you may want to switch to using a source
73 installation. See `bonsai/installation <./installation.html>`_ for details.
74
75 To see the demo module in action, you'll need to enable it. In
76 ``src/bonsai/bonsai/bim/__init__.py``, uncomment the line for the demo
77 module. When you restart Blender, you will see a new demo panel in your scene
78 properties interface tab. Have fun!
79
80 .. image:: images/demo-module.png
81
82 Now you're ready to learn how to code! Here are all the files associated with
83 the demo module. Feel free to read them in any order. Each file is heavily
84 commented with explanations about what each line of code does. Change some of
85 the code, reload Blender, and see what happens!
86
87 ::
88
89 src/bonsai/bonsai/bim/module/demo/__init__.py
90 src/bonsai/bonsai/bim/module/demo/operator.py
91 src/bonsai/bonsai/bim/module/demo/prop.py
92 src/bonsai/bonsai/bim/module/demo/ui.py
93 src/bonsai/bonsai/bim/module/demo/data.py
94 src/bonsai/bonsai/core/demo.py
95 src/bonsai/bonsai/tool/demo.py
96
97
98 Wow! That's a lot of files needed for a hello world! Don't worry, it's mostly
99 tutorial comments and it's there to teach you the basics from how Blender's
100 add-on system works, how interfaces work, to how Bonsai works, and how to test
101 and structure it so that you can build incredibly complex features in a
102 maintainable way.
103
104 Tests for quality checking also exist. The system is designed so that you can
105 do "Test Driven Development". For reference on how to run these tests, see `bonsai/running_tests <./running_tests.html>`_
106 for details. You can find the tests here:
107
108 ::
109
110 src/bonsai/test/bim/feature/demo.feature
111 src/bonsai/test/core/test_demo.py
112 src/bonsai/test/tool/test_demo.py
113
114 Not all developers, especially those learning how to code, are familiar with
115 testing and how to write tests. That's OK! Feel free to ignore the tests at
116 first until you get a bit more comfortable with coding, and others can help
117 guide you when you're ready to make the leap. Don't let this stop you from
118 building things, others can also help write tests for you and clean your code.
119 It's a great way to learn!
120
121 Once you're through, you should be able to understand how most of Bonsai is
122 built and where to find things.
123
124 There are many Blender Python tutorials out there. A good place to start is the
125 `Start coding for Blender
126 <https://wiki.osarch.org/index.php?title=Start_coding_for_Blender>`__ from the
127 OSArch Wiki. In addition, the Blender text editor comes with a menu called
128 ``Templates > Python`` which gives you a whole list of example code of how to
129 create an add-on which creates objects, creates gizmos, new buttons, interfaces,
130 and so on. This is a great way to try out how to build different extensions.
131
132 Naturally, if you just want to tweak Bonsai or build a small feature just for
133 yourself, you're free to ignore this advice, skip all the tests, and just write
134 half the code in a single file and it'll get the job done.
135
136 Software architecture
137 ---------------------
138
139 If code isn't good enough for you and you want to learn more about why the code
140 is structured the way it is, here is a list of design principles we follow:
141
142 1. Big systems are hard to maintain. Break big systems into small systems.
143 2. Separate abstract code from concrete code. Start with abstract code, and
144 deal with the details later.
145 3. Good code reads like poetry. Every usecase should have a poem.
146 4. Separate UI code from domain logic. UI code should be as dumb as possible.
147 5. Follow the Unix philosophy. We're dealing with a big industry problem here.
148 Building a shared ecosystem of tools is better than one behemoth.
149 6. Everything should be testable. You should be able to test first.
150 7. Have different types of tests. Inversely correlate test speed and scope.
151 8. Community first. Allow beginner programmers to join in the fun! Code should
152 feel easy, not like a course in design pattern jargon.
153 9. Incremental change, not waterfall. Don't trash and rebuild. Refactor and
154 redesign one commit at a time. With each commit, ask if you're making the
155 code nicer.
156 10. Perfect is the enemy of the good. Half broken is better than completely
157 broken.
158
159 The rest of this contains nasty software architecture jargon. If that's not your
160 thing, stop reading now.
161
162 Bonsai code may be understood in three separate layers: **Delivery**,
163 **Domain**, and **Data**. The Bonsai architecture separates these three layers
164 from one another. Because they are separate, they can be tested and built
165 separately.
166
167 .. image:: images/architecture.png
168
169 The **Delivery** mechanism is how the application is delivered to
170 the user and handles user interactions. It covers the interface and triggering
171 events as inputs into the application, and rendering responses.
172
173 As advertised in the name, the **Delivery** mechanism is based on **Blender**.
174 **Blender** is a well established 3D platform. Out of the box, it provides an
175 incredibly advanced interface to allow users to interact with geometry. The
176 delivery mechanism code extends Blender extensively, including new *Operations*
177 that users can perform, new *Properties* to store custom data, and new *UI*
178 layouts to display information.
179
180 When an event such as an *Operation* is triggered, the **Delivery** mechanism
181 executes the **Domain** layer through dependency injection. The **Domain** layer
182 will then decide how to process this input.
183
184 The **Domain** layer is divided into two halves: an abstract *Core* and concrete
185 *Tools*. The *Core* describes abstract, high-level application logic flow for
186 every single possible usecase in application. The *Tools* actually implement
187 this abstract logic, and figure out how things actually work, whether it is
188 manipulating the Blender scene, writing and reading files, building new IFC
189 graph relationships, and so on. The **Domain** layer also has interface classes
190 to describe what it needs.
191
192 Whenever the application needs to remember or store information, it does so
193 using a **Data** repository. The data ensures that stored information confirms
194 to a defined schema and is valid, and can be retrieved later. Some data is
195 stored in Blender, such as information about your working session and active
196 scene. Other data is stored in IFC, such as all the relationships in your BIM
197 model. We mention **Data** specifically because OpenBIM data authoring is such
198 a big aspect of Bonsai. In fact, it's so big that most of it is completely
199 separated from the Bonsai code and lives elsewhere.
200
201 For example, all the code that handles IFC data, which you can think of as a
202 graph database, is in a completely separate codebase, even under a different
203 software license. You can find it in the IfcOpenShell-python API module. Many of
204 the various data processing functions are built as separate Unix-like utilities,
205 even with their own CLI. This **Data** layer isn't a single folder of code we
206 can point to, it's an ecosystem of libraries and utilities that we want to share
207 with the entire industry.
208
209 IfcOpenShell Architecture
210 -------------------------
211
212 A large part of Bonsai is understanding how IFC data is modified. This code is
213 not technically part of the Bonsai codebase, but it is vital to understand. You
214 will need to be familiar with the IfcOpenShell Python module.
215
216 Manipulating IFC data is not simple. IFC may be serialised into multiple
217 formats, multiple schema versions must be supported, and geometry may be defined
218 in a highly parametric or implicit manner, which geometry kernels do not
219 natively support. All this heavy lifting is performed by the IfcOpenShell
220 library.
221
222 The IfcOpenShell library consists of a C++ based core. Its geometry processing
223 is done using OpenCascade, and optionally CGAL as an experimental option. By the
224 time Bonsai interacts with IFC, it uses the IfcOpenShell Python bindings, so
225 all IFC data is already deserialised into Python objects. The inner workings of
226 the C++ base is out of scope.
227
228 .. image:: images/ifcopenshell-architecture.png
229
230 IfcOpenShell offers a core set of low-level functionality to read and write this
231 data. An example of the core functionality would be:
232
233 .. code-block:: python
234
235 import ifcopenshell
236 model = ifcopenshell.open("foo.ifc")
237 wall = model.create_entity("IfcWall")
238 wall.Name = "Foobar"
239
240 Core functions are simple read and write operations with no post processing.
241 Core functions also include geometry processing, which converts IFC geometry
242 into OpenCascade objects.
243
244 Sometimes, there are repetitive actions that need to be performed. These
245 functions are grouped into a ``util`` module. These include utility functions
246 for coordinate calculations, date conversions, filtering elements, unit
247 conversions, and more. Utility functions make no assumption about the context in
248 which they are used, and so perform highly specific tasks and nothing else.
249 Here's an example of utility functionality:
250
251 .. code-block:: python
252
253 import ifcopenshell
254 import ifcopenshell.util.date
255 import ifcopenshell.util.geolocation
256 start = ifcopenshell.util.date.ifc2datetime(task_time.ScheduleStart)
257 coordinates = ifcopenshell.util.geolocation.local2global(matrix, eastings, ...)
258
259 When authoring, core and utility functions are usually too low-level. To cater
260 for this, a high level API is provided. The API is divided into mostly isolated
261 modules, each module representing a distinct set of concepts in the IFC schema.
262 Unlike the util module, these API modules are highly context-sensitive, and
263 assume that you intend to be authoring native IFC.
264
265 This context-sensitive assumption means that the functions within the modules
266 are designed around typical usecases in an authoring environment. It performs
267 all the necessary manipulations to achieve a domain-specific usecase. Authoring
268 is complex and requires a deep knowledge of IFC to perform correctly and ensure
269 that the IFC graph state is well maintained. Typically, any authoring operation
270 that does not use the API is likely to contain mistakes.
271
272 Here's an example of it in action:
273
274 .. code-block:: python
275
276 import ifcopenshell.api
277 ifcopenshell.api.run("grid.create_grid_axis", model, ...)
278 ifcopenshell.api.run("structural.add_structural_load", model, ...)
279
280 Because the API performs all the IFC manipulations to achieve a usecase, no
281 further interaction is required in a typical native IFC authoring environment.
282 For this reason, Bonsai only interacts with the API for its authoring
283 capabilities.
284
285 The code for IfcOpenShell's various systems can be found here:
286
287 - `ifcopenshell (core) <https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.8.0/src/ifcopenshell-python/ifcopenshell>`__
288 - `ifcopenshell.util <https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.8.0/src/ifcopenshell-python/ifcopenshell/util>`__
289 - `ifcopenshell.api <https://github.com/IfcOpenShell/IfcOpenShell/tree/v0.8.0/src/ifcopenshell-python/ifcopenshell/api>`__
290
291 .. _submitting-code-to-git:
292
293 Submitting code to Git
294 ----------------------
295
296 So, you've written some code, fixed a bug, made an improvement, and would like
297 to get your code added to the Git repository? If your change is relatively
298 small, you can submit your changes just using the Github website. Browse to the
299 `IfcOpenShell repository <https://github.com/ifcopenshell/ifcopenshell>`__ and
300 navigate to the file you want to edit the code of. Then just press the edit icon
301 to begin editing. When you're done, you'll be prompted to submit your changes.
302
303 .. image:: images/github-editing.png
304
305 If you're making a large change, you'll need to create a **Pull Request**.
306 Github has an excellent comprehensive guide on `how to contribute to projects
307 <https://docs.github.com/en/get-started/quickstart/contributing-to-projects>`__
308 which you can follow.
309
310 If you make regular contributions, you are also welcome to officially join the
311 IfcOpenShell developer team, where you'll be able to make changes without
312 waiting for code reviews and approvals.
313
314 Asking for help
315 ---------------
316
317 It's no fun to code alone! It's encouraged to reach out if there are any issues,
318 if you'd like to code together with another developer, need a code review, or
319 need further testing. Here are some places to reach out:
320
321 - `Github issues <https://github.com/IfcOpenShell/IfcOpenShell/issues>`__
322 - `OSArch live chat <https://osarch.org/chat>`__
323 - `OSArch community forum <https://community.osarch.org>`__