Because not every cutout of the spatial data consists of all layers, I can’t just rename them with a list and need some sort of conditional statement, which I have a hard time scripting.
In my understanding the script would first need to query the layers, skip missing layers and rename all accordingly based on the list above, which I have as csv/txt file.
If you have the data sorted in a dictionary of the form d=[“key_string”, “value_string”],
where “key string” = layerName
And “value string” = targetLayerName
which is easy to make, it is as simple as looping through the layer names, and retrieving the “value_string” by passing the layer name →
targetLayerName = d [ layerName ]
Then you take that targetLayerName and pass it to the function that sets the layer’s name
If you upload a rhino file with no geometry but all the layer names, and the table with how the mapping should happen, we could help you further
i first converted your txt file to a csv
here it is: Geobau-Layerbezeichnung.csv (4.5 KB)
download it and make sure you paste its path into the script below
then you can run Tools → Script → Edit
make a new file - white icon on the left - select python3 (might be different on a mac)
paste this and hit play
"""
renames layers via csv file
Version: alpha 0.1
Disclaimer: use as is on your own responsibility
Author: Adel Albloushi @2024
"""
#! python3
import rhinoscriptsyntax as rs
import Rhino
# import csv
import csv
#create container for csv data
data = {}
#make sure you put your own path where you saved 'Geobau-Layerbezeichnung.csv'
filepath = 'D:\Desktop\Geobau-Layerbezeichnung.csv'
# read csv file to a list of dictionaries
with open(filepath, mode = 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
data = [row for row in reader]
#convert data into a dictionary
dictionary = {}
for row in data:
dictionary[row["layerName"]] = row["TargetName"]
#get layers
layerTable = Rhino.RhinoDoc.ActiveDoc.Layers
layerCount = layerTable.Count
#renaming loop
for i in range(layerCount):
#print(layerTable[i].Name)
try:
#try to see if layer is in the dictionary, rename on hit
layerTable[i].Name = dictionary[layerTable[i].Name]
except:
#if layer name is not in the dictionary , skip
continue
Wow, i didn’t expect that. Thanks a lot.
I guess the script has worked on your computer? On mine it does seem to run, but somehow i doesn’t rename the layers. The terminal puts out all the layer numbers, like the were before and does not change them.
What did I do wrong?
I recently wrote this for one of my modelmaker colleagues in Zürich. However, your file has a zero in front of the number. I did a quick mod of the script - seems to work on your file.
Off topic: Where and how did you guys learn to code for rhino? Although I think i’m not entirely unintelligent, I sometimes have the feeling my thought processes are so much different from how codes are structured, that I thought it was a bad investment of my time. Now I mainly worked with, sometimes over complicated, grasshopper scripts, which I can’t use anymore, since I had to switch to mac. I’m thinking to get back to learning python, since I would be able to automate and solve many things much easier and it would also work on both OS.
The first time I ever coded something was on rhino 3 maybe. Not sure about the version, but it was 2008. Back then rhino had VB script.
I picked it quite fast because I had some knowledge about mathematics.
I started coding more when I was doing my Masters in Switzerland, but that was mostly in Java.
After my Masters I learned how to code in c++ because I wanted to translate an idea I had into a plugin for Cinema 4D.
Around the same time I was really short on money so I learned how to design a website with HTML CSS PHP in JavaScript so I could deliver one to a client - and get paid.
With Python I started around 5 or 6 years ago when I was studying AI models.
Csharp I learned because of rhino. Before rhino had sub d, I had my own way of getting something similar. It wasn’t perfect and I had the problem with surface continuity, but it worked for what I needed it.
I never actually took any programming courses. All of my learning was project based - I wanted to do something and I researched online on how to do it.
It is true that the logic of an algorithm is sometimes counterintuitive. But once you have a feeling for how to break down a problem into algorithmic steps, it doesn’t even matter what programming language you are using.
Python is a great programming language, and I can assure you that once you know how to use it you will find a lot of areas in your own work that could be automated. For example the layer naming thing.