mac m1 and vtk

i’m trying to run this function using vtk on a mac m1. i think the problem is with my OS and not the code itself since on another OS like windows it works

def get_thumbnail(PDF, axis):
    ## some code that work and create the actor
    renderer = vtkRenderer()  
    renderer.AddActor(actor)

    window = vtkRenderWindow()  

    window.SetOffScreenRendering(1)

    window.AddRenderer(renderer)


    interactor = vtkRenderWindowInteractor()  

    interactor.SetInteractorStyle(vtkInteractorStyleImage())  
    window.SetInteractor(interactor)
    ## HERE THE CODE GIVES THE ERROR
    window.Render()
    
    w2if = vtkWindowToImageFilter()  # noqa
    w2if.SetInput(window)
    w2if.Update()

    return w2if

getting the following error the problem suggest that i have to handle the invalidation on the Main Thread is there a way to do so that provide vtk itself

2024-02-11 21:29:43.958 python[29661:3966927] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
        0   CoreFoundation                      0x000000018cb27154 __exceptionPreprocess + 176
        1   libobjc.A.dylib                     0x000000018c6464d4 objc_exception_throw + 60
        2   CoreFoundation                      0x000000018cb503a0 _CFBundleGetValueForInfoKey + 0
        3   AppKit                              0x000000018fcf0a00 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 320
        4   AppKit                              0x000000018fcdecd4 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 816
        5   AppKit                              0x000000018fcde998 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
        6   libvtkRenderingOpenGL2-9.3.dylib    0x000000010ad8fdf8 _ZN20vtkCocoaRenderWindow13CreateAWindowEv + 1280
        7   libvtkRenderingOpenGL2-9.3.dylib    0x000000010ad90440 _ZN20vtkCocoaRenderWindow5StartEv + 64
        8   libvtkRenderingUI-9.3.dylib         0x0000000104ff13b0 _ZN30vtkCocoaRenderWindowInteractor10InitializeEv + 384
        9   libvtkRenderingCore-9.3.dylib       0x0000000106ea4574 _ZN15vtkRenderWindow6RenderEv + 148
        10  libvtkRenderingOpenGL2-9.3.dylib    0x000000010ae2f41c _ZN21vtkOpenGLRenderWindow6RenderEv + 104
        11  libvtkRenderingOpenGL2-9.3.dylib    0x000000010ad904d8 _ZN20vtkCocoaRenderWindow6RenderEv + 24
        12  vtkRenderingOpenGL2.cpython-39-darw 0x000000010e5c8cb4 _ZL29PyvtkCocoaRenderWindow_RenderP7_objectS0_ + 136
        13  libpython3.9.dylib                  0x000000010291e870 cfunction_call + 172
        14  libpython3.9.dylib                  0x00000001028dab70 _PyObject_MakeTpCall + 360
        15  libpython3.9.dylib                  0x00000001029ad25c call_function + 536
        16  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        17  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        18  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        19  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        20  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        21  libpython3.9.dylib                  0x00000001029aa648 _PyEval_EvalFrameDefault + 23396
        22  libpython3.9.dylib                  0x00000001029ae0a4 _PyEval_EvalCode + 2860
        23  libpython3.9.dylib                  0x00000001028db294 _PyFunction_Vectorcall + 236
        24  libpython3.9.dylib                  0x00000001029aa648 _PyEval_EvalFrameDefault + 23396
        25  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        26  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        27  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        28  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        29  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        30  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        31  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        32  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        33  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        34  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        35  libpython3.9.dylib                  0x00000001028da998 _PyObject_FastCallDictTstate + 272
        36  libpython3.9.dylib                  0x00000001028db674 _PyObject_Call_Prepend + 148
        37  libpython3.9.dylib                  0x000000010293ef7c slot_tp_call + 104
        38  libpython3.9.dylib                  0x00000001028dab70 _PyObject_MakeTpCall + 360
        39  libpython3.9.dylib                  0x00000001029ad25c call_function + 536
        40  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        41  libpython3.9.dylib                  0x00000001028ee260 gen_send_ex + 308
        42  libpython3.9.dylib                  0x00000001029a9da0 _PyEval_EvalFrameDefault + 21180
        43  libpython3.9.dylib                  0x00000001029ae0a4 _PyEval_EvalCode + 2860
        44  libpython3.9.dylib                  0x00000001028db294 _PyFunction_Vectorcall + 236
        45  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        46  libpython3.9.dylib                  0x00000001029aa3f0 _PyEval_EvalFrameDefault + 22796
        47  libpython3.9.dylib                  0x00000001029ae0a4 _PyEval_EvalCode + 2860
        48  libpython3.9.dylib                  0x00000001028db294 _PyFunction_Vectorcall + 236
        49  libpython3.9.dylib                  0x00000001028dd448 method_vectorcall + 168
        50  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        51  libpython3.9.dylib                  0x00000001029aa3f0 _PyEval_EvalFrameDefault + 22796
        52  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        53  libpython3.9.dylib                  0x00000001028dd448 method_vectorcall + 168
        54  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        55  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        56  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        57  libpython3.9.dylib                  0x00000001028dd448 method_vectorcall + 168
        58  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        59  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        60  libpython3.9.dylib                  0x00000001029ae0a4 _PyEval_EvalCode + 2860
        61  libpython3.9.dylib                  0x00000001028db294 _PyFunction_Vectorcall + 236
        62  libpython3.9.dylib                  0x00000001028dd448 method_vectorcall + 168
        63  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        64  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        65  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        66  libpython3.9.dylib                  0x00000001028da998 _PyObject_FastCallDictTstate + 272
        67  libpython3.9.dylib                  0x00000001028db674 _PyObject_Call_Prepend + 148
        68  libpython3.9.dylib                  0x0000000102940320 slot_tp_init + 104
        69  libpython3.9.dylib                  0x0000000102937800 type_call + 372
        70  libpython3.9.dylib                  0x00000001028dab70 _PyObject_MakeTpCall + 360
        71  libpython3.9.dylib                  0x00000001029ad25c call_function + 536
        72  libpython3.9.dylib                  0x00000001029aa374 _PyEval_EvalFrameDefault + 22672
        73  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        74  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        75  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        76  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        77  libpython3.9.dylib                  0x00000001028dd55c method_vectorcall + 444
        78  libpython3.9.dylib                  0x00000001029aa648 _PyEval_EvalFrameDefault + 23396
        79  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        80  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        81  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        82  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        83  libpython3.9.dylib                  0x00000001029ad1fc call_function + 440
        84  libpython3.9.dylib                  0x00000001029aa350 _PyEval_EvalFrameDefault + 22636
        85  libpython3.9.dylib                  0x00000001028db34c function_code_fastcall + 112
        86  libpython3.9.dylib                  0x00000001028dd4d4 method_vectorcall + 308
        87  libpython3.9.dylib                  0x0000000102a448c4 t_bootstrap + 72
        88  libpython3.9.dylib                  0x00000001029f5448 pythread_wrapper + 28
        89  libsystem_pthread.dylib             0x000000018c9cffa8 _pthread_start + 148
        90  libsystem_pthread.dylib             0x000000018c9cada0 thread_start + 8
)
libc++abi: terminating due to uncaught exception of type NSException
/Users/user/.pyenv/versions/3.9.18/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

Hello @DrMainIT

This error is known to occur when a thread that is not the main thread calls vtkRenderWindow::Render on mac. You can try to use an asyncio event loop or some other method to schedule the render call on main thread.

The main thread in this case would be the one that called int main() from the python executable.

1 Like

could you provide me an example

I don’t have a Mac, and asyncio hurts my head, but this might help?

These last few lines from your stack trace show that you called get_thumbnail() from a new thread. The simplest fix is to call that from main thread.

the problem is that i’m using flask and i’m calling this function inside a route, i think i should use the main thread of flask not pretty sure how to do it

Afraid I can’t help you with flask. If I were you, I’d start looking at their docs, examples and unit tests to see if there’s a way to schedule a function on the main thread.