added many checks to uvcopy so it dosnt raise errors.
authorCampbell Barton <ideasman42@gmail.com>
Tue, 12 Dec 2006 07:28:20 +0000 (07:28 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 12 Dec 2006 07:28:20 +0000 (07:28 +0000)
Minor changes to radiosity_export, updated to work with the new API (vertexColors MTFace)
obdatacopier - was getting the selection but not using it. made it ignore some read only properties.
BPyMesh - minor change: use face iterator rather then making a tuple()

release/scripts/bpymodules/BPyMesh.py
release/scripts/export_obj.py
release/scripts/mesh_cleanup.py
release/scripts/obdatacopier.py
release/scripts/radiosity_export.py
release/scripts/uvcopy.py

index 214fe4175eb389183ca367cf851510f9e41ee4c0..b97af70348321bcedb0b6e77a659e70dfe0925a2 100644 (file)
@@ -460,7 +460,7 @@ def edge_face_users(me):
        
        face_edges_dict= dict([(sorted_edge_indicies(ed), (ed.index, [])) for ed in me.edges])
        for f in me.faces:
        
        face_edges_dict= dict([(sorted_edge_indicies(ed), (ed.index, [])) for ed in me.edges])
        for f in me.faces:
-               fvi= [v.index for v in f.v]# face vert idx's
+               fvi= [v.index for v in f]# face vert idx's
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
@@ -490,7 +490,7 @@ def face_edges(me):
        
        face_edges_dict= dict([(sorted_edge_indicies(ed), []) for ed in me.edges])
        for fidx, f in enumerate(me.faces):
        
        face_edges_dict= dict([(sorted_edge_indicies(ed), []) for ed in me.edges])
        for fidx, f in enumerate(me.faces):
-               fvi= [v.index for v in f.v]# face vert idx's
+               fvi= [v.index for v in f]# face vert idx's
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
@@ -671,7 +671,7 @@ def edgeFaceUserCount(me, faces= None):
        edges_idx_dict= dict([(sorted_edge_indicies(ed), ed.index) for ed in me.edges])
 
        for f in faces:
        edges_idx_dict= dict([(sorted_edge_indicies(ed), ed.index) for ed in me.edges])
 
        for f in faces:
-               fvi= [v.index for v in f.v]# face vert idx's
+               fvi= [v.index for v in f]# face vert idx's
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
                for i in xrange(len(f)):
                        i1= fvi[i]
                        i2= fvi[i-1]
@@ -1000,8 +1000,9 @@ def meshCalcNormals(me, vertNormals=None):
                
        edges={}
        for f in me.faces:
                
        edges={}
        for f in me.faces:
+               f_v = f.v
                for i in xrange(len(f)):
                for i in xrange(len(f)):
-                       i1, i2= f.v[i].index, f.v[i-1].index
+                       i1, i2= f_v[i].index, f_v[i-1].index
                        if i1<i2:
                                i1,i2= i2,i1
                                
                        if i1<i2:
                                i1,i2= i2,i1
                                
@@ -1052,25 +1053,25 @@ def pointInsideMesh(ob, pt):
        Vector = Blender.Mathutils.Vector
        
        def ptInFaceXYBounds(f, pt):
        Vector = Blender.Mathutils.Vector
        
        def ptInFaceXYBounds(f, pt):
-                       
-               co= f.v[0].co
+               f_v = f.v
+               co= f_v[0].co
                xmax= xmin= co.x
                ymax= ymin= co.y
                
                xmax= xmin= co.x
                ymax= ymin= co.y
                
-               co= f.v[1].co
+               co= f_v[1].co
                xmax= max(xmax, co.x)
                xmin= min(xmin, co.x)
                ymax= max(ymax, co.y)
                ymin= min(ymin, co.y)
                
                xmax= max(xmax, co.x)
                xmin= min(xmin, co.x)
                ymax= max(ymax, co.y)
                ymin= min(ymin, co.y)
                
-               co= f.v[2].co
+               co= f_v[2].co
                xmax= max(xmax, co.x)
                xmin= min(xmin, co.x)
                ymax= max(ymax, co.y)
                ymin= min(ymin, co.y)
                
                xmax= max(xmax, co.x)
                xmin= min(xmin, co.x)
                ymax= max(ymax, co.y)
                ymin= min(ymin, co.y)
                
-               if len(f)==4: 
-                       co= f.v[3].co
+               if len(f_v)==4: 
+                       co= f_v[3].co
                        xmax= max(xmax, co.x)
                        xmin= min(xmin, co.x)
                        ymax= max(ymax, co.y)
                        xmax= max(xmax, co.x)
                        xmin= min(xmin, co.x)
                        ymax= max(ymax, co.y)
@@ -1088,9 +1089,10 @@ def pointInsideMesh(ob, pt):
                #return xmax, ymax, xmin, ymin
        
        def faceIntersect(f):
                #return xmax, ymax, xmin, ymin
        
        def faceIntersect(f):
-               isect = Intersect(f.v[0].co, f.v[1].co, f.v[2].co, ray, obSpacePt, 1) # Clipped.
+               f_v = f.v
+               isect = Intersect(f_v[0].co, f_v[1].co, f_v[2].co, ray, obSpacePt, 1) # Clipped.
                if not isect and len(f) == 4:
                if not isect and len(f) == 4:
-                       isect = Intersect(f.v[0].co, f.v[2].co, f.v[3].co, ray, obSpacePt, 1) # Clipped.
+                       isect = Intersect(f_v[0].co, f_v[2].co, f_v[3].co, ray, obSpacePt, 1) # Clipped.
                                
                if isect and isect.z > obSpacePt.z: # This is so the ray only counts if its above the point. 
                        return True
                                
                if isect and isect.z > obSpacePt.z: # This is so the ray only counts if its above the point. 
                        return True
index 9953ee2c8adc5d43f5320d3981b89783bae26765..119822227aa3414cf8ee15c44084de1b4d144159 100644 (file)
@@ -359,7 +359,7 @@ EXPORT_GROUP_BY_OB=False,  EXPORT_GROUP_BY_MAT=False):
                        if EXPORT_NORMALS:
                                for f in faces:
                                        if f.smooth:
                        if EXPORT_NORMALS:
                                for f in faces:
                                        if f.smooth:
-                                               for v in f.v:
+                                               for v in f:
                                                        noKey = veckey3d(v.no)
                                                        if not globalNormals.has_key( noKey ):
                                                                globalNormals[noKey] = totno
                                                        noKey = veckey3d(v.no)
                                                        if not globalNormals.has_key( noKey ):
                                                                globalNormals[noKey] = totno
index 63c28c611e3b7978f0ca97eae16c5f990749aa42..7abd64c255ea83ed799426b752b9aa0a351c70da 100644 (file)
@@ -48,7 +48,7 @@ meshWeight2Dict= BPyMesh.meshWeight2Dict
 def rem_free_verts(me):
        vert_users= [0] * len(me.verts)
        for f in me.faces:
 def rem_free_verts(me):
        vert_users= [0] * len(me.verts)
        for f in me.faces:
-               for v in f.v:
+               for v in f:
                        vert_users[v.index]+=1
        
        for e in me.edges:
                        vert_users[v.index]+=1
        
        for e in me.edges:
index 3dfdcf68daa157c1416fb89c34018a45ed562cca..561e40e15da7be4b3ba3778655fcb4173dfe85ac 100644 (file)
@@ -48,28 +48,25 @@ from Blender.Draw import *
 from Blender.BGL import *
 
 
 from Blender.BGL import *
 
 
-
-obsel = Object.GetSelected()
+scn= Blender.Scene.GetCurrent()
 
 type_func_method= type(dir)
 type_func= type(lambda:i)
 type_dict= type({})
 # type_list= type([])
 
 
 type_func_method= type(dir)
 type_func= type(lambda:i)
 type_dict= type({})
 # type_list= type([])
 
+IGNORE_VARS = 'users', 'fakeUser', 'edges', 'faces', 'verts',  'elements'
 
 def renew():
 
 def renew():
-       global obsel
        scn= Blender.Scene.GetCurrent()
        scn= Blender.Scene.GetCurrent()
-       act_ob= scn.getActiveObject()
+       act_ob= scn.objects.active
        if act_ob==None:
                return {}
 
        act_ob_type= act_ob.getType()
        act_ob_data= act_ob.getData(mesh=1)
        if act_ob==None:
                return {}
 
        act_ob_type= act_ob.getType()
        act_ob_data= act_ob.getData(mesh=1)
-       
-       obsel = Object.GetSelected()
 
 
-       if act_ob_data==None: # Surf
+       if act_ob_data==None: # Surf?
                return {}
        
        PARAM={}
                return {}
        
        PARAM={}
@@ -77,7 +74,7 @@ def renew():
        doc='doc' 
        
        for prop_name in dir(act_ob_data):
        doc='doc' 
        
        for prop_name in dir(act_ob_data):
-               if not prop_name.startswith('__'):
+               if not prop_name.startswith('__') and prop_name not in IGNORE_VARS:
                        # Get the type
                        try:            exec 'prop_type= type(act_ob_data.%s)' % prop_name
                        except:         prop_type= None
                        # Get the type
                        try:            exec 'prop_type= type(act_ob_data.%s)' % prop_name
                        except:         prop_type= None
@@ -130,7 +127,7 @@ def copy():
        
        print '\n\nStarting copy for object "%s"' % act_ob.name
        some_errors= False
        
        print '\n\nStarting copy for object "%s"' % act_ob.name
        some_errors= False
-       for ob in Blender.Object.GetSelected():
+       for ob in scn.objects.context:
                if ob != act_ob and ob.getType() == act_ob_type:
                        ob_data= None
                        for prop_name, value in PARAM.iteritems():
                if ob != act_ob and ob.getType() == act_ob_type:
                        ob_data= None
                        for prop_name, value in PARAM.iteritems():
@@ -170,7 +167,7 @@ def DRAW():
        global PARAM
        
        scn= Blender.Scene.GetCurrent()
        global PARAM
        
        scn= Blender.Scene.GetCurrent()
-       act_ob= scn.getActiveObject()
+       act_ob= scn.objects.active
        
        glColor3f(0.7, 0.7, 0.7)
        glClear(GL_COLOR_BUFFER_BIT)
        
        glColor3f(0.7, 0.7, 0.7)
        glClear(GL_COLOR_BUFFER_BIT)
@@ -187,7 +184,10 @@ def DRAW():
        Button("Renew",3,184,4,80,ligne)
 
        glRasterPos2f(20, ligne*2-8)
        Button("Renew",3,184,4,80,ligne)
 
        glRasterPos2f(20, ligne*2-8)
-       Text(act_ob.getType()+" DATA copier")
+       if act_ob:
+               Text(act_ob.getType()+" DATA copier")
+       else:
+               Text("Please select an object")
 
 
        max=size[3] / 22 -2
 
 
        max=size[3] / 22 -2
index 3d8bb57590e423f16678d070277091c3651f74b9..be41e5769d90879df7eed732b7bf7cafa2ce123a 100644 (file)
@@ -67,7 +67,7 @@ specular highlights to the vertex colors.
 #
 # ***** END GPL LICENCE BLOCK *****
 
 #
 # ***** END GPL LICENCE BLOCK *****
 
-import Blender, meshtools
+import Blender
 #import time
 import BPyMesh
 
 #import time
 import BPyMesh
 
@@ -87,19 +87,19 @@ def write(filename):
                filename += '.radio'
        
        scn= Blender.Scene.GetCurrent()
                filename += '.radio'
        
        scn= Blender.Scene.GetCurrent()
-       object= scn.getActiveObject()
-       if not object:
+       ob= scn.objects.active
+       if not ob:
                Blender.Draw.PupMenu('Error%t|Select 1 active object')
                return
                Blender.Draw.PupMenu('Error%t|Select 1 active object')
                return
-       objname= object.name
+       objname= ob.name
        
        file = open(filename, 'wb')
        
        file = open(filename, 'wb')
-       mesh = BPyMesh.getMeshFromObject(object, None, True, False, scn)
+       mesh = BPyMesh.getMeshFromObject(ob, None, True, False, scn)
        if not mesh:
                Blender.Draw.PupMenu('Error%t|Could not get mesh data from active object')
                return
                
        if not mesh:
                Blender.Draw.PupMenu('Error%t|Could not get mesh data from active object')
                return
                
-       mesh.transform(object.matrixWorld)
+       mesh.transform(ob.matrixWorld)
        
        
        
        
        
        
@@ -107,7 +107,8 @@ def write(filename):
        file = open(filename, "wb")
 
        if not mesh.faceUV:
        file = open(filename, "wb")
 
        if not mesh.faceUV:
-               mesh.faceUV= 1
+               mesh.vertexColors= 1
+               
                #message = 'Please assign vertex colors before exporting "%s"|object was not saved' % object.name
                #Blender.Draw.PupMenu("ERROR%t|"+message)
                #return
                #message = 'Please assign vertex colors before exporting "%s"|object was not saved' % object.name
                #Blender.Draw.PupMenu("ERROR%t|"+message)
                #return
@@ -133,7 +134,7 @@ def write(filename):
                
                file.write(struct.pack('<b', len(f) ))
                #for j in range(len(mesh.faces[i].v)):
                
                file.write(struct.pack('<b', len(f) ))
                #for j in range(len(mesh.faces[i].v)):
-               for v in f.v:
+               for v in f:
                        file.write(struct.pack('<h', v.index))
                
                f_col= f.col
                        file.write(struct.pack('<h', v.index))
                
                f_col= f.col
@@ -147,8 +148,9 @@ def write(filename):
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        end = Blender.sys.time()
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        end = Blender.sys.time()
-       message = 'Successfully exported "%s" in %.2f seconds' % (Blender.sys.basename(filename), end-start)
-       meshtools.print_boxed(message)
+       print '\nSuccessfully exported "%s" in %.2f seconds\n' % (Blender.sys.basename(filename), end-start)
+       
+       #meshtools.print_boxed(message)
 
 
 def main():
 
 
 def main():
index d6ef674dfa663f0f0f67b9b055d5e9f5bcff5131..7589b3e0fdbbcfb590d1729682ad56d9e94af55e 100644 (file)
@@ -1,6 +1,6 @@
 #!BPY
 """ Registration info for Blender menus: <- these words are ignored
 #!BPY
 """ Registration info for Blender menus: <- these words are ignored
-Name: 'UVcopy'
+Name: 'UV Copy from Active'
 Blender: 242
 Group: 'Object'
 Tip: 'Copy UV coords from a mesh to another that has same vertex indices'
 Blender: 242
 Group: 'Object'
 Tip: 'Copy UV coords from a mesh to another that has same vertex indices'
@@ -14,38 +14,81 @@ __version__ = "0.2 01/2006"
 
 __bpydoc__ = """\
 This script copies UV coords from a mesh to another (version of the same mesh).
 
 __bpydoc__ = """\
 This script copies UV coords from a mesh to another (version of the same mesh).
+All target meshes must have the same number of faces at the active
 """
 
 import Blender
 """
 
 import Blender
-scene = Blender.Scene.GetCurrent()
 
 
-unwrapped = scene.getActiveObject()
-targets = Blender.Object.GetSelected()
-if unwrapped: 
-    source = unwrapped.data
-else: 
-    raise RuntimeError, "No active object to copy UVs from." 
-if targets:
-    try:
-        targets.remove(unwrapped)
-    except ValueError:
-        print "ob for sourcedata was not in targets, so did not need to remove", unwrapped, targets
-    #try:
-    #    target = targets[0].data
-    #except IndexError:
-    if not targets:
-        raise RuntimeError, "no selected object other than the source, hence no target defined."
-else: 
-    raise RuntimeError, "No selected object(s) to copy UVs to."
+def face_key(me):
+       '''
+       Returns the face lengths for this mesh
+       only copy between meshes that have matching face_key's
+       '''
+       return tuple([len(f) for f in me.faces])
 
 
-if source and targets:
-    for target in targets:
-        target = target.data
-        for i in range(len(target.faces)): 
-            target.faces[i].uv = source.faces[i].uv
-            #print "copied to target:", target.name, target.data.faces[i].uv, ", source being:", source.faces[i].uv
-        target.update() 
-        print "Copied UV from object " + unwrapped.name + " to object(s) " + str([target.name for target in targets]) + "."
+def main():
+       scene = Blender.Scene.GetCurrent()
+
+       source_ob = scene.objects.active
+       
+       if not source_ob or source_ob.type != 'Mesh':
+               Blender.Draw.PupMenu("Error%t|No active object to copy UVs from.")
+               return
+       
+       source_me = source_ob.getData(mesh=1)
+       
+       target_mes = [ ob.getData(mesh=1) for ob in scene.objects.context if ob != source_ob if ob.type == 'Mesh']
+       
+       # remove double meshes
+       target_mes = dict([(me.name, me) for me in target_mes])
+       target_mes = target_mes.values()
+       
+       if not target_mes:
+               Blender.Draw.PupMenu("Error%t|No selected object(s) to copy UVs to.")
+               return
+       
+       source_me = source_ob.getData(mesh=1)
+       
+       if not source_me.faceUV:
+               Blender.Draw.PupMenu("Error%t|Active mesh has no UVs.")
+               return
+       
+       if not target_mes:
+               Blender.Draw.PupMenu("Error%t|no selected object other than the source, hence no target defined.")
+               return
+       error = False
+       
+       source_face_key = face_key(source_me)
+       source_faces = source_me.faces
+       
+       fail_count = 0
+       
+       for target in target_mes:
+               if len(source_me.faces) != len(target.faces):
+                       print '\ttarget "%s" facelen does not match "%s".' % (target.name, source_me.name)
+                       error = True
+                       fail_count +=1
+               else:
+                       # slow but worth comparing
+                       if source_face_key != face_key(target):
+                               print '\t\terror, face len dosnt match for ob "%s" next mesh...' % target.name
+                               error = True
+                               fail_count +=1
+                       else:
+                               target_faces = target.faces
+                               
+                               # Add uvs if there not there
+                               target.faceUV = True
+                               
+                               for i, f_source in enumerate(target.faces): 
+                                       target_faces[i].uv = f_source.uv
+       
+       msg = 'Copied UVs to %d of %d mesh(s)' % (len(target_mes)-fail_count, len(target_mes))
+       
+       if error:
+               msg += "|Could not copy some UV's, see console."
+       
+       Blender.Draw.PupMenu('UV Copy Finished%t|' + msg)
+
+if __name__ == '__main__':
+       main()
\ No newline at end of file