# How do I use python to independently animate the shape keys on several instances of a mesh?

I have created a mesh and it has a basis and relative shape key.

I can animate the Value of the relative shape key.

Unfortunately if I create a second instance of the mesh using alt-d or python's bpy.data.objects.new("second", bpy.data.meshes["space cow"]) the animation of the shape key affects both objects and they can not be separated.

It seems that obj.data.shape_keys is actually its own datablock from bpy.data.shape_keys and has its own animation_data separate from the object or the mesh.

Since I need to create and animate a herd of space cows using python I need code which can create independently animatable instances of this mesh.

My current instinct is that I'll have to duplicate the mesh data block and then duplicate the shape key. It makes me sad that I have to create so many copies of a mesh. I am hoping that someone else will know a better way.

• Given the suggested answer below and you stated [It makes me sad] ...What is the actual technical problem that remains ? Memory consumption? CPU Consumption? – atomicbezierslinger Mar 17 '16 at 17:52
• If I were rewriting the shape key system I would make it so that the animation of values is on the object datablock instead of the shape_key datablock. That way you could have a shared mesh and animate the instances independently (almost like how the pose on an object containing an armature is on the object datablock, not the armature datablock). – Mutant Bob Mar 18 '16 at 15:16
• Also, I'm remembering how my blender UI was unusably slow editing my insane clockwork animation until someone showed me how to use empties and dupligroups to replace my thousands of objects. youtube.com/watch?v=jO3gfGfxPkw . Although reusing mesh datablocks is a little different than empties with dupligroups. – Mutant Bob Mar 18 '16 at 15:19
• Plus, if I ever decided to make adjustments to the original mesh and or shape key, I would have to reclone all the derivative objects (which, given that I'm using python is less of a burden than if I had to do it manually). – Mutant Bob Mar 18 '16 at 15:20

## 2 Answers

The code I have developed since asking the question looks like this:

def cow_clone():
orig = bpy.data.meshes['grazer']
copy = orig.copy()
# that conveniently copied our shape keys too
return copy


and

def gobble(obj, frame5):

data_path = "key_blocks[\"close mouth\"].value"

shape_key = obj.data.shape_keys
key_block = shape_key.key_blocks["close mouth"]

key_block.value = 1
shape_key.keyframe_insert(data_path, frame=frame5-35)
shape_key.keyframe_insert(data_path, frame=frame5-10)
key_block.value = 0
shape_key.keyframe_insert(data_path, frame=frame5-30)
shape_key.keyframe_insert(data_path, frame=frame5-20)


So copy()ing the mesh datablock conveniently copies the shape_keys (which is good, because shape_keys seems to be read-only). Since I currently only have 10 of these meshes, it will not break the bank. Although when I get around to doing the 320 flowers that will probably bloat the file a bit. I am optimistic that it will not really stress blender at these numbers.

While this code works, I'm just dissatisfied with it; so I'm not going to accept this answer just in case someone smarter than me comes up with a better tactic, or maybe blender reworks its shape key system in a future release.

I suppose my primary complaint is that you can't make updates to a master mesh and have those updates affect all the derived instances.

User Preferences / Editing / Duplicate Data / Mesh [False] Setting

allows you to get a fresh, not shared copy, of the mesh data and the bundled shape keys. Perhaps this will work for part of your work flow. You may be copying manually and this setting can be enabled or disabled. Of course you can make the mesh single user in the mesh panel.

Suppose you moved every vertex in a shape key then we might suspect the shape key is the same size as the original mesh. Informal thinking with no software inspection.

Since the cow is a organic creature and could be served by an armature many readers would suspect you are using an armature. If not the suggestion is to use an armature. Perhaps armatures with a limited number of shape keys would be a good balance.

The count of duplication, whether you have 3 or 333 copies of the original would tell the readers more and allow them to suggest optimizations. Examples of your use of shape keys might allow other suggestions of Blender Modifiers.

• Unfortunately, the nature of the model (which resembles a low-poly whale shark crossed with a flat bacteria) means that animating the opening/closing of the mouth would probably require 12 bones (one for each vertex). – Mutant Bob Mar 18 '16 at 15:03