How to modify a sprite asset's animation2d bone and weights through editor scripts

Greetings unity communitiy!

I am using the official [animation2d][1] package.
I need to rig a sprite’s bone structure through an editor script. So I need to persist the asset modifications to the project files.

I have managed to read the sprite’s mesh, bones, and weight data perfectly fine through the [SpriteDataAccessExtensions][2] extension methods.
I have also managed to compose the desired bone list, and corresponding weight [VertexAttribute][3] lists.

However, writing those attributes to the sprite asset through [sprite.SetBones()][4] and [sprite.SetVertexAttribute()][5] doesn’t seem to be persisted to the asset. After executing the editor script, the original sprite is unaltered.

I assume the problem may be I am trying to write an in-memory instance of the asset (instead of the asset itself), as I’m accessing the sprite through a serialized property in a monobehaviour with an editor script.

[166395-spriteskinrigger.png*_|166395]

Pressing the button calls the following extension method on the sprite serialized on the monobehaviour.

public static class SpriteRiggingExtensions
{	//Extension method modifying the Sprite class
	public static void BonesFromVertexList (this Sprite _this, string boneBaseName)
	{
		//iterate through the vertex list creating corresponding bones and weights
		NativeSlice<Vector3> vertexList = _this.GetVertexAttribute<Vector3>(VertexAttribute.Position);
		//create lists to hold created bones and weights
		SpriteBone[] boneList = new SpriteBone[vertexList.Length];
		BoneWeight[] weightList = new BoneWeight[vertexList.Length];

		//loop over vertex list
		for (int i = 0, iLimit = vertexList.Length; i < iLimit; i++)
		{
			boneList _= CreateBoneForVertex(i, boneBaseName, vertexList*);*_

_ weightList = CreateSimpleVertexWeight(i);
* }*_

* //apply the lists of bones and weights to the sprite*
* _this.SetBones(boneList);
_this.SetVertexAttribute(VertexAttribute.BlendWeight, new NativeArray(weightList, Allocator.Temp));*

* AssetDatabase.CreateAsset(this, “Assets/Tmp/GeneratedAss.asset”);*
* }
private static SpriteBone CreateBoneForVertex (/**/) {/ Create a bone /}
private static BoneWeight CreateSimpleVertexWeight (/**/) {/ Create weight for bone & vertex /}
}
So I need to persist the changes to the asset.
Using [AssetDatabase.CreateAsset()][7] won’t let me write a new asset arguing the same asset already exists (even if writing to a different path).
[166396-cantstoreasset.png*|166396]*

I found no other apparent way to use [AssetDatabase][9] to alter an existing asset.
So, How can I persist the sprite asset changes?
Please, and thank you very much.
*[1]: https://docs.unity3d.com/Packages/com.unity.2d.animation@3.0/manual/index.html*_
[2]: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/U2D.SpriteDataAccessExtensions.html*_
_[3]: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Rendering.VertexAttribute.html*_

*[4]: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/U2D.SpriteDataAccessExtensions.SetBones.html*_
[5]: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/U2D.SpriteDataAccessExtensions.SetVertexAttribute.html*_
_
*[7]: https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html*_
*
_*[9]: https://docs.unity3d.com/ScriptReference/AssetDatabase.html*_

Update: In the end I did not find how to do this within unity.

Since unity’s .meta files are serialized in plain YAML (if serialization configured as plain text), I could bypass this by processing the .meta files through an external python script.

There I could read all the properties related to the sprite’s asset serialization, such as the skinning rig, and re-write it as necessary.
Then simply loading unity with the externally-altered assets.

At first it sounds daunting and dangerous messing with unity’s files, but since YAML is human readable it’s easy to get a hold of it. Now I’ve even gotten used to make small tweaks to my assets by simply altering the .meta files with sublime text.
It’s easy as long as you are careful to respect tabulation, as YAML formatting is tabulation-based.

You can use the DataProvider interface to get/set/save the sprite data.

https://docs.unity3d.com/Packages/com.unity.2d.sprite@1.0/manual/DataProvider.html

Have you ever found out how to make it persist ??
Running into the same problem…