VaM 1.x I'm trying to write a simple script for looks. How can I verify that a directory exists in the VAM space?

Threads regarding the original VaM 1.x
Joined
Jan 8, 2024
Messages
9
Reactions
0
I'm trying to write a simple script for looks. How can I verify that a directory exists in the VAM space?


C#:
var looks1 = new List<string>();
var path1 = SuperController.singleton.NormalizePath(path);
if (!SuperController.singleton.DirectoryExists(path1))
    return looks1;
var files1 = SuperController.singleton.GetFilesAtPath(path1, "*.json");
 
Yes, DirectoryExists.
You can't manipulate directories outside of "Saved" and "Custom" due to security measures.
 
Upvote 0
I don't remember SuperController containing DirectoryExists.

Your code might not compile in VAM.

For file system use FileManagerSecure.

This code should help you get started:

C#:
using System.Collections.Generic;
using MVR.FileManagementSecure; // FileManagerSecure (VaM's sandboxed file API)

public class LooksLister : MVRScript
{
    // Returns the .vap (appearance preset) files in a folder, or an empty list.
    private List<string> GetLooks(string path)
    {
        var looks = new List<string>();

        // Tidy up slashes etc. so the path is in VaM's expected format.
        string normalized = SuperController.singleton.NormalizePath(path);

        // Don't use System.IO (Directory.Exists): VaM blocks it for plugins.
        // FileManagerSecure is the allowed equivalent. It returns:
        //   true  -> folder exists and is inside VaM's allowed area
        //   false -> folder doesn't exist
        // It throws only if the folder exists but is OUTSIDE the allowed area, hence the try/catch below.
        try
        {
            if (!FileManagerSecure.DirectoryExists(normalized))
                return looks; // nothing there, give back the empty list

            // Returns full paths, or null if the path isn't allowed.
            string[] files = SuperController.singleton.GetFilesAtPath(normalized, "*.vap");
            if (files != null)
                looks.AddRange(files);
        }
        catch (System.Exception e) // path outside Saved/Custom
        {
            SuperController.LogError("Can't read '" + path + "': " + e.Message);
        }
        return looks;
    }

    // Typical call. Looks live under Custom/Atom/Person/Appearance.
    // Use "Saved/..." or "Custom/..." paths; anything else is blocked.
    private void Start()
    {
        List<string> looks = GetLooks("Custom/Atom/Person/Appearance");
        SuperController.LogMessage("Found " + looks.Count + " looks");
    }
}

For gender checking you can start with this:

C#:
// Returns true if the Person atom is a female character.
// 'atom' is the atom you're asking about; inside an MVRScript you can use
// the built-in 'containingAtom' for the atom the plugin is on.
private bool IsFemale(Atom atom)
{
    // Only Person atoms have a character. Other atom types (lights, cubes...) don't.
    if (atom == null || atom.type != "Person")
        return false;

    // "geometry" is the storable that owns the character's body/skin/morphs.
    // 'as' does a safe cast: gives null instead of throwing if it's the wrong type.
    var selector = atom.GetStorableByID("geometry") as DAZCharacterSelector;
    if (selector == null)
        return false; // character not ready yet

    return selector.gender == DAZCharacterSelector.Gender.Female;
}

// Every Person on stage, with their gender:
private void ListPeople()
{
    // GetAtoms() gives all atoms in the scene; we filter to People ourselves.
    foreach (Atom atom in SuperController.singleton.GetAtoms())
    {
        if (atom.type != "Person") continue;
        var selector = atom.GetStorableByID("geometry") as DAZCharacterSelector;
        if (selector == null) continue;

        // Enums print as their name, so this logs "Female" or "Male".
        SuperController.LogMessage(atom.uid + ": " + selector.gender);
    }
}
 
Upvote 0
Back
Top Bottom