2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2020-01-28 11:47:29 +01:00
|
|
|
extends Node
|
2019-07-31 20:53:00 +02:00
|
|
|
|
2019-10-07 20:41:57 +02:00
|
|
|
const STD_GENDEF_PATH = "res://addons/material_maker/nodes"
|
|
|
|
|
2020-02-15 11:17:41 +01:00
|
|
|
var predefined_generators = {}
|
2020-02-13 08:37:42 +01:00
|
|
|
|
|
|
|
func _ready()-> void:
|
2020-02-15 11:17:41 +01:00
|
|
|
update_predefined_generators()
|
|
|
|
|
|
|
|
func update_predefined_generators()-> void:
|
|
|
|
predefined_generators = {}
|
2020-02-13 08:37:42 +01:00
|
|
|
for path in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]:
|
|
|
|
var dir = Directory.new()
|
|
|
|
if dir.open(path) == OK:
|
|
|
|
dir.list_dir_begin()
|
|
|
|
var file_name = dir.get_next()
|
|
|
|
while file_name != "":
|
|
|
|
if !dir.current_is_dir() and file_name.get_extension() == "mmg":
|
|
|
|
var file : File = File.new()
|
|
|
|
if file.open(path+"/"+file_name, File.READ) == OK:
|
2020-02-15 11:17:41 +01:00
|
|
|
predefined_generators[file_name.get_basename()] = parse_json(file.get_as_text())
|
2020-02-13 08:37:42 +01:00
|
|
|
file.close()
|
|
|
|
file_name = dir.get_next()
|
|
|
|
|
2020-01-28 11:47:29 +01:00
|
|
|
func generator_name_from_path(path : String) -> String:
|
2019-10-12 07:10:25 +02:00
|
|
|
for p in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]:
|
|
|
|
print(p)
|
|
|
|
print(path.get_base_dir())
|
|
|
|
return path.get_basename().get_file()
|
|
|
|
|
2020-01-28 11:47:29 +01:00
|
|
|
func load_gen(filename: String) -> MMGenBase:
|
2019-07-31 20:53:00 +02:00
|
|
|
var file = File.new()
|
|
|
|
if file.open(filename, File.READ) == OK:
|
|
|
|
var data = parse_json(file.get_as_text())
|
|
|
|
return create_gen(data)
|
|
|
|
return null
|
|
|
|
|
2020-01-28 11:47:29 +01:00
|
|
|
func add_to_gen_graph(gen_graph, generators, connections) -> Dictionary:
|
2019-08-15 11:53:21 +02:00
|
|
|
var rv = { generators=[], connections=[] }
|
2019-09-17 08:12:53 +02:00
|
|
|
var gennames = {}
|
2019-08-15 11:53:21 +02:00
|
|
|
for n in generators:
|
|
|
|
var g = create_gen(n)
|
|
|
|
if g != null:
|
2019-09-17 08:12:53 +02:00
|
|
|
var orig_name = g.name
|
2019-11-22 07:47:45 +01:00
|
|
|
if gen_graph.add_generator(g):
|
|
|
|
rv.generators.append(g)
|
2019-09-29 10:07:32 +02:00
|
|
|
gennames[orig_name] = g.name
|
2019-08-15 11:53:21 +02:00
|
|
|
for c in connections:
|
2019-09-17 08:12:53 +02:00
|
|
|
if gennames.has(c.from) and gennames.has(c.to):
|
|
|
|
c.from = gennames[c.from]
|
|
|
|
c.to = gennames[c.to]
|
2019-10-19 01:30:25 +02:00
|
|
|
if gen_graph.connect_children(gen_graph.get_node(c.from), c.from_port, gen_graph.get_node(c.to), c.to_port):
|
|
|
|
rv.connections.append(c)
|
2019-08-15 11:53:21 +02:00
|
|
|
return rv
|
|
|
|
|
2020-01-28 11:47:29 +01:00
|
|
|
func create_gen(data) -> MMGenBase:
|
2019-10-07 20:41:57 +02:00
|
|
|
var guess = [
|
|
|
|
{ keyword="connections", type=MMGenGraph },
|
|
|
|
{ keyword="nodes", type=MMGenGraph },
|
|
|
|
{ keyword="shader_model", type=MMGenShader },
|
2019-11-04 07:58:17 +01:00
|
|
|
{ keyword="convolution_params", type=MMGenConvolution },
|
2019-10-07 20:41:57 +02:00
|
|
|
{ keyword="model_data", type=MMGenShader },
|
|
|
|
{ keyword="convolution_params", type=MMGenConvolution },
|
|
|
|
{ keyword="widgets", type=MMGenRemote }
|
|
|
|
]
|
|
|
|
var types = {
|
|
|
|
material = MMGenMaterial,
|
|
|
|
buffer = MMGenBuffer,
|
|
|
|
image = MMGenImage,
|
2019-11-04 07:58:17 +01:00
|
|
|
ios = MMGenIOs,
|
2019-11-02 08:43:54 +01:00
|
|
|
switch = MMGenSwitch,
|
|
|
|
export = MMGenExport,
|
2019-11-04 07:58:17 +01:00
|
|
|
comment = MMGenComment,
|
2019-11-02 08:43:54 +01:00
|
|
|
debug = MMGenDebug
|
2019-10-07 20:41:57 +02:00
|
|
|
}
|
2019-07-31 20:53:00 +02:00
|
|
|
var generator = null
|
2019-11-04 07:58:17 +01:00
|
|
|
for g in guess:
|
|
|
|
if data.has(g.keyword):
|
|
|
|
generator = g.type.new()
|
|
|
|
break
|
|
|
|
if generator == null and data.has("type"):
|
2019-11-02 08:43:54 +01:00
|
|
|
if types.has(data.type):
|
|
|
|
generator = types[data.type].new()
|
2020-02-28 08:41:17 +01:00
|
|
|
elif predefined_generators.has(data.type):
|
2020-02-15 11:17:41 +01:00
|
|
|
generator = create_gen(predefined_generators[data.type])
|
2019-10-07 20:41:57 +02:00
|
|
|
if generator == null:
|
2019-08-12 15:58:42 +02:00
|
|
|
print("Cannot find description for "+data.type)
|
2020-02-15 11:17:41 +01:00
|
|
|
else:
|
|
|
|
generator.model = data.type
|
2019-08-17 17:35:48 +02:00
|
|
|
if generator != null:
|
|
|
|
generator.name = data.type
|
2019-11-04 07:58:17 +01:00
|
|
|
if generator == null:
|
2019-09-09 22:00:18 +02:00
|
|
|
print("LOADER: data not supported:"+str(data))
|
2019-08-12 15:58:42 +02:00
|
|
|
if generator != null:
|
2019-11-04 07:58:17 +01:00
|
|
|
generator.deserialize(data)
|
2019-07-31 20:53:00 +02:00
|
|
|
return generator
|
2019-10-07 20:41:57 +02:00
|
|
|
|
2020-01-28 11:47:29 +01:00
|
|
|
func get_generator_list() -> Array:
|
2019-10-07 20:41:57 +02:00
|
|
|
var rv = []
|
|
|
|
var dir : Directory = Directory.new()
|
|
|
|
for p in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]:
|
|
|
|
dir.open(p)
|
|
|
|
dir.list_dir_begin(true)
|
|
|
|
while true:
|
|
|
|
var f = dir.get_next()
|
|
|
|
if f == "":
|
|
|
|
break
|
|
|
|
if f.right(f.length()-4) == ".mmg":
|
|
|
|
var n = f.left(f.length()-4)
|
|
|
|
if rv.find(n) == -1:
|
|
|
|
rv.push_back(n)
|
|
|
|
rv.sort()
|
|
|
|
return rv
|