Revision: 20789
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20789
Author:   campbellbarton
Date:     2009-06-10 21:14:05 +0200 (Wed, 10 Jun 2009)

Log Message:
-----------
Option to export curves as OBJ native curves (rather then a bunch of edges)
- nurbs and polyline (not bezier)
- closed / open curves
- OrderU works as expected
- Endpoint U works too.

Modified Paths:
--------------
    trunk/blender/release/scripts/export_obj.py

Modified: trunk/blender/release/scripts/export_obj.py
===================================================================
--- trunk/blender/release/scripts/export_obj.py 2009-06-10 18:02:54 UTC (rev 
20788)
+++ trunk/blender/release/scripts/export_obj.py 2009-06-10 19:14:05 UTC (rev 
20789)
@@ -181,12 +181,87 @@
                                copyCount+=1
        print '\tCopied %d images' % copyCount
 
+
+def test_nurbs_compat(ob):
+       if ob.type != 'Curve':
+               return False
+       
+       for nu in ob.data:
+               if (not nu.knotsV) and nu.type != 1: # not a surface and not 
bezier
+                       return True
+       
+       return False
+
+def write_nurb(file, ob, ob_mat):
+       cu = ob.data
+       
+       # use negative indices
+       Vector = Blender.Mathutils.Vector
+       for nu in cu:
+               
+               if nu.type==0:          DEG_ORDER_U = 1
+               else:                           DEG_ORDER_U = nu.orderU-1  # 
Tested to be correct
+               
+               if nu.type==1:
+                       print "\tWarning, bezier curve:", ob.name, "only poly 
and nurbs curves supported"
+                       continue
+               
+               if nu.knotsV:
+                       print "\tWarning, surface:", ob.name, "only poly and 
nurbs curves supported"
+                       continue
+               
+               if len(nu) <= DEG_ORDER_U:
+                       print "\tWarning, orderU is lower then vert count, 
skipping:", ob.name
+                       continue
+               
+               pt_num = 0
+               do_closed = (nu.flagU & 1)
+               do_endpoints = (do_closed==0) and (nu.flagU & 2)
+               
+               for pt in nu:
+                       pt = Vector(pt[0], pt[1], pt[2]) * ob_mat
+                       file.write('v %.6f %.6f %.6f\n' % (pt[0], pt[1], pt[2]))
+                       pt_num += 1
+               
+               
+               file.write('g %s\n' % (fixName(ob.name))) # 
fixName(ob.getData(1)) could use the data name too
+               file.write('cstype bspline\n') # not ideal, hard coded
+               file.write('deg %d\n' % DEG_ORDER_U) # not used for curves but 
most files have it still
+               
+               curve_ls = [-(i+1) for i in xrange(pt_num)]
+               
+               # 'curv' keyword
+               if do_closed:
+                       if DEG_ORDER_U == 1:
+                               pt_num += 1
+                               curve_ls.append(-1)
+                       else:
+                               val = DEG_ORDER_U - 1
+                               pt_num += val*2
+                               curve_ls = curve_ls[-val:] + curve_ls + 
curve_ls[0:val] # Blenders OrderU of 3 -> val==1, 4 -> 2 etc
+               
+               file.write('curv 0.0 1.0 %s\n' % (' '.join( [str(i) for i in 
curve_ls] ))) # Blender has no U and V values for the curve
+               
+               # 'parm' keyword
+               tot_parm = (DEG_ORDER_U + 1) + pt_num
+               tot_parm_div = float(tot_parm-1)
+               parm_ls = [(i/tot_parm_div) for i in xrange(tot_parm)]
+               
+               if do_endpoints: # end points, force param
+                       for i in xrange(DEG_ORDER_U+1):
+                               parm_ls[i] = 0.0
+                               parm_ls[-(1+i)] = 1.0
+               
+               file.write('parm u %s\n' % ' '.join( [str(i) for i in parm_ls] 
))
+
+               file.write('end\n')
+
 def write(filename, objects,\
 EXPORT_TRI=False,  EXPORT_EDGES=False,  EXPORT_NORMALS=False,  
EXPORT_NORMALS_HQ=False,\
 EXPORT_UV=True,  EXPORT_MTL=True,  EXPORT_COPY_IMAGES=False,\
 EXPORT_APPLY_MODIFIERS=True, EXPORT_ROTX90=True, EXPORT_BLEN_OBS=True,\
 EXPORT_GROUP_BY_OB=False,  EXPORT_GROUP_BY_MAT=False, 
EXPORT_KEEP_VERT_ORDER=False,\
-EXPORT_POLYGROUPS=False):
+EXPORT_POLYGROUPS=False, EXPORT_CURVE_AS_NURBS=True):
        '''
        Basic write function. The context and options must be alredy set
        This can be accessed externaly
@@ -263,9 +338,21 @@
        
        globalNormals = {}
        
+       
        # Get all meshes
        for ob_main in objects:
                for ob, ob_mat in BPyObject.getDerivedObjects(ob_main):
+                       
+                       # Nurbs curve support
+                       if EXPORT_CURVE_AS_NURBS and test_nurbs_compat(ob):
+                               if EXPORT_ROTX90:
+                                       ob_mat = ob_mat * mat_xrot90
+                               
+                               write_nurb(file, ob, ob_mat)
+                               
+                               continue
+                       # end nurbs
+                       
                        # Will work for non meshes now! :)
                        # getMeshFromObject(ob, container_mesh=None, 
apply_modifiers=True, vgroups=True, scn=None)
                        me= BPyMesh.getMeshFromObject(ob, containerMesh, 
EXPORT_APPLY_MODIFIERS, EXPORT_POLYGROUPS, scn)
@@ -585,7 +672,7 @@
                EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
                EXPORT_ANIMATION, EXPORT_COPY_IMAGES, EXPORT_BLEN_OBS,\
                EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, 
EXPORT_KEEP_VERT_ORDER,\
-               EXPORT_POLYGROUPS
+               EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS
        
        EXPORT_APPLY_MODIFIERS = Draw.Create(0)
        EXPORT_ROTX90 = Draw.Create(1)
@@ -604,6 +691,7 @@
        EXPORT_GROUP_BY_MAT = Draw.Create(0)
        EXPORT_KEEP_VERT_ORDER = Draw.Create(1)
        EXPORT_POLYGROUPS = Draw.Create(0)
+       EXPORT_CURVE_AS_NURBS = Draw.Create(1)
        
        
        # Old UI
@@ -693,7 +781,7 @@
                                EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
                                EXPORT_ANIMATION, EXPORT_COPY_IMAGES, 
EXPORT_BLEN_OBS,\
                                EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, 
EXPORT_KEEP_VERT_ORDER,\
-                               EXPORT_POLYGROUPS
+                               EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS
 
                        Draw.Label('Context...', ui_x+9, ui_y+239, 220, 20)
                        Draw.BeginAlign()
@@ -726,7 +814,9 @@
                        Draw.EndAlign()
                        EXPORT_POLYGROUPS = Draw.Toggle('Polygroups', 
EVENT_REDRAW, ui_x+9, ui_y+95, 120, 20, EXPORT_POLYGROUPS.val, 'Export vertex 
groups as OBJ groups (one group per face approximation).')
                        
+                       EXPORT_CURVE_AS_NURBS = Draw.Toggle('Nurbs', 
EVENT_NONE, ui_x+139, ui_y+95, 100, 20, EXPORT_CURVE_AS_NURBS.val, 'Export 3D 
nurbs curves and polylines as OBJ curves, (bezier not supported).')
                        
+                       
                        Draw.Label('Blender Objects as OBJ:', ui_x+9, ui_y+59, 
220, 20)
                        Draw.BeginAlign()
                        EXPORT_BLEN_OBS = Draw.Toggle('Objects', EVENT_REDRAW, 
ui_x+9, ui_y+39, 60, 20, EXPORT_BLEN_OBS.val, 'Export blender objects as "OBJ 
objects".', do_split)
@@ -779,6 +869,7 @@
        EXPORT_GROUP_BY_MAT = EXPORT_GROUP_BY_MAT.val
        EXPORT_KEEP_VERT_ORDER = EXPORT_KEEP_VERT_ORDER.val
        EXPORT_POLYGROUPS = EXPORT_POLYGROUPS.val
+       EXPORT_CURVE_AS_NURBS = EXPORT_CURVE_AS_NURBS.val
        
        
        base_name, ext = splitExt(filename)
@@ -828,7 +919,7 @@
                        EXPORT_COPY_IMAGES, EXPORT_APPLY_MODIFIERS,\
                        EXPORT_ROTX90, EXPORT_BLEN_OBS,\
                        EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, 
EXPORT_KEEP_VERT_ORDER,\
-                       EXPORT_POLYGROUPS)
+                       EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS)
                
                Blender.Set('curframe', orig_frame)
        


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to