The forum on this site is closed for new comments and posts. Continue the conversation in our Snap AR Discord server.

Change mesh or sprite object with code

  • Hey Vincent,

    Were you hoping to use the Soundboard template to change the presented resource?

    Cheers,
    Kaitlyn

    Comment actions Permalink
  • Since I have 9 buttons and each button has their own scripts. 
    For each script, there are 10 sprite objects to be added into each script. 
    I feel so troublesome to link all the variables in each script with each sprite objects. As the sprite objects are the same across the 9 scripts. 

    So, is there a way I can combine the 9 scripts into 1 script, and when user click each button, a part of the script will activate? what the button does is to display the next sprite object. So, it's like a button to switch sprite object.

     

    Comment actions Permalink
  • Hi Vincent,

    For your first question, here's how you can change the mesh asset of a MeshVisual:

    //@input Component.MeshVisual meshVisual
    //@input Asset.RenderMesh renderMesh

    script.meshVisual.mesh = script.renderMesh;

    And here's how to change the texture of a SpriteVisual:

    //@input Component.SpriteVisual spriteVisual
    //@input Asset.Texture texture

    script.spriteVisual.mainPass.baseTex = script.texture;

     

    About your second question, I'm a little confused about what your goal is in the Lens? Is there a button for each part of the face? Does each button change all the other parts of the face, or only some of them?

    I can give you some general tips in the meantime, like having a central script that is notified by all the button scripts. There's a lot of ways to do it, but here's a really straightforward way using global functions:

     

    // Controller.js (put under Initialized, only one of this script is needed)
    global.onLeftEarButtonTapped = function() {
    print("left ear button was tapped");
    // do whatever you need to do here
    }
    global.onRightEarButtonTapped = function() {
    print("right ear button was tapped");
    // do whatever you need to do here
    }

    // Left_Ear_Button.js (put this on the ear object under Tapped event)
    global.onLeftEarButtonTapped();
    // Right_Ear_Button.js (put this on the ear object under Tapped event)
    global.onRightEarButtonTapped();

    And continue doing this for all 9 buttons. 

     

    Note that there's cleaner ways of doing this where you don't have to write a separate script for each button. You can have a single button script that does something like this:

    // Button.js (put this under Tapped event)
    if (script.api.onTapped) {
    script.api.onTapped();
    }

     

    And then have your controller reference those buttons like:

    // Controller.js (put under Initialized, you only need one of these)
    // @input Component.ScriptComponent leftEarButton

    script.leftEarButton.api.onTapped = function() {
    print("left ear button tapped!");
    }

    But it's really up to you, and your organization preferences.


    Hope this has helped! If you need more guidance with this, please give some further description of your lens and I'll try to assist you on it.

    Thanks,
    Jacob

    Comment actions Permalink
  • Your suggestions are very good, however, this error appears when I double click the script in resources. including the initial script and button script. However, it only happen when I double tap the script, it will not happen when I singe tap the script. 
    Also, it does not affect the usage of the Lens. How can I resolve this error?

     

    Comment actions Permalink
  • Just realise this error appears on my other scripts too...

    Comment actions Permalink
  • Hi Vincent,

    It looks like the javascript file is being opened up by Windows Script Host, an external program. When you double click a javascript file in Lens Studio, it opens that file through your operating system, which will use whatever program is associated with .js file extensions. You can ignore that "script is undefined" error message in this case, since it's coming from an external program that doesn't know how to handle scripts meant for Lens Studio.

    If you want, you can change your default program for ".js" files to your text editor of choice, like Visual Studio Code or Sublime Text. That way, double clicking on script files will open them in a text editor instead of trying to execute them. Here's a guide for doing that on Windows (step 4 is the relevant part): https://support.microsoft.com/en-us/help/4028161/windows-10-change-default-programs 

    If you don't want to change the default program for .js, you can safely ignore this error. Just keep in mind that double clicking on script files will always try to open that file through your operating system.

     

    Comment actions Permalink

We're here to help! We just need a little bit of information...

What system are you using?

Have you downloaded the latest version of Lens Studio?

Please download the latest version of Lens Studio. If you still run into this issue, please come back and report it!

Is this issue causing Lens Studio to crash?

What system do you run Lens Studio on?

Version

Graphics

Follow the next steps to help us solve your issue:

  • Copy and paste this text into your TerminalCommand Window
    open ~/Library/Preferences/Snap/Lens\ Studio/ %LOCALAPPDATA%\Snap\Lens Studio Copy Text
  • Press ReturnEnter to run the command. The Lens Studio folder will automatically open
  • Prepare to upload your files: zip the "Log" Folder by right-clicking and choosing "compress."
    Locate the Log.txt file right above it.

    Attach a screenshot of the issue:

Name:

Email:

What is this most relevant to?

Please enter a brief description of your issue:

Thanks for submitting this issue.

Unfortunately, it's likely due to the operating system or hardware you're using – since they don't meet the system requirements for Lens Studio.

Still, we hear you loud and clear, and are logging the issue in case there's ever a workaround we can provide!

Minimum Requirements

Operating System: Windows 10 (64 bit); MacOS 10.11+

Hardware: Minimum of Intel Core i3 2.5Ghz or AMD Phenom II 2.6Ghz with 4 GB RAM; Intel HD Graphics 4000 / Nvidia GeForce 710 / AMD Radeon HD 6450 or better; screen resolution of 1280x768 or higher

We'll try to resolve this issue as soon as possible. Thanks for letting us know about it!

Keep an eye out for a followup email from us. We may have a couple more questions for you, or we might already have a solution to offer.

Happy creating!