SCNGeometry and .copy()

Up to now I have created multiple new SCNNodes using an instance of SCNGeometry and it was OK that they all had the same appearance. Now I want variety and when I make a copy of that instance using:

let newGeo = myGeoInstance.copy() as! SCNGeometry

(must be force cast because copy() -> any?)

all elements are verified present. :-)

Likewise:

node.geometry?.replaceMaterial(at: index, with: myNewMaterial)

is verified to correctly change the material(s) at the correct index(s). The only problem is the modified "teapot" is not visible, and yes I have set node.isHidden = false.

Has anyone experienced this?

In the old days reversing the verts was a solution. In desperation I tried that. |-(

curiously, when I use .copy() on an SCNGeometry the geometry?.geometrySourceChannels, geometry?.elements, and geometry?.materials are all == to the original but the test whether the geometries themselves are equal returns false. How can a copy not equal its original?

Idk what else to test for "as missing" from the copy() .

.copy() is broken

I have tested dozens of permutations in calling.

OK, I have just learned that objc methods can be called with Swift. Most examples are of custom functions or methods.

How can I call the existing objc version of .copy() since Swift's doesn't work?

Thanks

fyi: I could never grasp objc and stopped programming during that period. Please be generous with example code.

as an attempted work-around I created a global array of mySCNGeometry.copy() which should have allowed the materials to be changed independently as stated in the Developer Documentation. They are in fact all the same geometry whereas changing any one "copy" changes them all.

I have not found an example of using "#selector(copy)" to access the objc "copy" method. All the syntax I have tried fails.

To create a unique instance, you need to do a "deep clone" or otherwise use recursion to instance everything as you see fit.

extension SCNGeometry{

func deepClone() -> SCNGeometry? {
        // Use NSKeyedArchiver and NSKeyedUnarchiver to clone the geometry
        do {
                // Archive the geometry to Data
                let archivedData = try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: true)
                
                // Unarchive the geometry from Data, specifying the expected class to ensure type safety
                let clonedGeometry = try NSKeyedUnarchiver.unarchivedObject(ofClass: SCNGeometry.self, from: archivedData)
                return clonedGeometry
            } catch {
                print("Error cloning geometry: \(error)")
                return nil
            }
    }
}

So you can change the material without changing the original. Each of these will take up more memory. You want to just copy them when possible.

SCNGeometry and .copy()
 
 
Q