Scripting SharpCap from external program

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
aldebaran99
Posts: 9
Joined: Mon Feb 26, 2024 10:03 am

Scripting SharpCap from external program

#1

Post by aldebaran99 »

Hi,

I know the basics of coding in c#, but am rather new to python.
I would like to initate exposures in SharpCap from an external program, for example Win Explorer (or cmd, batch or another C#-program).
I have a python script snippet, that basically does what I want, but it's working only when executed inside SharpCap's console:

Code: Select all

indx="0001"
SharpCap.SelectedCamera=SharpCap.Cameras.Find(lambda x:x.DeviceName=="Test Camera 1 (Deep Sky)")
SharpCap.SelectedCamera.CaptureSingleFrameTo("d:\\SCTests\\sh216_240225_"+ indx +".fits")
As soon as I try to start it from the cmd-line, it terminates with the message:
"NameError: name 'SharpCap' is not defined".
(Python 3.11.8 is installed).

Do I have to load a module or s.th. similiar and how to do this?

Matt
User avatar
admin
Site Admin
Posts: 13349
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting SharpCap from external program

#2

Post by admin »

Hi Matt,

unfortunately it's a bit more complex than that ...

SharpCap runs its own Python environment internally (based on IronPython, which actually runs python on .NET). It's only that internal version of Python that has the 'SharpCap' object defined to work with.

The way to get around this is to use a python remoting tool called 'Pyro' - this can set up a server inside SharpCap that listens for instructions. Your external python can send commands, which are received and can then call methods on the 'SharpCap' object.

If you search for 'SharpCap Pyro' you will find some examples and discussions in the forums.

cheers,

Robin
aldebaran99
Posts: 9
Joined: Mon Feb 26, 2024 10:03 am

Re: Scripting SharpCap from external program

#3

Post by aldebaran99 »

Hi Robin,

thank you for the response!
What a pity, from MaximDl I am used to be able to control nearly anything
from outside, what is very comfortable for customizing everything.
I am running a dual setup and have a DIY software environment to control everything
(I don't use NINA).
So I will look into Pyro, there seems to be a lot to study for me...

> Your external python can send commands, which are received and can
> then call methods on the 'SharpCap' object.

Is there an option to call the methods via Pyro from a .net c# program?

Regards,
Matt
P.S. Perhaps I should mention, that I only need Sharpcap doing single exposures
and save them as a file.. no further functions needed. In fact, I could get away
with the option of starting with /runscript (and the right script), but repeatedly starting SharpCap
(and terminating after several exposures) seems a little cumbersome to me...
But I will investigate further regarding Pyro.
User avatar
admin
Site Admin
Posts: 13349
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting SharpCap from external program

#4

Post by admin »

Hi,

also consider having a look at this thread : viewtopic.php?t=6406 - someone has written a simple web server in the SharpCap Python, which maybe you could adapt to your usage.

Of course there are also 'quick and simple' approaches if you only want very basic 'capture an image' functionality. You could have your SharpCap script look for a particular file existing every 1s, if the file exists, delete it and capture a frame. Your external .NET code can create the file when it wants a frame captured.

cheers,

Robin
aldebaran99
Posts: 9
Joined: Mon Feb 26, 2024 10:03 am

Re: Scripting SharpCap from external program

#5

Post by aldebaran99 »

Hi,

thanks, I did read the thread you mention last night ( I searched the whole subforum
for informations about scripting and my problem...)
I also saw, that Pyro is rather complicated (to me) and maybe a little overcomplex
for the simple things I want to achieve.
I will study the webserver and Pyro further, but this will take some time.
So for a quick result I went the 'quick and simple' route you proposed.
In fact the basic scripts are already done... and it's working well.

Thanks for the hints and explanations, but most importantly thanks for this great program!
Matt

Oh, one more question: Is it possible to have SharpCap open the scripting console at
program start? Is it possible to start the console from a script?
User avatar
admin
Site Admin
Posts: 13349
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting SharpCap from external program

#6

Post by admin »

Hi,

glad you found a solution that worked.

Unfortunately there is no way to launch the console from within a script or on startup. The only options you have in that area are to run a script on startup using either the startup script settings or the /runscript command line parameter.

cheers,

Robin
aldebaran99
Posts: 9
Joined: Mon Feb 26, 2024 10:03 am

Re: Scripting SharpCap from external program

#7

Post by aldebaran99 »

admin wrote: Wed Feb 28, 2024 10:39 am Unfortunately there is no way to launch the console from within a script or on startup. The only options you have in that area are to run a script on startup using either the startup script settings or the /runscript command line parameter.
I can live with that, but I really wish the console window would remember it's last position and size when I restart the program!!

Matt
User avatar
admin
Site Admin
Posts: 13349
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Scripting SharpCap from external program

#8

Post by admin »

That ought to be possible... I will look

Robin
aldebaran99
Posts: 9
Joined: Mon Feb 26, 2024 10:03 am

Re: Scripting SharpCap from external program

#9

Post by aldebaran99 »

admin wrote: Fri Mar 01, 2024 10:47 am That ought to be possible... I will look
That would be GREAT!

Matt
Post Reply