fix for adding drivers to constraints via python.
authorCampbell Barton <ideasman42@gmail.com>
Thu, 26 Nov 2009 09:48:53 +0000 (09:48 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 26 Nov 2009 09:48:53 +0000 (09:48 +0000)
the RNA constraint api was checking the current context when getting the constraint driver path and renaming constraints.
this made scripts not work properly so changed this to search for the constraint pose channel user within the object (if the object its self is not the user).

source/blender/editors/include/ED_object.h
source/blender/editors/object/object_constraint.c
source/blender/makesrna/intern/rna_constraint.c

index 1785b0303cb054c84c3d02218569de199120da8e..9842dc1ef76704e1ebc31767064aea629f59343e 100644 (file)
@@ -92,6 +92,7 @@ int object_data_is_libdata(struct Object *ob);
 
 /* constraints */
 struct ListBase *get_active_constraints(struct Object *ob);
+struct ListBase *get_constraint_lb(struct Object *ob, struct bConstraint *con, struct bPoseChannel **pchan_r);
 struct bConstraint *get_active_constraint(struct Object *ob);
 
 void object_test_constraints(struct Object *ob);
index 6d4706d02b3774daebfc0e3e72d7fc8affb41c97..eababfe80809a2681ec04c1a3efe098511ce5766 100644 (file)
@@ -98,6 +98,33 @@ ListBase *get_active_constraints (Object *ob)
        return NULL;
 }
 
+ListBase *get_constraint_lb (Object *ob, bConstraint *con, bPoseChannel **pchan_r)
+{
+       if(pchan_r)
+               *pchan_r= NULL;
+
+       if (ELEM(NULL, ob, con))
+               return NULL;
+
+       if((BLI_findindex(&ob->constraints, con) != -1)) {
+               return &ob->constraints;
+       }
+       else if(ob->pose) {
+               bPoseChannel *pchan;
+               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;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
 /* single constraint */
 bConstraint *get_active_constraint (Object *ob)
 {
index dbe5db3967788408b00c42602b9425a896c1a791..12b26f8699e6243fab0fc3516e8b18f51af7450d 100644 (file)
@@ -92,6 +92,8 @@ EnumPropertyItem constraint_ik_axisref_items[] ={
 
 #ifdef RNA_RUNTIME
 
+#include <stdio.h>
+
 #include "BKE_animsys.h"
 #include "BKE_action.h"
 #include "BKE_constraint.h"
@@ -168,7 +170,7 @@ static void rna_Constraint_name_set(PointerRNA *ptr, const char *value)
        /* make sure name is unique */
        if (ptr->id.data) {
                Object *ob= ptr->id.data;
-               ListBase *list= get_active_constraints(ob);
+               ListBase *list = get_constraint_lb(ob, con, NULL);
                
                /* if we have the list, check for unique name, otherwise give up */
                if (list)
@@ -183,19 +185,17 @@ static char *rna_Constraint_path(PointerRNA *ptr)
 {
        Object *ob= ptr->id.data;
        bConstraint *con= ptr->data;
-       bPoseChannel *pchan= get_active_posechannel(ob);
-       ListBase *actlist= get_active_constraints(ob);
-       short inList = 0;
-       
-       /* check if constraint is in the given list */
-       if (actlist)
-               inList= (BLI_findindex(actlist, con) != -1);
-       
-       /* if constraint is in the list, the list is for the active bone... */
-       if ((inList) && (actlist != &ob->constraints) && (pchan))
+       bPoseChannel *pchan;
+       ListBase *lb = get_constraint_lb(ob, con, &pchan);
+
+       if(lb == NULL)
+               printf("rna_Constraint_path: internal error, constraint '%s' not found in object '%s'\n", con->name, ob->id.name);
+
+       if(pchan) {
                return BLI_sprintfN("pose.bones[\"%s\"].constraints[\"%s\"]", pchan->name, con->name);
-       else
-               return BLI_sprintfN("constraints[\"%s\"]", con->name);
+       }
+       
+       return BLI_sprintfN("constraints[\"%s\"]", con->name);
 }
 
 static void rna_Constraint_update(bContext *C, PointerRNA *ptr)