Add socket to skeletal mesh using Python

I need to automate the creation of sockets on all bones in a skeletal mesh. I use the provided script but it does not work as expected. In the first image you can see the result of the python script. And in the second it is the result by manually adding sockets in the editor.

If you are able to provide a Python snippet that does this in Unreal 5.8.2 I would be really grateful!

[Image Removed]

[Attachment Removed]

Steps to Reproduce
def add_sockets(skeletal_mesh_path):

skeletal\_mesh\_asset \= unreal.EditorAssetLibrary.load\_asset(skeletal\_mesh\_path)



skel\_modifier \= unreal.SkeletonModifier()

skel\_modifier.set\_skeletal\_mesh(skeletal\_mesh\_asset)

bone\_names \= skel\_modifier.get\_all\_bone\_names()



for bone\_name in bone\_names:



    socket\_name \= f'{bone\_name}\_socket'

    socket \= unreal.SkeletalMeshSocket(outer\=skeletal\_mesh\_asset, name\=socket\_name)



    skeletal\_mesh\_asset.add\_socket(socket, add\_to\_skeleton\=True)

    socket.set\_socket\_parent(skeletal\_mesh\_asset, bone\_name)



unreal.EditorAssetLibrary.save\_asset(skeletal\_mesh\_path)

[Attachment Removed]

Hi Johan,

I believe the socket needs to be created via new_object and outered to the mesh before calling AddSocket. Something like this should work in your for loop:

        socket_name = f"{bone_name}_socket"
 
        socket = unreal.new_object(
            unreal.SkeletalMeshSocket,
            outer=skeletal_mesh_asset,
            name=socket_name,
        )
 
        socket.set_editor_property("socket_name", socket_name)
        socket.set_editor_property("bone_name", bone_name)
 
        skeletal_mesh_asset.add_socket(socket, add_to_skeleton=True)

Let me know if you still run into trouble and I’ll have a look.

Best,

Cody

[Attachment Removed]

Hi,

Seems like the API has been updated: both bone_name and socket_name are now read only.

Error: Exception: SkeletalMeshSocket: Property ‘SocketName’ for attribute ‘socket_name’ on ‘SkeletalMeshSocket’ is read-only and cannot be set

Is there a new way to achieve this with Python?

[Attachment Removed]

Ah, sorry about that. The SkeletalMeshEditorSubsystem should have a functionyou can use:

unreal.SkeletalMeshEditorSubsystem.rename_socket(
            skeletal_mesh,
            old_name,
            new_name,
)

You’ll need to do this after the call to add_socket, as that’s where we’ll automatically generate a temporary name. You can then pass the resulting socket’s socket_name as the old_name for the rename operation.

Best,

Cody

[Attachment Removed]

Hi Cody,

Thanks for your input. I used the following snippet:

skel_modifier = unreal.SkeletonModifier()
skel_modifier.set_skeletal_mesh(skeletal_mesh_asset)
bone_names = skel_modifier.get_all_bone_names()
 
for bone_name in bone_names:
 
    socket_name = f"{bone_name}_socket"
 
    socket = unreal.new_object(
        unreal.SkeletalMeshSocket,
        outer=skeletal_mesh_asset,
        name=socket_name,
    )
 
 
    socket.set_socket_parent(skeletal_mesh_asset, bone_name)
    skeletal_mesh_asset.add_socket(socket, add_to_skeleton=True)
 
    unreal.SkeletalMeshEditorSubsystem.rename_socket(
        skeletal_mesh_asset,
        socket.socket_name,
        socket_name,
    )
    
 
unreal.EditorAssetLibrary.save_asset(skeletal_mesh_path)

It adds the socket to the correct bone and renames the socket as expected, but the socket is added to the Mesh not the skeleton as specified in the “add_socket” function? Am I missing something?

Best,

Johan

[Attachment Removed]

Hi,

The call to add_socket here should add it to both the mesh and the skeleton, though you may need to save the skeleton as well:

unreal.EditorAssetLibrary.save_loaded_asset(skeletal_mesh_asset.get_editor_property("skeleton"))If you don’t want to add it to the mesh and only want the socket added to the skeleton, you could remove it afterward with something like:

skeleton = skeletal_mesh_asset.get_editor_property("skeleton")
mesh_sockets = skeletal_mesh_asset.get_editor_property("sockets")
mesh_sockets.remove(socket)

[Attachment Removed]

Hi,

Maybe the API has changed, but if I use .get_editor_property(‘sockets’) I get an exception:

LogPython: Error: Exception: SkeletalMesh: Property ‘Sockets’ for attribute ‘sockets’ on ‘SkeletalMesh’ is protected and cannot be read

[Attachment Removed]

Ah, sorry about all the back and forth. It doesn’t seem like the Python API supports creating the socket on the skeleton without also creating it on the mesh, so that might require an engine change if it’s something you need to support. Otherwise, it should currently be adding to both, even if it only looks like it’s on the mesh.

[Attachment Removed]

Ok, Thank you for clarifying this!

[Attachment Removed]