Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bl_info = {
- "name": "Shape key nonsense",
- "location": "View3D > Add > Mesh > SKNonsense",
- "description": "Shape key addon test",
- "category": "Mesh"}
- import math
- import bpy
- from mathutils import Vector
- import mathutils
- def get_selected_obj():
- obj = bpy.context.active_object
- for ob2 in bpy.context.selected_objects:
- if ob2.type == 'MESH':
- if ob2.name == obj.name:
- pass
- else:
- selectedobj = ob2
- return selectedobj
- def precision_drop(num,precision):
- prec = 10**precision
- return int(num*prec)/prec
- def find_doubles_with_other(obj,selobj):
- mesh = bpy.data.objects[obj.name].data.name
- verts = bpy.data.meshes[mesh].vertices
- selobverts = selobj.data.vertices
- newdict = {}
- for vert in verts:
- try:
- newdict[tuple(vert.co)].append(vert)
- except Exception as e:
- newdict.update({tuple(vert.co):[vert]})
- matchesdict = {}
- nomatch = []
- for vert in selobverts:
- try:
- m = newdict[tuple(vert.co)]
- matchesdict.update({vert:m})
- except:
- nomatch.append(vert)
- return (matchesdict,nomatch)
- def copydoublesco(askey,nsk,matchdict):
- for vert in matchdict:
- obvert = matchdict[vert][0]
- nsk.data[vert.index].co = askey.data[obvert.index].co
- def round_pass(vertexlist,scale,precision,positionpassdict):
- obj = bpy.context.active_object
- askey = obj.active_shape_key
- newmatch = []
- keys = bpy.context.scene.objects.active.data.shape_keys.key_blocks
- selobj = get_selected_obj()
- nsk = selobj.data.shape_keys.key_blocks[-1]
- for vert in vertexlist:
- try:
- coords = vert.co.copy() * scale
- x = precision_drop(coords[0],precision)
- y = precision_drop(coords[1],precision)
- z = precision_drop(coords[2],precision)
- q = positionpassdict[(x,y,z)]
- #append the a list containing 0 =the vertices on the active object
- #found to match the current vertex and 1= the current vertex
- newmatch.append([q,vert])
- except Exception as e:
- pass
- for vertpair in newmatch:
- sobvert = vertpair[1]
- obvert = vertpair[0][0]
- sodelta = sobvert.co - obvert.co
- #since the first index is a list of vertices, find which vertex is closest to the
- #vertex
- for verts in vertpair[0]:
- sodelta2 = sobvert.co - verts.co
- if sodelta2 < sodelta:
- obvert = verts
- sodelta = sodelta2
- basis = keys[0]
- delta = basis.data[obvert.index].co - askey.data[obvert.index].co
- nsk.data[vertpair[1].index].co -= delta
- def generate_rounded_pos_dict(obj,precision,scale):
- mesh = bpy.data.objects[obj.name].data.name
- verts = bpy.data.meshes[mesh].vertices
- newdict = {}
- prec = 10 ** precision
- for vert in verts:
- coords = vert.co.copy() * scale
- x = int(coords[0]*prec)/prec
- y = int(coords[1]*prec)/prec
- z = int(coords[2]*prec)/prec
- try:
- newdict[(x,y,z)].append(vert)
- except Exception as e:
- newdict.update({(x,y,z):[vert]})
- return newdict
- #transfers the active shape key of the active object
- #to a new key in the selected object
- def transfer_by_pos2(precision,scale):
- obj = bpy.context.active_object
- mesh = bpy.data.objects[obj.name].data.name
- verts = bpy.data.meshes[mesh].vertices
- askindex = obj.active_shape_key_index
- askey = obj.active_shape_key
- keys = bpy.context.scene.objects.active.data.shape_keys.key_blocks
- basis = keys[0]
- selobj = get_selected_obj()
- selobmesh = bpy.data.objects[selobj.name].data.name
- selobverts = bpy.data.meshes[selobmesh].vertices
- selvgroups = selobj.vertex_groups
- selobkeys = selobj.data.shape_keys.key_blocks
- nsk = selobj.shape_key_add(name=askey.name,from_mix = False)
- ddict = find_doubles_with_other(obj,selobj)
- nomatch = ddict[1]
- ddict = ddict[0]
- copydoublesco(askey,nsk,ddict)
- #generates a dictionary which archives verts in active obj:matching vert in selected obj
- pass2 = generate_rounded_pos_dict(obj,precision,scale)
- round_pass(nomatch,scale,precision,pass2)
- class transfer_by_pos2_op(bpy.types.Operator):
- """Transfers active shape key of active object to selected object by comparing vertex positions"""
- bl_idname = "mesh.transfer_by_pos2"
- bl_label = "Transfer shape key by vertex position2"
- bl_space_type = 'VIEW_3D'
- bl_region_type = 'TOOLS'
- bl_category = "Shape keys"
- bl_options = {'UNDO', 'REGISTER'}
- precision = bpy.props.FloatProperty(default =3)
- scale =bpy.props.FloatProperty(default =.2)
- def execute(self, context):
- transfer_by_pos2(self.precision,self.scale)
- return {'FINISHED'}
- class ShapesPanel(bpy.types.Panel):
- """Creates a Panel in the Object properties window"""
- bl_label = "Shape key nonsense"
- bl_space_type = 'VIEW_3D'
- bl_region_type = 'TOOLS'
- bl_category = "Shape key nonsense"
- def draw(self, context):
- layout = self.layout
- obj = context.object
- row = layout.row()
- row.operator("mesh.transfer_by_pos2")
- def register():
- bpy.utils.register_class(transfer_by_pos2_op)
- bpy.utils.register_class(ShapesPanel)
- def unregister():
- bpy.utils.unregister_class(transfer_by_pos2_op)
- bpy.utils.unregister_class(ShapesPanel)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement