I can not figure out how Render plugin CreatePreview() should work.
Ok, I understand the basic concept, but how can is use it asynchronously?
I use progressive render type
protected override PreviewRenderTypes PreviewRenderType()
inside CreatePreview I handle only Quality.RefineThirdPass, and I use scene.PreviewNotifier.NotifyIntermediateUpdate(RenderWindow);
to update the preview.
I can do it fine if it is in the same thread as CreatePreview. But if I open a new Thread for preview calculations I can not call NotifyIntermediateUpdate from there because it triggers an exception. I guess after I return from CreatePreview the previewnotifier is not really valid anymore so my parallel calculation can not use it.
But what is the correct process?
CreatePreview is already called on a thread. There is no need to create a separate thread for your engine in that sense, as it is already non-blocking.
This is the Plugin.cs for the RhinoRenderCycles.rhp plug-in:
At the bottom you can see how PreviewRenderEngine is initialized, then its Renderer function is directly called. It can ‘block’ here, as CreatePreview already runs in a background thread created by the preview render job queue from the RDK. At the end you get your render result as a bitmap and set it on the scene parameter passed into CreatePreview.
Reason: accessing the preview notifier should happen on the same thread it was created.
It shouldn’t block. At least in Rhino WIP with Rhino Render set as default renderer (which is Raytraced/Cycles) this works just fine. AFAICR this preview creation code hasn’t fundamentally changed from Rhino 6. While previews are rendering I can use Rhino just fine.
Maybe test with a sleep of say 10 seconds? Or does that block as well for you?