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.
To listen to variant state events, subscribe to the PPX_OnMsgReceived
event in your HTML page JavaScript.
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.