Custom Script Question

tcarpenter
I’m a noob when it comes to custom scripts and JavaScript for that matter, but I have this small function I’m trying to implement: (function() { var c = api.getConfigurator(); var cn = c.getConfiguration(); var nSpc = c…

Comments

4 comments

  • Comment author
    tcarpenter

    I’m a noob when it comes to custom scripts and JavaScript for that matter, but I have this small function I’m trying to implement:

    (function() {
    var c = api.getConfigurator();
    var cn = c.getConfiguration();
    var nSpc = cn["Spacing"];
    var nCnt = cn["Posts"];
    c.setConfiguration({SpacingResult: nSpc, CountingResult: nCnt});
    var node = api.scene.findNode({ name: ’Box’ });
    
    api.scene.set({ id: node, plug: ’Polymesh’, property: ’Count’, nCnt, property: ’Translation’}, { x: 0, y: 0, z: nSpc });
    
    })();
    

    It works except for driving my array. I’m sure I’m not getting/setting the right thing - any help is appreciated.

    0
  • Comment author
    tcarpenter

    Oh - duh:

    (function() {
    var c = api.getConfigurator();
    var cn = c.getConfiguration();
    var nSpc = cn["Spacing"];
    var nCnt = cn["Posts"];
    c.setConfiguration({SpacingResult: nSpc, CountingResult: nCnt});
    var node = api.scene.findNode({ name: ’Box’ });
    
    api.scene.set({ id: node, plug: ’PolyMesh’, property: ’Count’}, nCnt);
    api.scene.set({ id: node, plug: ’PolyMesh’, property: ’translation’}, { x: 0, y: 0, z: nSpc });
    
    })();
    

    now it works.

    0
  • Comment author
    Phil

    Hey Todd,

    Glad to see this is working. However, I noticed a few things that may want to adjust.

    Firstly, api.getConfigurator returns a Promise which means you must wait for the promise to resolve before accessing the result (the configurator API in this case).

    You can do so easily by making your function an async and await the result.

    (async function () {
      var c = await api.getConfigurator();
    

    Secondly, it appears setting the translation is the only thing that requires a script. You may be able to simplify things by creating a rule with no conditions (i.e. one that always fires) with actions that set the count.

     

    Then you can simplify your script to something like this…

      (async function () {
      const c = await api.getConfigurator();
      const { Spacing } = c.getConfiguration();
      const node = api.scene.findNode({ name: "Box" });
    
      api.scene.set(
        { id: node, plug: "PolyMesh", property: "translation" },
        { x: 0, y: 0, z: Spacing }
      );
    })();
    0
  • Comment author
    tcarpenter

    Thanks for the great tip Phil - I’ll implement that!

    0

Please sign in to leave a comment.