How to add rotation with constraints


I want to add the cursor’s rotation to an object but also consider constraints. This code gives me what I want when the object has no rotation constraints:

float currot[3][3], obmat[3][3];
BKE_scene_cursor_rot_to_mat3(&scene->cursor, currot);
mul_m3_m4m3(obmat, ob->obmat, currot);
BKE_object_mat3_to_rot(ob, obmat, true);

If I was given an Euler rotation, then I would do something like:

if ((ob->protectflag & OB_LOCK_ROTX) == 0) {
    ob->rot[0] += scene->cursor.rotation_euler[0];

But then I would have to consider every axis for every rotation mode. So my question is is there a generic way to add rotation to an object that considers rotation constraints regardless of rotation mode?

I found some useful functions in transform.c :

  • protectedQuaternionBits()
  • protectedAxisAngleBits()
  • protectedRotateBits()

Will probably have to move them somewhere else since I need them in view3d_snap.c