So I'm using a Webengine in a sceneplate to display some HTML
I need to send some data to the HTML page, the only way to do this i've found is with the sendToWebEngine() function for python. For now im just trying to test it, trying to get a simple response. It's neither throwing an error message, nor is it doing what i want it to.
Hi,
I have the same problem with VRED 2021.3. When I open our scenes with pre-2021.3 Versions of VRED, it's working fine.
I found, that I have to change the order in Java script of defining the event listener and the function to be called. Then it was possible to call the function (sendToWebEngine(name, event, "")). But it is still not possible to send data to html.
Is there anybody who managed it to make it work in VRED 2021.3?
I have the opposite Problem whenever I try it in a pre 2021.3 version it throws an error, telling me "vrWebengineService is not defined"
in 2021.3 i can get it to send an event, but just like you said, as soon as I try to send anything more than an empty string "" it doesnt send anything at all
so I figured out, that although the function requires strings it does not accept strings that have characters in them, only ones that have ints, or floats. for axample
sendToWebEngine("ColorPickerOverlay","sendColor", "hello 123")
will not work, but
sendToWebEngine("ColorPickerOverlay","sendColor", "123")
or
sendToWebEngine("ColorPickerOverlay","sendColor", "0.5")
will work.
Hi,
Yes, unfortunately there was a bug prior to 2021.3 that resulted in the "data" parameter being ignored in the Javascript code.
If you don't want to pass data to the event, you need to use an empty string as data parameter.
You can refer to the documentation on CustomEvent here:
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
If you want to call a Python function in VRED from a Javascript event you can do it like this:
Python code in VRED:
def printFromJavascript(data):
print(data)
Html Example (I just copied this into a Web Frontplate named "EventFrontPlate":
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Custom Event</title>
</head>
<body style="background-color:white;">
<div class="note">JavaScript Custom Event</div>
<script>
document.addEventListener("testEvent", function(event) {
vred.executePython("printFromJavascript('" + event.detail.custom + "')");
});
</script>
</body>
</html>
And now in VRED you can call the event like this:
#new (V2) API
we = vrWebEngineService.getWebEngine("EventFrontPlate")
we.sendEvent("testEvent", "{custom: 'hello 123'}")
we.sendEvent("testEvent", "{custom: 'hello'}")
we.sendEvent("testEvent", "{custom: '123'}")
#old (V1) API
sendToWebEngine("EventFrontPlate", "testEvent", "{custom: 'hello 123'}")
sendToWebEngine("EventFrontPlate", "testEvent", "{custom: 'hello'}")
sendToWebEngine("EventFrontPlate", "testEvent", "{custom: '123'}")
This should print "hello 123", "hello" and "123" to the VRED console.
Hello,
there was indeed a change in version 2021.3 with regards to the data parameter in sendToWebengine. The vrdWebEngine and service API was added in 2021.3 as well.
TL;DR
To reproduce the behavior from 2021.2 and before, put extra apostrophes around your string in the data parameter ( "'hello 123'"):
sendToWebEngine("ColorPickerOverlay","sendColor", "'hello 123'")
More documentation can be found here: Custom Javascript events in Webengines
Since 2021.3 the content of the data parameter is directly injected into code, as in the linked documentation:
var e = new CustomEvent( event
{
detail: data
});
which would become
detail : hello 123
with data = "hello 123", which is no valid code.
With data = "'hello 123'" it becomes
detail : 'hello 123'
which is valid.
As shown by Markus, this allows you to setup more complex parameters with dictionaries.
Kind regards
Sinje
Can't find what you're looking for? Ask the community or share your knowledge.