A runtime registrations for a nodes: refactoring by py script #3

Closed
Iliya Katushenock wants to merge 3 commits from mod_moder-patch-1 into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

94
pars_files.py Normal file
View File

@ -0,0 +1,94 @@
from os import walk
lines = open(r"F:\Blender_build\first\blender\source\blender\nodes\NOD_static_types.h").readlines()
def nodes_functions(type):
result = []
for line in lines:
line = line.strip()
if line.startswith("DefNode(" + type):
args_line = line.removeprefix("DefNode(" + type).removesuffix(")")
args = args_line.split(",", -1)
if args[2].strip() != "0":
result.append({"ID" : args[1].strip(), "reg" : args[2].strip()})
return result
nodes = nodes_functions("GeometryNode")
#print(nodes)
directory = r"F:\Blender_build\first\blender\source\blender\nodes"
def node_files(type):
result = []
nodes_directory = directory + "\\" + type + "\\nodes"
for (dirpath, dirnames, filenames) in walk(nodes_directory):
for node_file in filenames:
result.append({"name" : node_file})
return {"directory" : nodes_directory, "files" : result}
files = node_files("geometry")
#print(files)
def file_by_node_id(files):
result = {}
for node in nodes:
node_id = node["ID"]
def id_in_file(file) :
data = open(files["directory"] + "\\" + file).read()
return node_id + "," in data
imp_files = [file for file in files["files"] if id_in_file(file["name"])]
if len(imp_files) == 1:
result[node_id] = {"File": imp_files[0]["name"]}
return result
node_files = file_by_node_id(files)
#print(node_files)
def node_reg_functions(nodes):
nodes_reg = open(r"F:\Blender_build\first\blender\source\blender\makesrna\intern\rna_nodetree.cc").read()
static_void_functions = nodes_reg.split("static void")
result = {}
for node in nodes:
node_id = node["ID"]
function_body = [func for func in static_void_functions if node["reg"] + "(" in func]
if len(function_body) == 1:
function = "static void" + function_body[0]
result[node["ID"]] = {"Function" : function}
return result
node_regs = node_reg_functions(nodes)
#print(result)
for node in nodes:
node_id = node["ID"]
node_reg_func_name = node["reg"]
node_file = node_files[node_id]["File"]
node_reg_func = node_regs[node_id]["Function"]
if "RNA_def_property_enum_funcs" in node_reg_func:
continue
impl_file_path = files["directory"] + "\\" + node_file
file = open(impl_file_path, 'r').read()
reg_func_to_rep = "static void node_register()"
node_reg_fixed = node_reg_func.replace(node_reg_func_name, "node_rna")
fixed_body = file.replace(reg_func_to_rep, node_reg_fixed + reg_func_to_rep)
node_reg_type = "nodeRegisterType(&ntype);"
fixed_decl = fixed_body.replace(node_reg_type, node_reg_type + '\n' + " node_rna(ntype.rna_ext.srna);")
node_add_include = "#include \"UI_interface.hh\""
fixed_include = fixed_decl.replace(node_add_include, "#include \"NOD_rna_define.hh\"" + '\n\n' + node_add_include)
node_update = open(impl_file_path, 'w')
node_update.write(fixed_include)
node_update.close()