Adding a texture to DecalMaker programmatically

mrmr32

Well-known member
Messages
123
Reactions
317
Points
63
Website
github.com
Patreon
mrmr32
Hi, I have to add a texture to DecalMaker, by @Chokaphi using a script.

Right now what I'm doing is to get the original JSON, and adding to the array a new JSON class with the path. You can check the code below for reference.
C#:
public static void LoadOriginTexturesIntoDecalMaker(JSONClass decalMaker, string cls, string part, string texturePath) {
            JSONArray elementsInPart = decalMaker[cls][part].AsArray;

            JSONClass texture = new JSONClass();
            texture["H"].AsFloat = 0f;
            texture["S"].AsFloat = 0f;
            texture["V"].AsFloat = 1f;
            texture["Alpha"].AsFloat = 0f;
            texture["RandomName"] = GetRandomName(7);
            texture["LinkID"] = "*";
            texture["Path"] = texturePath;
            
            elementsInPart[0] = texture;
}
You can call it with LoadOriginTexturesIntoDecalMaker(containingAtom.GetStorableByID("decal script id here").GetJSON(), "_DecalTex", "face", "path here") (note: I get the script id by code too, just not wanted to post it here).
This generates the right JSONClass output (returned by reference), but once I load it DecalMaker returns the values as default:
C#:
decalMaker.RestoreFromJSON(cls);

SuperController.LogMessage(cls.ToString()); // this is fine
SuperController.LogMessage(containingAtom.GetStorableByID(this._decalMakerId).GetJSON().ToString()); // this is not fine

Checking DecalMaker's code I can see that it overrides the RestoreFromJSON (instead of LateRestoreFromJSON), and it change the variable _savedData. This variable is loaded by calling the function PerformLoad(), but I cannot call it as I'd have to add DecalMaker in the .cslist (and by doing so the script doesn't work), or use reflection (and I cannot use it inside VAM).

Any suggestion?
 
First let me point out https://github.com/Chokaphi1/Decal-Maker/tree/main/Source
That is a cleaner copy of my source that the single file cs in the VAR's

I still do not have a clean and proper way for other addons to load DecalMaker but there is the JSONStorables requested by Jayjaywon for UI Assist that can be used. You already have the corect key but included it anyways

C#:
//So get all entries and find the correct key.
List<string> GetStorableIDs()
//regex/Linq wizardry here to find plugin

//if we are the first plugin "plugin#0_VAM_Decal_Maker.Decal_Maker"
JSONStorable dmStorable = containingAtom.GetStorableByID("plugin#0_VAM_Decal_Maker.Decal_Maker")

//Now you can use the Actions that already exist.
dmStorable.CallAction("ClearAll"); //removes any existing loaded decals
dmStorable.RestoreFromJSON(YourJSON); //add your JSON to the queue stored in the _savedData variable
dmStorable.CallAction("PerformLoad"); //load the data from the _savedData variable

If you want to stack decals on the whatever is there leave off the CallAction("ClearAll")>
 
Last edited:
I had no idea about the CallAction method. Thanks, it worked.

The the latest version there is a new storableURL "Load Preset File" Give it a path to a JSON file and it will load it directly.
There are also a lot more Storables for Adding a new panel or changing the image in an existing panel
 
Back
Top Bottom