Last active
April 26, 2024 18:22
-
-
Save aobond2/0fddb379b06375a6dee165af44a23ea6 to your computer and use it in GitHub Desktop.
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 json | |
import os | |
import csv | |
bucketURL = "https://storage.googleapis.com/glb-content-bucket/GLB/" | |
thumbnailLocalPath ="" | |
thumbnailLocalFolder = "C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/Thumbnails" | |
GLBfolder = "C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/" | |
csvData = [] | |
csvPath = "C:/Users/BondhanKimbalazani/Documents/parts.csv" | |
def getAllFiles(directory): | |
filePaths = [] | |
for root, _, files in os.walk(directory): | |
for file in files: | |
filePath = os.path.join(root, file) | |
filePaths.append(filePath) | |
return filePaths | |
def getAllThumbnails(thumbnailDirectory): | |
thumbnailFilePaths = [] | |
for root, _, files in os.walk(thumbnailDirectory): | |
for file in files: | |
filepath = os.path.join(root, file) | |
thumbnailFilePaths.append(filepath) | |
for t in thumbnailFilePaths: | |
print (t.split('C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/')[1]) | |
return thumbnailFilePaths | |
def getPartThumbnail(thumbnailFilePaths, partName): | |
return [element for element in thumbnailFilePaths if partName in element] | |
def processFiles(): | |
allFiles = getAllFiles(GLBfolder) | |
allThumbnails = getAllThumbnails(thumbnailLocalFolder) | |
for f in allFiles: | |
if ".glb" in f: | |
try: | |
correctFilePath = f.replace(os.sep, '/') | |
fileLoc = (f.split(GLBfolder)[1]).replace(os.sep, '/') | |
f = os.path.splitext(f)[0] | |
filename = os.path.basename(f) | |
# Get object name | |
name = (filename.split("_")[2:]) | |
name = "_".join(name) | |
lastIndex = name.split("_")[-1] | |
if lastIndex.isnumeric(): | |
name = name.split("_")[:-1] | |
name = "_".join(name) | |
# Local path | |
localPath = correctFilePath.split(GLBfolder)[1] | |
# Google bucket URL | |
gbURL = bucketURL + fileLoc | |
# Get thumbnail name | |
# Get part name | |
partName = (filename.split("_")[1]) | |
# Get body type | |
bodyType = (filename.split("_")[0]) | |
# Thumbnail local path | |
thumbnailName = (getPartThumbnail(allThumbnails, name))[0] | |
thumbnailLocalPath = thumbnailName | |
# Make json | |
#makeJson(name, partName, bodyType, gbURL, localPath, thumbnailLocalPath) | |
#break | |
# Make CSV | |
addToCSV(name, partName, thumbnailLocalPath, gbURL, bodyType) | |
except: | |
continue | |
def makeJson(name, partName, bodyType, gbURL, localPath, thumbnailLocalPath): | |
data = { | |
"data" : { | |
"name" : "_".join(name.split("_")[2:]), | |
"url" : gbURL, | |
"attachmentPoint" : partName, | |
"bodyType" : bodyType, | |
"localPath" : localPath, | |
"thumbnailLocalPath" : thumbnailLocalPath.split('C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/')[1] | |
} | |
} | |
with open((name + ".json"), "w") as file: | |
json.dump(data, file, indent = 4) | |
def addToCSV(name, partName, thumbnailLocalPath, glbURL, bodyType): | |
thumbnailLocalPath = thumbnailLocalPath.split('C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/')[1] | |
name = "_".join(name.split("_")[2:]) | |
data = [name, partName, thumbnailLocalPath, glbURL, bodyType] | |
csvData.append(data) | |
def makeCSV(): | |
with open(csvPath, mode='w', newline='') as file: | |
wrriter = csv.writer(file) | |
wrriter.writerows(csvData) | |
processFiles() | |
makeCSV() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment