We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Object Copies?

Home Forums Programming Object Copies?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1715
    jem
    Customer

    I am working on a scene in which a single object can be instantiated one to many times (1…n).

    I would prefer not to model all possible instances of the object into my original Blender scene and show or hide them. I would rather have a single copy of the object and duplicate it as needed at runtime. This might be more manageable. I think that I could achieve this with the API’s Mesh.clone() method, but before I go and implement this, has anyone attempted something like this before? Please let me know.

    Thanks,
    Jem

    Jeremy Wernick

    #1716
    Will Welker
    Customer

    I haven’t heard of anybody doing that yet. That kind of feature is probably on the list. Instancing with Puzzle blocks would be cool.

    #1721

    Yes, I guess this is the right way to go
    https://stackoverflow.com/questions/11919694/how-to-clone-an-object3d-in-three-js

    Chief 3D Verger | LinkedIn | Twitter

    #1726
    jem
    Customer

    I was able to get the object cloning to work. I will share my solution to this problem here for anyone who needs this technique.

    The goal was to create copies of objects at runtime. I needed this because my scenes are assembled from many objects and the quantities of each type object are not known ahead of time. The quantities can be large.

    I wrote a small function that uses the Object3D.clone() method from the API. The function takes two arguments. The srcObjectName is the name of the object to be cloned. The name is assigned in Blender. The targetObjectName is the name of the object to which we will snap the clone. Again, the name is assigned in Blender. The function requests a non-recursive copy. I think that this means clones will share meshes and materials (I may be wrong). Lastly, the function returns the clone. This is important because we need to keep track of the clones. In my example, I place all of the clones in an array so that I may programmatically destroy them later. The clones will not be callable from the puzzles.

        function cloneObject(srcObjectName, targetObjectName) {
            var srcObject = v3dApp.scene.getObjectByName(srcObjectName);
            //Create a shallow copy
            var cloneObject = srcObject.clone(false);
            var targetObject = v3dApp.scene.getObjectByName(targetObjectName);
            //Place the copy
            cloneObject.position.x = targetObject.position.x;
            cloneObject.position.y = targetObject.position.y;
            cloneObject.position.z = targetObject.position.z;
            cloneObject.rotation.x = targetObject.rotation.x;
            cloneObject.rotation.y = targetObject.rotation.y;
            cloneObject.rotation.z = targetObject.rotation.z;
            v3dApp.scene.add(cloneObject);
            return cloneObject;
        }

    If you look at the Blender screenshot, you will see that the chessboard is covered with an array of empties. I use these empties as my snapping targets. I found that using the arrows type empty is useful to set the snap orientation.

    Here is a link to the example on the Soft8soft CDN,
    https://cdn.soft8soft.com/AROAJSY2GOEHMOFUVPIOE:027349a7ae/applications/clone_pawn/clone_pawn.html

    Cheers,
    Jem

    Jeremy Wernick

    #1730
    Will Welker
    Customer

    Wow, very nice!
    Duplicate

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.