Configuring an HTML Page to Trigger Variants and Receive Variant State Information from a 3D HTML Display Actor

A 3D HTML display actor is a 3D viewer displaying the content of a website with which you can interact in the presentation mode. You can configure an HTML page to trigger variants from a 3D HTML display actor and to receive variant state changes.

When a variant state changes, Product Perception Experience sends Json messages to the HTML page. Your HTML page can listen to these messages and react accordingly, either to make realtime changes in the 3D scene or to update the HMI.

Important: Configuring bidirectional communication between the HTML page and Product Perception Experience requires basic HTML and JavaScript knowledge. Product Perception Experience does not automatically generate HTML and JavaScript logic to react to variant state events. In addition, viewpoint variant activation events are not always meaningful in VR mode because the viewpoint changes constantly. Panel names, variant line names, variant names, and variant types are case-sensitive.


Before you begin: Create an HTML page.
See Also
Creating 3D HTML Display Actors
  1. To trigger variants from the HTML page on a 3D HTML display actor, configure your HTML and Javascript to send commands with Json strings.

    The Json commands are sent in the following format:

    {
      "Panel": "Name of the panel",
      "Variant Line": "Name of the variant line",
      "Variant Name": either "Name of the variant", "Next", or "Previous"
    }

    Three types of messages are supported when triggering variants from the HTML page:

    • "Panel" "Variant Line" "Variant Name": Triggers the specified variant.
    • "Panel" "Variant Line" "NEXT": Triggers the next variant in the line. If no variant is currently active, triggers the first variant in the line.
    • "Panel" "Variant Line" "PREVIOUS": Triggers the previous variant in the line. If no variant is currently active, triggers the last variant in the line.

    "Panel" is optional. A command only containing variant line and name is equally valid. Instead of a variant name, you can also send "Variant Name": NEXT or "Variant Name": PREVIOUS.

    You can copy the code below and customize it as required.

    /**
     * Sends a message to default PPX HTML feedback channel. 
     * 
     * @param {string} msg - JSON string containing syntactically correct variant switching command.
     */
    function sendPPXMessage(msg) {
        if (typeof dscef != 'undefined' && dscef.sendString) {
            dscef.sendString('ppx_msg', msg);
        } else {
            console.warn('dscef.sendString is not available or JSON is empty.');
        }
    };
    
    /**
     * Creates and sends a JSON string to 3DEXPERIENCE, Product Perception Experience app to  
     * trigger a variant switch.
     *
     * @param {string} variantLine - The name of a variant line within the targeted PPX scene.
     * @param {string} variantName - The name of a variant state with the targeted variant line.
     * @param {string} [variantPanel=undefined] - The name of the variant panel containing the specified variant line.
     */
    function assembleJSON(variantLine, variantName, variantPanel = undefined) {
        let msg;
        if (variantPanel) {
            msg = JSON.stringify(
                {
                    'Panel': variantPanel,
                    'Variant Line': variantLine,
                    'Variant Name': variantName
                }
            );
        } else {
            msg = JSON.stringify(
                {
                    'Variant Line': variantLine,
                    'Variant Name': variantName
                }
            );
        }
        return msg
    }
    
    /**
     * Sends a variant control command to PPX. Does not check for malformed commands.
     * 
     * @param {string} variantLine 
     * @param {string} variantName 
     * @param {string} [variantPanel=undefined] - Name of the variant panel. Optional, can be omitted.
     */
    function switchLineToVariant(variantLine, variantName, variantPanel = undefined) {
        msg = assembleJSON(variantLine, variantName, variantPanel)
        sendPPXMessage(msg)
    }
    
    function switchLineNext(variantLine, variantPanel = undefined) {
        switchLineToVariant(variantLine, 'Next', variantPanel)
    }
    
    function switchLinePrevious(variantLine, variantPanel = undefined) {
        switchLineToVariant(variantLine, 'Previous', variantPanel)
    }

    Then in your HTML, call one of the following functions to send a variant triggering command:

    function switchLineToVariant(variantLine, variantName, variantPanel = undefined) {
        msg = assembleJSON(variantLine, variantName, variantPanel)
        sendPPXMessage(msg)
    }
    
    function switchLineNext(variantLine, variantPanel = undefined) {
        switchLineToVariant(variantLine, 'Next', variantPanel)
    }
    
    function switchLinePrevious(variantLine, variantPanel = undefined) {
        switchLineToVariant(variantLine, 'Previous', variantPanel)
    }

    These functions allow you to switch a variant line to a specific variant or to switch a variant line to its next or previous variant. For all three functions, the variant Panel argument is optional and can be omitted.

    One way to call a function based on user interaction, like clicking on a button, is to add the function call as the value of the onclick attribute to your HTML, for example:

    <button class="btn" id="redLightsON" onclick="switchLineToVariant('Lights', 'Red')

    The example below shows the integration of a "security code" HTML page in the 3D scene.



  2. To listen to variant state events, subscribe to the PPX_OnMsgReceived event in your HTML page JavaScript.

    Important: Subscribe to this event for each 3D HTML display actor individually.

    When a variant state changes, Product Perception Experience sends a Json message to the HTML page. Only one state value is sent at a time.

    The Json message contains the following keys:

    • Variant Line: the name of the variant line.
    • Variant Name: the name of the variant state.
    • Variant Type: the type of variant (for example, position, viewpoint, product, or stage).
    • Panel Name: the name of the variant panel.
    • State: the variant lifecycle state. Possible values are:
      • transitioningTo
      • switchComplete
      • leftState
      • isPlaying and hasStopped (applicable only to kinematics-based position variants, physics simulation variants, and human-animation-based human variants)

    Example:

    {
      "Variant Line": "Position Line 1",
      "Variant Name": "Robot Arm Up",
      "Variant Type": "Position",
      "Panel Name": "Robot Arm Positions",
      "State": "switchComplete"
    }

    You can copy the code below and customize it as required.

    document.addEventListener('PPX_OnMsgReceived', function (event) {
      const inputData = event.detail;
      if (!inputData) return;
    
      let loadData;
      try {
        loadData = JSON.parse(inputData);
      } catch(e) {
        console.error("Invalid JSON received:", inputData);
        return;
      }
    
      // React to loadData.Variant Line, loadData.Variant Name,
      // loadData.Variant Type, and loadData.State
    });

    The example below shows the HTML viewer receiving variant state information dynamically.



Result:

Tip: The system logs processed HTML viewer feedback commands. You can access the log of the operations performed using the 3D HTML Display Console. This console displays the messages that the 3D HTML viewer receives when switching variants in the presentation mode. If a command is invalid or cannot be processed, an error message is registered in the console (for example, InvalidCommand or InvalidMessageId).

In the Presentation section of the action bar, click 3D HTML Display Console .