Skip to content

Instantly share code, notes, and snippets.

@rob5300
Created August 29, 2021 12:22
Show Gist options
  • Save rob5300/5b1a676500a21e4274dac1e76a57f191 to your computer and use it in GitHub Desktop.
Save rob5300/5b1a676500a21e4274dac1e76a57f191 to your computer and use it in GitHub Desktop.
A script to extract all detault tint colours from VMTs and write out the data in json.
import os
import re
import json
# A script to extract all detault tint colours from VMTs
# Outputs a json file with all values, vmtname: defaultcolour.
# Where your VMT files are stored. This is relative to the scripts path.
vmtFolder = "_SourceVMT"
target_path = os.path.join(os.path.dirname(__file__), vmtFolder)
logfilename = r"vmtcoloursoutput"
param1 = "___$color2"
param2 = "$colortint_base"
num_regex = "([^_, ,\D][0-9]{1,3})"
logOutput = ""
jsonOutput = {}
def LogToFile():
global logOutput
files = os.listdir(target_path)
printed_tag = False
for file in files:
printed_tag = False
print("Checking " + file)
filehandle = open(target_path + "\\" + file, mode='r')
file_text = filehandle.readlines()
for line in file_text:
if param1 in line or param2 in line:
if "255 255 255" not in line and "{" in line and "}" in line and "resultVar" not in line and "srcVar" not in line:
printed_tag = TryTag(printed_tag, file)
logOutput += line
hex = GetHexColour(line)
logOutput += f"RGB Hex: {hex}\n"
print(f" {file} had a colour value of {hex}, writing to file")
AddToJSON(os.path.basename(file), hex)
if printed_tag:
logOutput += "\n"
filehandle.close()
if(logOutput != ""):
WriteFile()
if(jsonOutput != {}):
WriteJSON()
print("Done!")
def GetHexColour(line):
toreturn = ""
result = re.findall(num_regex, line)
for r in result:
toreturn += hex(int(r)).replace("0x", "")
return toreturn
def WriteFile():
print("Writing file")
logfile = open(f"{os.path.dirname(__file__)}\\{logfilename}.txt", "w+")
logfile.write(logOutput)
logfile.close()
def TryTag(printed_tag, file):
global logOutput
if not printed_tag:
logOutput += os.path.basename(file) + "\n"
return True
else:
return False
def AddToJSON(vmt, colour):
vmt = vmt.replace(".vmt", "")
jsonOutput[vmt] = colour;
def WriteJSON():
print("Writing json file")
jsonfile = open(f"{os.path.dirname(__file__)}\\{logfilename}.json", "w+")
jsonfile.write(json.dumps(jsonOutput))
jsonfile.close()
if __name__ == "__main__":
LogToFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment