c4d.threading

class c4d.threading
Programming a lower level thread is not as easy for the most people so this module helps to minimize the effort to run or handle code in another Thread.
Python is shipped with a standard module which already supports a Thread class, but Cinema 4D and Python use different implementations.
Use this module instead of the Python standard module, since all functions in this module are wrapped from the Cinema 4D C++ SDK and therefore stable.

See also

The important Threading Information.

Types

Functions Signatures

c4d.threading.GeThreadLock()
Locks using a global semaphore.
c4d.threading.GeThreadUnlock() Continues blocked threads after a call to GeThreadLock().
c4d.threading.GeIsMainThread() Checks if the code is run from within the main thread of Cinema 4D.
c4d.threading.GeIsMainThreadAndNoDrawThread() Checks if code is run from within the main thread of Cinema 4D and if the main thread does not execute any drawing code currently.
c4d.threading.GeGetCurrentThreadCount() Gets the number of threads for the current render context.
c4d.threading.GeGetCurrentThreadId() Returns a unique ID for the current thread.
c4d.threading.IdentifyThread(bt) Identifies a thread’s type.
c4d.threading.GeGetCurrentThread() Returns the current thread.
c4d.threading.GeGetDummyThread() Returns a dummy thread. The thread’s BaseThread.TestBreak() function always returns False.
c4d.threading.GeGetEscTestThread()
Returns a dummy thread for escape key testing.
c4d.threading.GeCheckBackgroundThreadsRunning(typeclass, all) Checks if any of the threads matching typeclass is running.
c4d.threading.GeStopBackgroundThreads(typeclass, flags) Stops all running background threads of the given typeclass.

Functions Documentation

c4d.threading.GeThreadLock()
Locks using a global semaphore.
When locked, any other thread trying to acquire the lock will have to wait.
Other threads will continue.

Note

As this blocks all threads it should only be used if and when necessary.
A local semaphore is a more elegant and efficient solution to multiple thread data access.
c4d.threading.GeThreadUnlock()

Continues blocked threads after a call to GeThreadLock().

c4d.threading.GeIsMainThread()

Checks if the code is run from within the main thread of Cinema 4D.

Return type:bool
Returns:True if called from the main thread, otherwise False.
c4d.threading.GeIsMainThreadAndNoDrawThread()

Checks if code is run from within the main thread of Cinema 4D and if the main thread does not execute any drawing code currently.

New in version R16.038.

Note

This routine can be used to make sure that no illegal code is called during a drawing operation.
In Cinema 4D the drawing will be started threaded or non-threaded, depending on the situation.
It is not allowed to add e.g. undo or delete objects or materials while the drawing is in progress (this would lead to immediate crashes).
If code calls other routines that are not aware of their context (e.g. some code within a Message() that does not know whether it was called from a drawing thread or during a command call).
GeIsMainThreadAndNoDrawThread() can be used to detect the correct situation.
Return type:bool
Returns:True if called from the main thread and main thread does not execute a drawing operation, otherwise False.
c4d.threading.GeGetCurrentThreadCount()

Gets the number of threads for the current render context.

New in version R16.021.

Return type:int
Returns:The number of current threads.
c4d.threading.GeGetCurrentThreadId()

Returns a unique ID for the current thread.

Note

Usually do not care about this.

Return type:int
Returns:The unique ID for the current thread.
c4d.threading.IdentifyThread(bt)

Identifies a thread’s type.

Parameters:bt (BaseThread) – The thread to identify.
Return type:int
Returns:The thread type:
THREADTYPE_NONE None.
THREADTYPE_EDITORREDRAW Editor redraw.
THREADTYPE_RENDEREDITOR Editor render.
THREADTYPE_RENDEREXTERNAL External render.
c4d.threading.GeGetCurrentThread()

Returns the current thread.

Return type:BaseThread
Returns:The current thread.
c4d.threading.GeGetDummyThread()

Returns a dummy thread. The thread’s BaseThread.TestBreak() function always returns False.

New in version R17.048.

Return type:BaseThread
Returns:A dummy thread .
c4d.threading.GeGetEscTestThread()
Returns a dummy thread for escape key testing.
The thread’s BaseThread.TestBreak() function returns True when the user presses the escape key.

New in version R17.048.

Return type:BaseThread
Returns:An escape key test thread.
c4d.threading.GeCheckBackgroundThreadsRunning(typeclass, all)

Checks if any of the threads matching typeclass is running.

Parameters:
  • typeclass (int) –
    The type class of the background threads to check. If 0, all threads are checked. Pass BACKGROUNDHANDLER_TYPECLASS_C4D to check Cinema 4D background handlers.

    For example GeCheckBackgroundThreadsRunning(BACKGROUNDHANDLER_TYPECLASS_C4D, True) checks if Cinema 4D is doing anything right now.
    If False were passed it would not check for the external renderer (which are always running in a BodyPaint 3D selection).
  • all (bool) – If True then background handler with negative priority are also checked.
Return type:

bool

Returns:

True if the specified threads are running, otherwise False.

c4d.threading.GeStopBackgroundThreads(typeclass, flags)

Stops all running background threads of the given typeclass.

Parameters:
  • typeclass (int) – The type class of the background threads to stop. If 0, all threads are stopped. Pass BACKGROUNDHANDLER_TYPECLASS_C4D to stop Cinema 4D background handlers.
  • flags (int) –
    If typeclass is BACKGROUNDHANDLER_TYPECLASS_C4D then the following flags can be used:
    BACKGROUNDHANDLERFLAGS_NONE None.
    BACKGROUNDHANDLERFLAGS_VIEWREDRAW View redraw.
    BACKGROUNDHANDLERFLAGS_EDITORRENDDER Editor render.
    BACKGROUNDHANDLERFLAGS_MATERIALPREVIEW Material preview.
    BACKGROUNDHANDLERFLAGS_RENDEREXTERNAL Render external.
    BACKGROUNDHANDLERFLAGS_PRIVATE_VIEWREDRAW Private.
    BACKGROUNDHANDLERFLAGS_SHUTDOWN Shutdown.

    GeStopBackgroundThreads(BACKGROUNDHANDLER_TYPECLASS_C4D, BACKGROUNDHANDLER_FLAGS_EDITORRENDDER) only stops the editor renderer (if it was running).
    GeStopBackgroundThreads(0, BACKGROUNDHANDLERFLAGS_SHUTDOWN) kills anything running.

    Pass flags as needed for custom type classes, they will be routed to the background handler function.