Use the 3D Viewer API
Introduction
The 3D Viewer API allows you to retrieve, create, and update 3D model entities, actions, and properties more programmatically. You can easily create your custom logic to interact and manipulate a 3D model.
API Reference
For details, see 3D Viewer API Reference.
Usage
Use Case Example
If you want to get properties of a selected part on the 3D Viewer, perform the following steps:
-
Retrieve selected object information as a string through the On Selection Change event attribute of the 3D Viewer.
-
Use
mx.viewer3D.getObjects()
to get the object instances. -
Use
IPart.getProperties()
to pass the selected object as a parameter. Then you get the properties in key-value pairs. The following is the sample code in a JavaScript action. It takes 2 parametersselectedObject
andpropertyObject
./** * @param {string} selectedObject * @param {string} propertyObject * @returns {Promise.<MxObject[]>} */ export async function GetProperties(selectedObject, propertyObject) { // BEGIN USER CODE console.info(selectedObject); // Get the underlying 3D objects given its selection ids. let instances = mx.viewer3D.getObjects(selectedObject); let promises = []; let objects = []; if (instances && instances.length > 0) { let properties = await instances[0].getProperties(); for(let key in properties) { if(properties.hasOwnProperty(key)) { promises.push(createPropertyObject(objects, key, properties[key])); } } } return Promise.all(promises).then(()=>objects); function createPropertyObject(objects, key, value) { return new Promise(function (resolve, reject) { mx.data.create({ entity: propertyObject, callback: function(prop) { prop.set("Key", key); prop.set("Value", value); objects.push(prop); resolve(prop); }, error: function(error) { reject(error.message); } }); }); } // END USER CODE }
-
Call this JavaScript action in a nanoflow to get all available properties of a given part.
-
Use a page to show all the properties returned :
Other Sample Code Snippets
Starting from version 2.2.0, 3D Viewer module includes some out-of-box API actions. The module allows you to manipulate model parts via RotatePart, ScalePart, SetPartColor, and TranslatePart API actions. You can find these actions in the Viewer3D > USE_ME > API Actions folder in the App Explorer in Studio Pro. You can directly use them to create your custom logic for manipulating models. Check out the code implementation of these JavaScript actions, along with the 3D Viewer API Reference. This may give you some ideas about how you want to make use of the 3D Viewer API.
Feedback
If you have problem using the 3D Viewer API listed in 3D Viewer API Reference, or you would like more APIs to be exposed, raise a ticket in Mendix Support portal. We will address them accordingly.