LogoLogo
ThreeKitDeveloper HubForumsSupportStatus
  • Guides
  • Documentation
  • Releases
  • Platform Documentation
  • Project Data
    • Platform Landing Page
    • Basic Concepts
      • Environments
      • Organizations
      • Attributes
        • Arrays
      • Tagging
      • Naming Conventions
      • Players
      • Branching
      • Alerts and Warnings
      • Supported Browsers
      • ThreeKit Platform Architecture
    • Catalog
      • Items
        • Item Bulk Edit
        • Item Variants
      • Data Tables
      • Item Rules and Logic
      • Publishing
    • Assets
      • Asset Management
        • Importing Assets
        • Exporting Assets
        • Folders
      • Asset Editor
        • Editor Layout Modes
          • Layout Editor
            • Local vs Global Transform
          • Logic Editor
      • Nodes
        • Common Properties
          • Node Tags
        • Helpers
          • Nulls
          • Model References
          • Connectors
        • Lights
          • Point Light
          • Directional Light
          • Spot Light
          • Area Light
          • Hemisphere Light
          • Shadow Plane
        • Cameras
        • Shapes
        • Annotations
        • Layout Containers
      • Scenes
      • Materials
        • Physical Material
        • Proxy Materials
        • Multi Material
        • Gem Material
        • JSON Configuration
      • Textures
      • Canvases
      • Composites
    • Operators
      • PolyMesh Operators
        • Array
        • Bend Deformer
        • Map Override
        • Stretch
        • Physics (Collision Detection)
      • Material Operators
        • Falloff
        • Tiling Override
        • Triplanar Mapping
        • Template Override
      • Image Operators
        • Canvas Composite
        • Canvas Fill
        • Canvas Linear Gradient
        • Canvas Text
        • BlackWhite
        • Posterization
      • PostEffects Operators
        • Bloom Pass
      • Comp Layer Operators
        • Vray Normals Properties
        • Vray Bump Normals Properties
    • Stages
      • Stage Effects
        • Turntable
    • Logic
      • Conditions
      • Actions
      • Queries
        • Node Queries
        • Metadata Value Query
        • Asset Query
        • Datatable Query
      • Custom Scripts
    • Virtual Photographer
      • Create Renders
      • View Renders
      • V-Ray Integration
    • Augmented Reality
      • AR Settings
  • Org Setup
    • Admin & Security
      • Org Profile
      • Users & Permissions
        • Threekit Sign-in
        • Members
        • User Profile
      • Tokens
      • Service Accounts
      • ISO Certification
      • Org Migration / Data Transfer
    • Jobs System
    • Orders
      • Orders List
      • Configurations
    • Analytics
      • Advanced Buyer Analytics Reports
      • Player Views
    • Project Settings
      • Features
      • Languages
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Project Data
  2. Logic

Custom Scripts

PreviousDatatable QueryNextVirtual Photographer

Last updated 1 year ago

Was this helpful?

While using the out-of-the-box toolset when creating configurators is encouraged, it is occasionally necessary to make use of custom scripts. Custom scripts may be added in the same manner as any other visual action, via the action drop down on a rule.

Example Use-Case

For a clearer understanding of a "complex" configuration requirement necessitating the use of custom script actions, consider the table below.

Within this configuration experience, the user may change the length, width, and height of the coffee table. Interact with the configurator by entering values for each of the attributes within the following limits: Height (Y): Min 24" - Max 48" Length (X): Min 60" - Max 96" Width (Z): Min 12" - Max 24"

Why is a custom script action necessary?

A custom script is required to perform mathematical operations on user-entered values. The table is being re-sized using a stretch operator that takes meters as its input. Mathematical operations must be performed on the user inputs to convert user inputs into required stretch distances.

Using a Custom Script

The code placed within the field will be run on the front end by the browser, so it needs to be compatible across most browsers (polyfill with babel can be used for this purpose).

Sample Code


const config = api.configurator.getConfiguration();

const tableMeshId = api.scene.find({from: api.instanceId, name: 'Table Mesh'});

// Set Dimensions (in inches) of mesh as uploaded into Threekit
const defaults = {
    height: 31.89,
    length: 72.05,
    width: 14.96
};

// User-defined values
const newHeight = config['Height (Y)'];
const newLength = config['Length (X)'];
const newWidth = config['Width (Z)'];

// Calculate Stretches in meters
const yStretch = (newHeight - defaults.height) * 0.0254 / 2;
const xStretch = (newLength - defaults.length)  * 0.0254 / 2;
const zStretch = (newWidth - defaults.width) * 0.0254 / 2;

// Note y-transform is equal to yStretch
const yTrans = yStretch;

// Set Stretches
api.scene.set({
        from: api.instanceId,
        name: 'Table Mesh',
        plug: 'PolyMesh',
        properties: { type: 'Stretch', axis: 1 },
        property: 'stretchDistance'
    }, 
        xStretch
);
api.scene.set({
        from: api.instanceId,
        name: 'Table Mesh',
        plug: 'PolyMesh',
        properties: { type: 'Stretch', axis: 2 },
        property: 'stretchDistance'
    }, 
        yStretch
);
api.scene.set({
        from: api.instanceId,
        name: 'Table Mesh',
        plug: 'PolyMesh',
        properties: { type: 'Stretch', axis: 3 },
        property: 'stretchDistance'
    }, 
        zStretch
);

// Set y-transform to preserve location of bottom of table
api.scene.set({
        from: api.instanceId,
        name: 'Table Mesh',
        plug: 'Transform',
        property: 'translation',
    }, { 
        x: 0, 
        y : yTrans, 
        z: 0
    }
);

Choosing the custom script action type exposes a field for entry of the script, allowing for use of the client-side . This will execute any time a change is made to the configuration attributes. Bear in mind that the script must be deterministic, it is not a stateful script. There is no capacity to accumulate something, the script must operate on the attributes that have been created.

The sample code below is not meant to reflect coding best practices, it is meant to be readable for the purposes of this example. The is used within this example.

Threekit Player API
Stretch Operator