Object Emitted from Mouth Help

  • Hi Sava,

    Yep this is possible!

    Animate Object Where Mouth Opened

    Since we want to detach the object from head movement once it's started, what we can do is just copy the rotation and position of the head binding and apply it to a non head binded object. 

    To have better control of where our animation will be placed, we can create a placeholder under our Head Binding object that we can use as reference for the position and rotation. 

    Next we will create a new object with a script triggered on Mouth Opened that will apply the position/rotation of our placeholder to our animation. We'll start by creating an input to get references to our placeholder and animation object. 

    // -----JS CODE-----
    // @input SceneObject animation
    // @input SceneObject placeholder

    Then from the placeholder, let's grab it's position  and rotation

    var currentPlaceholderPosition = script.placeholder.getTransform().getWorldPosition();
    var currentPlaceholderRotation = script.placeholder.getTransform().getWorldRotation();

    Then we can apply it to the animation: 

    script.animation.getTransform().setWorldPosition(currentPlaceholderPosition);
    script.animation.getTransform().setWorldRotation(currentPlaceholderRotation);

    Finally we'll play the animation:

    var am = script.animation.getFirstComponent("Component.AnimationMixer");
    am.setWeight("BaseLayer", 1);
    am.start("BaseLayer", 0, 1);

    Having Multiple Animations

    To have objects generated when mouth is opened, we can use "copyWholeHiearchy" http://lensstudio.snapchat.com/api/classes/SceneObject/. 

    Instead of modifying script.placeholder directly, we will apply it to a new copy of the animation object. Above where we setWorldPosition, we will create an object. Then we will set the transform from this new object. 

    var so = global.scene.createSceneObject("newAnimation");
    so.copyWholeHierarchy(script.animation);

    so.getTransform().setWorldPosition(currentPlaceholderPosition);
    so.getTransform().setWorldRotation(currentPlaceholderRotation);

    Since we're now copying our animation object, we can disable our original animation since we only want the instances to show up, not itself.

    Now we just need to turn on our animation instance. Notably, when we use copyWholeHierarchy, the object we copied is actually it's child. 

    var newAnimation = so.getChild(0);
    newAnimation.enabled = true;

    Then, we need to point our animation mixer variable to the one on this newAnimation object

    var am = newAnimation.getFirstComponent("Component.AnimationMixer");

    Finally, when this animation ends, we want our copy destroyed. So we use startWithCallback instead of start. Note that we destroy the original object (so), not the newAnimation.

    am.startWithCallback("BaseLayer", 0, 1, function () {
    so.destroy();
    });

    Now, when you open your mouth you should have a new object be created, starting where the mouth was, but detached from it!

     

    Our final code:

    // -----JS CODE-----
    // @input SceneObject animation
    // @input SceneObject placeholder

    var currentPlaceholderPosition = script.placeholder.getTransform().getWorldPosition();
    var currentPlaceholderRotation = script.placeholder.getTransform().getWorldRotation();

    var so = global.scene.createSceneObject("newAnimation");
    so.copyWholeHierarchy(script.animation);
    so.getTransform().setWorldPosition(currentPlaceholderPosition);
    so.getTransform().setWorldRotation(currentPlaceholderRotation);

    var newAnimation = so.getChild(0);
    newAnimation.enabled = true;
    var am = newAnimation.getFirstComponent("Component.AnimationMixer");
    am.setWeight("BaseLayer", 1);
    am.startWithCallback("BaseLayer", 0, 1, function () {
    so.destroy();
    });

     

    Can't wait to see what you create with this :) Don't forget to share.

    Cheers,

    Jon

    Comment actions Permalink
  • Jon, you are a legend, thank you so much.

    Comment actions Permalink
Have a comment?

Please sign in to leave a comment.

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!