Home › Forums › Programming › Object Copies?
- This topic has 4 replies, 3 voices, and was last updated 6 years, 10 months ago by Will Welker.
-
AuthorPosts
-
2018-01-18 at 9:01 pm #1715jemCustomer
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,
JemJeremy Wernick
2018-01-19 at 2:15 am #1716Will WelkerCustomerI haven’t heard of anybody doing that yet. That kind of feature is probably on the list. Instancing with Puzzle blocks would be cool.
2018-01-19 at 1:51 pm #1721Yuri KovelenovStaffYes, I guess this is the right way to go
https://stackoverflow.com/questions/11919694/how-to-clone-an-object3d-in-three-js2018-01-22 at 2:04 am #1726jemCustomerI 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.htmlCheers,
JemJeremy Wernick
2018-01-22 at 12:08 pm #1730Will WelkerCustomerWow, very nice!
-
AuthorPosts
- You must be logged in to reply to this topic.