[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4039] contrib/py/scripts/addons/ online_mat_lib/__init__.py: Bugfixing for OSL script nodes with internal scripts.
Peter Cassetta
peter at fingertipsoft.com
Wed Dec 12 03:27:13 CET 2012
Revision: 4039
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4039
Author: petercassetta
Date: 2012-12-12 02:27:08 +0000 (Wed, 12 Dec 2012)
Log Message:
-----------
Bugfixing for OSL script nodes with internal scripts.
Less than signs (<), greater than signs (>), amperstands (&), and double quote signs (") originally caused problems with the parser I was using.
These will now be converted to entity definitions when written to a .bcm file, and converted back to their real characters when read.
I actually attempted to use them in a previous commit I made, but didn't remember them that well and was using them incorrectly.
Now they are working properly.
Entity definitions are explained in more detail here:
http://www.w3.org/MarkUp/HTMLPlus/htmlplus_13.html
As well, there were some occasional errors which the parser encountered with the XML version-encoding-header-thingy, so I hopefully have fixed all of those too.
Modified Paths:
--------------
contrib/py/scripts/addons/online_mat_lib/__init__.py
Modified: contrib/py/scripts/addons/online_mat_lib/__init__.py
===================================================================
--- contrib/py/scripts/addons/online_mat_lib/__init__.py 2012-12-12 00:09:08 UTC (rev 4038)
+++ contrib/py/scripts/addons/online_mat_lib/__init__.py 2012-12-12 02:27:08 UTC (rev 4039)
@@ -1550,38 +1550,26 @@
material_file_contents = ""
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.filename = ""
- self.mat_name = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif library != "bundled" and os.path.exists(mat_lib_folder + os.sep + mat_lib_host + os.sep + library + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm"):
bcm_file = open(mat_lib_folder + os.sep + mat_lib_host + os.sep + library + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="r", encoding="UTF-8")
material_file_contents = ""
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.filename = ""
- self.mat_name = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif library == "bundled" and os.path.exists(mat_lib_folder + os.sep + "bundled" + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm"):
bcm_file = open(mat_lib_folder + os.sep + "bundled" + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="r", encoding="UTF-8")
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.filename = ""
- self.mat_name = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif working_mode == "online":
connection = http.client.HTTPConnection(mat_lib_host)
connection.request("GET", mat_lib_location + "cycles/" + category_filename + "/" + self.filename + ".bcm")
response = connection.getresponse().read()
+ #Check file for validitity
+ if '<?xml version="1.0" encoding="UTF-8"?>' not in str(response)[2:40]:
+ self.report({'ERROR'}, "Material file is either outdated or invalid.")
+ self.filename = ""
+ return {'CANCELLED'}
+
#Cache material
if library == "composite":
bcm_file = open(mat_lib_folder + os.sep + mat_lib_host + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="w+b")
@@ -1592,13 +1580,7 @@
bcm_file.write(response)
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in str(response)[2:40]:
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- self.filename = ""
- return {'CANCELLED'}
- material_file_contents = ""
- material_file_contents = str(response)[str(response).index("<material"):str(response).index("/material>") + 10]
+ material_file_contents = str(response)
else:
self.report({'ERROR'}, "Material is not cached; cannot download in offline mode!")
return {'CANCELLED'}
@@ -1660,12 +1642,16 @@
else:
mat_name = context.scene.mat_lib_bcm_name
- #Format nicely
- material_file_contents = material_file_contents.replace('<?xml version="1.0" encoding="UTF-8"?>', '')
- material_file_contents = material_file_contents.replace("\r\n",'')
- material_file_contents = material_file_contents.replace("\n",'')
- material_file_contents = material_file_contents.replace("\t",'')
- material_file_contents = material_file_contents.replace("\\",'')
+ if '<?xml version="1.0" encoding="UTF-8"?>' in material_file_contents[0:40]:
+ material_file_contents = material_file_contents[material_file_contents.index("<material"):(material_file_contents.rindex("</material>") + 11)]
+ else:
+ self.mat_name = ""
+ self.filename = ""
+ self.text_block = ""
+ self.open_location = ""
+ self.report({'ERROR'}, "Material file is either invalid or outdated.")
+ print(material_file_contents)
+ return {'CANCELLED'}
#Create new material
new_mat = bpy.data.materials.new(mat_name)
@@ -1680,7 +1666,7 @@
for s in scripts:
osl_datablock = bpy.data.texts.new(name=s.attributes['name'].value)
osl_text = s.toxml()[s.toxml().index(">"):s.toxml().rindex("<")]
- osl_text = osl_text[1:].replace("<br/>","\n").replace("lt;", "<").replace("gt;", ">")
+ osl_text = osl_text[1:].replace("<br/>","\n").replace("<", "<").replace(">", ">").replace(""", "\"").replace("&", "&")
osl_datablock.write(osl_text)
osl_scripts.append(osl_datablock)
@@ -1747,32 +1733,14 @@
bcm_file = open(mat_lib_folder + os.sep + mat_lib_host + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="r", encoding="UTF-8")
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.mat_name = ""
- self.filename = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif library != "bundled" and os.path.exists(mat_lib_folder + os.sep + mat_lib_host + os.sep + library + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm"):
bcm_file = open(mat_lib_folder + os.sep + mat_lib_host + os.sep + library + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="r", encoding="UTF-8")
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.mat_name = ""
- self.filename = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif library == "bundled" and os.path.exists(mat_lib_folder + os.sep + "bundled" + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm"):
bcm_file = open(mat_lib_folder + os.sep + "bundled" + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename + ".bcm", mode="r", encoding="UTF-8")
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.mat_name = ""
- self.filename = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
elif working_mode == "online":
connection = http.client.HTTPConnection(mat_lib_host)
connection.request("GET", mat_lib_location + "cycles/" + category_filename + "/" + self.filename + ".bcm")
@@ -1795,8 +1763,7 @@
bcm_file.write(response)
bcm_file.close()
- material_file_contents = ""
- material_file_contents = str(response)[str(response).index("<material"):str(response).index("/material>") + 10]
+ material_file_contents = str(response)
else:
self.report({'ERROR'}, "Material is not cached; cannot download in offline mode!")
self.mat_name = ""
@@ -1810,11 +1777,6 @@
material_file_contents = bcm_file.read()
bcm_file.close()
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents:
- self.open_location = ""
- self.report({'ERROR'}, "Material file is either outdated or invalid.")
- return {'CANCELLED'}
mat_name = ""
for word in self.open_location.split(os.sep)[-1][:-4].split("_"):
if mat_name is not "":
@@ -1834,11 +1796,6 @@
self.text_block = "";
return {'CANCELLED'}
- #Check file for validitity
- if '<?xml version="1.0" encoding="UTF-8"?>' not in material_file_contents[0:38]:
- self.report({'ERROR'}, "Material data is either outdated or invalid.")
- self.text_block = ""
- return {'CANCELLED'}
if context.scene.mat_lib_bcm_name is "":
separator = ""
if "_" in self.text_block:
@@ -1871,12 +1828,16 @@
context.active_object.active_material.use_nodes = True
context.active_object.active_material.node_tree.nodes.clear()
- #Format nicely
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list