[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4307] contrib/py/scripts/addons/ online_mat_lib/__init__.py: Material Library Add-on Improvements

Peter Cassetta peter at fingertipsoft.com
Tue Feb 19 14:08:16 CET 2013


Revision: 4307
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4307
Author:   petercassetta
Date:     2013-02-19 13:08:15 +0000 (Tue, 19 Feb 2013)
Log Message:
-----------
Material Library Add-on Improvements

Now my add-on supports all 2.66 nodes, OSL scripts within group nodes, and node widths.
The neccessary Python access for this last feature was added sometime after the Blender 2.65 release, and it will be used in the upcoming 2.66.

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	2013-02-19 12:51:21 UTC (rev 4306)
+++ contrib/py/scripts/addons/online_mat_lib/__init__.py	2013-02-19 13:08:15 UTC (rev 4307)
@@ -126,8 +126,14 @@
 save_filename = ""
 script_stack = []
 group_stack = []
+curve_stack = []
+group_curve_stack = []
+group_script_stack = []
+node_groups = []
 osl_scripts = []
-node_groups = []
+mapping_curves = []
+group_scripts = []
+group_curves = []
 
 bpy.types.Scene.mat_lib_auto_preview = bpy.props.BoolProperty(name = "Auto-download previews", description = "Automatically download material previews in online mode", default = True, options = {'SKIP_SAVE'})
 bpy.types.Scene.mat_lib_show_osl_materials = bpy.props.BoolProperty(name = "Show OSL materials", description = "Enable to show materials with OSL shading scripts", default = False, options = {'SKIP_SAVE'})
@@ -1810,6 +1816,7 @@
         global current_material_cached
         global osl_scripts
         global node_groups
+        global mapping_curves
         
         findLibrary()
         
@@ -1947,6 +1954,19 @@
             group_datablock = addNodeGroup(g.attributes['name'].value, group_text)
             node_groups.append(group_datablock)
         
+        #Prepare curve data
+        mapping_curves = []
+        curves = dom.getElementsByTagName("curve")
+        for curve in curves:
+            curve_points = []
+            cdom = xml.dom.minidom.parseString(curve.toxml())
+            points = cdom.getElementsByTagName("point")
+            for point in points:
+                loc_x = float(point.attributes['loc'].value.replace(" ", "").split(",")[0])
+                loc_y = float(point.attributes['loc'].value.replace(" ", "").split(",")[1])
+                curve_points.append(curvePoint(point.attributes['type'].value, loc_x, loc_y))
+            mapping_curves.append(mappingCurve(curve.attributes['extend'].value, curve_points))
+        
         #Add nodes
         nodes = dom.getElementsByTagName("node")
         addNodes(nodes, new_mat.node_tree)
@@ -2006,6 +2026,7 @@
         global current_material_cached
         global osl_scripts
         global node_groups
+        global mapping_curves
         
         findLibrary()
         
@@ -2151,6 +2172,19 @@
             group_datablock = addNodeGroup(g.attributes['name'].value, group_text)
             node_groups.append(group_datablock)
         
+        #Prepare curve data
+        mapping_curves = []
+        curves = dom.getElementsByTagName("curve")
+        for curve in curves:
+            curve_points = []
+            cdom = xml.dom.minidom.parseString(curve.toxml())
+            points = cdom.getElementsByTagName("point")
+            for point in points:
+                loc_x = float(point.attributes['loc'].value.replace(" ", "").split(",")[0])
+                loc_y = float(point.attributes['loc'].value.replace(" ", "").split(",")[1])
+                curve_points.append(curvePoint(point.attributes['type'].value, loc_x, loc_y))
+            mapping_curves.append(mappingCurve(curve.attributes['extend'].value, curve_points))
+        
         #Add nodes
         nodes = dom.getElementsByTagName("node")
         addNodes(nodes, context.active_object.active_material.node_tree)
@@ -3746,14 +3780,51 @@
         
         node_tree.links.new(input, output)
 
+class curvePoint:
+    def __init__(self, type, loc_x, loc_y):
+        self.type = type
+        self.loc_x = loc_x
+        self.loc_y = loc_y
+
+class mappingCurve:
+    def __init__(self, extend, points):
+        self.extend = extend
+        self.points = points
+
 def addNodeGroup(name, group_text):
+    global group_curves
+    global group_scripts
+    
     group = bpy.data.node_groups.new(name, 'SHADER')
     
     group_text = group_text[group_text.index("<group"):group_text.rindex("</group>") + 8]
     gdom = xml.dom.minidom.parseString(group_text)
     
+    #Prepare curve data
+    curves = gdom.getElementsByTagName("groupcurve")
+    group_curves = []
+    for curve in curves:
+        curve_points = []
+        cdom = xml.dom.minidom.parseString(curve.toxml())
+        points = cdom.getElementsByTagName("point")
+        for point in points:
+            loc_x = float(point.attributes['loc'].value.replace(" ", "").split(",")[0])
+            loc_y = float(point.attributes['loc'].value.replace(" ", "").split(",")[1])
+            curve_points.append(curvePoint(point.attributes['type'].value, loc_x, loc_y))
+        group_curves.append(mappingCurve(curve.attributes['extend'].value, curve_points))
+    
+    #Create internal OSL scripts
+    scripts = gdom.getElementsByTagName("groupscript")
+    group_scripts = []
+    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;", ">").replace("&quot;", "\"").replace("&amp;", "&")
+        osl_datablock.write(osl_text)
+        group_scripts.append(osl_datablock)
+    
     nodes = gdom.getElementsByTagName("groupnode")
-    addNodes(nodes, group)
+    addNodes(nodes, group, True)
     
     inputs = gdom.getElementsByTagName("groupinput")
     for input in inputs:
@@ -3800,7 +3871,7 @@
     
     return group
 
-def addNodes(nodes, node_tree):
+def addNodes(nodes, node_tree, group_mode = False):
     global node_message
     global osl_scripts
     global node_groups
@@ -4266,6 +4337,79 @@
             node.inputs['Color'].default_value = color(node_data['color'].value)
             node.inputs['Bright'].default_value = float(node_data['bright'].value)
             node.inputs['Contrast'].default_value = float(node_data['contrast'].value)
+            
+        elif node_type == "CURVE_RGB":
+            print ("CURVE_RGB")
+            if bpy.app.version[0] + (bpy.app.version[1] / 100.0) < 2.66:
+                node_message = ['ERROR', """The material file contains the node \"%s\".
+This node is not available in the Blender version you are currently using.
+You may need a newer version of Blender for this material to work properly.""" % node_type]
+                return
+            node = node_tree.nodes.new(node_type)
+            node.inputs['Fac'].default_value = float(node_data['fac'].value)
+            node.inputs['Color'].default_value = color(node_data['color'].value)
+            if group_mode:
+                curve_c = group_curves[int(node_data['curve_c'].value)]
+                curve_r = group_curves[int(node_data['curve_r'].value)]
+                curve_g = group_curves[int(node_data['curve_g'].value)]
+                curve_b = group_curves[int(node_data['curve_b'].value)]
+            else:
+                curve_c = mapping_curves[int(node_data['curve_c'].value)]
+                curve_r = mapping_curves[int(node_data['curve_r'].value)]
+                curve_g = mapping_curves[int(node_data['curve_g'].value)]
+                curve_b = mapping_curves[int(node_data['curve_b'].value)]
+            
+            #C Curve
+            node.mapping.curves[3].extend = curve_c.extend
+            if len(curve_c.points) > 2:
+                p = 2
+                while p < len(curve_c.points):
+                    node.mapping.curves[3].points.new(0.0, 0.0)
+                    p += 1
+            p = 0
+            while p < len(curve_c.points):
+                node.mapping.curves[3].points[p].location = (curve_c.points[p].loc_x, curve_c.points[p].loc_y)
+                node.mapping.curves[3].points[p].handle_type = curve_c.points[p].type
+                p += 1
+            
+            #R Curve
+            node.mapping.curves[0].extend = curve_r.extend
+            if len(curve_r.points) > 2:
+                p = 2
+                while p < len(curve_r.points):
+                    node.mapping.curves[0].points.new(0.0, 0.0)
+                    p += 1
+            p = 0
+            while p < len(curve_r.points):
+                node.mapping.curves[0].points[p].location = (curve_r.points[p].loc_x, curve_r.points[p].loc_y)
+                node.mapping.curves[0].points[p].handle_type = curve_r.points[p].type
+                p += 1
+            
+            #G Curve
+            node.mapping.curves[1].extend = curve_g.extend
+            if len(curve_g.points) > 2:
+                p = 2
+                while p < len(curve_g.points):
+                    node.mapping.curves[1].points.new(0.0, 0.0)
+                    p += 1
+            p = 0
+            while p < len(curve_g.points):
+                node.mapping.curves[1].points[p].location = (curve_g.points[p].loc_x, curve_g.points[p].loc_y)
+                node.mapping.curves[1].points[p].handle_type = curve_g.points[p].type
+                p += 1
+            
+            #B Curve
+            node.mapping.curves[2].extend = curve_b.extend
+            if len(curve_b.points) > 2:
+                p = 2
+                while p < len(curve_b.points):
+                    node.mapping.curves[2].points.new(0.0, 0.0)
+                    p += 1
+            p = 0
+            while p < len(curve_b.points):
+                node.mapping.curves[2].points[p].location = (curve_b.points[p].loc_x, curve_b.points[p].loc_y)
+                node.mapping.curves[2].points[p].handle_type = curve_b.points[p].type
+                p += 1
         
         elif node_type == "GAMMA":
             print ("GAMMA")
@@ -4315,6 +4459,64 @@
             node = node_tree.nodes.new(node_type)
             node.inputs["Strength"].default_value = float(node_data['strength'].value)
             
+        elif node_type == "CURVE_VEC":
+            print ("CURVE_VEC")
+            if bpy.app.version[0] + (bpy.app.version[1] / 100.0) < 2.66:
+                node_message = ['ERROR', """The material file contains the node \"%s\".
+This node is not available in the Blender version you are currently using.
+You may need a newer version of Blender for this material to work properly.""" % node_type]
+                return
+            node = node_tree.nodes.new(node_type)
+            node.inputs['Fac'].default_value = float(node_data['fac'].value)
+            node.inputs['Vector'].default_value = vector(node_data['vector'].value)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list