his script is designed to automate several tasks in Rhino, including setting all viewports to wireframe, purging unused items, saving files in a smaller format, and deleting backup files. The script also allows the user to save files in a different location with a timestamp appended to the filename.
Here’s a step-by-step summary of what the script does:
- The script first opens a dialog box asking the user to select a folder.
- The script then asks the user several questions about what tasks they want to perform. The user can choose whether to set all viewports to wireframe, purge unused items, save files in a smaller format, save files in a different location, and delete backup files.
- If the user chooses to save files in a different location, the script opens another dialog box asking the user to select a folder to save the files.
- The script then opens a log file in the selected folder to record the processing of each file.
- If a folder was selected, the script gets a list of all Rhino files in the folder and its subfolders.
- The script sorts the files by the date they were last modified in descending order.
- The script then loops through each file, performing the following tasks for each file:
- The script opens the file in Rhino.
- The script gets the current file path.
- If the document has a path, the script performs the following tasks:
- If the user chose to set all viewports to wireframe, the script sets all viewports to wireframe.
- If the user chose to purge unused items, the script purges unused items.
- If the user chose to save files in a smaller format, the script saves the document in a smaller format. If the user also chose to save files in a different location, the script saves the file in the selected folder with a timestamp appended to the filename. Otherwise, the script saves the file in its current location.
- The script writes the path of the processed file to the log file.
- The script opens a new document in Rhino.
- If the user chose to delete backup files, the script deletes all backup files in the selected folder and its subfolders.
The script is designed to be run as a command in Rhino.
import rhinoscriptsyntax as rs
import os
import glob
import datetime
# Open a dialog to choose a folder
folder = rs.BrowseForFolder(message="Select a folder")
# Ask the user for options
wireframe = rs.GetBoolean("Set all viewports to wireframe?", [("Wireframe", "Yes", "No")], [True])[0]
purge = rs.GetBoolean("Purge unused items?", [("Purge", "Yes", "No")], [True])[0]
delete_bak = rs.GetBoolean("Delete .3dmbak files?", [("Delete", "Yes", "No")], [True])[0]
save_small = rs.GetBoolean("Save small?", [("SaveSmall", "Yes", "No")], [True])[0]
save_as = rs.GetBoolean("Save as?", [("SaveAs", "Yes", "No")], [False])[0]
save_as_folder = None
if save_as:
save_as_folder = rs.BrowseForFolder(message="Select a folder to save files")
# Open a log file
with open(os.path.join(folder, "log.txt"), "w") as log_file:
# Check if a folder was selected
if folder:
# Get a list of all Rhino files in the folder and subfolders
files = [os.path.join(dirpath, file) for dirpath, dirnames, files in os.walk(folder) for file in files if file.endswith(".3dm")]
# Sort the files by modified date in descending order
files.sort(key=os.path.getmtime, reverse=True)
# Loop through each file
for file in files:
# Open the file
rs.Command("_-Open " + '"' + file + '"' + " _Enter", True)
# Get the current file path
current_path = rs.DocumentPath()
# Check if the document has a path
if current_path:
# Set all viewports to wireframe
if wireframe:
viewports = rs.ViewNames()
for viewport in viewports:
rs.ViewDisplayMode(viewport, mode="Wireframe")
# Purge unused items
if purge:
rs.Command("_-Purge _Enter", True)
# Save the document
if save_small:
if save_as and save_as_folder:
# Get the current date and time
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# Get the base file name
base_name = os.path.splitext(os.path.basename(current_path))[0]
# Create a new file name with the current date and time appended
new_file_name = base_name + "_" + now + ".3dm"
new_path = os.path.join(save_as_folder, new_file_name)
rs.Command("_-SaveAs " + '"' + new_path + '"' + " _Enter", True)
else:
rs.Command("_-SaveSmall " + '"' + current_path + '"' + " _Enter", True)
# Write to the log file
log_file.write("Processed file: " + current_path + "\n")
# Open a new document
rs.Command("_-New _None _Enter", True)
# Delete .3dmbak files
if delete_bak:
bak_files = [os.path.join(dirpath, file) for dirpath, dirnames, files in os.walk(folder) for file in files if file.endswith(".3dmbak")]
for bak_file in bak_files:
os.remove(bak_file)