I just made a simple demo.
A cube/a bezier line.
I want to add bezier as the target of Clamp To constrain by python.

enter image description here

bpy.context.object.constraints["Clamp To"].target = bpy.data.objects["bezier"]

It works well in Blender English version, but if I change the language to such as Chinese, after execute the first code line it adds a constrain but there is a little difference.

The default name change to "钳制到",as you can see in the second picture, it's the translation of "Clamp To".

enter image description here

Then the second code line raise error:

KeyError: 'bpy_prop_collection[key]: key "Clamp To" not found'

After change the code to

bpy.context.object.constraints["钳制到"].target = bpy.data.objects["bezier"]

It works.

So, the code is not work for different language version. I can work it out by replace the name with the index of list,but it's not beautiful.
Is there a way to get the constrain not by name but by type?

The code should work in any language. Or is there a better way to solve the problem?

  • $\begingroup$ instead of accessing it by name "bpy.context.object.constraints["Clamp To"]" you could access just the last constraint, wouldn't that work? $\endgroup$
    – Chris
    Commented Sep 17, 2022 at 11:36
  • $\begingroup$ @Chris It works,but not good,the same as the index of list.Assuming that I add 5 constrains and add targets.Several codes later,I want to change the target of one constrain,but I don't remember the index and it's not the last constrain I added,so I need to get it by type. $\endgroup$
    – NatureK
    Commented Sep 17, 2022 at 12:05

1 Answer 1


It's simple, rather than:

bpy.context.object.constraints["Clamp To"].target = bpy.data.objects["bezier"]


next(c for c in bpy.context.object.constraints if c.type=='CLAMP_TO').target = bpy.data.objects["bezier"]

For multiple constraints of the same type, instead of:

bpy.context.object.constraints["Clamp To.003"].target = bpy.data.objects["bezier"]


[c for c in bpy.context.object.constraints if c.type=='CLAMP_TO'][3].target = bpy.data.objects["bezier"]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .