On 23/07/2015 at 08:21, xxxxxxxx wrote:
Hi Mohamed,
finally I found the time to play around with this. Sorry, it took so long.
As said before, the problem is, that MATPREVIEW_GENERATE_IMAGE is send to a clone of your material. So you can't reset the flag, because you only reset it in the clone, not the original material.
My admittedly quite hacky approach is to have a BaseLink member (_linkOrig) in the material. Plus another boolean member (_flagRequestPreview) storing the "render request" flag.
In Init() I initialize as follows:
_linkOrig->SetLink(nullptr);
_flagRequestPreview = true; // assuming that a first initial preview is always wanted
When the material is copied (CopyTo()), I set the link. Either to the source material (if not previously set) or to the stored link. Like so:
MaterialPreviewExample* sdata = static_cast<MaterialPreviewExample*>(snode->GetNodeData());
BaseList2D* bl2d = sdata->_linkOrig->GetLink(snode->GetDocument());
if (bl2d)
static_cast<MaterialPreviewExample*>(dest)->_linkOriginalMaterial->SetLink(bl2d);
else
static_cast<MaterialPreviewExample*>(dest)->_linkOriginalMaterial->SetLink(snode);
In MSG_DESCRIPTION_COMMAND I do basically the same as you, simply setting _flagRequestPreview. Of course one could use a checkbox instead of two buttons, but that may be a matter of taste.
Then on MATPREVIEW_GENERATE_IMAGE I simply evaluate the link and use/reset the flag of the original material. Like so:
BaseList2D* bl2d = _linkOrig->GetLink(node->GetDocument());
if (!bl2d)
bl2d = _linkOrig->ForceGetLink(); // this will be the most probable path, as preview is rendered in another document
Bool flagRender;
if (bl2d)
{
MaterialPreviewExample* dOrig = static_cast<MaterialPreviewExample*>(bl2d->GetNodeData());
flagRender = dOrig->_flagRequestPreview;
dOrig->_flagRequestPreview = false;
}
else
{
flagRender = _flagRequestPreview;
_flagRequestPreview = false;
}
// Here: flagRender is true, only once after the button got pushed
As I said, it's a bit hacky. But as it is only used for rendering of previews, I think, it should work ok.
Let me know, what you think.
Another option would be the use of message MATPREVIEW_PREPARE_SCENE. As far as I can see, this message is still send to the original material. But if you get this message, heavily depends on your preview implementation.