How to render to texture by script

I’ve been trying to create preview image for every reource in the editor:
https://discourse.urho3d.io/t/quick-expriment-on-editor/3571

Rendertarget is needed to generate the preview image for model/material… However, it seems that I need to wait for the next frame to get the texture ready. Is there a way to get the render result immediately? (Maybe engine->Update/engine->Render will work, which I haven’t tryied. But they cannot be called from script) If so, we could add such a function to the View3D.

Edit: BTW, Constructing a Scene object in ResourceBackgroundLoaded event handler will result addref related crash which is very strange…

Thanks.

I used View::Render.

Stack and/or STR?

Call Stack:

crash


Script code:

// called from HandleResourceBackgroundLoaded
Texture@ TakePhoto(Model@ model)
{
    Scene@ previewScene = Scene("previewScene"); // crash happens, I cannot create Scene here
    
    // this can work, previewScenes are pre-created in initialization phase
    //Scene@ previewScene = previewScenes[scene_index++];
    
    // ...
}

Well, scene doesn’t have constructor from string.

Yes, it has.

RegisterNamedObjectConstructor<Scene>(engine, "Scene");

/// Template function for registering a named constructor for a class derived from Object.
template <class T> void RegisterNamedObjectConstructor(asIScriptEngine* engine, const char* className)
{
    String declFactoryWithName(String(className) + "@+ f(const String&in)");
    engine->RegisterObjectBehaviour(className, asBEHAVE_FACTORY, declFactoryWithName.CString(), asFUNCTION(ConstructNamedObject<T>), asCALL_CDECL);
}

Yeah, I missed it… I mean, try not to use it.
I have no idea why it crashes, however.

Yes, it’s annoying. I have no idea till now. So I have to work around, dirty…:sweat_smile:

PreviewRenderer@ GetFreePreviewRenderer()
{
    for (uint i = 0; i < previewRenderers.length; ++i)
    {
        PreviewRenderer@ renderer = previewRenderers[i]; 
        if (renderer.state == PREVIEW_FREE)
            return renderer;
    }

    return null;
}

Scene has default ctor that works perfercty. Also, you may just wait until I merge the fix.

Shall be fixed now…

Thank you Eugene. That’s nice :slight_smile: