Common Tasks
Below are some common tasks you might need to do while creating your plugins. These are not necessarily the only way to do them, but is provided to help demonstrate various plugin capabilities.
Many tasks can be accomplished with fewer lines of code using the Utils Modules introduced in Lens Studio 5.3. Check out the Utils Modules section for more information.
Importing or creating a texture of a material
You can import assets to the opened project in several ways, including from the user's file system, directly within the plugin (by using a template asset), or by utilizing assets that are built into Lens Studio itself.
Load Native Assets
There are two primary asset types: Native Assets:
- These assets are generated exclusively by Lens Studio through the createNativeAsset of the assetManager object.
- This process results in the creation of exactly one asset.
Imported Assets:
- You can import files in any format that Lens Studio supports (e.g., .png, .fbx) using the importExternalFile[Async] method.
- Depending on the file, Lens Studio may generate anywhere from zero to multiple assets upon import.
const textureType = 'ScreenTexture';
const newTextureName = 'Name For The Texture';
//parameter: asset type, new asset name, new asset directory (relative)
const screenTextureAsset = assetManager.createNativeAsset(
textureType,
newTextureName,
''
);
or you could use the Utils module (available from 5.3) to create a texture with the findOrCreateNativeAsset function from the LensStudio:AssetUtils.js module:
// import the AssetUtils module
import * as AssetUtils from 'LensStudio:AssetUtils.js';
const screenTextureAsset = findOrCreateNativeAsset(
/* textureType */ 'ScreenTexture',
/* assetManager */ assetManager,
/* newTextureName */ 'Screen Texture',
/* destination */ ''
);
Some Native Assets include:
- bodyDepthTexture
- bodyNormalsTexture
- depthTexture
- faceCropTexture
- faceTexture
- textTexture
- screenCropTexture
- screenTexture
- imagePickerTexture
- faceImagePickerTexture
- segmentationTexture
- objectTrackingTexture
- animationCurve
- animationLayer
- animationMixerLayer
- worldmesh
- Facemesh
- ...
Importing an image, svg or shader graph
You may place other files inside plugin directories, such as images, SVG files, ss_graphs (shader graphs) or others.
For example, here is how the gLTF example preset accesses the scene.gltf file which is placed at the root directory of the plugin in the create(destination) function using import.meta.resolve function and the assetManager component.
create(destination) {
console.log("Create Gltf preset")
// Getting the Lens Studio model component
// Assume we have saved the reference to pluginSystem in the constructor
const model = this.pluginSystem.findInterface(Editor.Model.IModel);
// Getting the Lens Studio asset manager component
const assetManager = model.project.assetManager
// Importing the file
const gltfFileName = 'scene.gltf'
const absoluteGltfPath = new Editor.Path(import.meta.url).appended(gltfFileName)
const importResult = assetManager.importExternalFile(absoluteGltfPath, destination, Editor.Model.ResultType.Packed)
// Use the imported assets
importResult.primary.sceneObjects.forEach((object) => {
const transform = object.localTransform
transform.scale = new vec3(1, 1, 1)
object.localTransform = transform
})
// Returning the asset that was imported
return importResult.primary
}