Daz3D script need help (auto bone weight filling)

I have a hair figure (with bones), and the bone names are something like “J_Sec_Hair1_02”, “J_Sec_Hair2_02”, “J_Sec_Hair3_02”, and so on.

Currently, these bones don’t have weights, and I want to assign weights to all of them.

My process is as follows:

Select “J_Sec_Hair1_02” (bone).
Go to Geometry Selection > Select By > Face Groups > “J_Sec_Hair1_02” (Face Groups).
Then, go to Weight Editing > Fill Selected.

This process successfully assigns a weight to the “J_Sec_Hair1_02” bone.

The issue is there are many bones, and I want to repeat this process for each one until all bones have weights.

I need a script that automatically detects all bones under the currently selected figure and performs the above steps, assigning weights to each bone based on its corresponding face group.

This is chatgpt’s version log error and the script

2024-11-17 11:55:28.662 [WARNING] :: Script Error: Line 48

2024-11-17 11:55:28.662 [WARNING] :: TypeError: Result of expression ‘geometry.selectFaceGroup’ [undefined] is not a function.

// Function to assign weights to bones based on matching face groups
function assignWeightsToBones() {
    // Get the selected figure in the scene
    var selectedNode = Scene.getPrimarySelection();
    if (!selectedNode) {
        print("No figure selected. Please select a figure.");
        return;
    }

    // Check if the selected node has a skeleton
    var skeleton = selectedNode.getSkeleton();
    if (!skeleton) {
        print("Selected figure has no skeleton.");
        return;
    }

    // Get all bones in the skeleton
    var bones = skeleton.getAllBones();
    if (!bones || bones.length === 0) {
        print("No bones found in the selected figure.");
        return;
    }

    print("Found " + bones.length + " bones. Processing...");

    // Iterate through each bone
    for (var i = 0; i < bones.length; i++) {
        var bone = bones[i];
        var boneName = bone.getName();
        print("Processing Bone: " + boneName);

        // Select the bone
        bone.select(true);

        // Use Geometry Selection to select the corresponding face group
        var geometry = selectedNode.getObject().getCurrentShape().getGeometry();
        if (!geometry) {
            print("No geometry found for the selected figure.");
            return;
        }

        // Select faces by the face group matching the bone name
        var faceGroupIndex = geometry.findFaceGroup(boneName);
        if (faceGroupIndex === -1) {
            print("Face group not found for bone: " + boneName);
            continue;
        }
        geometry.selectFaceGroup(faceGroupIndex, true);

        // Fill the selected faces with the bone's weight
        geometry.fillWeights(bone, 1.0); // 1.0 represents full weight
        print("Assigned weight to bone: " + boneName);

        // Deselect the face group to prepare for the next bone
        geometry.clearSelection();
    }

    print("Weight assignment complete for all bones.");
}

// Run the function
assignWeightsToBones();

This is a script that could save my life. I have no understanding of Daz script (or JavaScript), and since ChatGPT couldn’t fulfill my request (it keeps making errors), I have no choice but to shamelessly come here to seek help.

  [1]: https://i.sstatic.net/82v6yMzT.jpg
  [2]: https://i.sstatic.net/eANGJPXv.jpg
  [3]: https://i.sstatic.net/BRIs3bzu.jpg
  [4]: https://i.sstatic.net/c8e9PagY.jpg