Adding NoteTypes on initialisation

def install_note_types() -> None:
    def install_types() -> None:
        process_notetype = mw.col.models.by_name("Process")
        properties_notetype = mw.col.models.by_name("Properties")
        cloze_notetype = mw.col.models.by_name("Cloze")

        if process_notetype is None:
            process_notetype = cloze_notetype.copy()
            process_notetype["name"] = "Process"
            mw.col.models.add_dict(process_notetype)

        if properties_notetype is None:
            properties_notetype = cloze_notetype.copy()
            properties_notetype["name"] = "Properties"
            mw.col.models.add_dict(properties_notetype)

    t = Thread(target=install_types, args=())
    t.start()

gui_hooks.main_window_did_init.append(install_note_types)

Hi all, I have the above code that is trying to make two clones of the default Cloze notetype called Process and Properties respectively. However, it causes a panic on boot of my addon.

A fatal error occurred, and Anki must close. Please report this message on the forums.
Anki 2.1.63 (f356f177) Python 3.9.15 Qt 6.4.3 PyQt 6.4.0
Platform: Windows-10-10.0.22621
Flags: frz=True ao=True sv=?
Add-ons, last update check: 2023-10-15 16:50:21

Exception in thread Thread-4:
Traceback (most recent call last):
  File "threading", line 980, in _bootstrap_inner
  File "threading", line 917, in run
  File "C:\Users\samue\AppData\Roaming\Anki2\addons21\BetterCloze\__init__.py", line 36, in install_types
    mw.col.models.add_dict(process_notetype)
  File "anki.models", line 212, in add_dict
  File "anki._backend_generated", line 851, in add_notetype_legacy
  File "anki._backend", line 150, in _run_command
pyo3_runtime.PanicException: assertion failed: `(left == right)`
  left: `1574937728380`,
 right: `0`
Exception in threading.excepthook:
Exception ignored in thread started by: <bound method Thread._bootstrap of <Thread(Thread-4, started 11100)>>
Traceback (most recent call last):
  File "threading", line 937, in _bootstrap
  File "threading", line 982, in _bootstrap_inner
  File "threading", line 1264, in invoke_excepthook
AttributeError: 'ErrorHandler' object has no attribute 'flush'
Exception ignored in sys.unraisablehook: <built-in function unraisablehook>
AttributeError: 'ErrorHandler' object has no attribute 'flush'

Any suggestions on how I can achieve my goal?

The error occurs because Anki expects new notetypes to have an ID of 0. Try using the mw.col.models.copy() function, which will take care of cloning a notetype for you and setting the ID to 0.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.