Hello everyone, I need help with exporting a body from grasshopper. I am working on my optimization thesis, and my plan is to use HEEDS, Ansys, Rhino and Excel to optimize the hull shape of an AUV. I am able to make grasshopper read the parameters of an AUV from an excel file, and output the surface area and volume of the body to the same excel file. I am facing problem in writing the ghpython code to export the body generated in the grasshopper. After some debugging, the main error comes to be:
Exception occurred:', "‘module’ object has no attribute ‘AddBrep’
my current code is:
import System
import os
import Rhino
import rhinoscriptsyntax as rs
— Configuration —
EXPORT_DIR = r"D:\Study\Name 400\Parametric Modelling\Geometry"
FILE_NAME = “Hull_Optimized_Design.step”
EXPORT_PATH = os.path.join(EXPORT_DIR, FILE_NAME)
if ‘run’ not in globals():
run = False
if ‘x’ not in globals():
x = None
print(“=== DEBUG START ===”)
print(“Run trigger:”, run)
print(“Geometry exists:”, x is not None)
if x is not None:
print(“Geometry type:”, x.GetType().Name)
print(“Geometry is valid:”, x.IsValid)
def ExportBrepToStep(brep_in, run_trigger, export_path):
“”"
Main export function with detailed debugging
“”"
print(“\n— Inside Export Function —”)
print(“Run trigger:”, run_trigger)
print(“Brep exists:”, brep_in is not None)
if not run_trigger or brep_in is None:
return "Ready to export. Target: {}".format(export_path)
# Check 1: Is the BREP valid?
if not brep_in.IsValid:
return "ERROR: Input BREP geometry is not valid. Check upstream components."
# 2. Create the directory if it doesn't exist
export_dir = os.path.dirname(export_path)
print("Export directory:", export_dir)
try:
if not os.path.exists(export_dir):
print("Creating directory...")
os.makedirs(export_dir)
print("Directory created")
else:
print("Directory already exists")
except Exception as e:
return "ERROR: Failed to create directory: {}. Check folder permissions.".format(e)
# 3. Use rs.Command() to export the geometry
object_id = None
try:
print("Adding brep to document...")
# 3a. Add the geometry to the document temporarily
object_id = rs.AddBrep(brep_in)
print("Object ID:", object_id)
if object_id is None:
return "ERROR: Failed to add geometry to Rhino doc (rs.AddBrep failed)."
# 3b. Select the new object
print("Selecting object...")
rs.SelectObject(object_id)
# 3c. Build the export command string
command_string = "_-Export \"{}\" _Enter _Enter".format(export_path)
print("Command:", command_string)
# 3d. Run the command
print("Executing command...")
success = rs.Command(command_string, True)
print("Command success:", success)
# Check if file was created
file_exists = os.path.exists(export_path)
print("File exists after export:", file_exists)
if success and file_exists:
return "SUCCESS! File created: {}".format(export_path)
elif success and not file_exists:
return "WARNING: Command succeeded but file not found at: {}".format(export_path)
else:
return "FAILURE: Export command failed."
except Exception as e:
print("Exception occurred:", str(e))
return "CRITICAL ERROR: {}".format(e)
finally:
print("Cleaning up...")
if object_id is not None:
try:
if rs.IsObject(object_id):
rs.DeleteObject(object_id)
print("Temporary object deleted")
except:
print("Could not delete temporary object")
print(“\n— Calling Export Function —”)
out = ExportBrepToStep(x, run, EXPORT_PATH)
print(“Final result:”, out)
print(“=== DEBUG END ===”)