Search code examples
pythonqtpyqt5importerror

PyQt5 not detecting a Cosmographia window (Cosmoscripting import)


I am trying to use Cosmographia scripting to create videos of space probes flying through the Solar system but the scripting tool doesn't seem to work. I am having trouble importing the cosmoscripting package for the latest Cosmographia release (Version 4.0) into python.

I think the problem might have to do something with the PyQt5 library but I have never really used this library before.

Here is the beginning of the code of cosmoscripting.py

#import pyqt and swig wraper modules
from PyQt5.QtGui import *
from PyQt5 import QtCore as qtcore
from PyQt5.QtWidgets import QApplication
import time

# Find the scripting API object with its name in QT
widgets = QApplication.topLevelWidgets()
for widget in widgets:
    n = widget.objectName()
    if n == 'mainWindow':
        mainWindow = widget
children = mainWindow.children()      #This is where it crashes
for child in children:
    n = child.objectName()
    if n == 'scriptingApi':
        api = child

The import crashes at line 13 because the function QApplication.topLevelWidgets() returns an empty list. Exception has occurred: NameError name 'mainWindow' is not defined

I think that what's happening here is that the import is trying to find the Cosmographia window to work with but for some reason the PyQt5 library doesn't see it.

Did anyone experience a similar problem? Do you know how to open a program so that PyQt5/Qt detects it?


Solution

  • The cosmoscripting scripts can only be executed from the program.

    Top Menu: Menu bar > File > Run Script

    It turns out that the Run script option is missing on the Windows version of Cosmographia 4.0. However, running scripts on the Linux works.