Hi @eirannejad ,
I’m having a little confusion about the way to reference modules from a main script within a Script Editor Code Project.
According to the docs here:
the structure in my Code Project Libraries for my module should be like this:
└───UIThemeColors # I guess this is the main "Module Name"?
├───__init__.py # Is this everything before the GetThemeColors Function?
└───GetThemeColors.py # This is the function logic of GetThemeColors() saved as a
seperate .py file?
where this is the full “UIThemeColors” module code below that I used to call in my main script from a local network path like this:
import sys
Add the directory containing FindAnything.py to the Python path
modules_path = r"pathtomylocalcodefolders\modules"
sys.path.append(modules_path)
from UIThemeColors import GetThemeColors
This would allow me to call the function “GetThemeColors” in my main script and get the returned values.
here’s an example of that code:
#! python3
# UIThemeColors.py
import Eto.Drawing
# Define color variables based on the theme
bg_color_light = Eto.Drawing.Color(10, 10, 10)
bg_color_dark = Eto.Drawing.Color(220, 220, 220)
mg_color_light = Eto.Drawing.Color(1, 1, 1)
mg_color_dark = Eto.Drawing.Color(240, 240, 240)
fg_color_light = Eto.Drawing.Color(1, 1, 1)
fg_color_dark = Eto.Drawing.Color(180, 180, 180)
b_color_light = Eto.Drawing.Color(40, 40, 40)
b_color_dark = Eto.Drawing.Color(240, 240, 240)
s_color_light = Eto.Drawing.Colors.LightGrey # Shadow Color To Replace Color Highlight When In Light Mode
s_color_dark = Eto.Drawing.Color(240, 240, 240)
e_color_light = Eto.Drawing.Color(20, 20, 20)
e_color_dark = Eto.Drawing.Color(250, 250, 250)
d_color_light = fg_color_light
d_color_dark = Eto.Drawing.Color(210, 210, 210)
def GetThemeColors(dark_mode):
if dark_mode:
# Define theme colors for dark mode
theme_colors = {
"bg_color": bg_color_dark,
"mg_color": mg_color_dark,
"fg_color": fg_color_dark,
"b_color": b_color_dark,
"s_color": s_color_dark,
"e_color": e_color_dark,
"d_color": d_color_dark,
"t_color": fg_color_light
}
else:
# Define theme colors for light mode
theme_colors = {
"bg_color": bg_color_light,
"mg_color": mg_color_light,
"fg_color": fg_color_light,
"b_color": b_color_light,
"s_color": s_color_light,
"e_color": e_color_light,
"d_color": d_color_light,
"t_color": fg_color_dark
}
return theme_colors
Do I need to split out the function GetThemeColors as it’s own .py file and put everything above that in the __init__.py
file of a folder called UIThemeColors
Like so?
Where __init__.py
becomes:
#! python3
import Eto.Drawing
# Define color variables based on the theme
bg_color_light = Eto.Drawing.Color(10, 10, 10)
bg_color_dark = Eto.Drawing.Color(220, 220, 220)
mg_color_light = Eto.Drawing.Color(1, 1, 1)
mg_color_dark = Eto.Drawing.Color(240, 240, 240)
fg_color_light = Eto.Drawing.Color(1, 1, 1)
fg_color_dark = Eto.Drawing.Color(180, 180, 180)
b_color_light = Eto.Drawing.Color(40, 40, 40)
b_color_dark = Eto.Drawing.Color(240, 240, 240)
s_color_light = Eto.Drawing.Colors.LightGrey # Shadow Color To Replace Color Highlight When In Light Mode
s_color_dark = Eto.Drawing.Color(240, 240, 240)
e_color_light = Eto.Drawing.Color(20, 20, 20)
e_color_dark = Eto.Drawing.Color(250, 250, 250)
d_color_light = fg_color_light
d_color_dark = Eto.Drawing.Color(210, 210, 210)
and GetThemeColors.py
becomes:
def GetThemeColors(dark_mode):
if dark_mode:
# Define theme colors for dark mode
theme_colors = {
"bg_color": bg_color_dark,
"mg_color": mg_color_dark,
"fg_color": fg_color_dark,
"b_color": b_color_dark,
"s_color": s_color_dark,
"e_color": e_color_dark,
"d_color": d_color_dark,
"t_color": fg_color_light
}
else:
# Define theme colors for light mode
theme_colors = {
"bg_color": bg_color_light,
"mg_color": mg_color_light,
"fg_color": fg_color_light,
"b_color": b_color_light,
"s_color": s_color_light,
"e_color": e_color_light,
"d_color": d_color_light,
"t_color": fg_color_dark
}
return theme_colors
so if i make a new command that has a script “Checks Current Theme Colors”
within that script then I can call:
from UIThemeColors import GetThemeColors
because it “sees” that in my Libraries/ folder of the ScriptEditor code project.
Is that correct?
Thanks for the help!
EDIT:
If I try to make a new test command called “UIThemeTest” like so:
#! python3
from UIThemeColors import GetThemeColors
colors = GetThemeColors()
print(colors)
I get this error when running the command in Rhino:
Command: UIThemeTest
Traceback (most recent call last):
File "file:///C:/Users/micha/.rhinocode/stage/ttge5hr1.aue", line 3, in <module>
ImportError: cannot import name 'GetThemeColors' from 'UIThemeColors' (unknown location)
Error occured running command "7d5ae468-2482-4170-8fbd-1d9d2ee54fbc" | Traceback (most recent call last):
File "file:///C:/Users/micha/.rhinocode/stage/ttge5hr1.aue", line 3, in <module>
ImportError: cannot import name 'GetThemeColors' from 'UIThemeColors' (unknown location)