Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_constraint.c « object « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec3567454dfb393031e5e86a89d1d456f830386e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Joshua Leung, Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/object/object_constraint.c
 *  \ingroup edobj
 */


#include <stdio.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_dynstr.h"
#include "BLI_utildefines.h"

#include "DNA_constraint_types.h"
#include "DNA_curve_types.h"
#include "DNA_scene_types.h"
#include "DNA_text_types.h"
#include "DNA_object_types.h"

#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_constraint.h"
#include "BKE_context.h"
#include "BKE_depsgraph.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_report.h"
#include "BKE_tracking.h"
#include "BIK_api.h"

#ifdef WITH_PYTHON
#include "BPY_extern.h"
#endif

#include "WM_api.h"
#include "WM_types.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "ED_object.h"
#include "ED_armature.h"
#include "ED_screen.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "object_intern.h"

/* -------------- Get Active Constraint Data ---------------------- */

/* if object in posemode, active bone constraints, else object constraints */
ListBase *get_active_constraints (Object *ob)
{
	if (ob == NULL)
		return NULL;
	
	if (ob->mode & OB_MODE_POSE) {
		bPoseChannel *pchan;
		
		pchan = get_active_posechannel(ob);
		if (pchan)
			return &pchan->constraints;
	}
	else 
		return &ob->constraints;
	
	return NULL;
}

/* Find the list that a given constraint belongs to, and/or also get the posechannel this is from (if applicable) */
ListBase *get_constraint_lb (Object *ob, bConstraint *con, bPoseChannel **pchan_r)
{
	if (pchan_r)
		*pchan_r= NULL;
	
	if (ELEM(NULL, ob, con))
		return NULL;
	
	/* try object constraints first */
	if ((BLI_findindex(&ob->constraints, con) != -1)) {
		return &ob->constraints;
	}
	
	/* if armature, try pose bones too */
	if (ob->pose) {
		bPoseChannel *pchan;
		
		/* try each bone in order 
		 * NOTE: it's not possible to directly look up the active bone yet, so this will have to do
		 */
		for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
			if ((BLI_findindex(&pchan->constraints, con) != -1)) {
				
				if (pchan_r)
					*pchan_r= pchan;
				
				return &pchan->constraints;
			}
		}
	}
	
	/* done */
	return NULL;
}

/* single constraint */
bConstraint *get_active_constraint (Object *ob)
{
	return constraints_get_active(get_active_constraints(ob));
}

/* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */
#ifdef WITH_PYTHON
/* ------------- PyConstraints ------------------ */

/* this callback sets the text-file to be used for selected menu item */
static void validate_pyconstraint_cb (void *arg1, void *arg2)
{
	bPythonConstraint *data = arg1;
	Text *text= NULL;
	int index = *((int *)arg2);
	int i;
	
	/* exception for no script */
	if (index) {
		/* innovative use of a for...loop to search */
		for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next);
	}
	data->text = text;
}

/* this returns a string for the list of usable pyconstraint script names */
static char *buildmenu_pyconstraints (Text *con_text, int *pyconindex)
{
	DynStr *pupds= BLI_dynstr_new();
	Text *text;
	char *str;
	char buf[64];
	int i;
	
	/* add title first */
	sprintf(buf, "Scripts: %%t|[None]%%x0|");
	BLI_dynstr_append(pupds, buf);
	
	/* init active-index first */
	if (con_text == NULL)
		*pyconindex= 0;
	
	/* loop through markers, adding them */
	for (text=G.main->text.first, i=1; text; i++, text=text->id.next) {
		/* this is important to ensure that right script is shown as active */
		if (text == con_text) *pyconindex = i;
		
		/* only include valid pyconstraint scripts */
		if (BPY_is_pyconstraint(text)) {
			BLI_dynstr_append(pupds, text->id.name+2);
			
			sprintf(buf, "%%x%d", i);
			BLI_dynstr_append(pupds, buf);
			
			if (text->id.next)
				BLI_dynstr_append(pupds, "|");
		}
	}
	
	/* convert to normal MEM_malloc'd string */
	str= BLI_dynstr_get_cstring(pupds);
	BLI_dynstr_free(pupds);
	
	return str;
}
#endif /* WITH_PYTHON */

#if 0 // UNUSED, until pyconstraints are added back.
/* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */
static void update_pyconstraint_cb (void *arg1, void *arg2)
{
#ifndef WITH_PYTHON
	(void)arg1; /* unused */
	(void)arg2; /* unused */
#else
	Object *owner= (Object *)arg1;
	bConstraint *con= (bConstraint *)arg2;
	if (owner && con)
		BPY_pyconstraint_update(owner, con);
#endif
}
#endif // UNUSED

/* helper function for add_constriant - sets the last target for the active constraint */
static void set_constraint_nth_target (bConstraint *con, Object *target, const char subtarget[], int index)
{
	bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
	ListBase targets = {NULL, NULL};
	bConstraintTarget *ct;
	int num_targets, i;
	
	if (cti && cti->get_constraint_targets) {
		cti->get_constraint_targets(con, &targets);
		num_targets= BLI_countlist(&targets);
		
		if (index < 0) {
			if (abs(index) < num_targets)
				index= num_targets - abs(index);
			else
				index= num_targets - 1;
		}
		else if (index >= num_targets) {
			index= num_targets - 1;
		}
		
		for (ct=targets.first, i=0; ct; ct= ct->next, i++) {
			if (i == index) {
				ct->tar= target;
				BLI_strncpy(ct->subtarget, subtarget, sizeof(ct->subtarget));
				break;
			}
		}
		
		if (cti->flush_constraint_targets)
			cti->flush_constraint_targets(con, &targets, 0);
	}
}

/* ------------- Constraint Sanity Testing ------------------- */

/* checks validity of object pointers, and NULLs,
 * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag.
 */
static void test_constraints (Object *owner, bPoseChannel *pchan)
{
	bConstraint *curcon;
	ListBase *conlist= NULL;
	int type;
	
	if (owner==NULL) return;
	
	/* Check parents */
	if (pchan) {
		switch (owner->type) {
			case OB_ARMATURE:
				type = CONSTRAINT_OBTYPE_BONE;
				break;
			default:
				type = CONSTRAINT_OBTYPE_OBJECT;
				break;
		}
	}
	else
		type = CONSTRAINT_OBTYPE_OBJECT;
	
	/* Get the constraint list for this object */
	switch (type) {
		case CONSTRAINT_OBTYPE_OBJECT:
			conlist = &owner->constraints;
			break;
		case CONSTRAINT_OBTYPE_BONE:
			conlist = &pchan->constraints;
			break;
	}
	
	/* Check all constraints - is constraint valid? */
	if (conlist) {
		for (curcon = conlist->first; curcon; curcon=curcon->next) {
			bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
			ListBase targets = {NULL, NULL};
			bConstraintTarget *ct;
			
			/* clear disabled-flag first */
			curcon->flag &= ~CONSTRAINT_DISABLE;
			
			if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) {
				bKinematicConstraint *data = curcon->data;
				
				/* bad: we need a separate set of checks here as poletarget is 
				 *		optional... otherwise poletarget must exist too or else
				 *		the constraint is deemed invalid
				 */
				/* default IK check ... */
				if (exist_object(data->tar) == 0) {
					data->tar = NULL;
					curcon->flag |= CONSTRAINT_DISABLE;
				}
				else if (data->tar == owner) {
					if (!get_named_bone(get_armature(owner), data->subtarget)) {
						curcon->flag |= CONSTRAINT_DISABLE;
					}
				}
				
				if (data->poletar) {
					if (exist_object(data->poletar) == 0) {
						data->poletar = NULL;
						curcon->flag |= CONSTRAINT_DISABLE;
					}
					else if (data->poletar == owner) {
						if (!get_named_bone(get_armature(owner), data->polesubtarget)) {
							curcon->flag |= CONSTRAINT_DISABLE;
						}
					}
				}
				/* ... can be overwritten here */
				BIK_test_constraint(owner, curcon);
				/* targets have already been checked for this */
				continue;
			}
			else if (curcon->type == CONSTRAINT_TYPE_PIVOT) {
				bPivotConstraint *data = curcon->data;
				
				/* target doesn't have to exist, but if it is non-null, it must exist! */
				if (data->tar && exist_object(data->tar)==0) {
					data->tar = NULL;
					curcon->flag |= CONSTRAINT_DISABLE;
				}
				else if (data->tar == owner) {
					if (!get_named_bone(get_armature(owner), data->subtarget)) {
						curcon->flag |= CONSTRAINT_DISABLE;
					}
				}
				
				/* targets have already been checked for this */
				continue;
			}
			else if (curcon->type == CONSTRAINT_TYPE_ACTION) {
				bActionConstraint *data = curcon->data;
				
				/* validate action */
				if (data->act == NULL) 
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) {
				bFollowPathConstraint *data = curcon->data;
				
				/* don't allow track/up axes to be the same */
				if (data->upflag==data->trackflag)
					curcon->flag |= CONSTRAINT_DISABLE;
				if (data->upflag+3==data->trackflag)
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) {
				bTrackToConstraint *data = curcon->data;
				
				/* don't allow track/up axes to be the same */
				if (data->reserved2==data->reserved1)
					curcon->flag |= CONSTRAINT_DISABLE;
				if (data->reserved2+3==data->reserved1)
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) {
				bLockTrackConstraint *data = curcon->data;
				
				if (data->lockflag==data->trackflag)
					curcon->flag |= CONSTRAINT_DISABLE;
				if (data->lockflag+3==data->trackflag)
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			else if (curcon->type == CONSTRAINT_TYPE_SPLINEIK) {
				bSplineIKConstraint *data = curcon->data;
				
				/* if the number of points does not match the amount required by the chain length,
				 * free the points array and request a rebind...
				 */
				if ((data->points == NULL) || (data->numpoints != data->chainlen+1)) {
					/* free the points array */
					if (data->points) {
						MEM_freeN(data->points);
						data->points = NULL;
					}
					
					/* clear the bound flag, forcing a rebind next time this is evaluated */
					data->flag &= ~CONSTRAINT_SPLINEIK_BOUND;
				}
			}
			else if (curcon->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
				bFollowTrackConstraint *data = curcon->data;

				if ((data->flag&CAMERASOLVER_ACTIVECLIP)==0) {
					if (data->clip != NULL && data->track[0]) {
						MovieTracking *tracking= &data->clip->tracking;
						MovieTrackingObject *tracking_object;

						if (data->object[0])
							tracking_object= BKE_tracking_named_object(tracking, data->object);
						else
							tracking_object= BKE_tracking_get_camera_object(tracking);

						if (!tracking_object) {
							curcon->flag |= CONSTRAINT_DISABLE;
						}
						else {
							if (!BKE_tracking_named_track(tracking, tracking_object, data->track))
								curcon->flag |= CONSTRAINT_DISABLE;
						}
					}
					else curcon->flag |= CONSTRAINT_DISABLE;
				}
			}
			else if (curcon->type == CONSTRAINT_TYPE_CAMERASOLVER) {
				bCameraSolverConstraint *data = curcon->data;

				if ((data->flag&CAMERASOLVER_ACTIVECLIP)==0 && data->clip == NULL)
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			else if (curcon->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
				bObjectSolverConstraint *data = curcon->data;

				if ((data->flag&CAMERASOLVER_ACTIVECLIP)==0 && data->clip == NULL)
					curcon->flag |= CONSTRAINT_DISABLE;
			}
			
			/* Check targets for constraints */
			if (cti && cti->get_constraint_targets) {
				cti->get_constraint_targets(curcon, &targets);
				
				/* disable and clear constraints targets that are incorrect */
				for (ct= targets.first; ct; ct= ct->next) {
					/* general validity checks (for those constraints that need this) */
					if (exist_object(ct->tar) == 0) {
						/* object doesn't exist, but constraint requires target */
						ct->tar = NULL;
						curcon->flag |= CONSTRAINT_DISABLE;
					}
					else if (ct->tar == owner) {
						if (type == CONSTRAINT_OBTYPE_BONE) {
							if (!get_named_bone(get_armature(owner), ct->subtarget)) {
								/* bone must exist in armature... */
								// TODO: clear subtarget?
								curcon->flag |= CONSTRAINT_DISABLE;
							}
							else if (strcmp(pchan->name, ct->subtarget) == 0) {
								/* cannot target self */
								ct->subtarget[0] = '\0';
								curcon->flag |= CONSTRAINT_DISABLE;
							}
						}
						else {
							/* cannot use self as target */
							ct->tar = NULL;
							curcon->flag |= CONSTRAINT_DISABLE;
						}
					}
					
					/* target checks for specific constraints */
					if (ELEM3(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_SPLINEIK)) {
						if (ct->tar) {
							if (ct->tar->type != OB_CURVE) {
								ct->tar= NULL;
								curcon->flag |= CONSTRAINT_DISABLE;
							}
							else {
								Curve *cu= ct->tar->data;
								
								/* auto-set 'Path' setting on curve so this works  */
								cu->flag |= CU_PATH;
							}
						}						
					}
				}	
				
				/* free any temporary targets */
				if (cti->flush_constraint_targets)
					cti->flush_constraint_targets(curcon, &targets, 0);
			}
		}
	}
}

void object_test_constraints (Object *owner)
{
	if (owner->constraints.first)
		test_constraints(owner, NULL);
	
	if (owner->type==OB_ARMATURE && owner->pose) {
		bPoseChannel *pchan;
		
		for (pchan= owner->pose->chanbase.first; pchan; pchan= pchan->next) {
			if (pchan->constraints.first)
				test_constraints(owner, pchan);
		}
	}
}


/************************ generic functions for operators using constraint names and data context *********************/

#define EDIT_CONSTRAINT_OWNER_OBJECT	0
#define EDIT_CONSTRAINT_OWNER_BONE		1

static EnumPropertyItem constraint_owner_items[] = {
	{EDIT_CONSTRAINT_OWNER_OBJECT, "OBJECT", 0, "Object", "Edit a constraint on the active object"},
	{EDIT_CONSTRAINT_OWNER_BONE, "BONE", 0, "Bone", "Edit a constraint on the active bone"},
	{0, NULL, 0, NULL, NULL}};


static int edit_constraint_poll_generic(bContext *C, StructRNA *rna_type)
{
	PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", rna_type);
	Object *ob= (ptr.id.data) ? ptr.id.data : ED_object_active_context(C);

	if (!ob || ob->id.lib) return 0;
	if (ptr.id.data && ((ID*)ptr.id.data)->lib) return 0;

	return 1;
}

static int edit_constraint_poll(bContext *C)
{
	return edit_constraint_poll_generic(C, &RNA_Constraint);
}

static void edit_constraint_properties(wmOperatorType *ot)
{
	RNA_def_string(ot->srna, "constraint", "", MAX_NAME, "Constraint", "Name of the constraint to edit");
	RNA_def_enum(ot->srna, "owner", constraint_owner_items, 0, "Owner", "The owner of this constraint");
}

static int edit_constraint_invoke_properties(bContext *C, wmOperator *op)
{
	PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
	Object *ob= (ptr.id.data)?ptr.id.data:ED_object_active_context(C);
	bConstraint *con;
	ListBase *list;
	
	if (RNA_struct_property_is_set(op->ptr, "constraint") && RNA_struct_property_is_set(op->ptr, "owner"))
		return 1;
	
	if (ptr.data) {
		con = ptr.data;
		RNA_string_set(op->ptr, "constraint", con->name);
		
		list = get_constraint_lb(ob, con, NULL);
		
		if (&ob->constraints == list)
			RNA_enum_set(op->ptr, "owner", EDIT_CONSTRAINT_OWNER_OBJECT);
		else
			RNA_enum_set(op->ptr, "owner", EDIT_CONSTRAINT_OWNER_BONE);
		
		return 1;
	}
	
	return 0;
}

static bConstraint *edit_constraint_property_get(wmOperator *op, Object *ob, int type)
{
	char constraint_name[MAX_NAME];
	int owner = RNA_enum_get(op->ptr, "owner");
	bConstraint *con;
	ListBase *list=NULL;
	
	RNA_string_get(op->ptr, "constraint", constraint_name);
	
	if (owner == EDIT_CONSTRAINT_OWNER_OBJECT) {
		list = &ob->constraints;
	} 
	else if (owner == EDIT_CONSTRAINT_OWNER_BONE) {
		bPoseChannel *pchan= get_active_posechannel(ob);
		if (pchan)
			list = &pchan->constraints;
		else {
			//if (G.debug & G_DEBUG)
			//printf("edit_constraint_property_get: No active bone for object '%s'\n", (ob)? ob->id.name+2 : "<None>");
			return NULL;
		}
	}
	else {
		//if (G.debug & G_DEBUG)
		//printf("edit_constraint_property_get: defaulting to getting list in the standard way\n");
		list = get_active_constraints(ob);
	}
	
	con = constraints_findByName(list, constraint_name);
	//if (G.debug & G_DEBUG)
	//printf("constraint found = %p, %s\n", (void *)con, (con)?con->name:"<Not found>");

	if (con && (type != 0) && (con->type != type))
		con = NULL;
	
	return con;
}

/* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */

/* ---------- Distance-Dependent Constraints ---------- */
/* StretchTo, Limit Distance */

static int stretchto_reset_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_STRETCHTO);
	bStretchToConstraint *data= (con) ? (bStretchToConstraint *)con->data : NULL;
	
	/* despite 3 layers of checks, we may still not be able to find a constraint */
	if (data == NULL)
		return OPERATOR_CANCELLED;
	
	/* just set original length to 0.0, which will cause a reset on next recalc */
	data->orglength = 0.0f;
	ED_object_constraint_update(ob);
	
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
	return OPERATOR_FINISHED;
}

static int stretchto_reset_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return stretchto_reset_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_stretchto_reset (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Reset Original Length";
	ot->idname = "CONSTRAINT_OT_stretchto_reset";
	ot->description = "Reset original length of bone for Stretch To Constraint";
	
	ot->exec = stretchto_reset_exec;
	ot->invoke = stretchto_reset_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}


static int limitdistance_reset_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_DISTLIMIT);
	bDistLimitConstraint *data= (con) ? (bDistLimitConstraint *)con->data : NULL;
	
	/* despite 3 layers of checks, we may still not be able to find a constraint */
	if (data == NULL)
		return OPERATOR_CANCELLED;
	
	/* just set original length to 0.0, which will cause a reset on next recalc */
	data->dist = 0.0f;
	ED_object_constraint_update(ob);
	
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
	return OPERATOR_FINISHED;
}

static int limitdistance_reset_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return limitdistance_reset_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_limitdistance_reset (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Reset Distance";
	ot->idname = "CONSTRAINT_OT_limitdistance_reset";
	ot->description = "Reset limiting distance for Limit Distance Constraint";
	
	ot->exec = limitdistance_reset_exec;
	ot->invoke = limitdistance_reset_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}

/* ------------- Child-Of Constraint ------------------ */

static void child_get_inverse_matrix (Scene *scene, Object *ob, bConstraint *con, float invmat[4][4])
{
	bConstraint *lastcon = NULL;
	bPoseChannel *pchan= NULL;
	
	/* nullify inverse matrix first */
	unit_m4(invmat);
	
	/* try to find a pose channel - assume that this is the constraint owner */
	// TODO: get from context instead?
	if (ob && ob->pose)
		pchan= get_active_posechannel(ob);
	
	/* calculate/set inverse matrix:
	 * 	We just calculate all transform-stack eval up to but not including this constraint.
	 * 	This is because inverse should just inverse correct for just the constraint's influence
	 * 	when it gets applied; that is, at the time of application, we don't know anything about
	 * 	what follows.
	 */
	if (pchan) {
		float imat[4][4], tmat[4][4];
		float pmat[4][4];
		
		/* 1. calculate posemat where inverse doesn't exist yet (inverse was cleared above), 
		 * to use as baseline ("pmat") to derive delta from. This extra calc saves users 
		 * from having pressing "Clear Inverse" first
		 */
		where_is_pose(scene, ob);
		copy_m4_m4(pmat, pchan->pose_mat);
		
		/* 2. knock out constraints starting from this one */
		lastcon = pchan->constraints.last;
		pchan->constraints.last = con->prev;
		
		if (con->prev) {
			/* new end must not point to this one, else this chain cutting is useless */
			con->prev->next = NULL;
		}
		else {
			/* constraint was first */
			pchan->constraints.first = NULL;
		}
		
		/* 3. solve pose without disabled constraints */
		where_is_pose(scene, ob);
		
		/* 4. determine effect of constraint by removing the newly calculated 
		 * pchan->pose_mat from the original pchan->pose_mat, thus determining 
		 * the effect of the constraint
		 */
		invert_m4_m4(imat, pchan->pose_mat);
		mult_m4_m4m4(tmat, pmat, imat);
		invert_m4_m4(invmat, tmat);
		
		/* 5. restore constraints */
		pchan->constraints.last = lastcon;
		
		if (con->prev) {
			/* hook up prev to this one again */
			con->prev->next = con;
		}
		else {
			/* set as first again */
			pchan->constraints.first = con;
		}
		
		/* 6. recalculate pose with new inv-mat applied */
		where_is_pose(scene, ob);
	}
	else if (ob) {
		Object workob;
		
		/* use what_does_parent to find inverse - just like for normal parenting */
		what_does_parent(scene, ob, &workob);
		invert_m4_m4(invmat, workob.obmat);
	}
}

/* ChildOf Constraint - set inverse callback */
static int childof_set_inverse_exec (bContext *C, wmOperator *op)
{
	Scene *scene= CTX_data_scene(C);
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_CHILDOF);
	bChildOfConstraint *data= (con) ? (bChildOfConstraint *)con->data : NULL;

	/* despite 3 layers of checks, we may still not be able to find a constraint */
	if (data == NULL) {
		printf("DEBUG: Child-Of Set Inverse - object = '%s'\n", (ob)? ob->id.name+2 : "<None>");
		BKE_report(op->reports, RPT_ERROR, "Couldn't find constraint data for Child-Of Set Inverse");
		return OPERATOR_CANCELLED;
	}
	
	child_get_inverse_matrix(scene, ob, con, data->invmat);

	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
	
	return OPERATOR_FINISHED;
}

static int childof_set_inverse_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return childof_set_inverse_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Set Inverse";
	ot->idname = "CONSTRAINT_OT_childof_set_inverse";
	ot->description = "Set inverse correction for ChildOf constraint";
	
	ot->exec = childof_set_inverse_exec;
	ot->invoke = childof_set_inverse_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}

/* ChildOf Constraint - clear inverse callback */
static int childof_clear_inverse_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_CHILDOF);
	bChildOfConstraint *data= (con) ? (bChildOfConstraint *)con->data : NULL;
	
	if (data==NULL) {
		BKE_report(op->reports, RPT_ERROR, "Childof constraint not found");
		return OPERATOR_CANCELLED;
	}
	
	/* simply clear the matrix */
	unit_m4(data->invmat);
	
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
	
	return OPERATOR_FINISHED;
}

static int childof_clear_inverse_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return childof_clear_inverse_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear Inverse";
	ot->idname = "CONSTRAINT_OT_childof_clear_inverse";
	ot->description = "Clear inverse correction for ChildOf constraint";
	
	ot->exec = childof_clear_inverse_exec;
	ot->invoke = childof_clear_inverse_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}

/* ------------- Object Solver Constraint ------------------ */

static int objectsolver_set_inverse_exec (bContext *C, wmOperator *op)
{
	Scene *scene= CTX_data_scene(C);
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_OBJECTSOLVER);
	bObjectSolverConstraint *data= (con) ? (bObjectSolverConstraint *)con->data : NULL;

	/* despite 3 layers of checks, we may still not be able to find a constraint */
	if (data == NULL) {
		printf("DEBUG: Child-Of Set Inverse - object = '%s'\n", (ob)? ob->id.name+2 : "<None>");
		BKE_report(op->reports, RPT_ERROR, "Couldn't find constraint data for Child-Of Set Inverse");
		return OPERATOR_CANCELLED;
	}

	child_get_inverse_matrix(scene, ob, con, data->invmat);

	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);

	return OPERATOR_FINISHED;
}

static int objectsolver_set_inverse_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return objectsolver_set_inverse_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_objectsolver_set_inverse (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Set Inverse";
	ot->idname = "CONSTRAINT_OT_objectsolver_set_inverse";
	ot->description = "Set inverse correction for ObjectSolver constraint";

	ot->exec = objectsolver_set_inverse_exec;
	ot->invoke = objectsolver_set_inverse_invoke;
	ot->poll = edit_constraint_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}

static int objectsolver_clear_inverse_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, CONSTRAINT_TYPE_OBJECTSOLVER);
	bObjectSolverConstraint *data= (con) ? (bObjectSolverConstraint *)con->data : NULL;

	if (data==NULL) {
		BKE_report(op->reports, RPT_ERROR, "Childof constraint not found");
		return OPERATOR_CANCELLED;
	}

	/* simply clear the matrix */
	unit_m4(data->invmat);

	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);

	return OPERATOR_FINISHED;
}

static int objectsolver_clear_inverse_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return objectsolver_clear_inverse_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_objectsolver_clear_inverse (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear Inverse";
	ot->idname = "CONSTRAINT_OT_objectsolver_clear_inverse";
	ot->description = "Clear inverse correction for ObjectSolver constraint";

	ot->exec = objectsolver_clear_inverse_exec;
	ot->invoke = objectsolver_clear_inverse_invoke;
	ot->poll = edit_constraint_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	edit_constraint_properties(ot);
}

/***************************** BUTTONS ****************************/

void ED_object_constraint_set_active(Object *ob, bConstraint *con)
{	
	ListBase *lb = get_constraint_lb(ob, con, NULL);
	
	/* lets be nice and escape if its active already */
	// NOTE: this assumes that the stack doesn't have other active ones set...
	if ((lb && con) && (con->flag & CONSTRAINT_ACTIVE))
		return;
	
	constraints_set_active(lb, con);
}

void ED_object_constraint_update(Object *ob)
{

	if (ob->pose) update_pose_constraint_flags(ob->pose);

	object_test_constraints(ob);

	if (ob->type==OB_ARMATURE) DAG_id_tag_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
	else DAG_id_tag_update(&ob->id, OB_RECALC_OB);
}

void ED_object_constraint_dependency_update(Main *bmain, Scene *scene, Object *ob)
{
	ED_object_constraint_update(ob);

	if (ob->pose) ob->pose->flag |= POSE_RECALC;	// checks & sorts pose channels
	DAG_scene_sort(bmain, scene);
}

static int constraint_poll(bContext *C)
{
	PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
	return (ptr.id.data && ptr.data);
}

static int constraint_delete_exec (bContext *C, wmOperator *UNUSED(op))
{
	PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
	Object *ob= ptr.id.data;
	bConstraint *con= ptr.data;
	ListBase *lb = get_constraint_lb(ob, con, NULL);
	const short is_ik= ELEM(con->type, CONSTRAINT_TYPE_KINEMATIC, CONSTRAINT_TYPE_SPLINEIK);

	/* free the constraint */
	if (remove_constraint(lb, con)) {
		/* there's no active constraint now, so make sure this is the case */
		constraints_set_active(lb, NULL);
		
		ED_object_constraint_update(ob); /* needed to set the flags on posebones correctly */

		/* ITASC needs to be rebuilt once a constraint is removed [#26920] */
		if (is_ik) {
			BIK_clear_data(ob->pose);
		}

		/* notifiers */
		WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
		
		return OPERATOR_FINISHED;
	}
	else {
		/* couldn't remove due to some invalid data */
		return OPERATOR_CANCELLED;
	}
}

void CONSTRAINT_OT_delete (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Delete Constraint";
	ot->idname = "CONSTRAINT_OT_delete";
	ot->description = "Remove constraint from constraint stack";
	
	/* callbacks */
	ot->exec = constraint_delete_exec;
	ot->poll = constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO; 
}

static int constraint_move_down_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, 0);
	
	if (con && con->next) {
		ListBase *conlist= get_constraint_lb(ob, con, NULL);
		bConstraint *nextCon= con->next;
		
		/* insert the nominated constraint after the one that used to be after it */
		BLI_remlink(conlist, con);
		BLI_insertlinkafter(conlist, nextCon, con);
		
		WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
		
		return OPERATOR_FINISHED;
	}
	
	return OPERATOR_CANCELLED;
}

static int constraint_move_down_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return constraint_move_down_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}


void CONSTRAINT_OT_move_down (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Move Constraint Down";
	ot->idname = "CONSTRAINT_OT_move_down";
	ot->description = "Move constraint down in constraint stack";
	
	/* callbacks */
	ot->exec = constraint_move_down_exec;
	ot->invoke = constraint_move_down_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO; 
	edit_constraint_properties(ot);
}


static int constraint_move_up_exec (bContext *C, wmOperator *op)
{
	Object *ob = ED_object_active_context(C);
	bConstraint *con = edit_constraint_property_get(op, ob, 0);
	
	if (con && con->prev) {
		ListBase *conlist= get_constraint_lb(ob, con, NULL);
		bConstraint *prevCon= con->prev;
		
		/* insert the nominated constraint before the one that used to be before it */
		BLI_remlink(conlist, con);
		BLI_insertlinkbefore(conlist, prevCon, con);
		
		WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
		
		return OPERATOR_FINISHED;
	}
	
	return OPERATOR_CANCELLED;
}

static int constraint_move_up_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	if (edit_constraint_invoke_properties(C, op))
		return constraint_move_up_exec(C, op);
	else
		return OPERATOR_CANCELLED;
}

void CONSTRAINT_OT_move_up (wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Move Constraint Up";
	ot->idname = "CONSTRAINT_OT_move_up";
	ot->description = "Move constraint up in constraint stack";
	
	/* callbacks */
	ot->exec = constraint_move_up_exec;
	ot->invoke = constraint_move_up_invoke;
	ot->poll = edit_constraint_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO; 
	edit_constraint_properties(ot);
}

/***************************** OPERATORS ****************************/

/************************ remove constraint operators *********************/

static int pose_constraints_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain= CTX_data_main(C);
	Scene *scene= CTX_data_scene(C);
	Object *ob= object_pose_armature_get(CTX_data_active_object(C));
	
	/* free constraints for all selected bones */
	CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
	{
		free_constraints(&pchan->constraints);
		pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_SPLINEIK|PCHAN_HAS_CONST);
	}
	CTX_DATA_END;
	
	/* force depsgraph to get recalculated since relationships removed */
	DAG_scene_sort(bmain, scene);		/* sort order of objects */	
	
	/* note, calling BIK_clear_data() isn't needed here */

	/* do updates */
	DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
	
	return OPERATOR_FINISHED;
}

void POSE_OT_constraints_clear(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear Pose Constraints";
	ot->idname = "POSE_OT_constraints_clear";
	ot->description = "Clear all the constraints for the selected bones";
	
	/* callbacks */
	ot->exec = pose_constraints_clear_exec;
	ot->poll = ED_operator_posemode; // XXX - do we want to ensure there are selected bones too?
}


static int object_constraints_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain= CTX_data_main(C);
	Scene *scene= CTX_data_scene(C);
	
	/* do freeing */
	CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
	{
		free_constraints(&ob->constraints);
		DAG_id_tag_update(&ob->id, OB_RECALC_OB);
	}
	CTX_DATA_END;
	
	/* force depsgraph to get recalculated since relationships removed */
	DAG_scene_sort(bmain, scene);		/* sort order of objects */	
	
	/* do updates */
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_constraints_clear(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear Object Constraints";
	ot->idname = "OBJECT_OT_constraints_clear";
	ot->description = "Clear all the constraints for the active Object only";
	
	/* callbacks */
	ot->exec = object_constraints_clear_exec;
	ot->poll = ED_operator_object_active_editable;
}

/************************ copy all constraints operators *********************/

static int pose_constraint_copy_exec(bContext *C, wmOperator *op)
{
	Main *bmain= CTX_data_main(C);
	Scene *scene = CTX_data_scene(C);
	bPoseChannel *pchan = CTX_data_active_pose_bone(C);
	
	/* don't do anything if bone doesn't exist or doesn't have any constraints */
	if (ELEM(NULL, pchan, pchan->constraints.first)) {
		BKE_report(op->reports, RPT_ERROR, "No active bone with constraints for copying");
		return OPERATOR_CANCELLED;
	}
	
	/* copy all constraints from active posebone to all selected posebones */
	CTX_DATA_BEGIN(C, bPoseChannel*, chan, selected_pose_bones) 
	{
		/* if we're not handling the object we're copying from, copy all constraints over */
		if (pchan != chan) {
			copy_constraints(&chan->constraints, &pchan->constraints, TRUE);
			/* update flags (need to add here, not just copy) */
			chan->constflag |= pchan->constflag;
		}
	}
	CTX_DATA_END;
	
	/* force depsgraph to get recalculated since new relationships added */
	DAG_scene_sort(bmain, scene);		/* sort order of objects/bones */

	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
	
	return OPERATOR_FINISHED;
}

void POSE_OT_constraints_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Constraints to Selected";
	ot->idname = "POSE_OT_constraints_copy";
	ot->description = "Copy constraints to other selected bones";
	
	/* api callbacks */
	ot->exec = pose_constraint_copy_exec;
	ot->poll = ED_operator_posemode;

	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}

static int object_constraint_copy_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain= CTX_data_main(C);
	Scene *scene = CTX_data_scene(C);
	Object *obact = ED_object_active_context(C);
	
	/* copy all constraints from active object to all selected objects */
	CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
	{
		/* if we're not handling the object we're copying from, copy all constraints over */
		if (obact != ob) {
			copy_constraints(&ob->constraints, &obact->constraints, TRUE);
			DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
		}
	}
	CTX_DATA_END;
	
	/* force depsgraph to get recalculated since new relationships added */
	DAG_scene_sort(bmain, scene);		/* sort order of objects */
	
	/* notifiers for updates */
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, NULL);
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_constraints_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Constraints to Selected";
	ot->idname = "OBJECT_OT_constraints_copy";
	ot->description = "Copy constraints to other selected objects";
	
	/* api callbacks */
	ot->exec = object_constraint_copy_exec;
	ot->poll = ED_operator_object_active_editable;

	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}

/************************ add constraint operators *********************/

/* get the Object and/or PoseChannel to use as target */
static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add)
{
	Object *obact= ED_object_active_context(C);
	bPoseChannel *pchanact= get_active_posechannel(obact);
	short only_curve= 0, only_mesh= 0, only_ob= 0;
	short found= 0;
	
	/* clear tar_ob and tar_pchan fields before use 
	 *	- assume for now that both always exist...
	 */
	*tar_ob= NULL;
	*tar_pchan= NULL;
	
	/* check if constraint type doesn't requires a target
	 *	- if so, no need to get any targets 
	 */
	switch (con_type) {
		/* no-target constraints --------------------------- */
			/* null constraint - shouldn't even be added! */
		case CONSTRAINT_TYPE_NULL:
			/* limit constraints - no targets needed */
		case CONSTRAINT_TYPE_LOCLIMIT:
		case CONSTRAINT_TYPE_ROTLIMIT:
		case CONSTRAINT_TYPE_SIZELIMIT:
		case CONSTRAINT_TYPE_SAMEVOL:
			return 0;
			
		/* restricted target-type constraints -------------- */
		/* NOTE: for these, we cannot try to add a target object if no valid ones are found, since that doesn't work */
			/* curve-based constraints - set the only_curve and only_ob flags */
		case CONSTRAINT_TYPE_CLAMPTO:
		case CONSTRAINT_TYPE_FOLLOWPATH:
		case CONSTRAINT_TYPE_SPLINEIK:
			only_curve= 1;
			only_ob= 1;
			add= 0;
			break;
			
			/* mesh only? */
		case CONSTRAINT_TYPE_SHRINKWRAP:
			only_mesh= 1;
			only_ob= 1;
			add= 0;
			break;
			
			/* object only - add here is ok? */
		case CONSTRAINT_TYPE_RIGIDBODYJOINT:
			only_ob= 1;
			break;
	}
	
	/* if the active Object is Armature, and we can search for bones, do so... */
	if ((obact->type == OB_ARMATURE) && (only_ob == 0)) {
		/* search in list of selected Pose-Channels for target */
		CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
		{
			/* just use the first one that we encounter, as long as it is not the active one */
			if (pchan != pchanact) {
				*tar_ob= obact;
				*tar_pchan= pchan;
				found= 1;
				
				break;
			}
		}
		CTX_DATA_END;
	}
	
	/* if not yet found, try selected Objects... */
	if (found == 0) {
		/* search in selected objects context */
		CTX_DATA_BEGIN(C, Object*, ob, selected_objects) 
		{
			/* just use the first object we encounter (that isn't the active object) 
			 * and which fulfills the criteria for the object-target that we've got 
			 */
			if ( (ob != obact) &&
				 ((!only_curve) || (ob->type == OB_CURVE)) && 
				 ((!only_mesh) || (ob->type == OB_MESH)) )
			{
				/* set target */
				*tar_ob= ob;
				found= 1;
				
				/* perform some special operations on the target */
				if (only_curve) {
					/* Curve-Path option must be enabled for follow-path constraints to be able to work */
					Curve *cu= (Curve *)ob->data;
					cu->flag |= CU_PATH;
				}
				
				break;
			}
		}
		CTX_DATA_END;
	}
	
	/* if still not found, add a new empty to act as a target (if allowed) */
	if ((found == 0) && (add)) {
		Scene *scene= CTX_data_scene(C);
		Base *base= BASACT, *newbase=NULL;
		Object *obt;
		
		/* add new target object */
		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) {
			/* since by default, IK targets the tip of the last bone, use the tip of the active PoseChannel 
			 * if adding a target for an IK Constraint
			 */
			if (con_type == CONSTRAINT_TYPE_KINEMATIC)
				mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_tail);
			else
				mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_head);
		}
		else {
			copy_v3_v3(obt->loc, obact->obmat[3]);
		}

		/* restore, add_object sets active */
		BASACT= base;
		base->flag |= SELECT;
		
		/* make our new target the new object */
		*tar_ob= obt;
		found= 1;
	}
	
	/* return whether there's any target */
	return found;
}

/* used by add constraint operators to add the constraint required */
static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget)
{
	Main *bmain= CTX_data_main(C);
	Scene *scene= CTX_data_scene(C);
	bPoseChannel *pchan;
	bConstraint *con;
	
	if (list == &ob->constraints) {
		pchan= NULL;
	}
	else {
		pchan= get_active_posechannel(ob);
		
		/* ensure not to confuse object/pose adding */
		if (pchan == NULL) {
			BKE_report(op->reports, RPT_ERROR, "No active pose bone to add a constraint to");
			return OPERATOR_CANCELLED;
		}
	}
	/* check if constraint to be added is valid for the given constraints stack */
	if (type == CONSTRAINT_TYPE_NULL) {
		return OPERATOR_CANCELLED;
	}
	if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) {
		BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects");
		return OPERATOR_CANCELLED;
	}
	if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) {
		BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones");
		return OPERATOR_CANCELLED;
	}
	if ( (type == CONSTRAINT_TYPE_SPLINEIK) && ((!pchan) || (list != &pchan->constraints)) ) {
		BKE_report(op->reports, RPT_ERROR, "Spline IK Constraint can only be added to Bones");
		return OPERATOR_CANCELLED;
	}
	
	/* create a new constraint of the type requried, and add it to the active/given constraints list */
	if (pchan)
		con = add_pose_constraint(ob, pchan, NULL, type);
	else
		con = add_ob_constraint(ob, NULL, type);
	
	/* get the first selected object/bone, and make that the target
	 *	- apart from the buttons-window add buttons, we shouldn't add in this way
	 */
	if (setTarget) {
		Object *tar_ob= NULL;
		bPoseChannel *tar_pchan= NULL;
		
		/* get the target objects, adding them as need be */
		if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) {
			/* method of setting target depends on the type of target we've got 
			 *	- by default, just set the first target (distinction here is only for multiple-targeted constraints)
			 */
			if (tar_pchan)
				set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0);
			else
				set_constraint_nth_target(con, tar_ob, "", 0);
		}
	}
	
	/* do type-specific tweaking to the constraint settings  */
	switch (type) {
		case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore
		{
#ifdef WITH_PYTHON
			char *menustr;
			int scriptint= 0;
			/* popup a list of usable scripts */
			menustr = buildmenu_pyconstraints(NULL, &scriptint);
			// XXX scriptint = pupmenu(menustr);
			MEM_freeN(menustr);
			
			/* only add constraint if a script was chosen */
			if (scriptint) {
				/* add constraint */
				validate_pyconstraint_cb(con->data, &scriptint);
				
				/* make sure target allowance is set correctly */
				BPY_pyconstraint_update(ob, con);
			}
#endif
			break;
		}

		default:
			break;
	}
	
	/* make sure all settings are valid - similar to above checks, but sometimes can be wrong */
	object_test_constraints(ob);

	if (pchan)
		update_pose_constraint_flags(ob->pose);


	/* force depsgraph to get recalculated since new relationships added */
	DAG_scene_sort(bmain, scene);		/* sort order of objects */
	
	if ((ob->type==OB_ARMATURE) && (pchan)) {
		ob->pose->flag |= POSE_RECALC;	/* sort pose channels */
		DAG_id_tag_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
	}
	else
		DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
	
	/* notifiers for updates */
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob);
	
	return OPERATOR_FINISHED;
}

/* ------------------ */

/* dummy operator callback */
static int object_constraint_add_exec(bContext *C, wmOperator *op)
{
	Object *ob=ED_object_active_context(C);
	int type= RNA_enum_get(op->ptr, "type");
	short with_targets= 0;
	
	if (!ob) {
		BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to");
		return OPERATOR_CANCELLED;
	}
		
	/* hack: set constraint targets from selected objects in context is allowed when
	 *		operator name included 'with_targets', since the menu doesn't allow multiple properties
	 */
	if (strstr(op->idname, "with_targets"))
		with_targets= 1;

	return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets);
}

/* dummy operator callback */
static int pose_constraint_add_exec(bContext *C, wmOperator *op)
{
	Object *ob= object_pose_armature_get(ED_object_active_context(C));
	int type= RNA_enum_get(op->ptr, "type");
	short with_targets= 0;
	
	if (!ob) {
		BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to");
		return OPERATOR_CANCELLED;
	}
		
	/* hack: set constraint targets from selected objects in context is allowed when
	 *		operator name included 'with_targets', since the menu doesn't allow multiple properties
	 */
	if (strstr(op->idname, "with_targets"))
		with_targets= 1;
	
	return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets);
}

/* ------------------ */

void OBJECT_OT_constraint_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Constraint";
	ot->description = "Add a constraint to the active object";
	ot->idname = "OBJECT_OT_constraint_add";
	
	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = object_constraint_add_exec;
	ot->poll = ED_operator_object_active_editable;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
}

void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Constraint (with Targets)";
	ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones";
	ot->idname = "OBJECT_OT_constraint_add_with_targets";
	
	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = object_constraint_add_exec;
	ot->poll = ED_operator_object_active_editable;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
}

void POSE_OT_constraint_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Constraint";
	ot->description = "Add a constraint to the active bone";
	ot->idname = "POSE_OT_constraint_add";
	
	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = pose_constraint_add_exec;
	ot->poll = ED_operator_posemode;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
}

void POSE_OT_constraint_add_with_targets(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Constraint (with Targets)";
	ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones";
	ot->idname = "POSE_OT_constraint_add_with_targets";
	
	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = pose_constraint_add_exec;
	ot->poll = ED_operator_posemode;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
}

/************************ IK Constraint operators *********************/
/* NOTE: only for Pose-Channels */
// TODO: should these be here, or back in editors/armature/poseobject.c again?

/* present menu with options + validation for targets to use */
static int pose_ik_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(evt))
{
	Object *ob= object_pose_armature_get(CTX_data_active_object(C));
	bPoseChannel *pchan= get_active_posechannel(ob);
	bConstraint *con= NULL;
	
	uiPopupMenu *pup;
	uiLayout *layout;
	Object *tar_ob= NULL;
	bPoseChannel *tar_pchan= NULL;
	
	/* must have active bone */
	if (ELEM(NULL, ob, pchan)) {
		BKE_report(op->reports, RPT_ERROR, "Must have active bone to add IK Constraint to");
		return OPERATOR_CANCELLED;
	}
	
	/* bone must not have any constraints already */
	for (con= pchan->constraints.first; con; con= con->next) {
		if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
	}
	if (con) {
		BKE_report(op->reports, RPT_ERROR, "Bone already has IK Constraint");
		return OPERATOR_CANCELLED;
	}
	
	/* prepare popup menu to choose targetting options */
	pup= uiPupMenuBegin(C, "Add IK", ICON_NONE);
	layout= uiPupMenuLayout(pup);
	
	/* the type of targets we'll set determines the menu entries to show... */
	if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, 0)) {
		/* bone target, or object target? 
		 *	- the only thing that matters is that we want a target...
		 */
		if (tar_pchan)
			uiItemBooleanO(layout, "To Active Bone", ICON_NONE, "POSE_OT_ik_add", "with_targets", 1);
		else
			uiItemBooleanO(layout, "To Active Object", ICON_NONE, "POSE_OT_ik_add", "with_targets", 1);
	}
	else {
		/* we have a choice of adding to a new empty, or not setting any target (targetless IK) */
		uiItemBooleanO(layout, "To New Empty Object", ICON_NONE, "POSE_OT_ik_add", "with_targets", 1);
		uiItemBooleanO(layout, "Without Targets", ICON_NONE, "POSE_OT_ik_add", "with_targets", 0);
	}
	
	/* finish building the menu, and process it (should result in calling self again) */
	uiPupMenuEnd(C, pup);
	
	return OPERATOR_CANCELLED;
}

/* call constraint_add_exec() to add the IK constraint */
static int pose_ik_add_exec(bContext *C, wmOperator *op)
{
	Object *ob= CTX_data_active_object(C);
	int with_targets= RNA_boolean_get(op->ptr, "with_targets");
	
	/* add the constraint - all necessary checks should have been done by the invoke() callback already... */
	return constraint_add_exec(C, op, ob, get_active_constraints(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets);
}

void POSE_OT_ik_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add IK to Bone";
	ot->description = "Add IK Constraint to the active Bone";
	ot->idname = "POSE_OT_ik_add";
	
	/* api callbacks */
	ot->invoke = pose_ik_add_invoke;
	ot->exec = pose_ik_add_exec;
	ot->poll = ED_operator_posemode;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_boolean(ot->srna, "with_targets", 1, "With Targets", "Assign IK Constraint with targets derived from the select bones/objects");
}

/* ------------------ */

/* remove IK constraints from selected bones */
static int pose_ik_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
	Object *ob= object_pose_armature_get(CTX_data_active_object(C));
	
	/* only remove IK Constraints */
	CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
	{
		bConstraint *con, *next;
		
		// TODO: should we be checking if these contraints were local before we try and remove them?
		for (con= pchan->constraints.first; con; con= next) {
			next= con->next;
			if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
				remove_constraint(&pchan->constraints, con);
			}
		}
		pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
	}
	CTX_DATA_END;
	
	/* refresh depsgraph */
	DAG_id_tag_update(&ob->id, OB_RECALC_DATA);

	/* note, notifier might evolve */
	WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
	
	return OPERATOR_FINISHED;
}

void POSE_OT_ik_clear(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove IK";
	ot->description = "Remove all IK Constraints from selected bones";
	ot->idname = "POSE_OT_ik_clear";
	
	/* api callbacks */
	ot->exec = pose_ik_clear_exec;
	ot->poll = ED_operator_posemode;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}