Data history

python

(B Cochin) #1

Hi,
I have many backup files that I saved almost every day for a year. I recorded a lot of data as UserTexts on geometries. I’d like to build a script that could read every file and access UserTexts to see if and when it has been changed.
Is there a way to loop on the rhino files of a windows directory using python and then read UserTexts and store them in a variable?
Did anybody try to do something similar ?
Thanks


(Willem Derks) #2

Hi B Cohin,

The script below might get you started.
It reads all rhino files in a choosen foldertree and prints the usertext key-value for all objects in the files:

import Rhino
import rhinoscriptsyntax as rs

import os


def check_file_usertexts(filepath):
    rhinofile = Rhino.FileIO.File3dm.Read(filepath)
    
    objecttable = rhinofile.Objects
    
    for objref in objecttable:
        obj_id = objref.Attributes.ObjectId
        obj_name = objref.Attributes.Name
        keys = objref.Attributes.GetUserStrings()
        if len(keys) == 0: continue
        print 'object {} named {} in {} :'.format(obj_id,obj_name,filepath)
        for key in keys:
            print key,' -> ',objref.Attributes.GetUserString(key)
    
def walk_directory(directory):
    
    for root, directories, filenames in os.walk(directory):
        for filename in filenames:
            name, extension = os.path.splitext(filename)
            if extension == '.3dm':
                filepath = os.path.join(root,filename)
                check_file_usertexts(filepath)
    

if __name__ == '__main__':
    
    directory = rs.BrowseForFolder()
    if directory:
        walk_directory(directory)

Does this help?
-Willem


(B Cochin) #3

Hi Willem,
Thank you, this is just what I needed.
I can read a list of Rhino file without opening them and store what is inside into my script. I just need to think of a better algorythm now to process the data.
Thank you