GSOC'24 - Issue #85 - Scripts to generate light simulation input files from 3D models

Hey everyone in the community! 🙋‍♂️

I'm Chirag Singh, a Computer Science student from Vellore Institute of Technology, India. My expertise lies in applying 3D modeling and animation skills to the realms of Web, AR, and VR development. As a WebGL Developer with proficiency in Blender and Unity, I craft engaging and interactive ThreeJS web applications as well as automate Blender through Python scripting skills.
My Linkedin
My Instagram

This is my first time posting here, and I come bearing good news! I've been selected for GSOC'24 to contribute to Issue #85 - Scripts to generate light simulation input files from 3D models 🥳🥳

The project involves integrating the Blender BIM Add-On with Radiance, a tool utilized for scientifically accurate light simulations to generate 3D renders. Radiance necessitates 3D models in OBJ format and a set of input text files describing materials and scenes. Integration options also encompass filtering objects for conversion, developing a GUI in Blender, object proxying, and material swapping/mapping.

Hence, my tasks will include:

  • Developing Python scripts to convert an input 3D IFC model (the building model) into OBJ files and text files readable by Radiance (light simulation software).
  • Designing a graphical interface in Blender to facilitate this conversion and offer customizable settings.
  • Implementing settings to manage object filtering, object count limits, object geometry substitution, and material mapping/substitution.
  • Enabling single-plane substitution for glazing (often modeled as cuboids but required as flat planes for light simulation).
  • Providing options to choose from preset sky settings and automatic materials.

Here's my proposal for the project.

Looking forward to diving into this exciting project and contributing to its success!

JohnbrunopostleShegsKoAratheoryshawKarinacarlopavSavyGust27wmiatomkarincaand 5 others.

Comments

  • Weekly Progress of Issue 85

    Week 1 (May 27 – June 3):

    Phase 0: Practiced manual radiance rendering for a cube.
    Reference - https://www.radiance-online.org/learning/tutorials
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/tree/main/Radiance Practice/Just a Cube

    Phase 1: Created a simple Blender UI Button that would export the scene as an OBJ File format in the same directory as
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase1.py

    Phase 2: Created a simple Blender UI Button that would export the scene as an OBJ File format along with a materials.rad file by creating a new Folder named “Radiance Rendering” in the same directory where the .blend file is saved.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase2.py

    Week 2 (June 4 – June 11):

    Phase 3: Rendering the scene by running rpict command for Radiance Rendering using Subprocess
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase3.py

    Phase 4: Rendering the scene by generating a .rif file and used rad command for Radiance Rendering using Subprocess
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase4.py

    Week 3 (June 12 – June 19):

    Phase 5: Calculated Bounding Box values and Camera VIEW values and added to the .rif file.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase5.py

    Phase 6: Created a Blender GUI to take input for Resolution of the Image, calculated aspect ratio and added tot the .rif file.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase6.py

    Phase 7: In the materials.rad file, added some possible radiance materials and created alias for IFC materials mapped in the material_mapping.json file.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/blob/main/phase7.py

    Week 4 (June 20 – June 27):

    Phase 8: Practiced generating radiance render using Pyradiance Python Library.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/tree/main/PyRadiance testing

    Phase 9: Converted the entire manual radiance rendering code to Pyradiance rendering.
    Link - https://github.com/chiragsingh1711/gsoc-2024-Issue-85/tree/main/CodeConvertToPyradiance

    Week 5 (June 28 – July 5):

    This week I’ll be focusing on adding my entire progress to the BlenderBIM addon and make it publicly accessible for users to test and give feedback on the radiance rendering.

    carlopavShegsJohnatomkarincaGerardTbitacovirAce
  • edited July 2024

    Week 5 (June 28 – July 5):

    Phase 10:: I created a new branch named "radiance addon" and made my first commit that includes all the Blender GUI components for the addon.
    Phase 11:: This phase proved to be one of the most challenging for me. I encountered several issues along the way:
    1.There was a recent update in Geometry Serializer. Previously, the code looked like this:
    settings = ifcopenshell.geom.settings() settings.set(settings.STRICT_TOLERANCE, True) settings.set(settings.INCLUDE_CURVES, True) settings.set(settings.USE_ELEMENT_GUIDS, True) settings.set(settings.APPLY_DEFAULT_MATERIALS, True) settings.set(settings.USE_WORLD_COORDS, True) serialiser = ifcopenshell.geom.serializers.obj(obj_file_path, mtl_file_path, settings)

    However, in V0.8.0, there were some changes, and I had to modify the code to the following:
    settings = ifcopenshell.geom.settings() serializer_settings = ifcopenshell.geom.serializer_settings() settings.set("dimensionality", ifcopenshell.ifcopenshell_wrapper.SURFACES_AND_SOLIDS) settings.set("apply-default-materials", True) serializer_settings.set("use-element-guids", True) settings.set("use-world-coords", True) serialiser = ifcopenshell.geom.serializers.obj(obj_file_path, mtl_file_path, settings, serializer_settings)

    1. The second issue I encountered was that the OBJ conversion was happening synchronously, and the subsequent code would execute before the IFC was fully converted to OBJ. To ensure that the OBJ conversion finishes first, I implemented two separate buttons: one for conversion and another for rendering.

    2. The third issue arose from the fact that PyRadiance was installed using the BlenderBIM addon debug, which caused compatibility problems. To resolve this, I uninstalled PyRadiance from the addon and reinstalled it using Blender's pip. After doing so, there were no further issues.

    3. Initially, the mapping.json file was hardcoded. To enable users to test the addon on their own machines, I added a file path upload feature in the Blender GUI. This creates a copy of the mapping.json file where the Blend file is saved and allows the process to continue seamlessly.

    4. Finally, the output is successfully generated (locally 😂). I will be creating a pull request and inviting users to test the addon. If everything works as intended, the PR will be merged. In the upcoming week, I plan to focus entirely on enhancing the renders and making them visually appealing.

    atomkarincaJohnbruno_perdigaoKoAramanuvarkeybitacovirAcetlang
  • Week 6 (July 6 – July 12):
    - Made the initial pull request for the Radiance exporter feature.
    - Users tested the Radiance exporter on their machines, revealing several issues:
    1. JSON path error on Linux due to Windows-specific file path handling.
    2. Errors when project directory contained white spaces.
    3. Confusion about the necessity of the "IFC filename" field.
    4. Lack of clarity regarding the "JSON file" field and its purpose.

    • Began addressing these issues based on user feedback.

    Week 7 (July 13 – July 19):
    - Implemented changes to resolve issues reported in Week 6:
    1. Modified file path handling to be platform-agnostic.
    2. Addressed white space issues in file paths.
    3. Improved button labeling and UI logic.

    • Pushed updated code for further testing.
    • Received additional feedback on persistent issues:
      1. White space in filepath still causing problems, related to upstream PyRadiance.
      2. Permissions error for executables inside PyRadiance.

    Week 8 (July 20 – July 26):
    - Addressed remaining issues from Week 7:
    1. Updated PyRadiance to latest release (v0.4.2) to resolve white space issues.
    2. Modified file writing to properly handle paths with spaces.

    • Implemented significant UI improvements:

      1. Created a "Upload JSON" toggle for flexible material mapping.
      2. Enabled manual JSON building within the UI when toggle is off.
      3. Added user option to choose output file name and extension.
      4. Disabled Render button while geometry is being exported.
    • Began work on integrating materials from spectraldb.com.

    • Started implementation of gensky functionality.

    Week 9 (July 27 – August 2):
    - Addressed persistent error with non-setting of executable flags for Radiance binaries.
    - Received feedback on potential improvements:
    1. Suggestion to use UIList for material mappings.
    2. Proposal to run OBJ export and rendering in separate threads.
    3. Discussion on potential for rendering section views.

    • Implemented gensky code.
    • Fixed executable flag error.

    Week 10 (August 3 – August 9):
    - Had a meeting with mentor to discuss progress and next steps.
    - Began work on creating a JSON file containing all materials for mapping from spectralDB.
    - Implemented several improvements:
    1. Listed down materials to be mapped.
    2. Set default unmapped material to white.
    3. Added dropdown menus for category and subcategory selection from spectralDB.
    4. Corrected hardcoded "render123.hdr" filename as per user feedback.

    • Implemented UI List for material mappings:
      • This improvement enhances consistency with other parts of the addon.
      • Provides a more intuitive and organized interface for managing material mappings.
      • Allows for easier viewing and editing of multiple material assignments.

    Code Snippet: Platform-agnostic File Path Handling

    import os
    
    def get_filename_from_path(file_path):
        return os.path.basename(file_path)
    
    json_dest_path = os.path.join(blend_file_dir, get_filename_from_path(json_file_path))
    

    Code Snippet: Improved File Writing for Paths with Spaces

    file.write('void mesh model\n1 "' + rtm_file_path + '"\n0\n0\n')
    

    Challenges and Solutions

    1. Cross-platform Compatibility:

      • Problem: File path handling was initially Windows-specific, causing errors on Linux.
      • Solution: Implemented platform-agnostic file path handling using os.path functions.
    2. White Spaces in File Paths:

      • Problem: Errors occurred when project directories contained white spaces.
      • Solution: Updated PyRadiance to version 0.4.2 and modified file writing to properly handle paths with spaces.
    3. Executable Permissions:

      • Problem: PyRadiance binaries lacked executable permissions when installed through Blender.
      • Solution: Implemented a fix to ensure proper permissions are set during installation.
    4. User Interface Clarity:

      • Problem: Users were confused about certain fields and their purposes.
      • Solution: Improved labeling, added tooltips, and implemented toggles for better user experience.
    5. Material Mapping Complexity:

      • Problem: Manual material mapping was cumbersome for users.
      • Solution: Began implementation of a more intuitive system using spectralDB and dropdown menus for easy selection.
    brunopostleMoultJohntlangtheoryshawKoAraatomkarincaAce
  • Hi @chiragsingh1711, Thank you for the amazing addition to Bonsai!
    I'm busy going through attempting this and just want to confirm the process:
    1) Create Ifc file & save
    2) Load objects (walls, floors, doors) to render
    3) Load a camera (Ifc or otherwise)
    4) load a Sun light
    5) Load from memory to use current file:

    5.5) Materials default to white:

    Currently not certain how to import the Spectral DB materials but that should not be an issue if it is just substituted for white correct?
    6) Select an Output directory

    7) Export Geometry (OBJ) for Simulation

    8) Run the simulation:

    Output: Should get a render in the Output Directory
    I am completely uncertain if I have followed the correct steps, so I'm nearly certain this is user error
    but After I hit the Radiance Render button I get this error:

    Python: Traceback (most recent call last):
    File "C:\Users\user\AppData\Roaming\Blender Foundation\Blender\4.2\extensions.local\lib\python3.11\site-packages\pyradiance\anci.py", line 22, in wrapper
    result = func(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\user\AppData\Roaming\Blender Foundation\Blender\4.2\extensions.local\lib\python3.11\site-packages\pyradiance\gen.py", line 514, in gensky
    return sp.run(cmd, stderr=sp.PIPE, stdout=sp.PIPE, check=True).stdout
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files (x86)\Steam\steamapps\common\Blender\4.2\python\Lib\subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
    subprocess.CalledProcessError: Command '['C:\Users\user\AppData\Roaming\Blender Foundation\Blender\4.2\extensions\.local\lib\python3.11\site-packages\pyradiance\bin\gensky', '8', '4', '9.0', '-a', '-26.20560073852539', '-o', '28.033700942993164', '-m', '2', '-y', '2024']' returned non-zero exit status 3221226505.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Users\user\AppData\Roaming\Blender Foundation\Blender\4.2\extensions.local\lib\python3.11\site-packages\pyradiance\anci.py", line 25, in wrapper
    raise RuntimeError(
    RuntimeError: An error occurred with exit code 3221226505:

    atomkarinca
  • Hi @Ace ! Thankyou for pointing out this error. For me its working locally on my windows. What I think it might be an error on the installation of pyradiance. Give me some time, I'll check the origin of this error and get back to you with a fix.

    carlopavAceJohn
Sign In or Register to comment.