Open and close a 3dm file with python on mac


#1

I would like to create a script that opens a 3dm file, performs a simple operation (add a box), save the model and close the model. This should be repeated for a list of files, that are in different paths.

I tried to use this command to open the file but it doesn’t work:

rhinoFile = '/my/path/file.3dm'
rs.Command('_-Open' + rhinoFile + ' _-Enter')

Any suggestions?

Thanks in advance
M


(Dale Fugier) #2

@Alain, is this something you can help with?


(Alain Cormier) #3

Hi @dale,

Of course.

@matteo_ledri is this what you need?

import rhinoscriptsyntax as rs
import System

# hard coded list of files
files = ['C:\\Users\\rhino\\Documents\\__file1.3dm',
    'C:\\Users\\rhino\\Documents\\__file2.3dm',
    'C:\\Users\\rhino\\Documents\\__file3.3dm']

# or all files in a common root directory
files = System.IO.Directory.GetFiles(
    'C:\\Users\\rhino\\Documents\\filestoedit', 
    '*.3dm', 
    System.IO.SearchOption.AllDirectories)

for file in files:
    rs.Command('_-Open ' + file)
    rs.AddBox([(-5,-5,-5), (5,-5,-5), (5,5,-5), (-5,5,-5), (-5,-5,5), (5,-5,5), (5,5,5), (-5,5,5)])
    print "file:{0}".format(file)
    rs.Command('_-Save _Enter')

rs.Command('_-New None')

#4

Hi Alain,

Does this work on Mac? Last time I tried to script something that would batch process files on Mac, I didn’t manage because of the Mac’s MDI, when you hit “New”, it opens a new window, and does not close the previous file…

–Mitch


(Alain Cormier) #5

On the Mac! Right. I didn’t have my coffee yet. Let me try that …


#6

Yeah, I need some too… :smile:


(Alain Cormier) #7

You’re right. I’ll have to experiment a bit …


(Alain Cormier) #8

I couldn’t get Rhino to run the script on the Mac. You say you need a simple operation so perhaps you don’t need to do it in Rhino and can get by with the stand alone Rhino3dmIO build of RhinoCommon. After the build add the directory that contains the binaries (mine in case that helps) to the path (like in the python script below) and that should work. There are many File3dm.Objects.AddXXX(…) methods but unfortunately AddBox isn’t one that’s included in Rhino3dmIO so you’ll need to find an alternative.

Here’s an example script:

import sys
import clr
from System.IO import *

# directory where Rhino3dmIO.dll and rhino3dmio_native are located
sys.path.append('/Users/rhino/opennurbsbin')
clr.AddReference('Rhino3dmIO')

from Rhino.FileIO import *
from Rhino.Geometry import *

file_names = Directory.GetFiles(
    '/Users/rhino/Documents/filestoedit', 
    '*.3dm', SearchOption.AllDirectories)

for fn in file_names:
    file = File3dm.Read(fn)
    id = file.Objects.AddSphere(Sphere(Point3d.Origin, 5.0))
    print "id:{0} added to file:{1}".format(id, fn)
    file.Write(fn, File3dmWriteOptions())

run the script at the command line

ipy the-script.py

I hope this helps


#9

Yeah, this is one of the sticky points with Mac and it’s MDI - at least I assume that’s where the problem is - I don’t know how difficult it will be to adapt it to do batch file processing, which is asked for often on the forum. The script is running inside one window of Rhino (there could be many) and as far as I know, you cannot close the open file without closing the window, which also means aborting the script… Opening a file opens it in a new window, which not the one in which the script is currently running. Catch-22.

One workaround I can think of (haven’t tried it though, no Mac here today):

  • Open a new blank file
  • Run the script
  • Use Import in the script to import the first file in the directory*
  • Do your stuff
  • Use Export to export the file with a different name in the same directory
  • Get all the geometry in the file and Delete/Purge
  • Import the next file in the directory
  • Etc.

*The only thing i don’t remember is if Import also opens in a new window… Seems like it did at some time in the past, and if so, then the whole procedure above still won’t work… :grimacing:

–Mitch


#10

Thanks a lot for the help. I will try on windows, waiting for a solution for the Mac.