Python testing in Rhino

Hi Gijs.

I composed a setup to process all 3dm files in a folder and subfolders
gijstest.zip (16.2 KB)

the script is in the zip as well:

# coding=utf-8

import os
import rhinoscriptsyntax as rs
import scriptcontext as sc



def count_parts():
    # method returning a sting for the report
    return len(rs.AllObjects())
    

def process_file(filepath):
    """
    open the file and create report string
    write report string to txt file with same name as file
    """
    
    #create filepath for report
    base_filepath = os.path.splitext(filepath)[0]
    report_filepath = base_filepath+'.txt'
    
    
    #open the file
    sc.doc.Modified = False
    rs.Command('_-Open "{}" _Enter'.format(filepath))
    
    
    
    #collect data
    units = sc.doc.ModelUnitSystem
    count = count_parts()
    
    
    #costruct report string
    report = ''
    report += 'Units : {}'.format(units)
    report += '\n'
    report += 'object count : {}'.format(count)
    
    #write report to file
    with open(report_filepath, 'w','utf-8') as f: 
        f.write(report) 


def report_folders(this_dir):
    
    
    filepaths = []
    #collect all 3dm filepaths in all forlder sand subfolders
    for root, dirs, files in os.walk(this_dir):
        for file in files:
            extension = os.path.splitext(file)[1]
            if extension.lower() == '.3dm':
                filepath = os.path.join(root,file)
                filepaths.append(filepath)
    
    #process all files found
    for filepath in filepaths:
        process_file(filepath)

    #Open new empty file
    sc.doc.Modified = False
    rs.Command('_-New  _Enter')



if __name__ == '__main__':
    
    #this_dir is the directory where the script is located
    #you could aso choose for a rs.BrowseForFolder
    this_dir = os.path.dirname( __file__ )
    report_folders(this_dir)

Good luck!
Groeten
-Willem

1 Like