Passing parameters to Python scripts from sequences

Somewhere to ask questions about the best way to use SharpCap
Forum rules


If you have a problem or question, please check the FAQ to see if it already has an answer : https://www.sharpcap.co.uk/sharpcap-faqs
Post Reply
lowenthalm
Posts: 153
Joined: Mon May 07, 2018 12:27 am

Passing parameters to Python scripts from sequences

#1

Post by lowenthalm »

There doesn't seem to be anyway to pass a command line parameter to a python script that is called from a SharpCap sequence. I even tried quoting the path to the script in the sequence and that added a parameter, but SC just treats the whole thing including quotes as the path, so it fails.

The only text field that I have found that can be set by the sequencer is the target name. So, if really needed, I pass parameters to a python script that way. I can't find any other less useful string field I can use instead of this, so I am forced to zap the Target name when I need to do anything.

Is there a trick I am missing?
User avatar
admin
Site Admin
Posts: 13408
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Passing parameters to Python scripts from sequences

#2

Post by admin »

Hi,

you're right - there isn't anything that directly allows that to happen. The way the 'run python' step works is to just load the code in the supplied file and start the Python engine running that code. It's not so much launching a new python interpreter (where you would expect to be able to set command line parameters), it's more feeding code to an existing interpreter.

Have you got an example of what you want to achieve? I'm sure there must be some workarounds, but it's hard to be sure what to suggest without some concrete goals.

cheers,

Robin
lowenthalm
Posts: 153
Joined: Mon May 07, 2018 12:27 am

Re: Passing parameters to Python scripts from sequences

#3

Post by lowenthalm »

I see, of course. The Python environment needs to be kept running to maintain its state and all the objects created by startup scripts.

As an example, I have multiple script snippets to set certain interface elements in live-stack to different values, depending on what I want to do. If there was some way of setting a variable in the Python environment to a string supplied in some fashion by the calling sequence, this would simplify python/sequence coding a lot by merging multiple little code snippets into a single one that can grab the input parameter supplied by the sequence.

CONCRETE EXAMPLE
I have a sequence and python script suite for automatically creating an all sky auto-focus calibration table. I then use this calibration table to calculate the closest focus position for my scope, needing only to know the current scope altitude, azimuth and ambient temperature. The code creates a temperature to focus position function and an altitude and azimuth to focus position adjustment function that are added to produce a good guess at the correct focus position. This minimizes/eliminates doing time consuming focus sweeps by getting me very close to the right focus point and knowing when the focus is getting far enough out that a focus sweep may be needed.

Compiling the calibration data is a time consuming, but automated, so its a task perfect for full moon nights. I sweep a few parts of the sky per night. The sequence asks for Target name (!) with PROMPT FOR TARGET.

I enter in a program such as (I have a spreadsheet of the sweep programs to use for the entire sky at my observing site):
E, D20, 0, -1, -1.5, -2, -2.5, -3.0, -3.5

which translates to East side of pier, Dec 20, followed by the list of HAs at which to run multi-star focus sweeps. I'll do this sort of sweep for each Dec and pier side. The sequence handles most of the heavy lifting handling looping and running the focus sweeps, while the python code takes care of recording results and scope pointing.

As it happens, changing the target name when running this particular sequence/python script set isn't an issue, but I have other python/sequences that are more convoluted than needed because changing the target name would interfere with normal imaging operations when trying to use the various script suites.

SUGGESTED IMPLEMENATION
I suggest adding two sequence commands for passing parameters to a python script so that the Target name field doesn't have to be borrowed for this use. One sequence command would take a string parameter which it directly stuffs into a variable in the python environment. The second sequence command would be a prompt dialog that would have the prompt text string as its parameter, displaying the prompt text and an edit field. The text value entered into the edit field would then be stored in the same python environment variable mentioned above.
User avatar
admin
Site Admin
Posts: 13408
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Passing parameters to Python scripts from sequences

#4

Post by admin »

Hi,

Ok, I start to see the picture. I will have to have a think about that, but for now I can make a suggestion of another place to 'smuggle' data from the sequencer to python that might work out...

In the sequencer, use a SET CONTROL step to set the value of the Notes control - ie

Code: Select all

    SET CONTROL Notes TO "12345 abc"
This will appear in the notes box at the bottom of the camera controls. For some reason the 'Notes' control doesn't appear in the dropdown list of controls in the sequence editor, but typing in the name works.

On the python side, you can recover the data using

Code: Select all

SharpCap.SelectedCamera.Controls.FindByName("Notes").Value
There isn't a way to prompt for the notes, but you could show a sequencer prompt 'Enter Data in Notes Field' with the option to unlock the controls set, then manually paste into the Notes box.

I wonder if a sequencer step to run a line of python code (embedded in the sequence) would be useful?

cheers,

Robin
lowenthalm
Posts: 153
Joined: Mon May 07, 2018 12:27 am

Re: Passing parameters to Python scripts from sequences

#5

Post by lowenthalm »

I had looked for the notes control, but didn't see it in the drop down so had abandoned the idea and stuck with the target name field!

The idea for embedding a line of Python code in the sequence would be very useful. I have a bunch of single line python script files that look like this:

MDLAutoDarkFlat.AutosetDarkAndFlat( honor_gain = 1 )

So, having a way of embedding it right in the sequence (and so being able to set honor_gain to 0 or 1 right there, would be pretty coole and cut down on lots of little Python script files.

But there is a down side: having individual Python scripts for every entry point to your Python code makes code maintenance of both sequence and Python code sets a lot easier. Not implementing embedded Python within sequences would encourage what are probably in the long run better coding practices. Its a little annoying when writing quick and dirty sequences/scripts, but I think there isn't a lot one can do with a single line of Python code. You are always going to need to have some sort of multi-line Python script for pretty much anything you would want to do.
User avatar
admin
Site Admin
Posts: 13408
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Passing parameters to Python scripts from sequences

#6

Post by admin »

Hi,

my thought with the 'one line of python' suggestion was that you could either use it to set global python variables before running a script, or as you have suggested call a function that encapsulates a whole script and pass in the right parameters...

I think I will add that as it is nice and easy, unlikely to break anything and has some uses.

cheers,

Robin
Post Reply