Skip to content

Instantly share code, notes, and snippets.

@jlamarche
Created March 25, 2013 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlamarche/5239372 to your computer and use it in GitHub Desktop.
Save jlamarche/5239372 to your computer and use it in GitHub Desktop.
bl_info = {
"name": "Shape Key Extractor",
"author": "Jeff LaMarche",
"version": (1, 0),
"blender": (2, 6, 2),
"location": "View3D > Properties > Extract Shape Keys",
"description": "Export Shape Keys to Standalone Objects",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Mesh"}
"""
Version': '0.1'
This is a set of tools to help create game-ready characters from the HumanFactory blend file.
"""
import bpy
import mathutils
import os
import sys
import string
import math
import re
from bpy import *
from string import *
from struct import *
from math import *
from bpy.props import *
from mathutils import *
# The following function is adapted from
# Nick Keeline "Cloud Generator" addNewObject
# from object_cloud_gen.py (an addon that comes with the Blender 2.6 package)
#
def duplicateObject(scene, name, copyobj):
# Create new mesh
mesh = bpy.data.meshes.new(name)
# Create new object associated with the mesh
ob_new = bpy.data.objects.new(name, mesh)
# Copy data block from the old object into the new object
ob_new.data = copyobj.data.copy()
ob_new.scale = copyobj.scale
ob_new.location = copyobj.location
# Link new object to the given scene and select it
scene.objects.link(ob_new)
ob_new.select = True
return ob_new
class GenesisRigifyPanel(bpy.types.Panel):
bl_label = "Extract Shape Keys"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
layout = self.layout
ob = context.object
scn = context.scene
layout.label("Extract Shape Keys")
layout.operator("xs.extract_shape_keys", text="Extract Shape Keys")
def moveObjectToLayer(objectName, layerNum):
print("Moving object ", objectName, " to layer " + str(layerNum))
ob = bpy.data.objects[objectName]
ob.select = True
layers = 20*[False]
layers[layerNum]=True
bpy.ops.object.move_to_layer(layers=layers)
ob.select = False
class ExtractShapeKeys(bpy.types.Operator):
bl_idname = "xs.extract_shape_keys"
bl_label = "Extract shape keys into individual standalone objects"
def execute(self, context):
doExtract(context)
return {'FINISHED'}
def doExtract(context):
ob = context.object
bpy.ops.object.mode_set(mode='OBJECT')
scn = context.scene
if ob is None:
return {'FiNISHED'}
original_ob_name = ob.name
if ob.type == 'MESH' and ob.data.shape_keys:
keyBlocks = ob.data.shape_keys.key_blocks
print("number of blocks: " + str(len(keyBlocks)))
for block in keyBlocks:
if block.name == "Basis":
continue
bpy.ops.object.select_all(action='DESELECT')
bpy.context.scene.objects.active = ob
print(str(block))
print("Processing shape key: " + block.name)
ob_new = duplicateObject(bpy.context.scene, block.name, ob)
for copy_block in ob_new.data.shape_keys.key_blocks:
copy_shape_key_name = copy_block.name
if (copy_shape_key_name == block.name):
continue
copy_index = ob_new.data.shape_keys.key_blocks.keys().index(copy_shape_key_name)
ob_new.active_shape_key_index = copy_index
bpy.context.scene.objects.active = ob_new
bpy.ops.object.shape_key_remove()
bpy.context.scene.objects.active = ob
bpy.context.scene.objects.active = ob_new
copy_index = ob_new.data.shape_keys.key_blocks.keys().index(block.name)
bpy.ops.object.shape_key_remove()
bpy.context.scene.objects.active = ob
return {'FiNISHED'}
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment