Created
March 9, 2017 22:33
-
-
Save sariths/d3b4c2143ff2673deeca44a05d4ebe8b 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
"""This module contains fixes for radiance files exported through su2rad.""" | |
from __future__ import print_function,division | |
import shutil | |
import os | |
def fixAliasMaterialFile(materialFilePath): | |
""" | |
Materials assigned through su2rad are assigned as aliases. | |
Example: | |
For example the below code assigns the material definition of 1K101 to ExteriorWalls: | |
## 1K101 | |
void plastic 1K101 | |
0 | |
0 | |
5 0.683 0.668 0.585 0 0 | |
void alias ExteriorWalls 1K101 | |
This function will remove all the usages of alias change the above definition | |
to: | |
## 1K101 | |
void plastic ExteriorWalls | |
0 | |
0 | |
5 0.683 0.668 0.585 0 0 | |
:param materialFilePath: | |
:return: | |
""" | |
materialsLines = [] | |
lastVoidID = None | |
fileRequiresEditing = False | |
with open(materialFilePath) as materialFile: | |
for idx,lines in enumerate(materialFile): | |
if lines.strip().startswith('void'): | |
if 'alias' in lines.strip(): | |
aliasedMaterial = lines.split()[-2] | |
materialDefSplit =materialsLines[lastVoidID].split() | |
materialDefSplit[-1] = aliasedMaterial | |
materialsLines[lastVoidID] = " ".join(materialDefSplit) | |
fileRequiresEditing = True | |
else: | |
lastVoidID = idx | |
materialsLines.append(lines) | |
materialsLines = [matlines.strip() for matlines in materialsLines if 'alias' not in matlines] | |
if fileRequiresEditing: | |
shutil.copy(materialFilePath,materialFilePath+'bak') | |
with open(materialFilePath,'w') as newMaterialInfo: | |
print("# Original file from su2rad edited by python script to remove " | |
"aliases. The original file has been saved to %s"%(materialFilePath+'bak'),file=newMaterialInfo) | |
print("\n".join(materialsLines),file=newMaterialInfo) | |
print('The file {0} was edited. A backup file has been saved to ' | |
'{0}bak'.format(materialFilePath)) | |
else: | |
print("No editing was performed on the file {}".format(materialFilePath)) | |
def fixXformPathIssues(radianceSceneOrRifFile): | |
"""Add a ./ infront of paths so that they are handled well in Windows as well | |
as Unix based systems. Will work for rif files as well radiance scene files.""" | |
if os.path.isabs(radianceSceneOrRifFile): | |
os.chdir(os.path.split(radianceSceneOrRifFile)[0]) | |
fileLines = [] | |
fileShouldBeEdited = False | |
with open(radianceSceneOrRifFile) as radScene: | |
for lines in radScene: | |
if not lines.strip().startswith('#'): | |
lineValues = lines.split() | |
for word in lineValues: | |
if os.path.exists(word) or os.path.exists(os.path.split(word)[0]): | |
if not word.startswith(r'./'): | |
newWord = r'./'+word.strip() | |
lines = lines.replace(word.strip(),newWord) | |
fileShouldBeEdited = True | |
# print(lines) | |
# print(word) | |
fileLines.append(lines.strip()) | |
if fileShouldBeEdited: | |
shutil.copy(radianceSceneOrRifFile, radianceSceneOrRifFile + 'bak') | |
with open(radianceSceneOrRifFile,'w') as newFile: | |
print( "# Original file from su2rad edited by python script to fix " | |
"filePaths. The original file has been saved to %s" % ( | |
radianceSceneOrRifFile + 'bak'), file=newFile) | |
print("\n".join(fileLines), file=newFile) | |
print('The file {0} was edited. A backup file has been saved to ' | |
'{0}bak'.format(radianceSceneOrRifFile)) | |
else: | |
print('No edititing was performed on the file %s'%radianceSceneOrRifFile) | |
def fixEverythingFromRif(su2RadRifFile): | |
"""Use this function to be very lazy ! Load the rif file for the project | |
and then the scene file and material file will be fixed by this funciton.""" | |
materialFilePath = None | |
sceneFilePath = None | |
os.chdir(os.path.split(su2RadRifFile)[0]) | |
with open(su2RadRifFile)as rifFile: | |
rifFileList = rifFile.read().split() | |
if 'scene=' in rifFileList: | |
sceneFileNameIndex = rifFileList.index('scene=')+1 | |
sceneFilePath = os.path.abspath(rifFileList[sceneFileNameIndex]) | |
if 'materials=' in rifFileList: | |
materialFileNameIndex = rifFileList.index('materials=')+1 | |
materialFilePath = os.path.abspath(rifFileList[materialFileNameIndex]) | |
if materialFilePath: | |
fixAliasMaterialFile(materialFilePath) | |
if sceneFilePath: | |
fixXformPathIssues(sceneFilePath) | |
fixXformPathIssues(su2RadRifFile) | |
def createPlasticsAndGlass(): | |
"""I created this to generate some materials for Su2Rad. Don't think | |
will be using this again and again.""" | |
numbers = [value/100 for value in range(5,105,5)] | |
numStr = [] | |
for number in numbers: | |
strVal = str(int(number*100)) | |
if len(strVal) == 1: | |
strVal = "00"+strVal | |
if len(strVal) == 2: | |
strVal = "0"+strVal | |
numStr.append(strVal) | |
plasticMat = """ | |
void plastic plastic{0}pc | |
0 | |
0 | |
5 {1} {1} {1} 0 0 | |
""" | |
glassMat = """ | |
void glass glass{0}pc | |
0 | |
0 | |
3 {1} {1} {1} | |
""" | |
for idx,number in enumerate(numbers): | |
print(plasticMat.format(numStr[idx],number)) | |
for idx,number in enumerate(numbers): | |
print(glassMat.format(numStr[idx],number)) | |
# createPlasticsAndGlass() | |
if __name__ == "__main__": | |
fixEverythingFromRif(r"C:\Users\sarith\Desktop\matTest\unnamed_scene.rif") | |
# fixXformPathIssues(r"C:\Users\SummerLBNL\Dropbox\LBNL\models\simple\radiance2\west.rif") | |
# fixAliasMaterialFile(r"C:\Users\SummerLBNL\Dropbox\LBNL\models\simple\radiance2\materials.rad" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment