Skip to content

Instantly share code, notes, and snippets.

@himika
Created July 3, 2012 03:49
Show Gist options
  • Save himika/3037531 to your computer and use it in GitHub Desktop.
Save himika/3037531 to your computer and use it in GitHub Desktop.
(Blender 2.49b用) シェイプキーを別のオブジェクトに移すプラグイン
#!BPY
"""
Name: 'Transfer Shape Keys...'
Blender: 249
Group: 'Object'
Tooltip: 'Copy another selected object's shapes to this one by applying the relative offsets'
"""
__author__ = 'himika'
__url__ = ("blender.org",)
__version__ = "0.3"
__bpydoc__ = """\
Transfer Shape Keys
Copy another selected object's shapes to this one by applying the relative offsets
"""
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2012 himika
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
import bpy
import Blender
from Blender import Scene, Object, Window
import BPyMesh
from itertools import izip
def showMenu():
global SELECTED_SHAPE_ONLY
SELECTED_SHAPE_ONLY = Blender.Draw.Create(0)
pup_block = [\
('Selected Only', SELECTED_SHAPE_ONLY, 'transfer selected shape only')\
]
if not Blender.Draw.PupBlock('Options...', pup_block):
return
Blender.Window.WaitCursor(1)
SELECTED_SHAPE_ONLY = SELECTED_SHAPE_ONLY.val
def findShape(me, name):
shapeKeys = me.key.blocks
for i, n in enumerate(shapeKeys):
if shapeKeys[i].name == name:
return i
me.insertKey(1, 'relative')
me.key.blocks[-1].name = name
return -1
def transferOne(me_to, me_from, i):
shapeKeys_to = me_to.key.blocks
shapeKeys_from = me_from.key.blocks
verts_to_basis = shapeKeys_to[0].getData()
verts_from_basis = shapeKeys_from[0].getData()
name = shapeKeys_from[i].name
j = findShape(me_to, name)
verts_to = me_to.key.blocks[j].getData()
verts_from = me_from.key.blocks[i].getData()
if j == -1:
print(' add: '+name)
else:
print(' update: '+name)
for tv, tvb, fv, fvb in izip(verts_to, verts_to_basis, verts_from, verts_from_basis):
tv[0] = tvb[0] + fv[0] - fvb[0]
tv[1] = tvb[1] + fv[1] - fvb[1]
tv[2] = tvb[2] + fv[2] - fvb[2]
def transfer_shapes(ob_to, ob_from):
me_to = ob_to.getData(mesh=True)
me_from = ob_from.getData(mesh=True)
if not (me_from.key and len(me_from.key.blocks) > 1):
return
if not me_to.key:
me_to.insertKey(1, 'relative')
shapeKeys_from = me_from.key.blocks
verts_from_basis = shapeKeys_from[0].getData()
shapeKeys_to = me_to.key.blocks
verts_to_basis = shapeKeys_to[0].getData()
if SELECTED_SHAPE_ONLY:
i = ob_from.activeShape - 1
transferOne(me_to, me_from, i)
else:
for i in range(1, len(shapeKeys_from)):
transferOne(me_to, me_from, i)
def main():
print '\nStarting Transfer Shape Keys...'
scn= Scene.GetCurrent()
contextSel= Blender.Object.GetSelected()
act_ob= scn.objects.active
if not act_ob or act_ob.type != 'Mesh':
Blender.Draw.PupMenu('Error: no active mesh object, aborting.')
return
if not contextSel:
Blender.Draw.PupMenu('Error: 2 or more mesh objects need to be selected, aborting.')
return
for ob in contextSel:
if (act_ob.getName() != ob.getName()) and (ob.type == 'Mesh'):
act_me = act_ob.getData(mesh=True)
me = ob.getData(mesh=True)
m, n = (len(act_me.verts), len(me.verts))
if m != n:
Blender.Draw.PupMenu('Error: do not match num vertices. |\t%s = %d |\t%s = %d' % (act_ob.getName(), m, ob.getName(), n))
return
showMenu()
for ob in contextSel:
if (act_ob.getName() != ob.getName()) and (ob.type == 'Mesh'):
print(act_ob.getName()+' ===> '+ob.getName())
transfer_shapes(act_ob, ob)
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment