Advertisement
Guest User

Untitled

a guest
Jun 20th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1. bl_info = {
  2.     "name": "Shape key nonsense",
  3.     "location": "View3D > Add > Mesh > SKNonsense",
  4.     "description": "Shape key addon test",
  5.     "category": "Mesh"}
  6.  
  7.  
  8. import math
  9. import bpy
  10. from mathutils import Vector
  11. import mathutils
  12.  
  13.  
  14.  
  15.  
  16.  
  17. def get_selected_obj():
  18.     obj = bpy.context.active_object
  19.     for ob2 in bpy.context.selected_objects:
  20.         if ob2.type == 'MESH':
  21.             if ob2.name == obj.name:
  22.                 pass
  23.             else:
  24.               selectedobj = ob2
  25.     return selectedobj
  26.  
  27. def precision_drop(num,precision):
  28.     prec = 10**precision
  29.     return int(num*prec)/prec
  30.  
  31. def find_doubles_with_other(obj,selobj):
  32.     mesh = bpy.data.objects[obj.name].data.name
  33.     verts = bpy.data.meshes[mesh].vertices
  34.     selobverts = selobj.data.vertices
  35.     newdict = {}
  36.     for vert in verts:
  37.         try:
  38.            
  39.             newdict[tuple(vert.co)].append(vert)
  40.            
  41.            
  42.         except Exception as e:
  43.             newdict.update({tuple(vert.co):[vert]})
  44.     matchesdict = {}
  45.     nomatch = []
  46.     for vert in selobverts:
  47.         try:
  48.             m = newdict[tuple(vert.co)]
  49.             matchesdict.update({vert:m})
  50.         except:
  51.             nomatch.append(vert)
  52.     return (matchesdict,nomatch)
  53.  
  54.  
  55.  
  56. def copydoublesco(askey,nsk,matchdict):
  57.     for vert in matchdict:
  58.         obvert = matchdict[vert][0]
  59.         nsk.data[vert.index].co = askey.data[obvert.index].co
  60.  
  61.  
  62.  
  63. def round_pass(vertexlist,scale,precision,positionpassdict):
  64.     obj = bpy.context.active_object
  65.     askey = obj.active_shape_key
  66.     newmatch = []
  67.     keys = bpy.context.scene.objects.active.data.shape_keys.key_blocks
  68.     selobj = get_selected_obj()
  69.     nsk = selobj.data.shape_keys.key_blocks[-1]
  70.     for vert in vertexlist:
  71.         try:
  72.             coords = vert.co.copy() * scale
  73.             x = precision_drop(coords[0],precision)
  74.             y = precision_drop(coords[1],precision)
  75.             z = precision_drop(coords[2],precision)
  76.             q = positionpassdict[(x,y,z)]
  77.             #append the a list containing 0 =the vertices on the active object
  78.             #found to match the current vertex and 1= the current vertex
  79.             newmatch.append([q,vert])
  80.         except Exception as e:
  81.             pass
  82.  
  83.     for vertpair in newmatch:
  84.         sobvert = vertpair[1]    
  85.         obvert = vertpair[0][0]
  86.         sodelta = sobvert.co - obvert.co
  87.         #since the first index is a list of vertices, find which vertex is closest to the
  88.         #vertex
  89.         for verts in vertpair[0]:
  90.             sodelta2 = sobvert.co - verts.co
  91.             if sodelta2 < sodelta:
  92.                 obvert = verts
  93.                 sodelta = sodelta2
  94.         basis = keys[0]
  95.         delta = basis.data[obvert.index].co - askey.data[obvert.index].co
  96.         nsk.data[vertpair[1].index].co -= delta
  97.        
  98.  
  99.  
  100.  
  101. def generate_rounded_pos_dict(obj,precision,scale):
  102.     mesh = bpy.data.objects[obj.name].data.name
  103.     verts = bpy.data.meshes[mesh].vertices
  104.     newdict = {}
  105.     prec = 10 ** precision
  106.     for vert in verts:
  107.         coords = vert.co.copy() * scale
  108.         x = int(coords[0]*prec)/prec
  109.         y = int(coords[1]*prec)/prec
  110.         z = int(coords[2]*prec)/prec
  111.         try:
  112.             newdict[(x,y,z)].append(vert)
  113.         except Exception as e:
  114.             newdict.update({(x,y,z):[vert]})
  115.     return newdict
  116.  
  117.  
  118.        
  119. #transfers the active shape key of the active object
  120. #to a new key in the selected object
  121. def transfer_by_pos2(precision,scale):
  122.     obj = bpy.context.active_object
  123.     mesh = bpy.data.objects[obj.name].data.name
  124.     verts = bpy.data.meshes[mesh].vertices
  125.     askindex = obj.active_shape_key_index
  126.     askey = obj.active_shape_key
  127.     keys = bpy.context.scene.objects.active.data.shape_keys.key_blocks
  128.     basis = keys[0]
  129.  
  130.     selobj =  get_selected_obj()
  131.    
  132.  
  133.     selobmesh = bpy.data.objects[selobj.name].data.name
  134.     selobverts = bpy.data.meshes[selobmesh].vertices
  135.     selvgroups = selobj.vertex_groups
  136.     selobkeys = selobj.data.shape_keys.key_blocks
  137.     nsk = selobj.shape_key_add(name=askey.name,from_mix = False)
  138.  
  139.  
  140.     ddict = find_doubles_with_other(obj,selobj)
  141.     nomatch = ddict[1]
  142.     ddict = ddict[0]
  143.     copydoublesco(askey,nsk,ddict)
  144.    
  145.     #generates a dictionary which archives verts in active obj:matching vert in selected obj
  146.     pass2 = generate_rounded_pos_dict(obj,precision,scale)
  147.     round_pass(nomatch,scale,precision,pass2)
  148.  
  149.  
  150.  
  151.  
  152. class transfer_by_pos2_op(bpy.types.Operator):
  153.     """Transfers active shape key of active object to selected object by comparing vertex positions"""
  154.     bl_idname = "mesh.transfer_by_pos2"
  155.     bl_label = "Transfer shape key by vertex position2"
  156.     bl_space_type = 'VIEW_3D'
  157.     bl_region_type = 'TOOLS'
  158.     bl_category = "Shape keys"
  159.     bl_options = {'UNDO', 'REGISTER'}
  160.    
  161.     precision = bpy.props.FloatProperty(default =3)
  162.     scale =bpy.props.FloatProperty(default =.2)
  163.     def execute(self, context):
  164.        transfer_by_pos2(self.precision,self.scale)
  165.  
  166.        return {'FINISHED'}
  167.  
  168.  
  169. class ShapesPanel(bpy.types.Panel):
  170.     """Creates a Panel in the Object properties window"""
  171.     bl_label = "Shape key nonsense"    
  172.     bl_space_type = 'VIEW_3D'
  173.     bl_region_type = 'TOOLS'
  174.     bl_category = "Shape key nonsense"
  175.  
  176.     def draw(self, context):
  177.         layout = self.layout
  178.  
  179.         obj = context.object
  180.  
  181.  
  182.         row = layout.row()
  183.         row.operator("mesh.transfer_by_pos2")
  184.  
  185.  
  186. def register():
  187.     bpy.utils.register_class(transfer_by_pos2_op)
  188.     bpy.utils.register_class(ShapesPanel)
  189.  
  190.  
  191. def unregister():
  192.     bpy.utils.unregister_class(transfer_by_pos2_op)
  193.     bpy.utils.unregister_class(ShapesPanel)
  194.  
  195. if __name__ == "__main__":
  196.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement