XYZ Shapes


I am more of an animator/modeller and starting to explore the Python API in Blender.
I have been reading Jason Osipa’s “Stop Staring - Facial Modeliing and Animation Done Right”. Along the way, I am adapting some of the techniques done with Maya (that’s what the author uses) into my Blender workflow. Along the way, I also manage to adapt some Maya Mel scripts by using modifiers, drivers and seldom some fairly simple scripts. However I got stuck trying to translate one Mel Script.

The script basically picks the selected shape key and creates three new partial copies of it. In the first copy, we have the shape key animation along the X axis. On the second copy we have shape key animation along the Y axis. The third one is the animation along the Z action. I hope I was clear on this.

So far I made this:

import bpy

scene = bpy.context.scene
obj =
shape_index = bpy.context.object.active_shape_key_index

#do something here

bpy.context.object.active_shape_key_index = bpy.context.object.active_shape_key_index - 2

#do something here

bpy.context.object.active_shape_key_index = bpy.context.object.active_shape_key_index + 1

#do something here

The idea is to create the copies, and delete the axes that will not be used in each one of them.

I also looked into the bmesh module API to find some command that could delete the animation in each axis, like we can do in the graph editor, but I did not find anything. Maybe I am using a wrong approach to this problem. :mad:

As a sidenote, I am also planning to give a name to each copy, for example “_X”, “_Y”, “_Z”, but still have not found a way to do that.

I tried to assign a var to bpy.context.object.active_shape_key_index object , so I don’t have to write all of it while I am scripting, but somehow the script ceases to select the shape keys if I do that. :mad:

My plan is to turn this into an addon later, right now I prefer to focus on funtionality.

Can anyone help me with this one?

Thanks! :o

Well, I have made some advancements

Here is the code:

import bpy

context = bpy.context
scene = context.scene
obj = context.object
shape_name =
shape_keys =
obj.shape_key_add(name=str(shape_name) + "_X", from_mix=True)
obj.shape_key_add(name=str(shape_name) + "_Y", from_mix=True)
obj.shape_key_add(name=str(shape_name) + "_Z", from_mix=True)
#do something here
obj.active_shape_key_index = obj.active_shape_key_index - 2

#do something here
obj.active_shape_key_index = obj.active_shape_key_index + 1

#do something here

Now the script creates 3 new shape keys with an “_X”, “_Y” and “_Z” postfixes.
To have the last shaped_key selected, I created another shape key and then deleted it immediately. Yes, it is an ugly hack. If anyone knows a more elegant solution, I will be thankfull for the input.

I still don’t have a clue how to access the vertex data in a shape key, so I can separate the vertices movement into these three new shape keys.

Any ideas? :slight_smile:

So I managed to get it with the precious help from sambler from Stackexchange:

bl_info = {
    "name": "Split Shape Key",
    "author": "Eduardo Teixeira, sambler",  
    "version": (1, 0),
    "blender": (2, 73, 0),
    "location": "Object > Animation > Split Shape Key",
    "description": "Takes a shape key and splits its vertices translation into three new shape keys, one per axis, preserving the original",
    "warning": "",
    #"tracker_url": "",
    "category": "Animation",

import bpy

class SplitShapeKey(bpy.types.Operator):
    """Takes a shape key and splits its vertices translation"""\
    """ into three new shape keys, one per axis, preserving the original """
    bl_idname = "anim.split_shape_key"
    bl_label = "Split Shape Key"
    bl_options = {'REGISTER', 'UNDO'}
    def poll(cls, context):
        return context.active_object.active_shape_key is not None
    def execute(self, context):
        context = bpy.context
        scene = context.scene
        obj = context.object
        shape_name =

        ## enable shapekey before copying
        skey_value = obj.active_shape_key.value
        obj.active_shape_key.value = obj.active_shape_key.slider_max

        obj.shape_key_add(name=str(shape_name) + "_X", from_mix=True)
        xshape_idx = len(
        obj.shape_key_add(name=str(shape_name) + "_Y", from_mix=True)
        yshape_idx = len(
        obj.shape_key_add(name=str(shape_name) + "_Z", from_mix=True)
        zshape_idx = len(

        ## reset shapekey
        obj.active_shape_key.value = skey_value

        for vert in #Isolate the translation on the X axis
  [xshape_idx].data[vert.index].co.y =['Basis'].data[vert.index].co.y
  [xshape_idx].data[vert.index].co.z =['Basis'].data[vert.index].co.z
        for vert in #Isolate the translation on the Y axis
  [yshape_idx].data[vert.index].co.x =['Basis'].data[vert.index].co.x
  [yshape_idx].data[vert.index].co.z =['Basis'].data[vert.index].co.z
        for vert in #Isolate the translation on the Z axis
  [zshape_idx].data[vert.index].co.x =['Basis'].data[vert.index].co.x
  [zshape_idx].data[vert.index].co.y =['Basis'].data[vert.index].co.y

class SplitShapePanel(bpy.types.Panel):
    """Creates a Panel in the Tools Window > Animation Tab"""
    bl_space_type = 'VIEW_3D'    
    bl_region_type = 'TOOLS'
    bl_category = 'Animation'
    bl_label = "Split Shape Key"
    bl_context = "objectmode"
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        layout = self.layout
        row = layout.row()
def menu_func(self, context):
def register():
def unregister():

if __name__ == "__main__":

Hope it is useful to you all.

1 Like