Changeset View
Changeset View
Standalone View
Standalone View
release/datafiles/blender_icons_update.py
| Show All 14 Lines | |||||
| # You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | ||||
| # along with this program; if not, write to the Free Software Foundation, | # along with this program; if not, write to the Free Software Foundation, | ||||
| # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| # | # | ||||
| # ##### END GPL LICENSE BLOCK ##### | # ##### END GPL LICENSE BLOCK ##### | ||||
| # <pep8 compliant> | # <pep8 compliant> | ||||
| # This script updates icons from the SVG file | """ | ||||
| This script updates icons from the source SVG files. | |||||
| """ | |||||
| import os | import os | ||||
| import subprocess | import subprocess | ||||
| import sys | import sys | ||||
| import argparse | |||||
| from glob import glob | |||||
| def run(cmd, *, env=None): | |||||
| print(" ", " ".join(cmd)) | |||||
| subprocess.check_call(cmd, env=env) | |||||
| BASEDIR = os.path.abspath(os.path.dirname(__file__)) | BASEDIR = os.path.abspath(os.path.dirname(__file__)) | ||||
| DATATOC = os.path.join(BASEDIR, "..", "..", "source", "blender", "datatoc") | |||||
| datatoc_icon_split_py = os.path.join(DATATOC, "datatoc_icon_split.py") | |||||
| datatoc_icon_split_to_png_py = os.path.join(DATATOC, "datatoc_icon_split_to_png.py") | |||||
| env = {} | env = {} | ||||
| # Developers may have ASAN enabled, avoid non-zero exit codes. | # Developers may have ASAN enabled, avoid non-zero exit codes. | ||||
| env["ASAN_OPTIONS"] = "exitcode=0:" + os.environ.get("ASAN_OPTIONS", "") | env["ASAN_OPTIONS"] = "exitcode=0:" + os.environ.get("ASAN_OPTIONS", "") | ||||
| # These NEED to be set on windows for python to initialize properly. | # These need to be set on windows for python to initialize properly. | ||||
| if sys.platform[:3] == "win": | if sys.platform[:3] == "win": | ||||
| env["PATHEXT"] = os.environ.get("PATHEXT", "") | env["PATHEXT"] = os.environ.get("PATHEXT", "") | ||||
| env["SystemDrive"] = os.environ.get("SystemDrive", "") | env["SystemDrive"] = os.environ.get("SystemDrive", "") | ||||
| env["SystemRoot"] = os.environ.get("SystemRoot", "") | env["SystemRoot"] = os.environ.get("SystemRoot", "") | ||||
| inkscape_bin = os.environ.get("INKSCAPE_BIN", "inkscape") | inkscape_bin = os.environ.get("INKSCAPE_BIN", "inkscape") | ||||
| blender_bin = os.environ.get("BLENDER_BIN", "blender") | blender_bin = os.environ.get("BLENDER_BIN", "blender") | ||||
| if sys.platform == 'darwin': | if sys.platform == 'darwin': | ||||
| inkscape_app_path = '/Applications/Inkscape.app/Contents/MacOS/inkscape' | inkscape_app_path = '/Applications/Inkscape.app/Contents/MacOS/inkscape' | ||||
| if os.path.exists(inkscape_app_path): | if os.path.exists(inkscape_app_path): | ||||
| inkscape_bin = inkscape_app_path | inkscape_bin = inkscape_app_path | ||||
| blender_app_path = '/Applications/Blender.app/Contents/MacOS/Blender' | blender_app_path = '/Applications/Blender.app/Contents/MacOS/Blender' | ||||
| if os.path.exists(blender_app_path): | if os.path.exists(blender_app_path): | ||||
| blender_bin = blender_app_path | blender_bin = blender_app_path | ||||
| cmd = ( | |||||
| inkscape_bin, | |||||
| os.path.join(BASEDIR, "blender_icons.svg"), | |||||
| "--export-width=602", | |||||
| "--export-height=640", | |||||
| "--export-type=png", | |||||
| "--export-filename=" + os.path.join(BASEDIR, "blender_icons16.png"), | |||||
| ) | |||||
| run(cmd, env=env) | |||||
| def create_argparse(): | |||||
| parser = argparse.ArgumentParser(description=__doc__) | |||||
| parser.add_argument("-a", "--all", action="store_true", | |||||
| help="Force rebuild all icons from SVG source") | |||||
| parser.add_argument("-e", "--export", action="store_true", | |||||
| help="Export source SVG to PNG files") | |||||
| parser.add_argument("-p", "--preview", action="store_true", | |||||
| help="Generate PNG from current .dat files") | |||||
| parser.add_argument("-b", "--build", action="store_true", | |||||
| help="Build .dat files from PNG files") | |||||
| parser.add_argument("-c", "--clean", action="store_true", | |||||
| help="Clean up generated PNG files") | |||||
| return parser | |||||
| def run(cmd, *, env=None): | |||||
| # print(" ", " ".join(cmd)) | |||||
| subprocess.check_call(cmd, env=env) | |||||
| def run_silent(cmd, *, env=None): | |||||
| subprocess.check_call(cmd, env=env, | |||||
| stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) | |||||
| def clean_dir(px, ext): | |||||
| for file in glob(os.path.join(BASEDIR, "blender_icons%s" % px, ext)): | |||||
| os.remove(file) | |||||
| def dat_to_png(px): | |||||
| file_list = glob(os.path.join(BASEDIR, "blender_icons%s" % px, "*.dat")) | |||||
| cmd = ( | cmd = ( | ||||
| inkscape_bin, | datatoc_icon_split_to_png_py, | ||||
| os.path.join(BASEDIR, "blender_icons.svg"), | *file_list, | ||||
| "--export-width=1204", | |||||
| "--export-height=1280", | |||||
| "--export-type=png", | |||||
| "--export-filename=" + os.path.join(BASEDIR, "blender_icons32.png"), | |||||
| ) | ) | ||||
| run(cmd, env=env) | run(cmd, env=env) | ||||
| # For testing it can be good to clear all old | def svg_to_png(px): | ||||
| # rm ./blender_icons16/*.dat | source_dir = os.path.join(BASEDIR, "blender_icons") | ||||
| # rm ./blender_icons32/*.dat | if not os.path.exists(source_dir): | ||||
| sys.exit("Directory not found: %r" % source_dir) | |||||
| datatoc_icon_split_py = os.path.join(BASEDIR, "..", "..", "source", "blender", "datatoc", "datatoc_icon_split.py") | |||||
| export_dir = os.path.join(BASEDIR, "blender_icons%s" % px) | |||||
| if not os.path.exists(export_dir): | |||||
| os.mkdir(export_dir) | |||||
| svg_file_list = glob(os.path.join(source_dir, "*.svg")) | |||||
| actions = "--actions=" | |||||
| for s in svg_file_list: | |||||
| n = os.path.basename(s).split(".")[0] | |||||
| p = os.path.join(export_dir, "icon%s_%s.png" % (px, n)) | |||||
| actions += "file-open:%s;" % s \ | |||||
| + "export-filename:%s;" % p \ | |||||
| + "export-width:%s;" % px \ | |||||
| + "export-height:%s;" % px \ | |||||
| + "export-type:png;" \ | |||||
| + "export-do;" | |||||
| # create .dat pixmaps (which are stored in git) | |||||
| cmd = ( | cmd = ( | ||||
| blender_bin, "--background", "--factory-startup", "-noaudio", | inkscape_bin, | ||||
| "--python", datatoc_icon_split_py, "--", | actions, | ||||
| "--image=" + os.path.join(BASEDIR, "blender_icons16.png"), | |||||
| "--output=" + os.path.join(BASEDIR, "blender_icons16"), | |||||
| "--output_prefix=icon16_", | |||||
| "--name_style=UI_ICONS", | |||||
| "--parts_x", "26", "--parts_y", "30", | |||||
| "--minx", "3", "--maxx", "53", "--miny", "3", "--maxy", "8", | |||||
| "--minx_icon", "2", "--maxx_icon", "2", "--miny_icon", "2", "--maxy_icon", "2", | |||||
| "--spacex_icon", "1", "--spacey_icon", "1", | |||||
| ) | ) | ||||
| run(cmd, env=env) | # subprocess.check_call(cmd, env=env, cwd=BASEDIR) | ||||
| run_silent(cmd, env=env) | |||||
| def png_to_dat(px): | |||||
| source_dir = os.path.join(BASEDIR, "blender_icons%s" % px) | |||||
| svg_dir = os.path.join(BASEDIR, "blender_icons") | |||||
| cmd = ( | cmd = ( | ||||
| blender_bin, "--background", "--factory-startup", "-noaudio", | blender_bin, "--background", "--factory-startup", "-noaudio", | ||||
| "--python", datatoc_icon_split_py, "--", | "--python", datatoc_icon_split_py, "--", | ||||
| "--image=" + os.path.join(BASEDIR, "blender_icons32.png"), | "--source-dir=" + source_dir, | ||||
| "--output=" + os.path.join(BASEDIR, "blender_icons32"), | "--output-dir=" + source_dir, | ||||
| "--output_prefix=icon32_", | "--file-prefix=icon%s_" % px, | ||||
| "--name_style=UI_ICONS", | "--svg-dir=" + svg_dir, | ||||
| "--parts_x", "26", "--parts_y", "30", | |||||
| "--minx", "6", "--maxx", "106", "--miny", "6", "--maxy", "16", | |||||
| "--minx_icon", "4", "--maxx_icon", "4", "--miny_icon", "4", "--maxy_icon", "4", | |||||
| "--spacex_icon", "2", "--spacey_icon", "2", | |||||
| ) | ) | ||||
| run(cmd, env=env) | run(cmd, env=env) | ||||
| os.remove(os.path.join(BASEDIR, "blender_icons16.png")) | |||||
| os.remove(os.path.join(BASEDIR, "blender_icons32.png")) | |||||
| # For testing, if we want the PNG of each image | def main(): | ||||
| # ./datatoc_icon_split_to_png.py ./blender_icons16/*.dat | parser = create_argparse() | ||||
| # ./datatoc_icon_split_to_png.py ./blender_icons32/*.dat | args = parser.parse_args() | ||||
| for px in (16, 32): | |||||
| if args.preview: | |||||
| dat_to_png(px) | |||||
| continue | |||||
| if args.export: | |||||
| svg_to_png(px) | |||||
| continue | |||||
| if args.build: | |||||
| # clean_dir(px, "*.dat") | |||||
| png_to_dat(px) | |||||
| continue | |||||
| if args.clean: | |||||
| clean_dir(px, "*.png") | |||||
| continue | |||||
| # Force rebuild all icons by clearing old files. | |||||
| if args.all: | |||||
| clean_dir(px, "*.dat") | |||||
| # Generate PNG from SVG source. | |||||
| svg_to_png(px) | |||||
| # Create .dat pixmaps which are stored in git. | |||||
| png_to_dat(px) | |||||
| # Remove temporary PNG files. | |||||
| clean_dir(px, "*.png") | |||||
| if __name__ == "__main__": | |||||
| main() | |||||