Last active
February 21, 2024 17:29
-
-
Save aobond2/34eabe7ab65a6daf94b797c7c0dd99db to your computer and use it in GitHub Desktop.
Read csv for all categories and available GLBs, then write out MML avatar based on some rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import random | |
import os | |
# Source CSV | |
CSVfilepath = "C:/Users/BondhanKimbalazani/Documents/ModularCharacter/ClothingTest-Sheet3.csv" | |
TextOutputFolder = "C:/Users/BondhanKimbalazani/Documents/ModularCharacter/MMLOutput" | |
BodyArray = [] | |
HairArray = [] | |
LegBaseArray = [] | |
LegsUpperArray = [] | |
ShoesArray = [] | |
TorsoBaseArray = [] | |
TorsoUpperArray = [] | |
partsDictionary = { | |
"partName" : ["MainBody","Hair","LegBase","LegsUpper","Shoes","TorsoBase","TorsoUpper"], | |
"partArray" : [BodyArray, HairArray, LegBaseArray, LegsUpperArray, ShoesArray, TorsoBaseArray, TorsoUpperArray] | |
} | |
# Make folder if not already exist | |
os.makedirs(TextOutputFolder, exist_ok=True) | |
def readCSV(): | |
# Read and fill body parts array | |
with open(CSVfilepath, "r", newline="") as csvfile: | |
reader = csv.reader(csvfile) | |
# Skip header row if present | |
next(reader, None) | |
for row in reader: | |
if row: | |
for i, part in enumerate(partsDictionary["partArray"]): | |
if row[i].strip(): | |
part.append(row[i]) | |
def makeNewAvatarArray(): | |
newArray = [] | |
bodyType = "" | |
maxIterations = 30 | |
for currentPartArray in partsDictionary["partArray"]: | |
# Get body first | |
if currentPartArray == BodyArray: | |
randomPart = random.choice(currentPartArray) | |
bodyType = checkIfBodyAOrBodyB(os.path.basename(randomPart)) | |
newArray.append(randomPart) | |
# Get all other parts and only get if the name match body type | |
else: | |
for _ in range(maxIterations): | |
randomPart = random.choice(currentPartArray) | |
if bodyType in randomPart: | |
newArray.append(randomPart) | |
break | |
else: | |
newArray.append("") | |
return newArray | |
def makeNewAvatarArrayAnyParts(): | |
# Function to make avatar with any existing parts. Ignoring body type | |
newArray = [] | |
for currentPartArray in partsDictionary["partArray"]: | |
if currentPartArray: | |
randomPart = random.choice(currentPartArray) | |
if randomPart: | |
newArray.append(randomPart) | |
return newArray | |
def checkIfBodyAOrBodyB(bodyInput): | |
# Check the first string separated by '_' | |
bodyType = bodyInput.split("_")[0] | |
return bodyType | |
def createOutputFolder(): | |
os.makedirs(TextOutputFolder, exist_ok=True) | |
def writeOutAvatarArray(): | |
baseFileName = "avatar.txt" | |
avatarArray = makeNewAvatarArray() | |
# TODO: Remove this later | |
#avatarArray = makeNewAvatarArrayAnyParts() | |
filePath = os.path.join(TextOutputFolder, baseFileName) | |
# Increment file name if existed | |
counter = 1 | |
while os.path.exists(filePath): | |
newFileName = f"{baseFileName[:-len('.txt')]}({counter:05}).txt" | |
filePath = os.path.join(TextOutputFolder, newFileName) | |
counter += 1 | |
# Add various tags to avatar array | |
mmlArray = formatArrayToMML(avatarArray) | |
with open(filePath,"w") as file: | |
for item in mmlArray: | |
file.write(item + "\n") | |
def formatArrayToMML(avatarArray): | |
MMLArray = [] | |
mModelStartTag = "<m-model src=" | |
mModelEndTag = "></m-model>" | |
mCharacterStartTag = "<m-character src=" | |
mCharacterEndTag = "</m-character>" | |
# Add first character tag | |
firstCharacterTag = mCharacterStartTag + '"' + avatarArray[0] + '"' + ">" | |
MMLArray.append(firstCharacterTag) | |
# Add additional M-Model | |
for i in range(1, len(avatarArray)): | |
if avatarArray[i]: | |
newMModel = "\t" + mModelStartTag + '"' + avatarArray[i] + '"' + mModelEndTag | |
MMLArray.append(newMModel) | |
# Add end character tag | |
MMLArray.append(mCharacterEndTag) | |
return MMLArray | |
createOutputFolder() | |
readCSV() | |
writeOutAvatarArray() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment