This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/editors/object/object_shapekey.c

429 lines
9.9 KiB
C
Raw Normal View History

/*
* $Id$
*
* ***** 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,
2010-02-12 13:34:04 +00:00
* 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.
*
* Contributor(s): Blender Foundation, shapekey support
*
* ***** END GPL LICENSE BLOCK *****
*/
2011-02-27 20:29:51 +00:00
/** \file blender/editors/object/object_shapekey.c
* \ingroup edobj
*/
#include <math.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
#endif
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "DNA_curve_types.h"
#include "DNA_key_types.h"
#include "DNA_lattice_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "BKE_context.h"
#include "BKE_depsgraph.h"
#include "BKE_key.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_curve.h"
#include "BLO_sys_types.h" // for intptr_t support
#include "ED_mesh.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
#include "object_intern.h"
/*********************** add shape key ***********************/
static void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob, int from_mix)
{
if(object_insert_shape_key(scene, ob, NULL, from_mix)) {
Key *key= ob_get_key(ob);
ob->shapenr= BLI_countlist(&key->block);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
}
}
/*********************** remove shape key ***********************/
static int ED_object_shape_key_remove(bContext *C, Object *ob)
{
Main *bmain= CTX_data_main(C);
KeyBlock *kb, *rkb;
Key *key;
2.5: Blender "Animato" - New Animation System Finally, here is the basic (functional) prototype of the new animation system which will allow for the infamous "everything is animatable", and which also addresses several of the more serious shortcomings of the old system. Unfortunately, this will break old animation files (especially right now, as I haven't written the version patching code yet), however, this is for the future. Highlights of the new system: * Scrapped IPO-Curves/IPO/(Action+Constraint-Channels)/Action system, and replaced it with F-Curve/Action. - F-Curves (animators from other packages will feel at home with this name) replace IPO-Curves. - The 'new' Actions, act as the containers for F-Curves, so that they can be reused. They are therefore more akin to the old 'IPO' blocks, except they do not have the blocktype restriction, so you can store materials/texture/geometry F-Curves in the same Action as Object transforms, etc. * F-Curves use RNA-paths for Data Access, hence allowing "every" (where sensible/editable that is) user-accessible setting from RNA to be animated. * Drivers are no longer mixed with Animation Data, so rigs will not be that easily broken and several dependency problems can be eliminated. (NOTE: drivers haven't been hooked up yet, but the code is in place) * F-Curve modifier system allows useful 'large-scale' manipulation of F-Curve values, including (I've only included implemented ones here): envelope deform (similar to lattices to allow broad-scale reshaping of curves), curve generator (polynomial or py-expression), cycles (replacing the old cyclic extrapolation modes, giving more control over this). (NOTE: currently this cannot be tested, as there's not access to them, but the code is all in place) * NLA system with 'tracks' (i.e. layers), and multiple strips per track. (NOTE: NLA system is not yet functional, as it's only partially coded still) There are more nice things that I will be preparing some nice docs for soon, but for now, check for more details: http://lists.blender.org/pipermail/bf-taskforce25/2009-January/000260.html So, what currently works: * I've implemented two basic operators for the 3D-view only to Insert and Delete Keyframes. These are tempolary ones only that will be replaced in due course with 'proper' code. * Object Loc/Rot/Scale can be keyframed. Also, the colour of the 'active' material (Note: this should really be for nth material instead, but that doesn't work yet in RNA) can also be keyframed into the same datablock. * Standard animation refresh (i.e. animation resulting from NLA and Action evaluation) is now done completely separate from drivers before anything else is done after a frame change. Drivers are handled after this in a separate pass, as dictated by depsgraph flags, etc. Notes: * Drivers haven't been hooked up yet * Only objects and data directly linked to objects can be animated. * Depsgraph will need further tweaks. Currently, I've only made sure that it will update some things in the most basic cases (i.e. frame change). * Animation Editors are currently broken (in terms of editing stuff). This will be my next target (priority to get Dopesheet working first, then F-Curve editor - i.e. old IPO Editor) * I've had to put in large chunks of XXX sandboxing for old animation system code all around the place. This will be cleaned up in due course, as some places need special review. In particular, the particles and sequencer code have far too many manual calls to calculate + flush animation info, which is really bad (this is a 'please explain yourselves' call to Physics coders!).
2009-01-17 03:12:50 +00:00
//IpoCurve *icu;
key= ob_get_key(ob);
if(key==NULL)
return 0;
kb= BLI_findlink(&key->block, ob->shapenr-1);
if(kb) {
for(rkb= key->block.first; rkb; rkb= rkb->next)
if(rkb->relative == ob->shapenr-1)
rkb->relative= 0;
BLI_remlink(&key->block, kb);
key->totkey--;
if(key->refkey== kb) {
key->refkey= key->block.first;
if(key->refkey) {
/* apply new basis key on original data */
switch(ob->type) {
case OB_MESH:
key_to_mesh(key->refkey, ob->data);
break;
case OB_CURVE:
case OB_SURF:
key_to_curve(key->refkey, ob->data, BKE_curve_nurbs(ob->data));
break;
case OB_LATTICE:
key_to_latt(key->refkey, ob->data);
break;
}
}
}
if(kb->data) MEM_freeN(kb->data);
MEM_freeN(kb);
for(kb= key->block.first; kb; kb= kb->next)
if(kb->adrcode>=ob->shapenr)
kb->adrcode--;
2.5: Blender "Animato" - New Animation System Finally, here is the basic (functional) prototype of the new animation system which will allow for the infamous "everything is animatable", and which also addresses several of the more serious shortcomings of the old system. Unfortunately, this will break old animation files (especially right now, as I haven't written the version patching code yet), however, this is for the future. Highlights of the new system: * Scrapped IPO-Curves/IPO/(Action+Constraint-Channels)/Action system, and replaced it with F-Curve/Action. - F-Curves (animators from other packages will feel at home with this name) replace IPO-Curves. - The 'new' Actions, act as the containers for F-Curves, so that they can be reused. They are therefore more akin to the old 'IPO' blocks, except they do not have the blocktype restriction, so you can store materials/texture/geometry F-Curves in the same Action as Object transforms, etc. * F-Curves use RNA-paths for Data Access, hence allowing "every" (where sensible/editable that is) user-accessible setting from RNA to be animated. * Drivers are no longer mixed with Animation Data, so rigs will not be that easily broken and several dependency problems can be eliminated. (NOTE: drivers haven't been hooked up yet, but the code is in place) * F-Curve modifier system allows useful 'large-scale' manipulation of F-Curve values, including (I've only included implemented ones here): envelope deform (similar to lattices to allow broad-scale reshaping of curves), curve generator (polynomial or py-expression), cycles (replacing the old cyclic extrapolation modes, giving more control over this). (NOTE: currently this cannot be tested, as there's not access to them, but the code is all in place) * NLA system with 'tracks' (i.e. layers), and multiple strips per track. (NOTE: NLA system is not yet functional, as it's only partially coded still) There are more nice things that I will be preparing some nice docs for soon, but for now, check for more details: http://lists.blender.org/pipermail/bf-taskforce25/2009-January/000260.html So, what currently works: * I've implemented two basic operators for the 3D-view only to Insert and Delete Keyframes. These are tempolary ones only that will be replaced in due course with 'proper' code. * Object Loc/Rot/Scale can be keyframed. Also, the colour of the 'active' material (Note: this should really be for nth material instead, but that doesn't work yet in RNA) can also be keyframed into the same datablock. * Standard animation refresh (i.e. animation resulting from NLA and Action evaluation) is now done completely separate from drivers before anything else is done after a frame change. Drivers are handled after this in a separate pass, as dictated by depsgraph flags, etc. Notes: * Drivers haven't been hooked up yet * Only objects and data directly linked to objects can be animated. * Depsgraph will need further tweaks. Currently, I've only made sure that it will update some things in the most basic cases (i.e. frame change). * Animation Editors are currently broken (in terms of editing stuff). This will be my next target (priority to get Dopesheet working first, then F-Curve editor - i.e. old IPO Editor) * I've had to put in large chunks of XXX sandboxing for old animation system code all around the place. This will be cleaned up in due course, as some places need special review. In particular, the particles and sequencer code have far too many manual calls to calculate + flush animation info, which is really bad (this is a 'please explain yourselves' call to Physics coders!).
2009-01-17 03:12:50 +00:00
#if 0 // XXX old animation system
if(key->ipo) {
for(icu= key->ipo->curve.first; icu; icu= icu->next) {
if(icu->adrcode==ob->shapenr-1) {
BLI_remlink(&key->ipo->curve, icu);
free_ipo_curve(icu);
break;
}
}
for(icu= key->ipo->curve.first; icu; icu= icu->next)
if(icu->adrcode>=ob->shapenr)
icu->adrcode--;
2.5: Blender "Animato" - New Animation System Finally, here is the basic (functional) prototype of the new animation system which will allow for the infamous "everything is animatable", and which also addresses several of the more serious shortcomings of the old system. Unfortunately, this will break old animation files (especially right now, as I haven't written the version patching code yet), however, this is for the future. Highlights of the new system: * Scrapped IPO-Curves/IPO/(Action+Constraint-Channels)/Action system, and replaced it with F-Curve/Action. - F-Curves (animators from other packages will feel at home with this name) replace IPO-Curves. - The 'new' Actions, act as the containers for F-Curves, so that they can be reused. They are therefore more akin to the old 'IPO' blocks, except they do not have the blocktype restriction, so you can store materials/texture/geometry F-Curves in the same Action as Object transforms, etc. * F-Curves use RNA-paths for Data Access, hence allowing "every" (where sensible/editable that is) user-accessible setting from RNA to be animated. * Drivers are no longer mixed with Animation Data, so rigs will not be that easily broken and several dependency problems can be eliminated. (NOTE: drivers haven't been hooked up yet, but the code is in place) * F-Curve modifier system allows useful 'large-scale' manipulation of F-Curve values, including (I've only included implemented ones here): envelope deform (similar to lattices to allow broad-scale reshaping of curves), curve generator (polynomial or py-expression), cycles (replacing the old cyclic extrapolation modes, giving more control over this). (NOTE: currently this cannot be tested, as there's not access to them, but the code is all in place) * NLA system with 'tracks' (i.e. layers), and multiple strips per track. (NOTE: NLA system is not yet functional, as it's only partially coded still) There are more nice things that I will be preparing some nice docs for soon, but for now, check for more details: http://lists.blender.org/pipermail/bf-taskforce25/2009-January/000260.html So, what currently works: * I've implemented two basic operators for the 3D-view only to Insert and Delete Keyframes. These are tempolary ones only that will be replaced in due course with 'proper' code. * Object Loc/Rot/Scale can be keyframed. Also, the colour of the 'active' material (Note: this should really be for nth material instead, but that doesn't work yet in RNA) can also be keyframed into the same datablock. * Standard animation refresh (i.e. animation resulting from NLA and Action evaluation) is now done completely separate from drivers before anything else is done after a frame change. Drivers are handled after this in a separate pass, as dictated by depsgraph flags, etc. Notes: * Drivers haven't been hooked up yet * Only objects and data directly linked to objects can be animated. * Depsgraph will need further tweaks. Currently, I've only made sure that it will update some things in the most basic cases (i.e. frame change). * Animation Editors are currently broken (in terms of editing stuff). This will be my next target (priority to get Dopesheet working first, then F-Curve editor - i.e. old IPO Editor) * I've had to put in large chunks of XXX sandboxing for old animation system code all around the place. This will be cleaned up in due course, as some places need special review. In particular, the particles and sequencer code have far too many manual calls to calculate + flush animation info, which is really bad (this is a 'please explain yourselves' call to Physics coders!).
2009-01-17 03:12:50 +00:00
}
#endif // XXX old animation system
if(ob->shapenr>1) ob->shapenr--;
}
if(key->totkey==0) {
if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
free_libblock_us(&(bmain->key), key);
}
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return 1;
}
static int object_shape_key_mirror(bContext *C, Object *ob)
{
KeyBlock *kb;
Key *key;
key= ob_get_key(ob);
if(key==NULL)
return 0;
kb= BLI_findlink(&key->block, ob->shapenr-1);
if(kb) {
int i1, i2;
float *fp1, *fp2;
float tvec[3];
char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
if(ob->type==OB_MESH) {
Mesh *me= ob->data;
MVert *mv;
mesh_octree_table(ob, NULL, NULL, 's');
for(i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
i2= mesh_get_x_mirror_vert(ob, i1);
if(i2==i1) {
fp1= ((float *)kb->data) + i1*3;
fp1[0] = -fp1[0];
tag_elem[i1]= 1;
}
else if(i2 != -1) {
if(tag_elem[i1]==0 && tag_elem[i2]==0) {
fp1= ((float *)kb->data) + i1*3;
fp2= ((float *)kb->data) + i2*3;
VECCOPY(tvec, fp1);
VECCOPY(fp1, fp2);
VECCOPY(fp2, tvec);
/* flip x axis */
fp1[0] = -fp1[0];
fp2[0] = -fp2[0];
}
tag_elem[i1]= tag_elem[i2]= 1;
}
}
mesh_octree_table(ob, NULL, NULL, 'e');
}
/* todo, other types? */
MEM_freeN(tag_elem);
}
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return 1;
}
/********************** shape key operators *********************/
static int shape_key_mode_poll(bContext *C)
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
ID *data= (ob)? ob->data: NULL;
return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
}
static int shape_key_poll(bContext *C)
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
ID *data= (ob)? ob->data: NULL;
return (ob && !ob->id.lib && data && !data->lib);
}
static int shape_key_add_exec(bContext *C, wmOperator *op)
{
Scene *scene= CTX_data_scene(C);
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
int from_mix = RNA_boolean_get(op->ptr, "from_mix");
ED_object_shape_key_add(C, scene, ob, from_mix);
return OPERATOR_FINISHED;
}
void OBJECT_OT_shape_key_add(wmOperatorType *ot)
{
/* identifiers */
ot->name= "Add Shape Key";
ot->idname= "OBJECT_OT_shape_key_add";
ot->description= "Add shape key to the object";
/* api callbacks */
ot->poll= shape_key_mode_poll;
ot->exec= shape_key_add_exec;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
/* properties */
RNA_def_boolean(ot->srna, "from_mix", 1, "From Mix", "Create the new shape key from the existing mix of keys.");
}
static int shape_key_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
if(!ED_object_shape_key_remove(C, ob))
return OPERATOR_CANCELLED;
return OPERATOR_FINISHED;
}
void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name= "Remove Shape Key";
ot->idname= "OBJECT_OT_shape_key_remove";
ot->description= "Remove shape key from the object";
/* api callbacks */
ot->poll= shape_key_mode_poll;
ot->exec= shape_key_remove_exec;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
Key *key= ob_get_key(ob);
KeyBlock *kb= ob_get_keyblock(ob);
if(!key || !kb)
return OPERATOR_CANCELLED;
for(kb=key->block.first; kb; kb=kb->next)
kb->curval= 0.0f;
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
{
/* identifiers */
ot->name= "Clear Shape Keys";
ot->description= "Clear weights for all shape keys";
ot->idname= "OBJECT_OT_shape_key_clear";
/* api callbacks */
ot->poll= shape_key_poll;
ot->exec= shape_key_clear_exec;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int shape_key_mirror_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
if(!object_shape_key_mirror(C, ob))
return OPERATOR_CANCELLED;
return OPERATOR_FINISHED;
}
void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
{
/* identifiers */
ot->name= "Mirror Shape Key";
ot->idname= "OBJECT_OT_shape_key_mirror";
/* api callbacks */
ot->poll= shape_key_mode_poll;
ot->exec= shape_key_mirror_exec;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int shape_key_move_exec(bContext *C, wmOperator *op)
{
Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
int type= RNA_enum_get(op->ptr, "type");
Key *key= ob_get_key(ob);
if(key) {
KeyBlock *kb, *kb_other;
int shapenr_act= ob->shapenr-1;
int shapenr_swap= shapenr_act + type;
kb= BLI_findlink(&key->block, shapenr_act);
if((type==-1 && kb->prev==NULL) || (type==1 && kb->next==NULL)) {
return OPERATOR_CANCELLED;
}
for(kb_other= key->block.first; kb_other; kb_other= kb_other->next) {
if(kb_other->relative == shapenr_act) {
kb_other->relative += type;
}
else if(kb_other->relative == shapenr_swap) {
kb_other->relative -= type;
}
}
if(type==-1) {
/* move back */
kb_other= kb->prev;
BLI_remlink(&key->block, kb);
BLI_insertlinkbefore(&key->block, kb_other, kb);
ob->shapenr--;
}
else {
/* move next */
kb_other= kb->next;
BLI_remlink(&key->block, kb);
BLI_insertlinkafter(&key->block, kb_other, kb);
ob->shapenr++;
}
}
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_shape_key_move(wmOperatorType *ot)
{
static EnumPropertyItem slot_move[] = {
{-1, "UP", 0, "Up", ""},
{1, "DOWN", 0, "Down", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name= "Move Shape Key";
ot->idname= "OBJECT_OT_shape_key_move";
/* api callbacks */
ot->poll= shape_key_mode_poll;
ot->exec= shape_key_move_exec;
/* flags */
ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
}