destruction of previous slot api. if it returns, it'll
[blender.git] / release / scripts / bpymodules / BPyMesh.py
index 712b6b72f80d8efa6cea10a97f90f60df1c76c1c..415c2a12c6972374f7a5542859645054405549f4 100644 (file)
@@ -19,6 +19,7 @@
 
 
 import Blender
+import bpy
 import BPyMesh_redux # seperated because of its size.
 # reload(BPyMesh_redux)
 redux= BPyMesh_redux.redux
@@ -55,7 +56,7 @@ def meshWeight2List(me):
        
        if not len_groupNames:
                # no verts? return a vert aligned empty list
-               return [[] for i in xrange(len(me.verts))]
+               return [[] for i in xrange(len(me.verts))], []
        
        else:
                vWeightList= [[0.0]*len_groupNames for i in xrange(len(me.verts))]
@@ -78,6 +79,8 @@ def list2MeshWeight(me, groupNames, vWeightList):
        if len(vWeightList) != len(me.verts):
                raise 'Error, Lists Differ in size, do not modify your mesh.verts before updating the weights'
        
+       act_group = me.activeGroup
+       
        # Clear the vert group.
        currentGroupNames= me.getVertGroupNames()
        for group in currentGroupNames:
@@ -100,6 +103,9 @@ def list2MeshWeight(me, groupNames, vWeightList):
                                except:
                                        pass # vert group is not used anymore.
        
+       try:    me.activeGroup = act_group
+       except: pass
+       
        me.update()
 
 
@@ -134,6 +140,8 @@ def dict2MeshWeight(me, groupNames, vWeightDict):
        if len(vWeightDict) != len(me.verts):
                raise 'Error, Lists Differ in size, do not modify your mesh.verts before updating the weights'
        
+       act_group = me.activeGroup
+       
        # Clear the vert group.
        currentGroupNames= me.getVertGroupNames()
        for group in currentGroupNames:
@@ -159,6 +167,9 @@ def dict2MeshWeight(me, groupNames, vWeightDict):
                        except:
                                pass # vert group is not used anymore.
        
+       try:    me.activeGroup = act_group
+       except: pass
+       
        me.update()
 
 def dictWeightMerge(dict_weights):
@@ -290,6 +301,78 @@ def mesh2linkedFaces(me):
        return [fg for fg in face_groups if fg]
 
 
+def getFaceLoopEdges(faces, seams=[]):
+       '''
+       Takes me.faces or a list of faces and returns the edge loops
+       These edge loops are the edges that sit between quads, so they dont touch
+       1 quad, not not connected will make 2 edge loops, both only containing 2 edges.
+       
+       return a list of edge key lists
+       [ [(0,1), (4, 8), (3,8)], ...]
+       
+       optionaly, seams are edge keys that will be removed
+       '''
+       
+       OTHER_INDEX = 2,3,0,1 # opposite face index
+       
+       edges = {}
+       
+       for f in faces:
+               if len(f) == 4:
+                       edge_keys = f.edge_keys
+                       for i, edkey in enumerate(f.edge_keys):
+                               edges.setdefault(edkey, []).append(edge_keys[OTHER_INDEX[i]])
+       
+       for edkey in seams:
+               edges[edkey] = []
+       
+       # Collect edge loops here
+       edge_loops = [] 
+       
+       for edkey, ed_adj in edges.iteritems():
+               if 0 <len(ed_adj) < 3: # 1 or 2
+                       # Seek the first edge
+                       context_loop = [edkey, ed_adj[0]]
+                       edge_loops.append(context_loop)
+                       if len(ed_adj) == 2:
+                               other_dir = ed_adj[1]
+                       else:
+                               other_dir = None
+                       
+                       ed_adj[:] = []
+                       
+                       flipped = False
+                       
+                       while 1:
+                               # from knowing the last 2, look for th next.
+                               ed_adj = edges[context_loop[-1]]
+                               if len(ed_adj) != 2:
+                                       
+                                       if other_dir and flipped==False: # the original edge had 2 other edges
+                                               flipped = True # only flip the list once
+                                               context_loop.reverse()
+                                               ed_adj[:] = []
+                                               context_loop.append(other_dir) # save 1 lookiup
+                                               
+                                               ed_adj = edges[context_loop[-1]]
+                                               if len(ed_adj) != 2:
+                                                       ed_adj[:] = []
+                                                       break
+                                       else:
+                                               ed_adj[:] = []
+                                               break
+                               
+                               i = ed_adj.index(context_loop[-2])
+                               context_loop.append( ed_adj[ not  i] )
+                               
+                               # Dont look at this again
+                               ed_adj[:] = []
+
+       
+       return edge_loops
+       
+
+
 def getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=True, scn=None):
        '''
        ob - the object that you want to get the mesh from
@@ -302,18 +385,17 @@ def getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=Tru
        '''
        
        if not scn:
-               scn= Blender.Scene.GetCurrent()
+               scn= bpy.data.scenes.active
        if not container_mesh:
-               mesh = Blender.Mesh.New()       
+               mesh = bpy.data.meshes.new(ob.name)     
        else:
                mesh= container_mesh
                mesh.verts= None
        
-       
-       type = ob.getType()
+       ob_type = ob.type
        dataname = ob.getData(1)
        tempob= None
-       if apply_modifiers or type != 'Mesh':
+       if apply_modifiers or ob_type != 'Mesh':
                try:
                        mesh.getFromObject(ob)
                except:
@@ -324,16 +406,15 @@ def getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=Tru
                Dont apply modifiers, copy the mesh. 
                So we can transform the data. its easiest just to get a copy of the mesh. 
                '''
-               tempob= Blender.Object.New('Mesh')
-               tempob.shareFrom(ob)
-               scn.link(tempob)
+               tempob= scn.objects.new(ob.getData(mesh=1))
                mesh.getFromObject(tempob)
-               scn.unlink(tempob)
+               scn.objects.unlink(tempob)
        
-       if type == 'Mesh':
+       if ob_type == 'Mesh':
                if vgroups:
                        if tempob==None:
                                tempob= Blender.Object.New('Mesh')
+                       
                        tempob.link(mesh)
                        try:
                                # Copy the influences if possible.
@@ -346,7 +427,7 @@ def getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=Tru
        return mesh
 
 
-def faceRayIntersect(f, orig, dir):
+def faceRayIntersect(f, orig, rdir):
        '''
        Returns face, side
        Side is the side of a quad we intersect.
@@ -354,23 +435,23 @@ def faceRayIntersect(f, orig, dir):
                side 1 == 0,2,3
        '''
        f_v= f.v
-       isect= Blender.Mathutils.Intersect(f_v[0].co, f_v[1].co, f_v[2].co, dir, orig, 1) # 1==clip
+       isect= Blender.Mathutils.Intersect(f_v[0].co, f_v[1].co, f_v[2].co, rdir, orig, 1) # 1==clip
        
        if isect:
                return isect, 0
        
        if len(f_v)==4:
-               isect= Blender.Mathutils.Intersect(f_v[0].co, f_v[2].co, f_v[3].co, dir, orig, 1) # 1==clip
+               isect= Blender.Mathutils.Intersect(f_v[0].co, f_v[2].co, f_v[3].co, rdir, orig, 1) # 1==clip
                if isect:
                        return isect, 1
        return False, 0
 
 
-def pickMeshRayFace(me, orig, dir):
+def pickMeshRayFace(me, orig, rdir):
        best_dist= 1000000
        best_isect= best_side= best_face= None
        for f in me.faces:
-               isect, side= faceRayIntersect(f, orig, dir)
+               isect, side= faceRayIntersect(f, orig, rdir)
                if isect:
                        dist= (isect-orig).length
                        if dist<best_dist:
@@ -382,8 +463,8 @@ def pickMeshRayFace(me, orig, dir):
        return best_face, best_isect, best_side
 
 
-def pickMeshRayFaceWeight(me, orig, dir):
-       f, isect, side = pickMeshRayFace(me, orig, dir)
+def pickMeshRayFaceWeight(me, orig, rdir):
+       f, isect, side = pickMeshRayFace(me, orig, rdir)
        
        if f==None:
                return None, None, None, None, None
@@ -409,8 +490,8 @@ def pickMeshRayFaceWeight(me, orig, dir):
 
 
 
-def pickMeshGroupWeight(me, act_group, orig, dir):
-       f, side, w0, w1, w2= pickMeshRayFaceWeight(me, orig, dir)
+def pickMeshGroupWeight(me, act_group, orig, rdir):
+       f, side, w0, w1, w2= pickMeshRayFaceWeight(me, orig, rdir)
        
        if f==None:
                return None
@@ -428,9 +509,9 @@ def pickMeshGroupWeight(me, act_group, orig, dir):
        
        return w0*vws[0] + w1*vws[1]  + w2*vws[2]
 
-def pickMeshGroupVCol(me, orig, dir):
+def pickMeshGroupVCol(me, orig, rdir):
        Vector= Blender.Mathutils.Vector
-       f, side, w0, w1, w2= pickMeshRayFaceWeight(me, orig, dir)
+       f, side, w0, w1, w2= pickMeshRayFaceWeight(me, orig, rdir)
        
        if f==None:
                return None
@@ -704,10 +785,6 @@ def getUvPixelLoc(face, pxLoc, img_size = None, uvArea = None):
        return None
 
 
-type_tuple= type( (0,) )
-type_list= type( [] )
-
-
 # Used for debugging ngon
 """
 def draw_loops(loops):
@@ -787,7 +864,7 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                '''
                Normal single concave loop filling
                '''
-               if type(from_data) in (type_tuple, type_list):
+               if type(from_data) in (tuple, list):
                        verts= [Vector(from_data[i]) for ii, i in enumerate(indices)]
                else:
                        verts= [from_data.verts[i].co for ii, i in enumerate(indices)]
@@ -804,7 +881,7 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                This is used by lightwave LWO files a lot
                '''
                
-               if type(from_data) in (type_tuple, type_list):
+               if type(from_data) in (tuple, list):
                        verts= [vert_treplet(Vector(from_data[i]), ii) for ii, i in enumerate(indices)]
                else:
                        verts= [vert_treplet(from_data.verts[i].co, ii) for ii, i in enumerate(indices)]
@@ -976,8 +1053,7 @@ def meshCalcNormals(me, vertNormals=None):
        for f in me.faces:
                f_v = f.v
                for edkey in f.edge_keys:
-                       try:    edges[edkey].append(f.no)
-                       except: edges[edkey]= [f.no]
+                       edges.setdefault(edkey, []).append(f.no)
        
        # Weight the edge normals by total angle difference
        for fnos in edges.itervalues():
@@ -1075,6 +1151,33 @@ def pointInsideMesh(ob, pt):
        return len([None for f in me.faces if ptInFaceXYBounds(f, obSpacePt) if faceIntersect(f)]) % 2
 
 
+def faceAngles(f):
+       '''
+       Returns the angle between all corners in a tri or a quad
+       
+       '''
+       AngleBetweenVecs = Blender.Mathutils.AngleBetweenVecs
+       def Ang(a1,a2):
+               try:            return AngleBetweenVecs(a1,a2)
+               except:         return 180
+       
+       if len(f) == 3:
+               if type(f) in (tuple, list):    v1,v2,v3 = f
+               else:                                                   v1,v2,v3 = [v.co for v in f]
+               a1= Ang(v2-v1,v3-v1)
+               a2= Ang(v1-v2,v3-v2)
+               a3 = 180 - (a1+a2) # a3= Mathutils.AngleBetweenVecs(v2-v3,v1-v3)
+               return a1,a2,a3
+       
+       else:
+               if type(f) in (tuple, list):    v1,v2,v3,v4 = f
+               else:                                                   v1,v2,v3,v4 = [v.co for v in f]
+               a1= Ang(v2-v1,v4-v1)
+               a2= Ang(v1-v2,v3-v2)
+               a3= Ang(v2-v3,v4-v3)
+               a4= Ang(v3-v4,v1-v4)
+               return a1,a2,a3,a4
+
 # NMesh wrapper
 Vector= Blender.Mathutils.Vector
 class NMesh(object):