Community
VRED Forum
Welcome to Autodesk’s VRED Forums. Share your knowledge, ask questions, and explore popular VRED topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't get sendToWebEngine() to work

6 REPLIES 6
Reply
Message 1 of 7
Anonymous
552 Views, 6 Replies

Can't get sendToWebEngine() to work

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.

Labels (3)
6 REPLIES 6
Message 2 of 7
j.kaestle
in reply to: Anonymous

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?

Message 3 of 7
Anonymous
in reply to: j.kaestle

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

Message 4 of 7
Anonymous
in reply to: Anonymous

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. 

Message 5 of 7
markus_keller
in reply to: Anonymous

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.

 



Markus Keller
Sr. Software Engineer
Message 6 of 7
sinje_thiedemann
in reply to: Anonymous

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

Message 7 of 7
j.kaestle
in reply to: Anonymous

Thanks a lot. It's working now. I was close to it, but just missed some details.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report