Is there a way to execute a GH script on every file in a directory?

Hi All,

My typical scripts I make are hooked up to a LunchBox bake component, controlled by a button. As of right now, the script is controlled every time I push the button, and it bakes the result of the script into the Rhino file.

Anyways, I have had several past projects where I need to process close to 1,000 files (and there is an upcoming project as well where I need to process a large amount of files). As you imagine, I have been processing those files individually (opening them one-by-one over a week at a time). I would imagine that there is a more efficient way of doing this, automating the process somehow…?

I’m currently learning Python, so I have tried to utilize some code from this post, Open and Close a 3DM File. With that code I can successfully open each file in a directory, but I am having a hard time saving any added features in it. I’m not sure what to do.

I would appreciate any and all help.

Thanks in advance!

1 Like

@dale or @alain - I would love to hear your thoughts. :slight_smile:

Is what I am trying to do even possible?

Mi Michael,

Edit: I think I was too quick to answer and my info might. Not be of much use. Why can’t you save the added geometry?

Maybe this can help you get started:

-Willem

Hi Willem,

Thank you for the quick reply. Your example helped me figure out a solution.

To answer your question:

Why can’t you save the added geometry?

I was not calling the active doc… :blush:.

This is what I ended up doing:

import rhinoscriptsyntax as rs
import System
import Rhino as rh
files = System.IO.Directory.GetFiles(
‘C:\Users\UserA\Desktop\test\’,
‘*.3dm’,
System.IO.SearchOption.AllDirectories)
if Run_Script:
for file in files:
rs.Command(‘-Open ’ + file)
rh.RhinoDoc.ActiveDoc.Objects.Add(Mesh1)
rh.RhinoDoc.ActiveDoc.Objects.Add(Mesh2)
rs.Command('
-Save _Enter’)

image

I simply plugged inputs in, formatted the code, and it works!

Thank you for the help.

-Mike

3 Likes