From BlenderWiki
[edit] Adding a Theme Colour
Adding a new colour to Blender's GUI theme system is quite easy, but involves a few steps. I've catalogued them here as a quick walk-through. In this example I'm adding theme colours for the various types of sequence editor strips.
[edit] 1. Define names for the theme colours
Here we need to give the theme colours defined names so we can refer to them in our code.
source/blender/include/BIF_resources.h
Add to the end of enum { TH_AUTO, TH_BUT_OUTLINE, ... } your new theme colours, following the same naming scheme. Colours that are used in the GUI (i.e. consistent across window space types) should be added before the TH_THEMEUI define. Other window space specific colours should be added at the end.
TH_NODE_GROUP,
TH_SEQ_MOVIE,
TH_SEQ_IMAGE,
TH_SEQ_SCENE,
...
[edit] 2. Create variables to store the colours in
source/makesdna/DNA_userdef_types.h
This file contains the structs that get saved in SDNA, to the .B.blend user settings file. You need to add variables here to store the theme colours in when they are saved in the file. If the the colour you're adding is a cross-space UI theme colour, add the necessary variables to struct ThemeUI, otherwise add them to struct ThemeSpace.
You will need to follow the DNA struct padding rules here.
char movie[4], image[4], scene[4], audio[4]; // for sequence editor
...
[edit] 3. Set up theme API and define colour values for the default theme
source/src/resources.c
BIF_ThemeGetColorPtr() is the function that finds the right colour values in the User Preferences struct based on the theme colour name. Find BIF_ThemeGetColorPtr() and add in the appropriate code, referencing the variables you just made in DNA_userdef_types.h . Again, refer to the existing code, and add your theme colours in the same places, depending on whether they are UI theme colours or window space theme colours.
case TH_SEQ_MOVIE:
cp= ts->movie; break;
case TH_SEQ_IMAGE:
cp= ts->image; break;
...
BIF_InitTheme() defines the values of the new theme colours in the Default theme. Find the correct place there to add your new colours:
/* space seq */
btheme->tseq= btheme->tv3d;
SETCOL(btheme->tseq.back, 116, 116, 116, 255);
SETCOL(btheme->tseq.movie, 94, 165, 92, 255);
SETCOL(btheme->tseq.image, 39, 127, 137, 255);
...
BIF_ThemeColorsPup() generates the theme colour menu in the user preferences window. You also need to add your theme colour names here so users can select and edit the theme colours themselves. Find the appropriate place and add in your new names.
case SPACE_SEQ:
str += sprintf(str, "Grid %%x%d|", TH_GRID);
str += sprintf(str, "Window Sliders %%x%d|", TH_SHADE1);
str += sprintf(str, "%%l|");
str += sprintf(str, "Movie Strip %%x%d|", TH_SEQ_MOVIE);
str += sprintf(str, "Image Strip %%x%d|", TH_SEQ_IMAGE);
...
You can use
str += sprintf(str, "%%l|");
to insert a separator in the menu.
[edit] 4. Set default values for the theme colours, for when opening an old .blend file
Old .blend files (mainly the .B.blend) saved from before the theme colour was added won't have any data for the new theme colours in its DNA.
source/src/usiblender.c
init_userdef_file() initialises the UserDef struct, which is where the theme colours are stored. You will need to initialise old files with default data if they're older than the current release. So if the next release (that will contain your new theme colours) is version 2.42, then search for the section that says:
if (G.main->versionfile <= 241) {
If your theme colour does not store alpha, then a good way to check that the the colour doesn't exist in the file is to check to see if alpha = 0 (if it doesn't use alpha, alpha should be set to 255 by default). These snippet of code will go through the themes stored in the .blend file and initialise the new values:
bTheme *btheme;
for (btheme= U.themes.first; btheme; btheme= btheme->next) {
/* Sequence editor theme, check for alpha==0 is safe, then color was never set */
if(btheme->tseq.movie[3]==0) {
SETCOL(btheme->tseq.movie, 94, 165, 92, 255);
SETCOL(btheme->tseq.image, 39, 127, 137, 255);
...
}
}
Of course you can integrate this into any existing code that is there to handle the same thing.
[edit] 5. Make exceptions for special theme values
If your theme colour uses values that aren't the usual RGB triplet you'll need to make an exception for them in the theme UI display code, to show users the correct controls needed to manipulate the values. This is necessary if, for example, the theme colour involves a user-defined alpha value, or if it's controlling an number value, such as the vertex point size, or if it's a choice between multiple options, such as the UI theme's draw type.
source/src/space.c
Make the relevant modifications in info_user_themebuts(). You can see examples of other exceptions underneath the line marked with
/* first handle exceptions, special single values, row selection, etc */
If you just want to enable access to the alpha value, add your theme colour name to the list in
if ELEM7(th_curcol, TH_PANEL, TH_LAMP, ...
And use the relevant ELEM() macro for the amount of names.
[edit] 6. Update the Python API
Python has access to the theme colours, in order to load and save themes via scripts. These should be updated too. If you're adding new UI theme colours you will need to edit ThemeUI_getAttr() and ThemeUI_setAttr(). In this example, we're adding theme colours for the sequence editor, so we need to add it in ThemeSpace_getAttr() and ThemeSpace_setAttr() instead. The procedure is just about identical for either.
python/api2_2x/windowTheme.c
In ThemeSpace_getAttr(), add entries for the new theme colours, using the same names as the variables created in DNA_userdef_types.h. If they're just simple RGB colours, they should be added before any of the "else if" lines that deal with single number values.
...
ELSEIF_TSP_RGBA( movie )
ELSEIF_TSP_RGBA( image )
...
You also seem to need to add an extra amount of 's' in the line
attrib = Py_BuildValue("[sssssssssssssssssssssssssssssssssss]", "theme", ...
for how many new items you are adding, and also add your variables to that function in the same positions as you added them above.
... "movie", "image", ... );
Then edit ThemeSpace_setAttr() as well. You can just copy and paste the ELSEIF_TSP_RGBA lines from above into the same position here.
python/api2_2x/doc/Theme.py
Also update the documentation file with the new variables in the same locations in either class ThemeUI or class ThemeSpace
[edit] 7. Use the theme colour in your code!
To set the current OpenGL colour to your theme colour use the function:
void BIF_ThemeColor(int colorid);
as
BIF_ThemeColor(TH_SEQ_MOVIE);
With whatever theme colour name you like. In BIF_resources.h, you can also see other functions such as:
void BIF_ThemeColorShade(int colorid, int offset);
Sets the current OpenGL colour to the theme colour, but darkened or lightened by the specified amount (offset)
void BIF_ThemeColorShadeAlpha(int colorid, int coloffset, int alphaoffset);
Sets the current OpenGL colour to the theme colour, darkened or lightened by the specified amount (offset) and with less or more alpha (alphaoffset)
void BIF_ThemeColorBlend(int colorid1, int colorid2, float fac);
Sets the current OpenGL colour to a blend between two theme colours (colorid1, colorid2), blended with a specified proportion of the first to the second (fac)
void BIF_ThemeColorBlendShade(int colorid1, int colorid2, float fac, int offset);
Sets the current OpenGL colour to a blend between two theme colours (colorid1, colorid2), blended with a specified proportion of the first to the second (fac), and darkened or lightened by a specified amount (offset)
There are more theme colour functions, to return the colour's actual numeric values rather than setting it as the current OpenGL drawing colour, and to get a theme colour from a specified window space type. See source/include/BIF_resources.h for all of the available functions you can use.







![[]](/skins/blender/open.png)
