Editor Scripts 🔥: Alpha Release

editor-script-play-sound

Adds a right click context menu for .sound .wav and .ogg files to play them with ffplay. ffplay is a required dependency and must be added to your path.

You can get ffplay from downloading FFmpeg and adding its bin to your path https://ffmpeg.zeranoe.com/builds/ search Google for help if you get stuck!

Problems: files can sometimes be locked with Java when playing!?, you can’t stop audio playing once it begins, you can’t play multiple sounds at once, and must wait for previous sounds to finish, you can’t set the audio level so make sure your system level is low enough

Hopefully some day soon Defold gets built in playing of audio to preview in the editor with start/stop buttons.

https://github.com/subsoap/editor-script-play-sound/archive/master.zip


I’d like to make a version which does not depend on ffplay. Is there any cross platform sound player that could be good to be used to play silently (and maybe be able to play only the first second of a sound) and would have win/mac/linux bins under 1mb together?

4 Likes

I’d like to make a right click -> rename file extension editor script, but need to be able to have a user input popup, and be able to select any file.

At the moment, you have to rename file extensions outside of the editor which is annoying.


It appears that when there are bundle errors the current directory is not the root of the project.

So if I have a bundle error like

/example/test.particlefx
	the tile source '/fake.tilesource' can't be found

I get

on_bundle_finished

ERROR:EXT: ./bundle_finished_fail.mp3: No such file or directory

With

function M.on_bundle_finished(opts)
	print("on_bundle_finished")
	if opts.success then
		return {
			{
				action = "shell", 
				command = {"ffplay", "./bundle_finished_success.mp3", "-loglevel", "warning", "-autoexit", "-nodisp"}
			}
		}
	else
		return {
			{
				action = "shell", 
				command = {"ffplay", "./bundle_finished_fail.mp3", "-loglevel", "warning", "-autoexit", "-nodisp"}
			}
		}	
	end
end

But when bundling is successful the directory is correct.

Sample


No more than 3 consecutive replies are allowed. Please edit your previous reply, or wait for someone to reply to you.

Why was this enabled in this forum? It didn’t use to be enabled. Really annoying! Being forced to merge multiple posts on different subjects is stupid! And what about dev journals, devs will no longer be able to journal properly without having the mercy of replies! :anger:


It would be useful to have some actions allow the editor to continue without waiting. For example, with the play sound related scripts it forces waiting for the sound to finish before allowing to play a different sound. And a similar case happens when bundle finished plays a sound - the bundle folder doesn’t appear until the sound is finished.

1 Like

I shall give you the mercy of reply
keep going :wink:

1 Like

Oh, I wasn’t aware of this. That does indeed seem stupid. I’ll take a look.

1 Like

Would it be possible to override existing functionality of menu buttons with editor scripts? I would love to be able to add a confirmation dialog for doing Project->Rebuild since I often accidentally hit it and on big projects I have no choice but to let it finish.

Better yet I remember old editor concepts which had a build button in the top middle of the main UI. I want to be able to add that in.

1 Like

I found the setting but it’s been like that for ages. The rationale behind Discourse having this setting at all is that Discourse is a forum, not a chat. You’re not supposed to hit Reply after every line of text you write… If users need to reply to their own posts they are encouraged to instead edit their original post. While I do agree with this it’s also kinda silly and I’d rather encourage it some other way, instead of having a hard limit in a forum settings page. I have increased the limit for now at least.

3 Likes

Does it not count for threads you’ve made yourself? Because my dev journal threads had many more than 3 posts in a row.

It makes sense in the chat sense, but it’s much cleaner to have individual unrelated posts as their own post and not all merged.

2 Likes

Are you 100% sure file name is correct? Because when bundling is successful it’s correct, and we have some code path for both success/fail bundle, with only difference being success key…

I’ll need to implement os.rename for lua first…

Hmm. We wait until process finishes, I think that’s sensible default. Can you use a script that launches ffplay in another process?

1 Like

D’oh. I had fail and failure. My mistake!

I was having trouble getting sh scripts to work on Windows. I have gitbash installed with .sh associated with it… I’d like something portable that would work on all platforms Defold supports. Are .sh working for anyone on Windows?

ERROR:EXT: Cannot run program "./scripts/test.sh" (in directory "...\subsoap-editor-scripts"): CreateProcess error=2, The system cannot find the file specified

I don’t think they are working, you’ll need different (probably *.bat?) script on windows and check editor.platform to return path to correct platform-specific script

1 Like

I tried linking to a *.bat file too and it shows the same error for me.

Think you need to run it through cmd, something like cmd /c magic.bat

3 Likes

This doesn’t work?

ERROR:EXT: {["action"] = "cmd", ["command"] = {"./scripts/test.bat"}} needs "action" key to be "set" or "shell"
                    {
                        action = "cmd", 
                        command = {"./scripts/test.bat"}
                    }

If you meant

                    {
                        action = "shell", 
                        command = {"cmd /c ./scripts/test.bat"}
                    }

does not work either.

Edit…

Just realized it should be this, which does work. :man_facepalming:

                    {
                        action = "shell", 
                        command = {"cmd", "./scripts/test.bat"}
                    }
3 Likes

I am trying to run a python script from a editor script.

You can run a python script from the editor with

command = {"python",  "./editor-script/scripts/main.py"}

if the script can be found by the system. Meaning, you can not use it as a dependency, if your script will at some point be imported as a dependency then you need to run it as text. This is done like this.

command = {"python", "-c", editor.get("/editor-script/scripts/main.py", "text")}

My problem is that Defold will pick up the 2.7 from the macOS., this can be verified by command = {"python", "-c", "import sys; print(sys.version)"}. This means that if I am relying on external libraries installed with pip I first need to extend the PATH.

To figure out where python is I have a shell script that will source the .bash_profilebefore running the python script, a user can then extend the PATH themselves.

It simply looks like this

#!/bin/bash
source "$HOME/.bash_profile"
python "$@"

I then call this by running

local function command_workaround()
  if editor.platform == "x86_64-darwin" then
    return "./editor-script/scripts/extend_path.sh"
  end
  return "python"
end

---- Using the command
command = {command_workaround(), "./editor-script/scripts/main.py"}

As I mentioned before this works fine when having it locally but it will fail if I use it as a dependency.
Error:

ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "./editor-script/scripts/extend_path.sh" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory

As we said this is because when used as a dependency the relative path is no longer correct.
One would think I could do

editor.get("/editor-script/scripts/extend_path.sh", "path")

to first get the (absolute) path but it reports the same relative path (expected).

"/editor-script/scripts/extend_path.sh"

If we try to load the shell script as text

local function command_workaround()
  if editor.platform == "x86_64-darwin" then
    return editor.get("/editor-script/scripts/extend_path.sh", "text")
  end
  return "python"
end

We get

ERROR:EXT: Cannot run program "#!/bin/bash
ERROR:EXT: source '$HOME/.bash_profile'" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory

Which makes sense, the text string is not a command, normally this can be worked around with a call with eval. But putting it as a command gives an error

command = {"eval", command_workaround(), "./editor-script/scripts/main.py"}
------
ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "eval" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory

And putting it into a the .sh script gives ann error too

eval "#!/bin/bash
\nsource '$HOME/.bash_profile'
\npython "$@""

-----
ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "eval "#!/bin/bash
ERROR:EXT: \nsource '$HOME/.bash_profile'
ERROR:EXT: \npython "$@""" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory

Which leads me to my question: Do anyone know a way to get PATH extended by sourcing .bash_profile and then calling a python script inside that extended shell?

3 Likes

I have played around with this for a while now and there are a few things that I miss

I would like access/functionality from these Defold libaries:

  • http : Would like way to do http requests
  • vmath : For doing calculations (don’t have an idea where this is needed yet but I bet someone will eventually)
  • json : Pure lua implementations of json tends to be slow, if we am to parse thousands of files then we need something faster.
  • sys : Would be nice to be able to get .project values. Would also like to have a sys.get_project_path so I wouldn’t need to rely on cwd all the time.
  • (parts of) sound : For playing a sound on complete or button click or w/e
  • zlib : Eventually we will want to pack unpack stuff
  • timer : Will need to wait for something at some point.

Lua/other libraries:

  • lfs : File system stuff
  • lua-pb : When we get access to the proto buff files this will be needed.
  • luaexpat : Someone might prefer XML
  • imgui : We need a way to create gui
  • Penlight : If we are not able to extend lua ourselves then this library does cover a lot of user cases.
2 Likes

New editor script feature just got released: Outline Properties :tada:

Now your commands can target any item in the Outline View using "outline" query type. editor.get() now has access to properties displayed in Properties View. Since some of these properties are sometimes read-only, and sometimes unavailable, new API is added: editor.can_get() and editor.can_set() to check them beforehand. Hover over property names in Properties view to see how they are named in editor scripts.

Example command:

{
    locations = {"Outline"},
    label = "Reset Transform",
    query = {selection = {
       type = "outline", cardinality = "one"
    }},
    active = function(opts) 
        local id = opts.selection
        return editor.can_set(id, "position") 
               or editor.can_set(id, "rotation") 
               or editor.can_set(id, "scale")
    end,
    run = function(opts)
        local id = opts.selection
        local ret = {}
        if editor.can_set(id, "position") then
            table.insert(ret, {
                action = "set", 
                node_id = id, 
                property = "position", 
                value = {0, 0, 0}
            })
        end
        if editor.can_set(id, "rotation") then
            table.insert(ret, {
                action = "set", 
                node_id = id, 
                property = "rotation", 
                value = {0, 0, 0}
            })
        end
        if editor.can_set(id, "scale") then
            table.insert(ret, {
                action = "set", 
                node_id = id, 
                property = "scale", 
                value = {1, 1, 1}
            })
        end
        return ret
    end
}

Check for updates and take a look at updated manual!

10 Likes

Ohhhhhh this is really nice!

Anyone have a simple way already of modifying game.project values?

What I want to do is get and increment the Android version number then save automatically when making bundles from the editor.

bundle_timestamp_test.zip (2.3 KB)

Try to build this once and I get this error if the file does not already exist on Windows 10. If I try to build again then it works and appears to update with proper time. This is still concerning because it means if there are things which take time to do pre-build there won’t be enough time to do them or is something else happening?

ERROR:SCRIPT: /main/test.script:2: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
	[C]: in function 'decode'
	/main/test.script:2: in function </main/test.script:1>

For bundling even with the file deleted it does appear to generate it in time properly.

1 Like

Hmm, editor is supposed to sync resources you create, so it looks like a bug, can you create an issue please?

1 Like