Introduction
Imagine you've converted CAD data into a viewable online format within SAP Integrated Product Development (IPD) Visualization. To optimize these visualizations for future use or authoring, you need to perform modifications. For example, cleaning up scenes, adding metadata, or creating smaller visualizations from larger ones. SAP Integrated Product Development Visualization's built-in scripting engine allows administrators to create scripts to automate these tasks, streamlining your workflow.
Data Sources, Cards, and Viewer Templates are powerful tools for building use cases within IPD Visualization. However, complex requirements may need custom code for advanced analysis or processing. The SAP Integrated Product Development Visualization scripting engine enables the creation of custom solutions to meet these needs.
Previously, scripts were executed through workflows. Now, they can be triggered directly from a visualization or during import/publish processes. While nonadmin users can run scripts, creating them requires access to the System Administration application.
Scripts
To create a new script, navigate to the System Administration tile and choose the SCRIPTS tab.

To create a new script, choose Add.

- Enter a Name and Description. The Purpose field determines when the script can be triggered:
- General: Typically used to call the script from another script.
- Import: Used during data import.
- Visualization: Used to trigger the script from a visualization's ... button.
A basic script imports core scripting tools, logs a message to the console, and exits. Here's an example:
123456import { console, exit, fetch, params } from 'sys:runtime/v1'
import { fetchData } from 'sys:data_sources/v1'
console.debug(params);
exit(true, params)
Execute Script
To run a script, return to the Browse tab and find a visualization. Select the "…" button for the visualization, choose Execute Script..., select the desired script, and choose Execute.

This action runs the script and logs the input parameters to the console, simplifying future executions.
Rerun Script
After running the script, revisit the Script section in the System Administration app. Select the script and choose Edit and Run. The previous script execution details, including Parameters, Result, and Log Messages, are displayed on the right.
To rerun the script, you can either repeat the Execute Script command from the visualization or copy the parameters from a previous run and paste them into the popup window after choosing the Run.
When executed, the script produces the same results as before. This process streamlines script modification and testing against the same visualization.

General Scripts
A primary use for General scripts is creating utility files containing helper functions. Here's an example of a utility script with a `callDestination` helper function:
12345// Utility script with a callDestination function
export function callDestination(destinationName) {
// Implementation to call a destination
console.log('Calling destination: ' + destinationName);
}
To use this function in a visualization script, add an import statement:
1234// Import the utility script
import { callDestination } from 'usr:ipd_450_util'
// Call the callDestination function
const destResult = await callDestination("SOMEDESTINATION", "SUFFIX_URL_TO_API", "POST", {"SOME":"DATA"});Once imported, the callDestination function can be called like any other function.
Errors
Errors are displayed in the Messages panel. For example, attempting to access a nonexistent destination triggers an error.

Visualization API
The Visualization API can be accessed using the fetch command. To retrieve details about a visualization using its ID (passed in the params object), use the following code:
1234567// Accessing the Visualization API
let url = `visualization:v1/visualizations/${params[0].id}`;
console.debug(url);
const response = await fetch(url, {method: 'GET'})
const responseJson = await response.json();
console.debug(`${JSON.stringify(responseJson)}`)This provides access to scene IDs for each visualization version. Using the latest scene ID, you can make more API calls to retrieve the scene hierarchy, access and modify metadata, or change entire structures.
