Getting the Active Shape Key

Hi again :slight_smile:

I have a set of shape keys on an object. I’m creating a new one in my script from one of them via “bpy.ops.object.shape_key_add(from_mix=True)” (Shape Key from Mix) Now, I need to get to the newly created shape key, define it and do something with it, but have absolutely no idea how to get to it.

Any help would be wildly appreciated. Thank you in advance.

Cheers;

AJ

I would suggest not using bpy.ops. Instead use:


obj = bpy.data.objects['myObject'] #get your object
activeKey = obj.shape_key_add('myKey', True) #add a new shapekey, name it, use from_mix, and store it in a variable 

… then you can access the newly created shape key’s properties via the variable.

In Python, the last item in a list has the index -1, and in Blender an added shape key will be the active shape key immediately after it is added.
bpy.context.active_object.data.shape_keys.key_blocks[-1]
or
bpy.context.active_object.active_shape_key

1 Like

A couple of examples for you. This line of code will set the y co-ordinate of the first vertex of the last shape key of your active object to 3:

bpy.context.active_object.data.shape_keys.key_blocks[-1].data[0].co.y = 3

This line of code will set the x co-ordinate of the first vertex of the active shape key of your active object to 5:

bpy.context.active_object.active_shape_key.data[0].co.x = 5

In Python, the first item of a list has the index 0, so data[0] refers to the first vertex of your mesh object.