constraints unique name length was set too long (could overrun the buffer), use sizeo...
[blender.git] / source / blender / blenkernel / intern / ipo.c
index 18a8210c68d294e5700e8237d3444c960ec86b8d..dbde6403226fc300603ccb6ee5ef6cdb07f229d2 100644 (file)
@@ -16,7 +16,7 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * 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.
@@ -295,10 +295,7 @@ static char *pchan_adrcodes_to_paths (int adrcode, int *array_index)
                        *array_index= 1; return "rotation_euler";
                case AC_EUL_Z:
                        *array_index= 2; return "rotation_euler";
-                       
-               case -1: /* special case for euler-rotations used by old drivers */
-                       *array_index= 0; return "rotation_euler";
-                       
+               
                case AC_LOC_X:
                        *array_index= 0; return "location";
                case AC_LOC_Y:
@@ -868,12 +865,15 @@ static char *get_rna_access (int blocktype, int adrcode, char actname[], char co
                
                case ID_SO: /* sound */
                        propname= sound_adrcodes_to_paths(adrcode, &dummy_index);
+                       break;
                
                case ID_WO: /* world */
                        propname= world_adrcodes_to_paths(adrcode, &dummy_index);
+                       break;
 
                case ID_PA: /* particle */
                        propname= particle_adrcodes_to_paths(adrcode, &dummy_index);
+                       break;
                        
                /* XXX problematic blocktypes */
                case ID_CU: /* curve */
@@ -885,6 +885,17 @@ static char *get_rna_access (int blocktype, int adrcode, char actname[], char co
                        
                case ID_SEQ: /* sequencer strip */
                        //SEQ_FAC1:
+                       switch (adrcode) {
+                       case SEQ_FAC1:
+                               propname= "effect_fader";
+                               break;
+                       case SEQ_FAC_SPEED:
+                               propname= "speed_fader";
+                               break;
+                       case SEQ_FAC_OPACITY:
+                               propname= "blend_opacity";
+                               break;
+                       }
                        //      poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
                        break;
                        
@@ -933,14 +944,17 @@ static char *get_rna_access (int blocktype, int adrcode, char actname[], char co
                strcpy(buf, ""); /* empty string */
        BLI_dynstr_append(path, buf);
        
-       /* append property to path (only if applicable) */
-       if (blocktype > 0) {
-               /* need to add dot before property if there was anything precceding this */
-               if (buf[0])
-                       BLI_dynstr_append(path, ".");
-               
-               /* now write name of property */
-               BLI_dynstr_append(path, propname);
+       /* need to add dot before property if there was anything precceding this */
+       if (buf[0])
+               BLI_dynstr_append(path, ".");
+       
+       /* now write name of property */
+       BLI_dynstr_append(path, propname);
+       
+       /* if there was no array index pointer provided, add it to the path */
+       if (array_index == NULL) {
+               sprintf(buf, "[\"%d\"]", dummy_index);
+               BLI_dynstr_append(path, buf);
        }
        
        /* convert to normal MEM_malloc'd string */
@@ -954,11 +968,40 @@ static char *get_rna_access (int blocktype, int adrcode, char actname[], char co
 /* *************************************************** */
 /* Conversion Utilities */
 
+/* Convert adrcodes to driver target transform channel types */
+static short adrcode_to_dtar_transchan (short adrcode)
+{
+       switch (adrcode) {
+               case OB_LOC_X:  
+                       return DTAR_TRANSCHAN_LOCX;
+               case OB_LOC_Y:
+                       return DTAR_TRANSCHAN_LOCY;
+               case OB_LOC_Z:
+                       return DTAR_TRANSCHAN_LOCZ;
+               
+               case OB_ROT_X:  
+                       return DTAR_TRANSCHAN_ROTX;
+               case OB_ROT_Y:
+                       return DTAR_TRANSCHAN_ROTY;
+               case OB_ROT_Z:
+                       return DTAR_TRANSCHAN_ROTZ;
+               
+               case OB_SIZE_X: 
+                       return DTAR_TRANSCHAN_SCALEX;
+               case OB_SIZE_Y:
+                       return DTAR_TRANSCHAN_SCALEX;
+               case OB_SIZE_Z:
+                       return DTAR_TRANSCHAN_SCALEX;
+                       
+               default:
+                       return 0;
+       }
+}
+
 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
 static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
 {
        ChannelDriver *cdriver;
-       DriverTarget *dtar=NULL, *dtar2=NULL;
        
        /* allocate memory for new driver */
        cdriver= MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
@@ -966,85 +1009,59 @@ static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
        /* if 'pydriver', just copy data across */
        if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
                /* PyDriver only requires the expression to be copied */
-               // TODO: but the expression will be useless...
+               // FIXME: expression will be useless due to API changes, but at least not totally lost
                cdriver->type = DRIVER_TYPE_PYTHON;
-               strcpy(cdriver->expression, idriver->name); // XXX is this safe? 
+               if (idriver->name[0])
+                       BLI_strncpy(cdriver->expression, idriver->name, sizeof(cdriver->expression));
        }
        else {
-               /* what to store depends on the 'blocktype' (ID_OB or ID_PO - object or posechannel) */
-               if (idriver->blocktype == ID_AR) {
-                       /* ID_PO */
+               DriverVar *dvar = NULL;
+               DriverTarget *dtar = NULL;
+               
+               /* this should be ok for all types here... */
+               cdriver->type= DRIVER_TYPE_AVERAGE;
+               
+               /* what to store depends on the 'blocktype' - object or posechannel */
+               if (idriver->blocktype == ID_AR) { /* PoseChannel */
                        if (idriver->adrcode == OB_ROT_DIFF) {
-                               /* Rotational Difference is a special type of driver now... */
-                               cdriver->type= DRIVER_TYPE_ROTDIFF;
+                               /* Rotational Difference requires a special type of variable */
+                               dvar= driver_add_new_variable(cdriver);
+                               driver_change_variable_type(dvar, DVAR_TYPE_ROT_DIFF);
                                
-                               /* make 2 driver targets */
-                               dtar= driver_add_new_target(cdriver);
-                               dtar2= driver_add_new_target(cdriver);
-                               
-                               /* driver must use bones from same armature... */
-                               dtar->id= dtar2->id= (ID *)idriver->ob;
+                                       /* first bone target */
+                               dtar= &dvar->targets[0];
+                               dtar->id= (ID *)idriver->ob;
+                               if (idriver->name[0])
+                                       BLI_strncpy(dtar->pchan_name, idriver->name, 32);
                                
-                               /* paths for the two targets get the pointers to the relevant Pose-Channels 
-                                *      - return pointers to Pose-Channels not rotation channels, as calculation code is picky
-                                *      - old bone names were stored in same var, in idriver->name
-                                *
-                                *      - we use several hacks here - blocktype == -1 specifies that no property needs to be found, and
-                                *        providing a name for 'actname' will automatically imply Pose-Channel with name 'actname'
-                                */
-                               dtar->rna_path= get_rna_access(-1, -1, idriver->name, NULL, NULL);
-                               dtar2->rna_path= get_rna_access(-1, -1, idriver->name+DRIVER_NAME_OFFS, NULL, NULL);
+                                       /* second bone target (name was stored in same var as the first one) */
+                               dtar= &dvar->targets[1];
+                               dtar->id= (ID *)idriver->ob;
+                               if (idriver->name[0]) // xxx... for safety
+                                       BLI_strncpy(dtar->pchan_name, idriver->name+DRIVER_NAME_OFFS, 32);
                        }
                        else {
-                               /* 'standard' driver */
-                               cdriver->type= DRIVER_TYPE_AVERAGE;
+                               /* only a single variable, of type 'transform channel' */
+                               dvar= driver_add_new_variable(cdriver);
+                               driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
                                
-                               /* make 1 driver target */
-                               dtar= driver_add_new_target(cdriver);
+                               /* only requires a single target */
+                               dtar= &dvar->targets[0];
                                dtar->id= (ID *)idriver->ob;
-                               
-                               switch (idriver->adrcode) {
-                                       case OB_LOC_X:  /* x,y,z location are quite straightforward */
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_LOC_X, idriver->name, NULL, &dtar->array_index);
-                                               break;
-                                       case OB_LOC_Y:
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Y, idriver->name, NULL, &dtar->array_index);
-                                               break;
-                                       case OB_LOC_Z:
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Z, idriver->name, NULL, &dtar->array_index);
-                                               break;
-                                               
-                                       case OB_SIZE_X: /* x,y,z scaling are also quite straightforward */
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_X, idriver->name, NULL, &dtar->array_index);
-                                               break;
-                                       case OB_SIZE_Y:
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Y, idriver->name, NULL, &dtar->array_index);
-                                               break;
-                                       case OB_SIZE_Z:
-                                               dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Z, idriver->name, NULL, &dtar->array_index);
-                                               break;  
-                                               
-                                       case OB_ROT_X:  /* rotation - we need to be careful with this... */     
-                                       case OB_ROT_Y:
-                                       case OB_ROT_Z:
-                                       {
-                                               /* -1 here, not rotation code, since old system didn't have eulers */
-                                               dtar->rna_path= get_rna_access(ID_PO, -1, idriver->name, NULL, NULL);
-                                               dtar->array_index= idriver->adrcode - OB_ROT_X;
-                                       }
-                                               break;
-                               }
+                               if (idriver->name[0])
+                                       BLI_strncpy(dtar->pchan_name, idriver->name, 32);
+                               dtar->transChan= adrcode_to_dtar_transchan(idriver->adrcode);
                        }
                }
-               else {
-                       /* ID_OB */
-                       cdriver->type= DRIVER_TYPE_AVERAGE;
-                       
-                       /* make 1 driver target */
-                       dtar= driver_add_new_target(cdriver);
+               else { /* Object */
+                       /* only a single variable, of type 'transform channel' */
+                       dvar= driver_add_new_variable(cdriver);
+                       driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
                        
+                               /* only requires single target */
+                       dtar= &dvar->targets[0];
                        dtar->id= (ID *)idriver->ob;
-                       dtar->rna_path= get_rna_access(ID_OB, idriver->adrcode, NULL, NULL, &dtar->array_index);
+                       dtar->transChan= adrcode_to_dtar_transchan(idriver->adrcode);
                }
        }
        
@@ -1055,7 +1072,7 @@ static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
 /* Add F-Curve to the correct list 
  *     - grpname is needed to be used as group name where relevant, and is usually derived from actname
  */
-static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, char *grpname)
+static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, char *grpname, int muteipo)
 {
        /* If we're adding to an action, we will have groups to write to... */
        if (groups && grpname) {
@@ -1082,10 +1099,12 @@ static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, c
                                agrp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
                                
                                agrp->flag = AGRP_SELECTED;
-                               BLI_snprintf(agrp->name, 64, grpname);
+                               if(muteipo) agrp->flag |= AGRP_MUTED;
+
+                               strncpy(agrp->name, grpname, sizeof(agrp->name));
                                
                                BLI_addtail(&tmp_act.groups, agrp);
-                               BLI_uniquename(&tmp_act.groups, agrp, "Group", '.', offsetof(bActionGroup, name), 64);
+                               BLI_uniquename(&tmp_act.groups, agrp, "Group", '.', offsetof(bActionGroup, name), sizeof(agrp->name));
                        }
                }
                
@@ -1093,6 +1112,9 @@ static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, c
                /* WARNING: this func should only need to look at the stuff we initialised, if not, things may crash */
                action_groups_add_channel(&tmp_act, agrp, fcu);
                
+               if(agrp->flag & AGRP_MUTED) /* flush down */
+                       fcu->flag |= FCURVE_MUTED;
+
                /* set the output lists based on the ones in the temp action */
                groups->first= tmp_act.groups.first;
                groups->last= tmp_act.groups.last;
@@ -1110,7 +1132,7 @@ static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, c
  *     actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
  *     constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
  */
-static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname)
+static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname, int muteipo)
 {
        AdrBit2Path *abp;
        FCurve *fcu;
@@ -1226,7 +1248,7 @@ static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, cha
                        }
                        
                        /* add new F-Curve to list */
-                       fcurve_add_to_list(groups, list, fcurve, actname);
+                       fcurve_add_to_list(groups, list, fcurve, actname, muteipo);
                }
        }
        else {
@@ -1261,8 +1283,10 @@ static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, cha
                                /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
                                dst->hide= BEZT_KEYTYPE_KEYFRAME;
                                        
-                               /* correct values for euler rotation curves - they were degrees/10 */
-                               // XXX for now, just make them into radians as RNA sets/reads directly in that form
+                               /* correct values for euler rotation curves 
+                                *      - they were degrees/10 
+                                *      - we need radians for RNA to do the right thing
+                                */
                                if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
                                         ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
                                {
@@ -1277,14 +1301,11 @@ static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, cha
                                 *      - need to go from degrees to radians...
                                 *      - there's only really 1 target to worry about 
                                 */
-                               if (fcu->driver && fcu->driver->targets.first) {
-                                       DriverTarget *dtar= fcu->driver->targets.first;
+                               if (fcu->driver && fcu->driver->variables.first) {
+                                       DriverVar *dvar= fcu->driver->variables.first;
+                                       DriverTarget *dtar= &dvar->targets[0];
                                        
-                                       /* since drivers could only be for objects, we should just check for 'rotation' being 
-                                        * in the name of the path given
-                                        *      - WARNING: this will break if we encounter a bone or object explictly named in that way...
-                                        */
-                                       if ((dtar && dtar->rna_path) && strstr(dtar->rna_path, "rotation")) {
+                                       if (ELEM3(dtar->transChan, DTAR_TRANSCHAN_ROTX, DTAR_TRANSCHAN_ROTY, DTAR_TRANSCHAN_ROTZ)) {
                                                const float fac= (float)M_PI / 180.0f;
                                                
                                                dst->vec[0][0] *= fac;
@@ -1301,7 +1322,7 @@ static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, cha
                }
                
                /* add new F-Curve to list */
-               fcurve_add_to_list(groups, list, fcu, actname);
+               fcurve_add_to_list(groups, list, fcu, actname, muteipo);
        }
 }
 
@@ -1342,7 +1363,7 @@ static void ipo_to_animato (Ipo *ipo, char actname[], char constname[], ListBase
                if (icu->driver) {
                        /* Blender 2.4x allowed empty drivers, but we don't now, since they cause more trouble than they're worth */
                        if ((icu->driver->ob) || (icu->driver->type == IPO_DRIVER_TYPE_PYTHON)) {
-                               icu_to_fcurves(NULL, drivers, icu, actname, constname);
+                               icu_to_fcurves(NULL, drivers, icu, actname, constname, ipo->muteipo);
                        }
                        else {
                                MEM_freeN(icu->driver);
@@ -1350,7 +1371,7 @@ static void ipo_to_animato (Ipo *ipo, char actname[], char constname[], ListBase
                        }
                }
                else
-                       icu_to_fcurves(animgroups, anim, icu, actname, constname);
+                       icu_to_fcurves(animgroups, anim, icu, actname, constname, ipo->muteipo);
        }
        
        /* if this IPO block doesn't have any users after this one, free... */
@@ -1604,6 +1625,7 @@ void do_versions_ipos_to_animato(Main *main)
        ListBase drivers = {NULL, NULL};
        ID *id;
        AnimData *adt;
+       Scene *scene;
        
        if (main == NULL) {
                printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
@@ -1781,6 +1803,69 @@ void do_versions_ipos_to_animato(Main *main)
                }
        }
        
+       /* worlds */
+       for (id= main->world.first; id; id= id->next) {
+               World *wo= (World *)id;
+               
+               if (G.f & G_DEBUG) printf("\tconverting world %s \n", id->name+2);
+               
+               /* we're only interested in the IPO */
+               if (wo->ipo) {
+                       /* Add AnimData block */
+                       adt= BKE_id_add_animdata(id);
+                       
+                       /* Convert World data... */
+                       ipo_to_animdata(id, wo->ipo, NULL, NULL);
+                       wo->ipo->id.us--;
+                       wo->ipo= NULL;
+               }
+       }
+       
+       /* sequence strips */
+       for(scene = main->scene.first; scene; scene = scene->id.next) {
+               if(scene->ed && scene->ed->seqbasep) {
+                       Sequence * seq;
+                       
+                       for(seq = scene->ed->seqbasep->first; 
+                           seq; seq = seq->next) {
+                               short adrcode = SEQ_FAC1;
+                               
+                               if (G.f & G_DEBUG) 
+                                       printf("\tconverting sequence strip %s \n", seq->name+2);
+                               
+                               if (!seq->ipo || !seq->ipo->curve.first) {
+                                       seq->flag |= 
+                                               SEQ_USE_EFFECT_DEFAULT_FADE;
+                                       continue;
+                               }
+                               
+                               /* patch adrcode, so that we can map
+                                  to different DNA variables later 
+                                  (semi-hack (tm) )
+                               */
+                               switch(seq->type) {
+                               case SEQ_IMAGE:
+                               case SEQ_META:
+                               case SEQ_SCENE:
+                               case SEQ_MOVIE:
+                               case SEQ_COLOR:
+                                       adrcode = SEQ_FAC_OPACITY;
+                                       break;
+                               case SEQ_SPEED:
+                                       adrcode = SEQ_FAC_SPEED;
+                                       break;
+                               }
+                               ((IpoCurve*) seq->ipo->curve.first)
+                                       ->adrcode = adrcode;
+                               ipo_to_animdata((ID*) seq, seq->ipo,
+                                               NULL, NULL);
+                               seq->ipo->id.us--;
+                               seq->ipo = NULL;
+                       }
+               }
+       }
+
+
        /* textures */
        for (id= main->tex.first; id; id= id->next) {
                Tex *te= (Tex *)id;