mirror of
https://github.com/overte-org/overte.git
synced 2025-04-08 21:33:00 +02:00
adding python baking scripts
This commit is contained in:
parent
11f5e7812b
commit
0c6f5bb04d
2 changed files with 173 additions and 0 deletions
91
tools/bake-tools/bake.py
Normal file
91
tools/bake-tools/bake.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
import os, json, sys, shutil, subprocess, shlex, time
|
||||
EXE = os.environ['HIFI_OVEN']
|
||||
|
||||
def listFiles(directory, extension):
|
||||
items = os.listdir(directory)
|
||||
fileList = []
|
||||
for f in items:
|
||||
if f.endswith('.' + extension):
|
||||
fileList.append(f)
|
||||
return fileList
|
||||
|
||||
def camelCaseString(string):
|
||||
string = string.replace('-', ' ')
|
||||
return ''.join(x for x in string.title() if not x.isspace())
|
||||
|
||||
def groupFiles(originalDirectory, newDirectory, files):
|
||||
for file in files:
|
||||
newPath = os.sep.join([newDirectory, file])
|
||||
originalPath = os.sep.join([originalDirectory, file])
|
||||
shutil.move(originalPath, newPath)
|
||||
|
||||
def groupKTXFiles(directory, filePath):
|
||||
baseFile = os.path.basename(filePath)
|
||||
filename = os.path.splitext(baseFile)[0]
|
||||
camelCaseFileName = camelCaseString(filename)
|
||||
path = os.sep.join([directory, camelCaseFileName])
|
||||
files = listFiles(directory, 'ktx')
|
||||
if len(files) > 0:
|
||||
createDirectory(path)
|
||||
groupFiles(directory, path, files)
|
||||
|
||||
newFilePath = os.sep.join([path, baseFile+'.baked.fbx'])
|
||||
originalFilePath = os.sep.join([directory, baseFile+'.baked.fbx'])
|
||||
originalFilePath.strip()
|
||||
shutil.move(originalFilePath, newFilePath)
|
||||
|
||||
def bakeFile(filePath, outputDirectory):
|
||||
createDirectory(outputDirectory)
|
||||
cmd = EXE + ' -i ' + filePath + ' -o ' + outputDirectory + ' -t fbx'
|
||||
args = shlex.split(cmd)
|
||||
process = subprocess.Popen(cmd, stdout=False, stderr=False)
|
||||
process.wait()
|
||||
bakedFile = os.path.splitext(filePath)[0]
|
||||
groupKTXFiles(outputDirectory, bakedFile)
|
||||
|
||||
def bakeFilesInDirectory(directory, outputDirectory):
|
||||
for root, subFolders, filenames in os.walk(directory):
|
||||
for filename in filenames:
|
||||
if filename.endswith('.fbx'):
|
||||
filePath = os.sep.join([root, filename])
|
||||
absFilePath = os.path.abspath(filePath)
|
||||
outputFolder = os.path.join(outputDirectory, os.path.relpath(root))
|
||||
print "Baking file: " + filename
|
||||
bakeFile(absFilePath, outputFolder)
|
||||
else:
|
||||
filePath = os.sep.join([root, filename])
|
||||
absFilePath = os.path.abspath(filePath)
|
||||
outputFolder = os.path.join(outputDirectory, os.path.relpath(root))
|
||||
newFilePath = os.sep.join([outputFolder, filename])
|
||||
createDirectory(outputFolder)
|
||||
print "moving file: " + filename + " to: " + outputFolder
|
||||
shutil.copy(absFilePath, newFilePath)
|
||||
|
||||
def createDirectory(directory):
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
def checkIfExeExists():
|
||||
if not os.path.isfile(EXE) and os.access(EXE, os.X_OK):
|
||||
print 'HIFI_OVEN evironment variable is not set'
|
||||
sys.exit()
|
||||
|
||||
def handleOptions():
|
||||
option = sys.argv[1]
|
||||
if option == '--help' or option == '-h':
|
||||
print 'Usage: bake.py INPUT_DIRECTORY[directory to bake] OUTPUT_DIRECTORY[directory to place backed files]'
|
||||
print 'Note: Output directory will be created if directory does not exist'
|
||||
sys.exit()
|
||||
|
||||
def main():
|
||||
argsLength = len(sys.argv)
|
||||
if argsLength == 3:
|
||||
checkIfExeExists()
|
||||
rootDirectory = sys.argv[1]
|
||||
outputDirectory = os.path.abspath(sys.argv[2])
|
||||
createDirectory(outputDirectory)
|
||||
bakeFilesInDirectory(rootDirectory, outputDirectory)
|
||||
elif argsLength == 2:
|
||||
handleOptions()
|
||||
|
||||
main()
|
82
tools/bake-tools/convertToRelativePaths.py
Normal file
82
tools/bake-tools/convertToRelativePaths.py
Normal file
|
@ -0,0 +1,82 @@
|
|||
import json, os, sys, gzip
|
||||
|
||||
prefix = 'file:///~/'
|
||||
MAP = {}
|
||||
def createAssetMapping(assetDirectory):
|
||||
baseDirectory = os.path.basename(os.path.normpath(assetDirectory))
|
||||
for root, subfolder, filenames in os.walk(assetDirectory):
|
||||
for filename in filenames:
|
||||
if not filename.endswith('.ktx'):
|
||||
substring = os.path.commonprefix([assetDirectory, root])
|
||||
newPath = root.replace(substring, '');
|
||||
filePath = os.sep.join([newPath, filename])
|
||||
if filePath[0] == '\\':
|
||||
filePath = filePath[1:]
|
||||
finalPath = prefix + baseDirectory + '/' + filePath
|
||||
finalPath = finalPath.replace('\\', '/')
|
||||
file = os.path.splitext(filename)[0]
|
||||
file = os.path.splitext(file)[0]
|
||||
MAP[file] = finalPath
|
||||
|
||||
def hasURL(prop):
|
||||
if "URL" in prop:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def handleURL(url):
|
||||
newUrl = url
|
||||
if "atp:" in url:
|
||||
baseFilename = os.path.basename(url)
|
||||
filename = os.path.splitext(baseFilename)[0]
|
||||
newUrl = MAP[filename]
|
||||
print newUrl
|
||||
return newUrl
|
||||
|
||||
def handleOptions():
|
||||
option = sys.argv[1]
|
||||
if option == '--help' or option == '-h':
|
||||
print 'Usage: convertToRelativePaths.py INPUT[json file you want to update the urls] INPUT[directory that the baked files are located in]'
|
||||
sys.exit()
|
||||
|
||||
def main():
|
||||
argsLength = len(sys.argv)
|
||||
if argsLength == 3:
|
||||
jsonFile = sys.argv[1]
|
||||
gzipFile = jsonFile + '.gz'
|
||||
assetDirectory = sys.argv[2]
|
||||
createAssetMapping(assetDirectory)
|
||||
f = open(jsonFile)
|
||||
data = json.load(f)
|
||||
f.close()
|
||||
for entity in data['Entities']:
|
||||
for prop in entity:
|
||||
value = entity[prop]
|
||||
if hasURL(prop):
|
||||
value = handleURL(value)
|
||||
if prop == "script":
|
||||
value = handleURL(value)
|
||||
if prop == "textures":
|
||||
try:
|
||||
tmp = json.loads(value)
|
||||
for index in tmp:
|
||||
tmp[index] = handleURL(tmp[index])
|
||||
value = json.dumps(tmp)
|
||||
except:
|
||||
value = handleURL(value)
|
||||
|
||||
if prop == "serverScripts":
|
||||
value = handleURL(value)
|
||||
|
||||
entity[prop] = value
|
||||
|
||||
|
||||
jsonString = json.dumps(data)
|
||||
jsonBytes= jsonString.encode('utf-8')
|
||||
with gzip.GzipFile(gzipFile, 'w') as fout: # 4. gzip
|
||||
fout.write(jsonBytes)
|
||||
|
||||
elif argsLength == 2:
|
||||
handleOptions()
|
||||
|
||||
main()
|
Loading…
Reference in a new issue