-/* context: active object in posemode, active channel, optional selected channel */
-void add_constraint (Scene *scene, View3D *v3d, short only_IK)
-{
- Object *ob= OBACT, *obsel=NULL;
- bPoseChannel *pchanact=NULL, *pchansel=NULL;
- bConstraint *con=NULL;
- Base *base;
- short nr;
-
- /* paranoia checks */
- if ((ob==NULL) || (ob==scene->obedit))
- return;
-
- if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
- bArmature *arm= ob->data;
-
- /* find active channel */
- pchanact= get_active_posechannel(ob);
- if (pchanact==NULL)
- return;
-
- /* find selected bone */
- for (pchansel=ob->pose->chanbase.first; pchansel; pchansel=pchansel->next) {
- if (pchansel != pchanact) {
- if (pchansel->bone->flag & BONE_SELECTED) {
- if (pchansel->bone->layer & arm->layer)
- break;
- }
- }
- }
- }
-
- /* find selected object */
- for (base= FIRSTBASE; base; base= base->next) {
- if ((TESTBASE(v3d, base)) && (base->object!=ob))
- obsel= base->object;
- }
-
- /* the only_IK caller has checked for posemode! */
- if (only_IK) {
- for (con= pchanact->constraints.first; con; con= con->next) {
- if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
- }
- if (con) {
- error("Pose Channel already has IK");
- return;
- }
-
- if (pchansel)
- nr= pupmenu("Add IK Constraint%t|To Active Bone%x10");
- else if (obsel)
- nr= pupmenu("Add IK Constraint%t|To Active Object%x10");
- else
- nr= pupmenu("Add IK Constraint%t|To New Empty Object%x10|Without Target%x11");
- }
- else {
- if (pchanact) {
- if (pchansel)
- nr= pupmenu("Add Constraint to Active Bone%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
- else if ((obsel) && (obsel->type==OB_CURVE))
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|Stretch To%x7|%l|Action%x16|Script%x18");
- else if ((obsel) && (obsel->type==OB_MESH))
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Shrinkwrap%x22|Stretch To%x7|%l|Action%x16|Script%x18");
- else if (obsel)
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
- else
- nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
- }
- else {
- if ((obsel) && (obsel->type==OB_CURVE))
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|%l|Action%x16|Script%x18");
- else if ((obsel) && (obsel->type==OB_MESH))
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Shrinkwrap%x22|%l|Action%x16|Script%x18");
- else if (obsel)
- nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
- else
- nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
- }
- }
-
- if (nr < 1) return;
-
- /* handle IK separate */
- if (nr==10 || nr==11) {
- /* ik - prevent weird chains... */
- if (pchansel) {
- bPoseChannel *pchan= pchanact;
- while (pchan) {
- if (pchan==pchansel) break;
- pchan= pchan->parent;
- }
- if (pchan) {
- error("IK root cannot be linked to IK tip");
- return;
- }
-
- pchan= pchansel;
- while (pchan) {
- if (pchan==pchanact) break;
- pchan= pchan->parent;
- }
- if (pchan) {
- error("IK tip cannot be linked to IK root");
- return;
- }
- }
-
- con = add_new_constraint(CONSTRAINT_TYPE_KINEMATIC);
- BLI_addtail(&pchanact->constraints, con);
- unique_constraint_name(con, &pchanact->constraints);
- pchanact->constflag |= PCHAN_HAS_IK; /* for draw, but also for detecting while pose solving */
- if (nr==11)
- pchanact->constflag |= PCHAN_HAS_TARGET;
- if (proxylocked_constraints_owner(ob, pchanact))
- con->flag |= CONSTRAINT_PROXY_LOCAL;
- }
- else {
- /* normal constraints - add data */
- if (nr==1) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIKE);
- else if (nr==2) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIKE);
- else if (nr==3) con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
- else if (nr==4) con = add_new_constraint(CONSTRAINT_TYPE_MINMAX);
- else if (nr==5) con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
- else if (nr==6) {
- Curve *cu= obsel->data;
- cu->flag |= CU_PATH;
- con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
- }
- else if (nr==7) con = add_new_constraint(CONSTRAINT_TYPE_STRETCHTO);
- else if (nr==8) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIKE);
- else if (nr==13) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIMIT);
- else if (nr==14) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIMIT);
- else if (nr==15) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIMIT);
- else if (nr==16) {
- /* TODO: add a popup-menu to display list of available actions to use (like for pyconstraints) */
- con = add_new_constraint(CONSTRAINT_TYPE_ACTION);
- }
- else if (nr==17) {
- Curve *cu= obsel->data;
- cu->flag |= CU_PATH;
- con = add_new_constraint(CONSTRAINT_TYPE_CLAMPTO);
- }
- else if (nr==18) {
- char *menustr;
- int scriptint= 0;
-#ifndef DISABLE_PYTHON
- /* popup a list of usable scripts */
- menustr = buildmenu_pyconstraints(NULL, &scriptint);
- scriptint = pupmenu(menustr);
- MEM_freeN(menustr);
-
- /* only add constraint if a script was chosen */
- if (scriptint) {
- /* add constraint */
- con = add_new_constraint(CONSTRAINT_TYPE_PYTHON);
- validate_pyconstraint_cb(con->data, &scriptint);
-
- /* make sure target allowance is set correctly */
- BPY_pyconstraint_update(ob, con);
- }
-#endif
- }
- else if (nr==19) {
- con = add_new_constraint(CONSTRAINT_TYPE_CHILDOF);
-
- /* if this constraint is being added to a posechannel, make sure
- * the constraint gets evaluated in pose-space
- */
- if (pchanact) {
- con->ownspace = CONSTRAINT_SPACE_POSE;
- con->flag |= CONSTRAINT_SPACEONCE;
- }
- }
- else if (nr==20) con = add_new_constraint(CONSTRAINT_TYPE_TRANSFORM);
- else if (nr==21) con = add_new_constraint(CONSTRAINT_TYPE_DISTLIMIT);
- else if (nr==22) con = add_new_constraint(CONSTRAINT_TYPE_SHRINKWRAP);
-
- if (con==NULL) return; /* paranoia */
-
- if (pchanact) {
- BLI_addtail(&pchanact->constraints, con);
- unique_constraint_name(con, &pchanact->constraints);
- pchanact->constflag |= PCHAN_HAS_CONST; /* for draw */
- if (proxylocked_constraints_owner(ob, pchanact))
- con->flag |= CONSTRAINT_PROXY_LOCAL;
- }
- else {
- BLI_addtail(&ob->constraints, con);
- unique_constraint_name(con, &ob->constraints);
- if (proxylocked_constraints_owner(ob, NULL))
- con->flag |= CONSTRAINT_PROXY_LOCAL;
- }
- }
-
- /* set the target */
- if (pchansel) {
- set_constraint_nth_target(con, ob, pchansel->name, 0);
- }
- else if (obsel) {
- set_constraint_nth_target(con, obsel, "", 0);
- }
- else if (ELEM4(nr, 11, 13, 14, 15)==0) { /* add new empty as target */
- Base *base= BASACT, *newbase;
- Object *obt;
-
- obt= add_object(scene, OB_EMPTY);
- /* set layers OK */
- newbase= BASACT;
- newbase->lay= base->lay;
- obt->lay= newbase->lay;
-
- /* transform cent to global coords for loc */
- if (pchanact) {
- if (only_IK)
- VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
- else
- VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_head);
- }
- else
- VECCOPY(obt->loc, ob->obmat[3]);
-
- set_constraint_nth_target(con, obt, "", 0);
-
- /* restore, add_object sets active */
- BASACT= base;
- base->flag |= SELECT;
- }
-
- /* active flag */
- con->flag |= CONSTRAINT_ACTIVE;
- for (con= con->prev; con; con= con->prev)
- con->flag &= ~CONSTRAINT_ACTIVE;
-
- DAG_scene_sort(scene); // sort order of objects
-
- if (pchanact) {
- ob->pose->flag |= POSE_RECALC; // sort pose channels
- DAG_object_flush_update(scene, ob, OB_RECALC_DATA); // and all its relations
- }
- else
- DAG_object_flush_update(scene, ob, OB_RECALC_OB); // and all its relations
-
- if (only_IK)
- BIF_undo_push("Add IK Constraint");
- else
- BIF_undo_push("Add Constraint");
-
-}
-