Author Topic: Script state at camera switch off  (Read 419 times)

garry23

  • Hero Member
  • *****
  • Posts: 1387
Script state at camera switch off
« on: December 07, 2017, 07:53:10 PM »
Although I have picked up a bit of scripting knowledge, thanks to the help I've received on the ML Forum, the one area I'm weak in is io.

For example, what is the easiest/simplest way to have my script's 'state', ie as set in the script's menus, recorded like the menu states in the ML core, ie at camera switch off. So my script starts in the same configuration as I left it at camera switch off, rather than a hard coded state.

I would welcome some hints or pointers.

Cheers

Garry

a1ex

  • Administrator
  • Hero Member
  • *****
  • Posts: 10716
  • 5D Mark Free
Re: Script state at camera switch off
« Reply #1 on: December 07, 2017, 08:12:23 PM »
https://bitbucket.org/hudson/magic-lantern/src/lua_fix/scripts/copy2m.lua (config.create_from_menu - if I understand it well, it should "just work" for complex scripts with simple menus)
https://bitbucket.org/hudson/magic-lantern/commits/2c8a5d0 (sokoban - example for simple scripts)
https://bitbucket.org/hudson/magic-lantern/src/manual_lens_info/scripts/lens.lua (example for menu with custom state)

BTW, event.config_save should be called whenever ML saves its own settings (that is, after closing ML menu - if you have clicked something - or at shutdown).

Would be nice to have arbitrary variables tagged as persistent somehow, so they will save their state at shutdown and auto-restore at startup. In modules and ML core, we declare them as CONFIG_INT and the backend does the rest.

garry23

  • Hero Member
  • *****
  • Posts: 1387
Re: Script state at camera switch off
« Reply #2 on: December 07, 2017, 08:15:41 PM »
@a1ex

Thanks, I'll read those links.

BTW I did try and look at the Lua Config module but it was not easy for me to see how to use it in my script.

If I can see how someone else has used config, then I can adapt for my use.

Cheers

Garry

garry23

  • Hero Member
  • *****
  • Posts: 1387
Re: Script state at camera switch off
« Reply #3 on: December 07, 2017, 11:27:54 PM »
@a1ex

Sorry to bother you, but I hope you can help; as I feel I'm so close, after reading the links you set me.

I have used config to save my menu set up and proved the savings work.

I then read one setting back in OK, ie at camera restart.

I'm stumbling on how to access a menu item that is more than one whole word.

Here is a snippet of my code.

Code: [Select]
Focus_Bar_Menu = menu.new
{
    parent = "Focus",
    name = "Focus Bar",
    help = "Shows depth of field info",
    depends_on = DEPENDS_ON.LIVEVIEW,
    submenu =
    {
        {
            name = "Display",
            help = "Switches the focus bar display on & off",
            choices = {CON,COFF},
            value = CON
        },
        {
            name = "Show Info",
            help = "Full shows additional near DoF info",
            choices = options2,
            value = "Simple" -- start up default
        },
        {
            name = "Unity Blur Multiplier",
            help = "Used for focus bar display, ie the red zone",
            min = 1,
            max = 10,
            value = 5 -- start up default
        },
        {
            name = "Diffraction",
            help = "Switches the diffraction on & off in the focus bar",
            help2 = "Focus Bar total blur is dynamically linked to ML total blur (CoC) setting",
            choices = {CON,COFF},
        },
        {
            name = "Focus Bar Multiplier",
            help = "Zoom out the focus bar by the specified amount",
            help2 = "1 means no zoom. 0 = place FP at focus bar max. -1 = DoF mode",
            min = -1,
            max = 9,
            value = -1 -- start up default
        },
        {
            name = "Near DoF Criterion",
            help = "Provides near DoF based on different infinity blurs",
            help2 = "Based on the logic that near blur can be larger than the infinity blur",
            choices = options,
            value = "Defocus" -- start up default
        },
        {
            name = "Focus Stacking",
            help = "Only works in DoF mode",
            help2 = "Magenta bar shows the amount of overlap re the last image",
            choices = {CON,COFF},
            value = COFF -- start up default
        }
    }
}
config.create_from_menu(Focus_Bar_Menu)

if cfg.data ~= nil then
    cfg = config:load()
    menu.set("Focus Bar","Display",cfg.data.Display)
    menu.set("Focus Bar","Focus Stacking",cfg.data."Focus Stacking")
end


Can you put me out of my misery, ie how to handle menus with a space in their title, ie the Focus Stacking menu item: which is obviously wrong.

Cheers

Garry

dmilligan

  • Developer
  • Hero Member
  • *****
  • Posts: 3212
  • 60Da / 1100D / EOSM
Re: Script state at camera switch off
« Reply #4 on: December 08, 2017, 03:42:35 AM »
Doing this is completely unnecessary:
Code: [Select]
if cfg.data ~= nil then
    cfg = config:load()
    menu.set("Focus Bar","Display",cfg.data.Display)
    menu.set("Focus Bar","Focus Stacking",cfg.data."Focus Stacking")
end

config.create_from_menu does everything (setting up the save, initializing, and reloading menu values on startup). You simply pass your menu to that function and everything just works.

but to answer you question, since it's a general Lua syntax question:
Code: [Select]
menu.set("Focus Bar", "Focus Stacking", cfg.data["Focus Stacking"])

garry23

  • Hero Member
  • *****
  • Posts: 1387
Re: Script state at camera switch off
« Reply #5 on: December 08, 2017, 07:10:33 AM »
@dmilligan

Many thanks.

As you can see, I'm still very much in learning mode, and really appreciate your help.

Cheers

Garry

garry23

  • Hero Member
  • *****
  • Posts: 1387
Re: Script state at camera switch off
« Reply #6 on: December 08, 2017, 07:37:36 AM »
@dmilligan

David just a repeat thanks, as I've just proved the use of 'only' require("config") and config.create_from_menu(Focus_Bar_Menu) handles everything seamlessly in my focus bar script.

If only I had realised this before.

I have looked at the config module many times, but couldn't work out how to use it.

Once again, many thanks to you and @a1ex for finding the time to support/mentor the likes of myself.

Cheers

Garry

a1ex

  • Administrator
  • Hero Member
  • *****
  • Posts: 10716
  • 5D Mark Free
Re: Script state at camera switch off
« Reply #7 on: December 08, 2017, 03:16:03 PM »
Well, that's a sign the documentation needs some improvement in this area.

garry23

  • Hero Member
  • *****
  • Posts: 1387
Re: Script state at camera switch off
« Reply #8 on: December 08, 2017, 03:42:49 PM »
@a1ex @dmilligan

I have found Lua API documentation mostly helpful, but some entries are rather minimal.

I've considered suggesting changes, but feel my knowledge not up to it.

If those with the knowledge can find effort, I think it has two parts.

First ensure all the API entries are complete, for example, the current config menu entry is this:

Quote
config:create_from_menu(m)
    Create a new config instance from a menu structure, filename will be determined automagically
    Parameters:

        m the menu to create a config for


When I read this, after being pointed to it, I thought OK it creates the config, but I need to load it back to use it. Until David educated me that create_from_menu does it all.

Second, some of the API entries would benefit from a demo code snippet, to help scripters see/understand how to use the API call or function.

Bottom line: I am happy to support, ie I share my humble scripting attempts to help others, but I truly believe the Lua API documentation needs someone with 'insider knowledge' to add (more) value. That is 'right first time'.

Cheers

Garry