Spaces:
Sleeping
Sleeping
| import re | |
| import colorsys | |
| import json | |
| import pandas as pd | |
| def colorStringProcessRGB(color_str): | |
| color_str = color_str.strip() | |
| if color_str.startswith("#"): | |
| color_str = color_str[1:] | |
| if len(color_str) == 3: | |
| r = int(color_str[0]*2, 16) | |
| g = int(color_str[1]*2, 16) | |
| b = int(color_str[2]*2, 16) | |
| return r, g, b | |
| elif len(color_str) == 6: | |
| r = int(color_str[0:2], 16) | |
| g = int(color_str[2:4], 16) | |
| b = int(color_str[4:6], 16) | |
| return r, g, b | |
| else: | |
| raise ValueError("Invalid HEX color format") | |
| match00 = re.match(r"rgb\((\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*)\)", color_str) | |
| if match00 is not None: | |
| r = round(float(match00.group(1))) | |
| g = round(float(match00.group(2))) | |
| b = round(float(match00.group(3))) | |
| return r, g, b | |
| match00 = re.match(r"rgba\((\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*)\)", color_str) | |
| if match00 is not None: | |
| r = round(float(match00.group(1))) | |
| g = round(float(match00.group(2))) | |
| b = round(float(match00.group(3))) | |
| return r, g, b | |
| match00 = re.match(r"hsl\((\s*[0-9\.]+\s*),(\s*[0-9\.]+%\s*),(\s*[0-9\.]+%\s*)\)", color_str) | |
| if match00 is not None: | |
| h = float(match00.group(1)) | |
| s = float(match00.group(2).replace("%","")) | |
| l = float(match00.group(3).replace("%","")) | |
| r, g, b = colorsys.hls_to_rgb(h/360.0, l/100.0, s/100.0) | |
| r = round(r * 255) | |
| g = round(g * 255) | |
| b = round(b * 255) | |
| return r, g, b | |
| raise ValueError("Invalid color string format") | |
| def cdiParametersListFileGet(input_path, output_path): | |
| with open(input_path, "r", encoding="utf8") as file: | |
| data_raw = json.load(file) | |
| data_result = cdiParametersListGet(data_raw) | |
| with open(output_path, "w", encoding="utf8") as file: | |
| json.dump(data_result, file, ensure_ascii=False) | |
| return 0 | |
| def cdiParametersListGet(data_raw): | |
| data_result = {} | |
| for param in data_raw["Parameters"]: | |
| pid = param["Id"] | |
| pnom = param["Name"] | |
| if pid in data_result: | |
| raise ValueError(f"Duplicate parameter ID found: {pid}") | |
| else: | |
| data_result[pid] = {"Name": pnom} | |
| return data_result | |
| def cdiParametersGroupsDataFileGet(input_path, output_path): | |
| with open(input_path, "r", encoding="utf8") as file: | |
| data_raw = json.load(file) | |
| data_result = cdiParametersGroupsDataGet(data_raw) | |
| with open(output_path, "w", encoding="utf8") as file: | |
| json.dump(data_result, file, ensure_ascii=False) | |
| return 0 | |
| ## 目前仅处理单层分组 | |
| def cdiParametersGroupsDataGet(data_raw): | |
| data_result = {} | |
| for group in data_raw["ParameterGroups"]: | |
| gid = group["Id"] | |
| gnom = group["Name"] | |
| data_result[gid] = {"Name":gnom, "Parameters":{}} | |
| for param in data_raw["Parameters"]: | |
| pid = param["Id"] | |
| pnom = param["Name"] | |
| pgid = param["GroupId"] | |
| if pgid in data_result: | |
| data_result[pgid]["Parameters"][pid] = {"Name": pnom} | |
| else: | |
| raise ValueError(f"unknown group ID : {pgid}") | |
| return data_result | |
| def parametersListTextGet(parameters_list_dict): | |
| df = pd.DataFrame(([id, parameters_list_dict[id]["Name"]] for id in parameters_list_dict), columns=["id", "name"]) | |
| df["text"] = df.apply(lambda row : row["id"] + " : " + row["name"], axis=1) | |
| return df | |
| def parameterPresetDataFramePreviewGet(parameter_preset_dict): | |
| result_list = [] | |
| text_list = [] | |
| for ppid, ppdata in parameter_preset_dict.items(): | |
| text_list.clear() | |
| for pid in ppdata: | |
| pname = ppdata[pid]["Name"] | |
| text_list.append(f"{pid} : {pname}") | |
| result_list.append([ppid, text_list.copy()]) | |
| df = pd.DataFrame(result_list, columns=["预设名称", "参数列表"]) | |
| return df | |
| def expressionParametersMigration(parameters_list: list|dict, data_acceptor, data_donor): | |
| data_result = data_acceptor.copy() | |
| pdata_acceptor = dict((prm["Id"],prm) for prm in data_acceptor["Parameters"]) | |
| pdata_donor = dict((prm["Id"],prm) for prm in data_donor["Parameters"]) | |
| pdata_result = pdata_acceptor.copy() | |
| for parameter in parameters_list: | |
| if parameter in pdata_donor: | |
| pdata_result[parameter] = pdata_donor[parameter] | |
| else: | |
| if parameter in pdata_result: | |
| del pdata_result[parameter] | |
| parameters_list_result = list((pdata for pid, pdata in pdata_result.items())) | |
| data_result["Parameters"] = parameters_list_result | |
| return data_result | |
| def expressionParametersMigrationByGroups(parameters_groups: dict, data_acceptor, data_donor): | |
| parameters_list = [] | |
| for gid, gdata in parameters_groups.items(): | |
| for param in gdata["Parameters"]: | |
| if param not in parameters_list: | |
| parameters_list.append(param) | |
| data_result = expressionParametersMigration(parameters_list, data_acceptor, data_donor) | |
| return data_result | |
| ## 引用: | |
| ## gr.Markdown("新表情的参数会覆盖原有的同名参数,目前有『迁移』『叠加』两种操作。<br>『迁移』和之前一样会删掉参考列表中原表情有而新表情没有的参数,『叠加』则不会。<br>当参数列表包含所有相关参数时,其『迁移』结果新表情将会完全覆盖原表情,『叠加』结果则类似于VTS的表情叠加。") | |
| ## 起名可能不大准确,总之就是这么回事。 | |
| def expressionParametersOverlay(parameters_list: list|dict, data_acceptor, data_donor): | |
| data_result = data_acceptor.copy() | |
| pdata_acceptor = dict((prm["Id"],prm) for prm in data_acceptor["Parameters"]) | |
| pdata_donor = dict((prm["Id"],prm) for prm in data_donor["Parameters"]) | |
| pdata_result = pdata_acceptor.copy() | |
| for parameter in parameters_list: | |
| ## 就是没有else部分 | |
| if parameter in pdata_donor: | |
| pdata_result[parameter] = pdata_donor[parameter] | |
| parameters_list_result = list((pdata for pid, pdata in pdata_result.items())) | |
| data_result["Parameters"] = parameters_list_result | |
| return data_result | |
| def expressionParametersOverlayByGroups(parameters_groups: dict, data_acceptor, data_donor): | |
| parameters_list = [] | |
| for gid, gdata in parameters_groups.items(): | |
| for param in gdata["Parameters"]: | |
| if param not in parameters_list: | |
| parameters_list.append(param) | |
| data_result = expressionParametersOverlay(parameters_list, data_acceptor, data_donor) | |
| return data_result | |
| ###### | |
| #color_str = "rgb(190.71875, 42.242530153508774, 42.242530153508774)" | |
| #color_str = "rgba(190.71875, 42.242530153508774, 42.242530153508774, 1)" | |
| #r, g, b = colorStringProcessRGB(color_str) | |
| #print(r, g, b) | |
| #cdiParametersListFileGet("data/general_model_Juumon/general_model_Juumon.cdi3.json", "data/cdiParametersList.json") | |
| #cdiParametersGroupsDataFileGet("data/general_model_Juumon/general_model_Juumon.cdi3.json", "data/cdiParametersGroupsData.json") | |
| #with open("data/test.exp3.json", "r", encoding="utf8") as file: | |
| # data = json.load(file) | |
| #expressionParametersMigration(["Param2", "HC82"], data, {"Parameters":[]}) | |