About
C4DThread is the base class for custom Cinema API threads in Cinema 4D. It can be used to perform operations on multiple cores or in the background.
- Warning
- Threads must not use any OS functions. This is strictly forbidden. The only system calls allowed are memory (de-)allocations.
-
For custom Maxon API threads see Threads Manual and Jobs Manual.
class RenderThread : public C4DThread
{
private:
BaseDocument* _doc;
BaseBitmap* _bitmap;
public:
RenderThread() { _doc = nullptr; }
~RenderThread() { this->DeleteDoc(); }
void DeleteDoc() { BaseDocument::Free(_doc); }
void SetData(BaseDocument*
const doc, BaseBitmap*
const bitmap)
{
this->DeleteDoc();
_bitmap = bitmap;
}
void Main()
{
if (_doc == nullptr)
return;
if (_bitmap)
{
RenderData* const rdata = _doc->GetActiveRenderData();
const BaseContainer renderSettings = rdata->GetDataInstanceRef();
BaseThread*
const thread = this->
Get();
}
this->DeleteDoc();
}
const Char* GetThreadName() {
return "RenderThread"; }
};
class RenderThreadMessages : public MessageData
{
Bool CoreMessage(
Int32 id,
const BaseContainer& bc)
{
if (id == CUSTOM_ID_RENDER_FINISH)
{
if (g_displayBitmap)
{
}
}
return true;
}
};
PyCompilerFlags * flags
Definition: ast.h:14
NODOCUMENTCLONE
Set to avoid an automatic clone of the scene sent to RenderDocument().
Definition: ge_prepass.h:2
RENDERFLAGS
Definition: ge_prepass.h:4725
maxon::Char Char
Definition: ge_sys_math.h:47
maxon::Bool Bool
Definition: ge_sys_math.h:46
Bool ShowBitmap(const Filename &fn)
void SpecialEventAdd(Int32 messageid, UInt p1=0, UInt p2=0)
maxon::Int32 Int32
Definition: ge_sys_math.h:51
RENDERRESULT RenderDocument(BaseDocument *doc, const BaseContainer &rdata, ProgressHook *prog, void *private_data, BaseBitmap *bmp, RENDERFLAGS renderflags, BaseThread *th, WriteProgressHook *wprog=nullptr, void *data=nullptr)
const Class< R > & Get(const Id &cls)
Definition: objectbase.h:2090
const char * doc
Definition: pyerrors.h:226
if (g_renderThread == nullptr)
{
{
if (g_renderThread == nullptr)
return false;
}
}
if (g_renderThread->IsRunning())
{
g_renderThread->End(true);
}
g_renderThread->SetData(documentClone, g_displayBitmap);
const Bool started = g_renderThread->Start();
#define ifnoerr(...)
The opposite of iferr.
Definition: errorbase.h:393
#define NewObj(T,...)
Definition: newobj.h:108
Creation
A custom thread class is created by implementing a class based on C4DThread:
class ExampleThread : public C4DThread
{
public:
ExampleThread() { }
virtual void Main() { }
virtual const Char* GetThreadName()
{
return "ExampleThread";
}
};
Now an instance of this custom thread class can be created and used:
ExampleThread* g_exampleThread = nullptr;
{
if (g_exampleThread == nullptr)
{
}
}
{
});
return OK
Definition: apibase.h:2771
#define MAXON_INITIALIZATION(...)
Definition: module.h:877
#define DeleteObj(obj)
Definition: newobj.h:159
#define iferr_scope
Definition: resultbase.h:1396
#define iferr_return
Definition: resultbase.h:1531
Custom Threads
A custom thread class has to implement these virtual functions:
- C4DThread::Main(): The main function of the thread that is executed when the thread is started.
- C4DThread::GetThreadName(): Returns the thread name.
- C4DThread::TestDBreak(): The implementation of BaseThread::TestBreak() has to return false if the thread should stop.
void Main()
{
{
if (this->TestBreak())
return;
}
}
const Char* GetThreadName()
{
return "ExampleCustomThread";
}
{
if (_startTime == -1)
return true;
if ((time - _startTime) > 20000)
return true;
return false;
}
Py_ssize_t i
Definition: abstract.h:645
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
void GeSleep(Int32 milliseconds)
Int32 GeGetTimer()
Definition: c4d_general.h:453
- Note
- To send a message from a thread to the main thread use SpecialEventAdd(). See Core Messages Manual.
Use
The custom thread can be started and stopped with these functions:
- C4DThread::Start(): Starts the custom thread.
- C4DThread::End(): Stops the custom thread.
const Bool started = g_exampleThread->Start();
if (started == false)
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
- Note
- The THREADPRIORITY should typically not be changed.
Read
Further thread properties are accessed with:
- C4DThread::Get(): Returns the corresponding BaseThread for the custom thread.
- C4DThread::IsRunning(): Returns true if the thread is running.
- C4DThread::TestBreak(): Returns true if the thread encountered a break condition.
- C4DThread::Wait(): Waits until the thread finished.
if (g_exampleThread->IsRunning())
{
g_exampleThread->End(true);
}
Further Reading