From 85599c2feab5bcd522ff71b505dcf477b96fe116 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Sun, 10 Nov 2019 15:02:00 +0100 Subject: [PATCH 1/2] Added menu to show nodes documentation and updated import plugin --- addons/material_maker/engine/gen_material.gd | 12 +++-- addons/material_maker/graph_edit.gd | 13 +++--- .../import_plugin/ptex_import.gd | 20 +++++--- .../import_plugin/ptex_spatial_material.gd | 29 ++++++++++++ addons/material_maker/library.gd | 21 +++++---- addons/material_maker/main_window.gd | 46 ++++++++++++------- addons/material_maker/main_window.tscn | 10 ++-- addons/material_maker/plugin.gd | 13 +----- 8 files changed, 104 insertions(+), 60 deletions(-) create mode 100644 addons/material_maker/import_plugin/ptex_spatial_material.gd diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index bd59ae1..8679726 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -150,7 +150,8 @@ func get_generated_texture(slot, file_prefix = null) -> ImageTexture: if file_prefix != null: var file_name = "%s_%s.png" % [ file_prefix, slot ] if File.new().file_exists(file_name): - return load(file_name) as ImageTexture + var texture = load(file_name) + return texture else: return null else: @@ -162,7 +163,6 @@ func update_spatial_material(m, file_prefix = null) -> void: if m is SpatialMaterial: # Make the material double-sided for better visiblity in the preview m.params_cull_mode = SpatialMaterial.CULL_DISABLED - m.albedo_color = parameters.albedo_color m.albedo_texture = get_generated_texture("albedo", file_prefix) m.metallic = parameters.metallic @@ -239,11 +239,13 @@ func export_textures(prefix, editor_interface = null) -> SpatialMaterial: if Engine.editor_hint and editor_interface != null: var resource_filesystem = editor_interface.get_resource_filesystem() resource_filesystem.scan() - yield(resource_filesystem, "filesystem_changed") + yield(resource_filesystem, "resources_reimported") + print("resources_reimported") var new_material = SpatialMaterial.new() update_spatial_material(new_material, prefix) - ResourceSaver.save("%s.tres" % [ prefix ], new_material) - resource_filesystem.scan() + var file_name : String = "%s.tres" % [ prefix ] + ResourceSaver.save(file_name, new_material) + resource_filesystem.update_file(file_name) return new_material return null diff --git a/addons/material_maker/graph_edit.gd b/addons/material_maker/graph_edit.gd index 6cae875..e8fa448 100644 --- a/addons/material_maker/graph_edit.gd +++ b/addons/material_maker/graph_edit.gd @@ -47,7 +47,7 @@ func add_node(node) -> void: func connect_node(from, from_slot, to, to_slot): if generator.connect_children(get_node(from).generator, from_slot, get_node(to).generator, to_slot): var disconnect = get_source(to, to_slot) - if disconnect != null: + if !disconnect.empty(): .disconnect_node(disconnect.node, disconnect.slot, to, to_slot) .connect_node(from, from_slot, to, to_slot) send_changed_signal() @@ -214,12 +214,11 @@ func save_file(filename) -> void: func export_textures() -> void: if save_path != null: var prefix = save_path.left(save_path.rfind(".")) - for c in get_children(): - if c is GraphNode: - if c.generator.has_method("render_textures"): - c.generator.render_textures() - if c.generator.has_method("export_textures"): - c.generator.export_textures(prefix, editor_interface) + for g in top_generator.get_children(): + if g.has_method("render_textures"): + g.render_textures() + if g.has_method("export_textures"): + g.export_textures(prefix, editor_interface) # Cut / copy / paste diff --git a/addons/material_maker/import_plugin/ptex_import.gd b/addons/material_maker/import_plugin/ptex_import.gd index 0cf9a5a..0221743 100644 --- a/addons/material_maker/import_plugin/ptex_import.gd +++ b/addons/material_maker/import_plugin/ptex_import.gd @@ -3,10 +3,10 @@ extends EditorImportPlugin var plugin = null -const PRESET_NAMES = [ "Skip", "Import with Material Maker" ] +const PRESET_NAMES = [ "Render in game", "Prerender" ] const PRESET_OPTIONS = [ - [{ name="skip", default_value=true }], - [{ name="skip", default_value=false }] + [{ name="render", default_value=false }], + [{ name="render", default_value=true }] ] func _init(p) -> void: @@ -37,7 +37,7 @@ func get_recognized_extensions() -> Array: return [ "ptex" ] func get_resource_type() -> String: - return "Material" + return "SpatialMaterial" func get_save_extension() -> String: return "tres" @@ -46,11 +46,19 @@ func get_visible_name() -> String: return "Material Maker Importer" func import(source_file: String, save_path: String, options: Dictionary, platform_variants: Array, gen_files: Array) -> int: - if !options.skip: - var filename = save_path + "." + get_save_extension() + var filename = save_path + "." + get_save_extension() + if options.render: var material = plugin.generate_material(source_file) while material is GDScriptFunctionState: material = yield(material, "completed") if material != null: ResourceSaver.save(filename, material) + else: + var material : SpatialMaterial = SpatialMaterial.new() + material.set_script(preload("res://addons/material_maker/import_plugin/ptex_spatial_material.gd")) + var file : File = File.new() + if file.open(source_file, File.READ) == OK: + material.set_ptex_no_render(to_json(parse_json(file.get_as_text()))) + file.close() + ResourceSaver.save(filename, material) return OK diff --git a/addons/material_maker/import_plugin/ptex_spatial_material.gd b/addons/material_maker/import_plugin/ptex_spatial_material.gd new file mode 100644 index 0000000..3e77ee9 --- /dev/null +++ b/addons/material_maker/import_plugin/ptex_spatial_material.gd @@ -0,0 +1,29 @@ +tool +extends SpatialMaterial + +export var ptex : String = "" setget set_ptex + +func set_ptex_no_render(s : String) -> void : + ptex = s + +func set_ptex(s : String) -> void : + if ptex == s: + return + ptex = s + call_deferred("update_texture") + +func update_texture() -> void: + var loader = MMGenLoader.new() + var mm_graph = loader.create_gen(parse_json(ptex)) + if mm_graph == null: + return + var mm_material : MMGenMaterial = mm_graph.get_node("Material") + if ! (mm_material is MMGenMaterial): + return + mm_renderer.add_child(mm_graph) + var status = mm_material.render_textures() + while status is GDScriptFunctionState: + status = yield(status, "completed") + mm_material.update_spatial_material(self) + mm_graph.queue_free() + \ No newline at end of file diff --git a/addons/material_maker/library.gd b/addons/material_maker/library.gd index 598827c..353f022 100644 --- a/addons/material_maker/library.gd +++ b/addons/material_maker/library.gd @@ -22,15 +22,16 @@ func _unhandled_input(event : InputEvent) -> void: filter_line_edit.select_all() func get_selected_item_name() -> String: - var tree_item : TreeItem = tree.get_selected() - var rv = "" - while tree_item != null and tree_item != tree.get_root(): - if rv == "": - rv = tree_item.get_text(0) - else: - rv = tree_item.get_text(0)+"/"+rv - tree_item = tree_item.get_parent() - return rv + return get_item_path(tree.get_selected()) + +func get_selected_item_doc_name() -> String: + var item : TreeItem = tree.get_selected() + if item == null: + return "" + var m : Dictionary = item.get_metadata(0) + if m == null or !m.has("icon"): + return "" + return m.icon func add_library(file_name : String, filter : String = "") -> bool: var root = tree.get_root() @@ -110,6 +111,8 @@ func add_item(item, item_name, item_icon = null, item_parent = null, force_expan return add_item(item, suffix, item_icon, new_parent, force_expand) func get_item_path(item : TreeItem) -> String: + if item == null: + return "" var item_path = item.get_text(0) var item_parent = item.get_parent() while item_parent != tree.get_root(): diff --git a/addons/material_maker/main_window.gd b/addons/material_maker/main_window.gd index bfac63f..3a69b66 100644 --- a/addons/material_maker/main_window.gd +++ b/addons/material_maker/main_window.gd @@ -40,7 +40,8 @@ const MENU = [ { menu="Tools", command="add_to_user_library", description="Add selected node to user library" }, { menu="Tools", command="export_library", description="Export the nodes library" }, - { menu="Help", command="show_doc", description="User manual" }, + { menu="Help", command="show_doc", shortcut="F1", description="User manual" }, + { menu="Help", command="show_library_item_doc", shortcut="Control+F1", description="Show selected library item documentation" }, { menu="Help", command="bug_report", description="Report a bug" }, { menu="Help" }, { menu="Help", command="about", description="About" } @@ -100,6 +101,8 @@ func create_menu(menu, menu_name) -> PopupMenu: for i in MENU.size(): if MENU[i].has("standalone_only") and MENU[i].standalone_only and Engine.editor_hint: continue + if MENU[i].has("editor_only") and MENU[i].editor_only and !Engine.editor_hint: + continue if MENU[i].menu != menu_name: continue if MENU[i].has("submenu"): @@ -366,27 +369,38 @@ func export_library() -> void: func do_export_library(path : String) -> void: library.export_libraries(path) -func show_doc() -> void: +func get_doc_dir() -> String: var base_dir = OS.get_executable_path().replace("\\", "/").get_base_dir() - # In release builds, documentation is expected to be located in # a subdirectory of the program directory - var release_doc_path = base_dir.plus_file("doc/index.html") - + var release_doc_path = base_dir.plus_file("doc") # In development, documentation is part of the project files. # We can use a globalized `res://` path here as the project isn't exported. - var devel_doc_path = ProjectSettings.globalize_path("res://addons/material_maker/doc/_build/html/index.html") + var devel_doc_path = ProjectSettings.globalize_path("res://addons/material_maker/doc/_build/html") + for p in [ release_doc_path, devel_doc_path ]: + var file = File.new() + if file.file_exists(p+"/index.html"): + return p + return "" - var file = File.new() - if file.file_exists(release_doc_path): - # Open local prebuilt documentation (used in release builds) - OS.shell_open(release_doc_path) - elif file.file_exists(devel_doc_path): - # Open local documentation built from source (used during development) - OS.shell_open(devel_doc_path) - else: - # Open online documentation - OS.shell_open("https://rodzill4.github.io/godot-procedural-textures/doc/") +func show_doc() -> void: + var doc_dir = get_doc_dir() + if doc_dir != "": + OS.shell_open(doc_dir+"/index.html") + +func show_doc_is_disabled() -> bool: + return get_doc_dir() == "" + +func show_library_item_doc() -> void: + var doc_dir : String = get_doc_dir() + if doc_dir != "": + var doc_name = library.get_selected_item_doc_name() + if doc_name != "": + var doc_path : String = doc_dir+"/node_"+doc_name+".html" + OS.shell_open(doc_path) + +func show_library_item_doc_is_disabled() -> bool: + return get_doc_dir() == "" or library.get_selected_item_doc_name() == "" func bug_report() -> void: OS.shell_open("https://github.com/RodZill4/godot-procedural-textures/issues") diff --git a/addons/material_maker/main_window.tscn b/addons/material_maker/main_window.tscn index 87c140a..1ff20a2 100644 --- a/addons/material_maker/main_window.tscn +++ b/addons/material_maker/main_window.tscn @@ -45,35 +45,35 @@ margin_bottom = 20.0 margin_right = 35.0 margin_bottom = 20.0 text = "File" -items = [ "New material", null, 0, false, false, 0, 0, null, "", false, "Load material", null, 0, false, false, 1, 268435535, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Save material", null, 0, false, false, 4, 268435539, null, "", false, "Save material as...", null, 0, false, false, 5, 301989971, null, "", false, "Save all materials...", null, 0, false, false, 6, 0, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Export material", null, 0, false, false, 8, 268435525, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Close material", null, 0, false, false, 10, 0, null, "", false, "Quit", null, 0, false, false, 11, 268435537, null, "", false ] +items = [ "New material", null, 0, false, false, 0, 0, null, "", false, "Load material", null, 0, false, false, 1, 268435535, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Save material", null, 0, false, false, 4, 268435539, null, "", false, "Save material as...", null, 0, false, false, 5, 301989971, null, "", false, "Save all materials...", null, 0, false, false, 6, 0, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Export material", null, 0, false, false, 8, 268435525, null, "", false, "Export embedded PTEX", null, 0, false, false, 9, 301989957, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Close material", null, 0, false, false, 11, 0, null, "", false, "Quit", null, 0, false, false, 12, 268435537, null, "", false ] [node name="Edit" type="MenuButton" parent="VBoxContainer/Menu"] margin_left = 39.0 margin_right = 75.0 margin_bottom = 20.0 text = "Edit" -items = [ "Cut", null, 0, false, false, 12, 268435544, null, "", false, "Copy", null, 0, false, false, 13, 268435523, null, "", false, "Paste", null, 0, false, false, 14, 268435542, null, "", false ] +items = [ "Cut", null, 0, false, false, 13, 268435544, null, "", false, "Copy", null, 0, false, false, 14, 268435523, null, "", false, "Paste", null, 0, false, false, 15, 268435542, null, "", false ] [node name="View" type="MenuButton" parent="VBoxContainer/Menu"] margin_left = 79.0 margin_right = 121.0 margin_bottom = 20.0 text = "View" -items = [ "Center view", null, 0, false, false, 15, 67, null, "", false, "Reset zoom", null, 0, false, false, 16, 268435504, null, "", false ] +items = [ "Center view", null, 0, false, false, 16, 67, null, "", false, "Reset zoom", null, 0, false, false, 17, 268435504, null, "", false ] [node name="Tools" type="MenuButton" parent="VBoxContainer/Menu"] margin_left = 125.0 margin_right = 171.0 margin_bottom = 20.0 text = "Tools" -items = [ "Create", null, 0, false, false, -1, 0, null, "PopupMenu", false, "Create group", null, 0, false, false, 18, 268435527, null, "", false, "Make selected nodes editable", null, 0, false, false, 19, 268435525, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Add selected node to user library", null, 0, false, false, 21, 0, null, "", false, "Export the nodes library", null, 0, false, false, 22, 0, null, "", false ] +items = [ "Create", null, 0, false, false, -1, 0, null, "PopupMenu", false, "Create group", null, 0, false, false, 19, 268435527, null, "", false, "Make selected nodes editable", null, 0, false, false, 20, 268435525, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "Add selected node to user library", null, 0, false, false, 22, 0, null, "", false, "Export the nodes library", null, 0, false, false, 23, 0, null, "", false ] [node name="Help" type="MenuButton" parent="VBoxContainer/Menu"] margin_left = 175.0 margin_right = 217.0 margin_bottom = 20.0 text = "Help" -items = [ "User manual", null, 0, false, false, 23, 0, null, "", false, "Report a bug", null, 0, false, false, 24, 0, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "About", null, 0, false, false, 26, 0, null, "", false ] +items = [ "User manual", null, 0, false, false, 24, 16777244, null, "", false, "Show selected library item documentation", null, 0, false, false, 25, 285212700, null, "", false, "Report a bug", null, 0, false, false, 26, 0, null, "", false, "", null, 0, false, false, -1, 0, null, "", true, "About", null, 0, false, false, 28, 0, null, "", false ] [node name="HBoxContainer" type="HSplitContainer" parent="VBoxContainer"] margin_top = 24.0 diff --git a/addons/material_maker/plugin.gd b/addons/material_maker/plugin.gd index d51acde..21a6471 100644 --- a/addons/material_maker/plugin.gd +++ b/addons/material_maker/plugin.gd @@ -7,19 +7,8 @@ var importer = null func _enter_tree() -> void: add_tool_menu_item("Material Maker", self, "open_material_maker") - add_tool_menu_item("Register Material Maker Import", self, "register_material_maker_import") - -func register_material_maker_import(__) -> void: importer = preload("res://addons/material_maker/import_plugin/ptex_import.gd").new(self) add_import_plugin(importer) - remove_tool_menu_item("Register Material Maker Import") - add_tool_menu_item("Unregister Material Maker Import", self, "unregister_material_maker_import") - -func unregister_material_maker_import(__) -> void: - remove_import_plugin(importer) - importer = null - remove_tool_menu_item("Unregister Material Maker Import") - add_tool_menu_item("Register Material Maker Import", self, "register_material_maker_import") func _exit_tree() -> void: remove_tool_menu_item("Material Maker") @@ -59,7 +48,7 @@ func generate_material(ptex_filename: String) -> Material: if generator.has_node("Material"): var gen_material = generator.get_node("Material") if gen_material != null: - var return_value = gen_material.render_textures(mm_renderer) + var return_value = gen_material.render_textures() while return_value is GDScriptFunctionState: return_value = yield(return_value, "completed") var prefix = ptex_filename.left(ptex_filename.rfind(".")) From 40fa352dc62ffdacc59d172e0d0ce2051075fec9 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Sun, 10 Nov 2019 15:38:54 +0100 Subject: [PATCH 2/2] Fixed icon name in saved library --- addons/material_maker/main_window.gd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/material_maker/main_window.gd b/addons/material_maker/main_window.gd index 3a69b66..cffcc66 100644 --- a/addons/material_maker/main_window.gd +++ b/addons/material_maker/main_window.gd @@ -347,11 +347,11 @@ func do_add_to_user_library(name, nodes) -> void: dir.make_dir("user://library") dir.make_dir("user://library/user") data.library = "user://library/user.json" - data.icon = name.right(name.rfind("/")+1).to_lower() + data.icon = library.get_icon_name(name) var result = nodes[0].generator.render(0, 64) while result is GDScriptFunctionState: result = yield(result, "completed") - result.save_to_file("user://library/user/"+library.get_icon_name(name)+".png") + result.save_to_file("user://library/user/"+data.icon+".png") result.release() library.add_item(data, name, library.get_preview_texture(data)) library.save_library("user://library/user.json")