Hello Rhino Community,
I’m trying to automate the process of creating Rhino environments using a batch of HDRI files. I am not very experienced in scripting and tried to come up with something using ChatGPT.
I need a script that performs the following tasks:
- For each HDRI file, create a new Rhino environment (type: High Dynamic Range Texture).
- Set the HDRI file as the environment background.
- Adjust the saturation to 0.
- Save the environment in a specified folder with the HDRI’s filename.
Here’s the script I’ve been working on:
import os
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
import System
def create_environment_from_hdri(hdri_path, save_folder):
hdri_name = os.path.splitext(os.path.basename(hdri_path))[0]
render_environment_type_id = System.Guid("1e591e9e-5ef5-484e-bcad-fd5c3e41f869")
render_texture_type_id = System.Guid("8e0ab46d-d29c-49d1-a61d-6cbe6d0b0139")
environment = Rhino.Render.RenderContent.Create(render_environment_type_id, hdri_name, sc.doc)
if environment is None:
print("Failed to create environment for {}".format(hdri_name))
return
texture = Rhino.Render.RenderContent.Create(render_texture_type_id, hdri_name, sc.doc)
texture.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
texture.SetParameter("filename", hdri_path)
texture.EndChange()
environment.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
environment.SetChild(texture, "background-texture")
environment.EndChange()
environment.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
environment.SetParameter("saturation", 0.0)
environment.EndChange()
sc.doc.RenderEnvironments.Add(environment)
save_path = os.path.join(save_folder, hdri_name + ".renv")
environment.SaveToFile(save_path)
def main():
hdri_folder = rs.BrowseForFolder(message="Select folder containing HDRI files")
if not hdri_folder:
return
save_folder = rs.BrowseForFolder(message="Select folder to save Rhino environments")
if not save_folder:
return
for file_name in os.listdir(hdri_folder):
if file_name.lower().endswith(".hdr") or file_name.lower().endswith(".exr"):
hdri_path = os.path.join(hdri_folder, file_name)
create_environment_from_hdri(hdri_path, save_folder)
rs.MessageBox("Finished creating environments from HDRI files", 0, "Completed")
if __name__ == "__main__":
main()
Unfortunately, I keep encountering errors such as “‘type’ object has no attribute ‘ClassId’” and “‘type’ object has no attribute ‘NewRenderEnvironmentContentTypeId’”. I’m not sure if I’m using the correct methods or type IDs.
Could anyone provide guidance on how to properly create and configure Rhino render environments using HDRI files? Any help would be greatly appreciated!
Thank you!