Fix overwrite existing ground material
RadianceObj.addMaterial() now overwrites existing material with new instance based on matching name and type.
If previous instance preferred, set parameter
>>> rewrite=False
@@ -286,13 +286,47 @@ class RadianceObj:
pickle.dump(self, f)
print('Saved to file {}'.format(savefile))
- def addMaterial(self, material, Rrefl, Grefl, Brefl, materialtype='plastic', spec=0, rough=0, material_file=None, comment=None):
+ def addMaterial(self, material, Rrefl, Grefl, Brefl, materialtype='plastic', spec=0, rough=0, material_file=None, comment=None, rewrite=True):
if material_file is None:
material_file = 'ground.rad'
matfile = os.path.join('materials', material_file)
+ '''
+ found = reWriteMaterial(matfile, materialtype, material, comment, rewrite)
+ if (found and rewrite) or (not found):
+ # append --
+ '''
+ file = open(matfile, 'r', newline='')
+ buffer = []
+ for line in file:
+ buffer.append(line)
+ file.close()
+
+ # search buffer for material matching requested addition
+ found = False
+ for i in buffer:
+ if materialtype and material in i:
+ loc = buffer.index(i)
+ found = True
+ break
+ if found:
+ if rewrite:
+ print('Material exists, overwriting...\n')
+ if comment is None:
+ pre = loc - 1
+ else:
+ pre = loc - 2
+ # commit buffer without material match
+ # -- CHANGE --
+ # figure out newline error
+ with open(matfile, 'w', newline='') as ict:
+ for i in buffer[0:pre]:
+ ict.write(i)
+ for i in buffer[loc+4:]:
+ ict.write(str(i)+'\n')
+ if (found and rewrite) or (not found):
# append -- This will create the file if it doesn't exist
file_object = open(matfile, 'a')
file_object.write("\n\n")
@@ -305,6 +339,8 @@ class RadianceObj:
file_object.write("\n0\n0\n5 {} {} {} {} {}".format(Rrefl, Grefl, Brefl, spec, rough))
file_object.close()
print('Added material {} to file {}'.format(material, material_file))
+ if (found and not rewrite):
+ print('Material already exists\n')
def exportTrackerDict(self, trackerdict=None,
savefile=None, reindex=None):
