Need scripting help

Discussions on extending SharpCap using the built in Python scripting functionality
putty tat
Posts: 8
Joined: Mon May 08, 2017 2:34 pm

Need scripting help

#1

Post by putty tat »

Trying to learn Iron Python but running into problems with the procedure creating a custom button on the lower frame.
Using the sample script provided returns a "4 arguments required" There are 4 arguments listed.
What am I doing wrong? :?
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Need scripting help

#2

Post by admin »

Can you share your code so far please?

cheers,

Robin
putty tat
Posts: 8
Joined: Mon May 08, 2017 2:34 pm

Re: Need scripting help

#3

Post by putty tat »

Hello Robin
First, may my wife from Ireland and myself offer to you and the people of England our heartfelt sorrow and sympathies for sad attack by scumbags in Manchester.

Here's what the I used copied from the help() screen:

#Do the same a slightly simpler way
tSharpCap.SelectedCamera.GetControl(CommonPropertyIDs.Exposure).Value = 0.1

#Add a custom button to the toolbar that will open the first camera when it is clicked
def selectFirstCamera():
SharpCap.SelectedCamera=SharpCap.Cameras[0]
SharpCap.AddCustomButton("Test", None, "Select the first camera", selectFirstCamera)


Responed also
Traceback (most recent call last):
File "<string>", line 2, in <module>
NameError: name 'tSharpCap' is not defined

Could it be the interpreter is corrupted? About 2 years I had a project with a Raspberry that used Python.
I had read that Python 3 had serious problems at that time.

Best regards, Frank (putty tat)
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Need scripting help

#4

Post by admin »

Hi Frank,

sorry, but it's a rogue letter 't' at the beginning of this line of script

Code: Select all

tSharpCap.SelectedCamera.GetControl(CommonPropertyIDs.Exposure).Value = 0.1
take out the t at the beginning (typo on my part) and you should be fine.

cheers,

Robin
putty tat
Posts: 8
Joined: Mon May 08, 2017 2:34 pm

Re: Need scripting help

#5

Post by putty tat »

Hi Robin,

Yes I did notice the t and removed it in my previous tries but that didn't help.

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

Re: Need scripting help

#6

Post by admin »

Ok, next suggestion - the python language considers whitespace at the beginning of lines important (it denotes block nesting in the way that curly brackets - { } - do in other languages.

The code should look just like this as you enter it

Code: Select all

>>> SharpCap.SelectedCamera.GetControl(CommonPropertyIDs.Exposure).Value = 0.1
>>> def selectFirstCamera():
... 	SharpCap.SelectedCamera=SharpCap.Cameras[0]
... 
>>> SharpCap.AddCustomButton("Test", None, "Select the first camera", selectFirstCamera)
The first line after the def that starts ... instead of >>> - you have to put a tab at the beginning of that line.
The above code works correctly for me, so give it a try from scratch.

cheers,

Robin
putty tat
Posts: 8
Joined: Mon May 08, 2017 2:34 pm

Re: Need scripting help

#7

Post by putty tat »

Hi Robin,
No luck. Still getting either syntax error or unexpected tokens. Either I'm still doing something wrong or there's something corrupted in the interpreter or maybe even the compiler. Below is what I'm doing. :cry:


#List the cameras available
print SharpCap.Cameras

#Open the first camera
SharpCap.SelectedCamera = SharpCap.Cameras[0]

#List the controls on the open camera
print SharpCap.SelectedCamera.Controls

#Print the value of the first control
print SharpCap.SelectedCamera.Controls[0].Value

#Find the exposure control and set its value to 0.1
SharpCap.SelectedCamera.Controls.Find(lambda x:x.Id == CommonPropertyIDs.Exposure).Value = 0.5

Returns error:
>>>
File "", line 20
... def selectFirstCamera():

^
SyntaxError: unexpected token '.'
>>>




# SharpCap.SelectedCamera.GetControl(CommonPropertyIDs.Exposure).Value = 0.5
... def selectFirstCamera():
... SharpCap.SelectedCamera=SharpCap.Cameras[0]
...
>>> SharpCap.AddCustomButton("Test", None, "Select the first camera", selectFirstCamera)



Best regards,

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

Re: Need scripting help

#8

Post by admin »

Ah, I think you are typing the '.' and '>' characters at the beginning of the lines - you don't want to type those as they are the prompts that the Python interpreter puts up to show you where and what to type.
Capture.PNG
Capture.PNG (13.32 KiB) Viewed 4992 times
I have highlighted in yellow the text that I actually typed in the screenshot above. Note that there is a tab typed at the beginning of the SharpCap.SelectedCamera = SharpCap.Cameras[0] line. Also that I needed to press <Enter> twice after that line - once to get the second '...' prompt and a second time to get back to the >>> prompt. If I'd have wanted to put more lines of code in my function definition, I'd have continued to press tab at the beginning of each line where prompted with ...

hope this helps,

Robin
putty tat
Posts: 8
Joined: Mon May 08, 2017 2:34 pm

Re: Need scripting help

#9

Post by putty tat »

Hi Robin,

Sorry to be a P.I.T.A. So far it seems to work except for adding a button to the tool bar. When I run the script it does not create
the button. This what I run as the last lines:

#Add a custom button to the toolbar that will open the first camera when it is clicked
def selectFirstCamera():
SharpCap.SelectedCamera=SharpCap.Cameras[0]
SharpCap.AddCustomButton("Test", None, "Select the first camera", selectFirstCamera)
pgn
Posts: 17
Joined: Wed Mar 08, 2017 9:46 am

Re: Need scripting help

#10

Post by pgn »

Been learning scripting. Just to say I copied the very last 3 lines of script you posted. Indented the second one ran it and it works. At first it didn't seem to then realised I hadn't made the SharpCap window wide enough to see the button tacked onto the end.

Hope that's the final hurdle :)

And thanks - needed that button.
Post Reply