From b598a0c5d5a0ef6736d02403a282bdc20f23564d Mon Sep 17 00:00:00 2001 From: Alex <24834740+GreenXenith@users.noreply.github.com> Date: Mon, 22 Jul 2019 10:18:03 -0700 Subject: [PATCH] Allow specifying filename (#1) * Allow specifying filename * Update README --- README.md | 2 +- export.lua | 14 +++++++++++--- init.lua | 13 ++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 670a213..e6d0902 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This mod is still in the "alpha" phase; as such, many types of nodes are not yet ## Usage -Use `/mesh1` and `/mesh2` to set the corners of the area you want exported, then use `/meshport` to export the mesh. The saved `.obj` and `.mtl` files will be located in the `meshport` folder of the world directory, within a subfolder. +Use `/mesh1` and `/mesh2` to set the corners of the area you want exported, then use `/meshport [filename]` to export the mesh (filename is optional). The saved `.obj` and `.mtl` files will be located in the `meshport` folder of the world directory, within a subfolder. ### Importing into Blender diff --git a/export.lua b/export.lua index 4baf808..d8aa1c5 100644 --- a/export.lua +++ b/export.lua @@ -132,7 +132,7 @@ function meshport.create_node(idx, area, content, param2, playerName) return faces end -function meshport.create_mesh(playerName, p1, p2) +function meshport.create_mesh(playerName, p1, p2, filename) meshport.print(playerName, "info", "Generating mesh...") p1, p2 = vector.sort(p1, p2) local vm = minetest.get_voxel_manip() @@ -163,13 +163,21 @@ function meshport.create_mesh(playerName, p1, p2) end end + filename = filename or os.date("%Y-%m-%d_%H-%M-%S") + -- Create path for exported mesh. local path = string.format("%s%smeshport%s%s_%s", - minetest.get_worldpath(), DIR_DELIM, DIR_DELIM, playerName, os.date("%Y-%m-%d_%H-%M-%S")) + minetest.get_worldpath(), DIR_DELIM, DIR_DELIM, playerName, filename) + + if file_exists(path) then + meshport.print(playerName, "error", "File already exists.") + return + end + minetest.mkdir(path) mesh:write_obj(path) mesh:write_mtl(path, playerName) - meshport.print(playerName, "info", "Finished.") + meshport.print(playerName, "info", "Finished. Saved to " .. path) end diff --git a/init.lua b/init.lua index 20bbdaf..68d37b8 100644 --- a/init.lua +++ b/init.lua @@ -53,8 +53,8 @@ for i = 1, 2 do end minetest.register_chatcommand("meshport", { - params = "", - description = "Save a mesh of the selected area.", + params = "[filename]", + description = "Save a mesh of the selected area (filename optional).", privs = {meshport = true}, func = function(name, param) @@ -63,6 +63,13 @@ minetest.register_chatcommand("meshport", { return end - meshport.create_mesh(name, meshport.p1[name], meshport.p2[name]) + if param:find("[^%w-_]") then + meshport.print(name, "error", "Invalid name supplied. Please use valid characters ([A-Z][a-z][0-9][-_]).") + return + elseif param == "" then + param = nil + end + + meshport.create_mesh(name, meshport.p1[name], meshport.p2[name], param) end, })