Python Grasshopper: The file writing plug-in failed

Hi, I’m trying to create a simple python script inside a grasshopper python component that exports fbx (I’ve ecountered the same problem also when exporting normal objs too) that gives me an error and tells me Failed to save as /Users/jorgemuyo/Desktop/GRASSHOPPER_EXPERIMENTATION/OBJ_TEST/combined_animation.fbx.

The file writing plug-in failed.

When exporting as obj or fbx from rhino itself I have no problem, only when doing it through python. Does anybody knows why this could be happening. I’m currently using a mcbook pro m2

Here is the scrip I’m using:
import os
import Rhino
from System import Array

def combine_fbx_files(folder_path):
# Check if the specified folder exists
if not os.path.exists(folder_path):
print(f"Folder ‘{folder_path}’ does not exist.")

# Get a list of all FBX files in the specified folder
fbx_files = [f for f in os.listdir(folder_path) if f.endswith('.fbx')]

if not fbx_files:
    print("No FBX files found in the specified folder.")

# Sort the files based on the numeric part in their names (test_1, test_2, ...)
def extract_number(filename):
        return int(''.join(filter(str.isdigit, filename)))
    except ValueError:
        return float('inf')


# Initialize Rhino document
rhino_doc = Rhino.RhinoDoc.ActiveDoc
if rhino_doc is None:
    print("No active Rhino document.")

# List to hold all object ids imported from FBX files
all_object_ids = []

    # Import each FBX file and collect object ids
    for fbx_file in fbx_files:
        fbx_file_path = os.path.join(folder_path, fbx_file)
        result = rhino_doc.Import(fbx_file_path)
        if result:
            print(f"Imported {fbx_file_path}")
            # Get object ids of newly imported objects
            new_objects = rhino_doc.Objects.FindByObjectType(Rhino.DocObjects.ObjectType.AnyObject)
            if new_objects:
                all_object_ids.extend([obj.Id for obj in new_objects])
            print(f"Failed to import {fbx_file_path}")

    # Check if any objects were imported
    if not all_object_ids:
        print("No objects imported.")

    # Create a list to store selected object references
    selected_object_refs = []

    # Get selected objects
    selected_objects = rhino_doc.Objects.GetSelectedObjects(False, False)

    for obj in selected_objects:

    # Export selected objects to a single FBX file
    output_path = os.path.join(folder_path, "combined_animation.fbx")
    options = Rhino.FileIO.FileWriteOptions()
    options.SelectedObjects = Array[Rhino.DocObjects.ObjRef](selected_object_refs)
    options.WriteSelectedObjectsOnly = True
    options.WriteGeometryOnly = False  # Include animation data
    options.WriteFileUnitsAndTolerances = False  # Use defaults
    success = rhino_doc.WriteFile(output_path, options)

    if success:
        print(f"Exported combined animation to {output_path}")
        print("Failed to export combined animation")

except Exception as ex:
    print(f"An error occurred: {ex}")

    # Clean up: delete all imported objects from the document
    if all_object_ids:
        rhino_doc.Objects.Delete(all_object_ids, True)

Example usage:

if name == “main”:
folder_path = “/Users/jorgemuyo/Desktop/GRASSHOPPER_EXPERIMENTATION/OBJ_TEST”