LearnRevitAPI
🌚Automate Dark Theme Icons for pyRevit with THIS Script
Dec 2, 2024
🌚Automate Dark Icons
As you know Revit has a dark theme. Some people love it, some hate it. But it certainly affects how our extensions look like.Your extension might not look good in dark theme if you use dark icons... So, let's solve this.
And being a developer, don't you even dare fixing it manually! Let's put our coding skills to work and create dark theme icons for all our tools with a single click.
Prepare project
We will use any python 3 version so we can use Pillow package. This is the most used package for Image manipulation with python.
If you use pyCharm, open Interpreter Settings and install pillow package. If you use different IDE, you would need to use pip install pillow via console.
💡 I recommend to create a new virtual environment so you don't clutter you main space. But you can also uninstall it later.
Convert icon.png
Next, add any icon as a test file in the same folder with your python script. Once we make the code work for one image, we will adjust to go through the whole extension.
Now place any icon.png next to your python file and let's write the following code:
Here is what's going on:
Open image with Image class from pillow library
Define Color to replace with
Load Image Data (list of pixels)
Replace visible pixels (Keep Transparency)
Save Updated Image as icon.dark.png
Refactor to a function
Before going further, let's convert our code into a function to make it easier to work with.
Get All Icons
Now we need to scale and find all icons in our extension. There are many ways to do that, I will use os.walk function.
Here is a simple function:
os.walk will provide:
root - the provided root path
dirs - all nested folders
files - list of all files in all nested folders
So we will use files and filter those ending with icon.png.
yield - creates a generator from the function. Instead of returning whole list at once, it will be giving one item at the time.
Create Dark Icons
And lastly, you can convert all your icons for the dark theme by calling these 2 functions.
And now congratulations. If you followed correctly, you should have dark-theme icons in pyRevit. Here is how my EF-Tools looks like:
P.S.
Don't forget to watch the full tutorial here:
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 7500+ people!