#(UniVRM) Save Asset Changes after loading it with LoadAssetAtPath()

1 messages · Page 1 of 1 (latest)

shell aurora
#

heyo
I'm working on an auto-setup that bulk creates assets for another extension
for context
uniVrm requires for blenshapes to be assigned within it's custom asset type BlendshapeClip

what i'm doing is bulk creating them since it's a predetermined format (ARKIT)

BlendShapeClip clip;
string clip_path;

// does clip exist
string[] clip_guids = AssetDatabase.FindAssets(
    filter: clip_name,
    searchInFolders: new string[] { BlendShapeAvatarPath }
);

// maybe delete old asset? NOTE: asset is serialized and values cannot be overriden if already created
if (clip_guids.Length > 0)
{
    clip_path = AssetDatabase.GUIDToAssetPath(clip_guids[0]);
    clip = AssetDatabase.LoadAssetAtPath<BlendShapeClip>(clip_path);
}
else
{
    clip_path =
        $"{BlendShapeAvatarPath.Replace($"{BlendShapeAvatar.name}.asset", "")}{Path.DirectorySeparatorChar}{clip_name}.asset";
    clip = CreateInstance<BlendShapeClip>();

    AssetDatabase.CreateAsset(clip, clip_path);
}

the problem is that when i create the asset from scratch the changes i do to the asset are reflected and properly saved onto it, but when i instead load it using LoadAssetAtPath it does not reflect the changes and instead wipes any previous change completely

properties like clip.BlendShapeName are marked as [SerilizedField] in the uniVRM script

how can i have the asset changes save when loading from path an existing asset?
i never worked with serialized objects/field before so not sure where to go from here

i tried with this to no avail ```C#
clip.name = clip_name;
SerializedObject SerializedClip = new SerializedObject(clip);

SerializedClip.WriteProperty("BlendShapeName", clip_name);

SerializedClip.ApplyModifiedProperties();

novel breach
#

What's WriteProperty? That's not an API I've ever seen

lyric dragon
#

Yea if possible (if the fields/properties are public) I usually load the asset, make changes and set as dirty/use undo or save the asset again after.

shell aurora
novel breach
#

That should work (with the correct name)

shell aurora
#

still didn't apply the changes

#

the strange thing about is it is the correct name and still won't save it instead it resets the field

#

screenshot for visual clarity of what the source of what i'm trying to modify looks like

#

unknow is the preset name for custom names outside of the vrm preset a,i,u,e,o

novel breach
#
var serializedClip = new SerializedObject(clip);
serializedClip.FindProperty("BlendShapeName").stringValue = clip_name;
serializedClip.ApplyModifiedProperties();

Should work fine. Add some logs/break out the debugger to find out where things are going wrong

shell aurora
#

this what happens
no errors no throwing, the assets get stuck in a pre-load preview we can call it, and the field is entierely gone

shell aurora
#

and as visible in the video the console is fully empty

#

it goes well only if created from scratch the assets

which i'm trying to avoid autodeletion as this is to work with VTuber models
and if i delete or break something in the process i could ruin work for models in price ranges of multiple thousands

novel breach
#

I don't know what you mean, your logs aren't printing?

#

At the bare minimum you need to check that clip_name actually contains the right value, else you could just be resetting the name yourself and everything's actually working fine

shell aurora
#

the logs don't report any strange value, it all resebles what it should look like
the value internally is logging as correct too which is even stranger

for the clip name, it's what the asset name is, the asset itself would have no name i believe if that was the case i will add it tho
as clip.name = clip_name is what's shown in the Current clip field which is reported as correct

#

log confirms all names are correct

novel breach
#
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CreateAssetMenu]
public sealed class Test : ScriptableObject
{
    [SerializeField] private string _example;
}

public sealed class TestEditorWindow : EditorWindow
{
    [MenuItem("Window/Test")]
    private static void Open() => GetWindow<TestEditorWindow>().Show();

    private void CreateGUI() => rootVisualElement.Add(new Button(Randomize) { text = "Randomize" });

    private static void Randomize()
    {
        foreach (string guid in AssetDatabase.FindAssets("t:" + nameof(Test)))
        {
            var asset = AssetDatabase.LoadAssetAtPath<Test>(AssetDatabase.GUIDToAssetPath(guid));
            var so = new SerializedObject(asset);
            so.FindProperty("_example").stringValue = GUID.Generate().ToString();
            so.ApplyModifiedProperties();
        }
    }
}
#

There's nothing special about it, so you'll have to figure out what extra things you're doing to blow your work away

shell aurora
#

at this point i'm starting to wonder if it's a problem with the extension
other devs constantly fight with it as it has gone into decadence for a while now after all but one dev left

because this is the whole file, nothing in it is doing anything to the asset itself

lyric dragon
#

assets with importers should have settings changed via their importer (e.g. a texture), does this have one?

shell aurora
#

the workflow with this is to manually create it in the editor one by one i will show what it looks like normally

#

vrm is a file format that you export after, this is the setup in unity so there's nothing to import before except the fbx from which you create the vrm

#

and this has to be repeated for 52 times per export if it's not right on the first time
which is why i'm trying to automate it

novel breach
#
string[] clip_guids = AssetDatabase.FindAssets(
    filter: clip_name,
    searchInFolders: new string[] { BlendShapeAvatarPath }
);

This line is incorrect, BlendShapeAvatarPath isn't a folder path, so surely this is returning nothing?

#

Unless the API's flexible enough to take that abuse, sometimes it is, I've never tested it

shell aurora
#

now that i think about it that's a good point, i might have forgoten to strip the avatar asset name from that path lol

novel breach
#

Just tested it, and it doesn't seem to work

shell aurora
#

i assumed it was searching it in the folder the asset is in, not that i needed to provide the folder path on it's own without the asset name
that's my bad

#

it's the first time i'm using this function

lyric dragon
#

use a debugger to actually debug this properly, you will see if it returns 0 results pretty quickly

#

you can also search for assets of type doing t:AnimationClip for example

shell aurora
#

i can confirm this is now working as intended
that's my bad for coding it at 1am in the morning to try and get it out quickly

thanks a lot for the help junowaCorazoncito

#

i started less than a week ago with c# for unity and unity, haven't learnt about debuggers yet, will check what those are out