Swift问题:无法通过名称获取SCNMorpher目标
Hey there! Let's work through why your setWeight(_:forTargetNamed:) method isn't behaving as expected with your dinoNode's blend shapes. These are some of the most common fixes for this exact scenario:
Confirm the target name is 100% accurate
It's easy to miss a typo, case difference, or hidden suffix in the target name. Instead of guessing, print out the exact names of your morpher targets and use that value directly:dinoNode.morpher?.targets?.forEach { target in print("Blend shape target name: \(target.name ?? "Unnamed Target")") }Copy the exact string from the console and paste it into your
forTargetNamed:parameter—no exceptions. Even a single uppercase letter where there should be lowercase can break this.Make sure the morpher is enabled
Sometimes SCNMorpher instances default to disabled, which means weight changes won't take effect. Double-check and enable it if needed:dinoNode.morpher?.isEnabled = trueVerify the target exists in the morpher's array
Even if you see the target in the printed list, explicitly check if it's actually present when you try to set the weight. This helps rule out any weird loading quirks:guard let targets = dinoNode.morpher?.targets else { print("No morpher targets found!") return } let targetExists = targets.contains { $0.name == "jawOpenMesh" } print("Target 'jawOpenMesh' exists: \(targetExists)")If this returns
false, your name is definitely wrong—go back to the printed target names.Try setting weight via target index instead
If name matching is failing for some reason, bypass it entirely by using the target's index in the array:guard let targets = dinoNode.morpher?.targets, let targetIndex = targets.firstIndex(where: { $0.name == "jawOpenMesh" }) else { return } dinoNode.morpher?.setWeight(jawOpenQ, forTargetAt: targetIndex)Check the morpher's calculation mode
The calculation mode determines how blend shape weights are applied. If it's set to an incompatible mode, your weight changes might not be visible. Stick to.additivefor most standard blend shape use cases:dinoNode.morpher?.calculationMode = .additiveEnsure the node's geometry is mutable
SceneKit sometimes uses immutable geometry instances, which can prevent morphers from modifying the mesh. Create a mutable copy of the geometry to fix this:if let originalGeometry = dinoNode.geometry { dinoNode.geometry = originalGeometry.copy() as? SCNGeometry }You'll need to re-associate the morpher if this resets it, but this often resolves hidden immutability issues.
内容的提问来源于stack exchange,提问作者Nikita Alexander

