sharpcap object is not accessible in sharpcap scripting environment

Anything that doesn't fit into any of the other forums
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

sharpcap object is not accessible in sharpcap scripting environment

#1

Post by jldodge »

I am unable to access the sharpcap object in the scripting module. The following script is an example of what I am encountering in SharpCap Pro:


import time
import clr
import os

from datetime import datetime
from datetime import date
from datetime import time
from System import TimeSpan

clr.AddReference("SharpCap")

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


When I run this script (and many other variations of scripts I collected from various AI queries) I get one of the following error messages in virtually every case:


Traceback (most recent call last):
File "<string>", line 2, in <module>
AttributeError: 'SharpCap' object has no attribute 'Cameras'
>>>
Traceback (most recent call last):
File "<string>", line 2, in <module>
ImportError: No module named 'sharpcap'
>>>
Traceback (most recent call last):
File "<string>", line 13, in <module>
AttributeError: 'SharpCap' object has no attribute 'Cameras'


I have uninstalled and reinstalled SharpCap Pro, uninstalled and reinstalled camera drivers (Player One), tried using the two default cameras in SharpCap, and verified that the camera is connected and functioning properly (for example, I can change the camera settings manually but cannot modify them in a script). All of my efforts have failed. I need some help please! Thanks in advance ...
User avatar
admin
Site Admin
Posts: 17580
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: sharpcap object is not accessible in sharpcap scripting environment

#2

Post by admin »

Hi,

that's odd. Can you make sure that you have the very latest version of SharpCap 4.1 from our downloads page and then test this line of code in the script console immediate window (titled 'IronPyton Console')

Code: Select all

print (SharpCap.Cameras)
You should see a result that lists the cameras detected (in a sort of long, horizontal line of text, starting something like 'List[ICamera]([<SharpCap.Models.Camera object at 0x000000000000002B [*your first detected camera type/name*]>, ...)

I am just wondering if there is something in the start of your script that is somehow redefining or overwriting the 'SharpCap' object - for the selection of the camera to work, you shouldn't need any of the other lines - just this one

Code: Select all

SharpCap.SelectedCamera = SharpCap.Cameras[0]
That one should work in the console too. As usual with this sort of thing, find something that works, then modify it one step at a time back towards your original code until you find the change that breaks things.

cheers,

Robin
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

Re: sharpcap object is not accessible in sharpcap scripting environment

#3

Post by jldodge »

Both of the script statements worked as you indicated. However, when I run the attached script, I get the following error:

>>>
Available Cameras:
--------------------------------------------------
Traceback (most recent call last):
File "<string>", line 59, in <module>
File "<string>", line 14, in enumerate_cameras
AttributeError: 'SharpCap' object has no attribute 'Cameras'
>>>

This does not make sense to me. I have the most recent version of SharpCap, the most recent Player One Camera driver, Device Manager also "sees" the camera, and the camera is operational. Between AI research and suggestions from other users, the attached script should work. I would appreciate any help you can give me as I am "stuck" at this point in automating an imaging session with my equipment. Thanks in advance for any help.
Attachments
new 1.txt
(3.42 KiB) Downloaded 124 times
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

Re: sharpcap object is not accessible in sharpcap scripting environment

#4

Post by jldodge »

Not sure the attachment went through with my previous message.
Attachments
new 1.txt
(3.42 KiB) Downloaded 151 times
User avatar
admin
Site Admin
Posts: 17580
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: sharpcap object is not accessible in sharpcap scripting environment

#5

Post by admin »

Hi,

that script worked for me with a couple of minor changes (it's 'DeviceName', not 'Name' for the name of a camera). I don't see the error you have.

I am testing with SharpCap 4.1.13484, and I pasted your code into the bottom area of the 'Iron Python Console' window, then pressed the green 'Run' button near the right of the toolbar separating the upper and lower areas of that window. Does that match how you are testing?

cheers,

Robin


Script that works:

Code: Select all

# Camera Identification and Properties Script
# For SharpCap Pro with IronPython 3.4

import clr
clr.AddReference("System")
from System import String

def enumerate_cameras():
    """Enumerate all available cameras and their properties"""
    print("Available Cameras:")
    print("-" * 50)
    
    cameras = SharpCap.Cameras
    camera_list = []
    
    for i, camera in enumerate(cameras):
        camera_info = {
            'index': i,
            'name': camera.DeviceName,
            'device_id': camera.VideoDeviceId if hasattr(camera, 'VideoDeviceId') else 'N/A',
            'can_capture': camera.CanCapture if hasattr(camera, 'CanCapture') else False,
            'can_still_capture': camera.CanStillCapture if hasattr(camera, 'CanStillCapture') else False
        }
        
        camera_list.append(camera_info)
        print(f"Camera {i}: {camera.DeviceName}")
        print(f"  Device ID: {camera_info['device_id']}")
        print(f"  Can Capture Video: {camera_info['can_capture']}")
        print(f"  Can Capture Stills: {camera_info['can_still_capture']}")
        print()
    
    return camera_list

def find_camera_by_name(camera_name):
    """Find a camera by its name or partial name"""
    cameras = SharpCap.Cameras
    for camera in cameras:
        if camera_name.lower() in camera.Name.lower():
            return camera
    return None

def get_camera_details(camera):
    """Get detailed information about a specific camera"""
    if camera is None:
        print("No camera specified")
        return None
    
    details = {
        'name': camera.Name,
        'is_open': camera.IsOpen if hasattr(camera, 'IsOpen') else False,
        'is_previewing': camera.IsPreviewing if hasattr(camera, 'IsPreviewing') else False,
        'capturing': camera.Capturing if hasattr(camera, 'Capturing') else False
    }
    
    return details

# Execute camera enumeration
camera_list = enumerate_cameras()
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

Re: sharpcap object is not accessible in sharpcap scripting environment

#6

Post by jldodge »

I updated my SharpCap to the version you are using (I was previously on 4.1.13447.0) and made the edits to Device.Name for all instances in the script. When I ran the script, I got the following output:

Available Cameras:
--------------------------------------------------
Camera 0: Apollo-M (IMX174)
Device ID: Player One Cameras::Apollo-M (IMX174)
Can Capture Video: False
Can Capture Stills: False

Camera 1: Chicony USB2.0 Camera
Device ID: DirectShow Cameras::Chicony USB2.0 Camera
Can Capture Video: False
Can Capture Stills: False

Camera 2: ASCOM JustAHub Camera
Device ID: ASCOM Cameras::ASCOM JustAHub Camera
Can Capture Video: False
Can Capture Stills: False

Camera 3: ASCOM OmniSim Camera
Device ID: ASCOM Cameras::ASCOM OmniSim Camera
Can Capture Video: False
Can Capture Stills: False

Camera 4: Camera V3 simulator
Device ID: ASCOM Cameras::Camera V3 simulator
Can Capture Video: False
Can Capture Stills: False

Camera 5: Folder Monitor Camera
Device ID: Virtual Cameras::Folder Monitor Camera
Can Capture Video: False
Can Capture Stills: False

Camera 6: Test Camera 1 (Deep Sky)
Device ID: Test Cameras::Test Camera 1 (Deep Sky)
Can Capture Video: False
Can Capture Stills: False

Camera 7: Test Camera 2 (High Speed)
Device ID: Test Cameras::Test Camera 2 (High Speed)
Can Capture Video: False
Can Capture Stills: False

Traceback (most recent call last):
File "<string>", line 104, in <module>
File "<string>", line 73, in print_camera_controls
AttributeError: 'SharpCap' object has no attribute 'SelectedCamera'

When I executed "dir(SharpCap)" neither SelectedCamera nor Cameras was listed as an attribute.

I then ran the script a second time and got the following:

AttributeError: 'SharpCap' object has no attribute 'Cameras'

This is very frustrating for me. The SharpCap installation completes successfully and I am not doing anything outside of the SharpCap environment. It seems that it's not a problem with the camera. Would appreciate any help you can give me. Thanks again ...
Jean-Francois
Posts: 858
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

Re: sharpcap object is not accessible in sharpcap scripting environment

#7

Post by Jean-Francois »

Hello,

If I start the above script with the following setups:

(Desktop) W10 22H2 Pro, SharpCap 4.1.13484, ASCOM 6.6 SP2: I have all the cameras listed and no error at the end.
(Laptop) W10 22H2 Home, SharpCap 4.1.13447, ASCOM 7.2: I have all the cameras listed and no error at the end.

What is your setup ? W11 ?

Regards,
Jean-Francois
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

Re: sharpcap object is not accessible in sharpcap scripting environment

#8

Post by jldodge »

I am running Windows 11, 64bit.

What puzzles me is that I can run the script one time, and it works. If I then run it a second time, I get the error message. Sometimes, not all the time, I exit SharpCap and re-open it, and the script will execute successfully. However, I can also get the exact same error message. Seems a bit of hit-or-miss. I've done some AI research, since there is not much in the forum on this issue, and the responses are generally (1) a typo (checked and that is not the case), (2) outdated version of SharpCap (not the case as you already know), (3) device drivers (I have the most current device drivers AND Device Manager "sees" the camera), and (4) exit and restart SharpCap.

Number 4 seems to work but not always as I have stated. Have you tested on Windows 11? What is exiting and restarting doing, e.g. emptying cache? I'm still a bit confused since an "exit-restart" solution is not common with most software applications. Again, any help you can give me would be greatly appreciated. Thanks again ....\
User avatar
admin
Site Admin
Posts: 17580
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: sharpcap object is not accessible in sharpcap scripting environment

#9

Post by admin »

Hi,

I still think something is changing the value of the global 'SharpCap' variable so that it is no longer pointing to the main API object. If you just enter 'SharpCap' (or 'print(SharpCap)' for that matter) in the script console then you should get

Code: Select all

<SharpCap.Models.Application object at 0x000000000000002B [SharpCap.Models.Application]>
as the result (the value 0x2B might vary slightly). I would suggest you try

Code: Select all

print(SharpCap)
after you have managed to get into the broken state to see if you still have the same object in that global variable.

cheers,

Robin
jldodge
Posts: 17
Joined: Sat Dec 14, 2024 11:32 am

Re: sharpcap object is not accessible in sharpcap scripting environment

#10

Post by jldodge »

I did as you suggested:

SCRIPT #1
>>>
>>> print(SharpCap)
<SharpCap.Models.Application object at 0x000000000000002B [SharpCap.Models.Application]>
>>>
>>>
>>>
Traceback (most recent call last):
File "<string>", line 17, in <module>
NameError: name 'date' is not defined
>>>
>>> print(SharpCap)
<SharpCap.Models.Application object at 0x000000000000002B [SharpCap.Models.Application]>
>>>

SCRIPT #2
>>>
>>> print(SharpCap)
<SharpCap.Models.Application object at 0x000000000000002B [SharpCap.Models.Application]>
>>>
>>>
>>>
List[ICamera]([<SharpCap.Models.Camera object at 0x000000000000002C [Player One Cameras::Apollo-M (IMX174)]>, <SharpCap.Models.Camera object at 0x000000000000002D [DirectShow Cameras::Chicony USB2.0 Camera]>, <SharpCap.Models.Camera object at 0x000000000000002E [ASCOM Cameras::ASCOM JustAHub Camera]>, <SharpCap.Models.Camera object at 0x000000000000002F [ASCOM Cameras::ASCOM OmniSim Camera]>, <SharpCap.Models.Camera object at 0x0000000000000030 [ASCOM Cameras::Camera V3 simulator]>, <SharpCap.Models.Camera object at 0x0000000000000031 [Virtual Cameras::Folder Monitor Camera]>, <SharpCap.Models.Camera object at 0x0000000000000032 [Test Cameras::Test Camera 1 (Deep Sky)]>, <SharpCap.Models.Camera object at 0x0000000000000033 [Test Cameras::Test Camera 2 (High Speed)]>])
<SharpCap.Models.Controls object at 0x0000000000000034 [SharpCap.Models.Controls]>
Average
Traceback (most recent call last):
File "<string>", line 88, in <module>
AttributeError: 'SharpCap' object has no attribute 'SelectedCamera'
>>>
>>>
>>> print(SharpCap)
<module 'SharpCap' (CLS module, 3 assemblies loaded)>
>>>

I ran two different scripts following your suggestion. What do these error messages suggest to you?
Post Reply