Script result to SDK how to (SOLVED)
hcsaba68
Posts: 12
Hi all,
How can I get the result of DzScript ?
QVariant result;QString str;DzScript *Script = new DzScript;Script->clear();Script->addLine("var oViewportMgr = MainWindow.getViewportMgr();");Script->addLine("var oViewport = oViewportMgr.getActiveViewport().get3DViewport();");Script->addLine("var oCamera = oViewport.getCamera();");Script->addLine("print(String(\"%1 : \").arg(oCamera.pixelsWidth));");//result=Script->result();result.setValue(Script->result());if (Script->execute() == true) { dzApp->statusLine("Script OK", FALSE); //str = result.toString();str = "Success";}else { dzApp->statusLine(Script->errorMessage(), FALSE);str = "Fail";}
I can get Success, but I can not convert to C++ value.
Thanks.
Post edited by hcsaba68 on
Comments
EDIT:
Actually, forget everything I said below. I think it is a matter of syntax in your script lines. Try this:
Test those lines in the DazStudio IDE Script panel and check the "result: XXXX" in the output to see if it is what you want. This value should hopefully be returned by the evaluate() function in C++.
BTW, it looks like this is the same value as the resolution configured in the Render Options panel. If that is the value you want, then using this script requires you to add a Camera to your scene. If you are trying to query render options from a DzRenderer (like yaluxplug), then use the DzRenderOptions argument that is passed to the DzRenderer::render (DzRenderHandler *handler, DzCamera *camera, const DzRenderOptions &opt) function. That way, you are not required to have a Camera in the scene.
Disregard Below (maybe)
I think the above code might be failing because the result can't be put into a QScriptValue data object. (https://doc.qt.io/archives/qt-4.8/qscriptvalue.html)
If you are calling the DzScript from C++ and want to pass the value back into C++ class that called it, then you can expose a C++ "Set Return Value" function to DzScript and use that to pass the value back. Note, that in order to pass a value back that is a class or other complex data structure, then you will need to do extra class handling.
A theoretical atlernative: you could possibly extend QScriptValue, but I think that would require also extending the DzScript engine.
Here is an example that I used to pass a generic object back to C++ and then dynamically cast it into whatever I want:
C++ declaration for exposed functions for script to use:
https://github.com/danielbui78/DazToRuntime/blob/9b4960f855dc1ee4b4741e6fd081205ff6d52092/Common/DzRuntimePluginAction.h#L16-L19
https://github.com/danielbui78/DazToRuntime/blob/9b4960f855dc1ee4b4741e6fd081205ff6d52092/Common/DzRuntimePluginAction.h#L49-L54
C++ definition for exposed functions for script to use:
https://github.com/danielbui78/DazToRuntime/blob/9b4960f855dc1ee4b4741e6fd081205ff6d52092/Common/DzRuntimePluginAction.cpp#L279-L306
C++ connection of signal to slot:
https://github.com/danielbui78/DazToRuntime/blob/9b4960f855dc1ee4b4741e6fd081205ff6d52092/Common/DzRuntimePluginAction.cpp#L49-L50
Script:
https://github.com/danielbui78/DazToRuntime/blob/unofficial-dtu/Unity/DazStudioPlugin/Resources/ScriptFunctionFindSimulationSettingsProvider.dsa
C++ usage (call script and retrieve script value in C++):
https://github.com/danielbui78/DazToRuntime/blob/9b4960f855dc1ee4b4741e6fd081205ff6d52092/Unity/DazStudioPlugin/DzUnityAction.cpp#L664-L730
General information on making applications scriptable: https://doc.qt.io/archives/qt-4.8/scripting.html
I have no experience with this, but it seems suspicious that the script is not returning a value and so might be returning a default value. Is there an exit() function or something similar?
Are you sure about calling Script->result() before Script->execute()? The QVariant might hold nothing, instead of an integer value, causing toInt() to fail.
An aside: It is modern C++ style to declare your variables not at the top of the function as in C, but rather at the point of initialization, and by using the auto keyword to make the compiler deduce the correct types automatically... the less explicit types there are for one to get wrong or have to change in the future, the better.
Thank You for all !
Daniel version working well (returnval;)
Thaks again !