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

Is it possible to duplicate a scene object via code?

  • It's good question I am not sure on the specific method if even possbile to manipulate like what kinda object?

    like a cube you can do with meshbuilder..its has examples of code so not going to help with syntax as its well documented already

    https://lensstudio.snapchat.com/api/classes/MeshBuilder/

    I was thinking maybe you could use the procedurla method  https://lensstudio.snapchat.com/api/classes/RenderObjectProvider/

    their is a link in the featured on how to use procedual its very cool

    my concern would be memory performance hit and you lense not been submited.

    another route might be

    to hide the objects then when the user taps incrementally enable each one, you could set the object to look at the camera so its feels more like what your trying to achieve.

    Comment actions Permalink
  • Hi art Nelson,

    Here's an example script that might help. I tested it with the "Touch Started" event to create a copy of objectToBeSpawned on touch start. 

    // Makes a copy of "objectToBeSpawned" at the same position and rotation as "positionTarget"

    //@input SceneObject objectToBeSpawned
    //@input SceneObject positionTarget

    // Store objectToBeSpawned's transform as "sourceTr"
    var sourceTr = script.objectToBeSpawned.getTransform();

    // Store objectToBeSpawned's original position and rotation
    var oldPos = sourceTr.getWorldPosition();
    var oldRot = sourceTr.getWorldRotation();

    // Set objectToBeSpawned's position and rotation to match positionTarget
    var targetTr = script.positionTarget.getTransform();
    sourceTr.setWorldPosition(targetTr.getWorldPosition());
    sourceTr.setWorldRotation(targetTr.getWorldRotation());

    // Create a new SceneObject
    var newObj = global.scene.createSceneObject("copy");

    // Copy objectToBeSpawned into newObj
    newObj.copyWholeHierarchy(script.objectToBeSpawned);

    // Restore objectToBeSpawned's original position and rotation
    sourceTr.setWorldPosition(oldPos);
    sourceTr.setWorldRotation(oldRot);

    Note that after copying an object, the new object doesn't get fully initialized right away, so you can't immediately make changes to it. That's why I repositioned the source object before making a copy, and restored its original position afterwards.

    Hope this works for you! Let me know if you have any additional questions.

    Comment actions Permalink
  • Jacob, at what point does the copied object get fully initialized?

    Comment actions Permalink
  • Hey Robert,

    The sample code I gave is actually pretty outdated, I would now recommend using Prefabs instead.

    Sample code for doing this with a prefab:

    //@input Asset.ObjectPrefab prefab
    //@input SceneObject parent
    var instance = script.prefab.instantiate(script.parent || null);

    Note that the parent object is optional in this case, since if null is passed in the object will spawn with no parent.

    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!