2009-02-21 10:38:58 +00:00
/**
* $ 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 ,
* Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* The Original Code is Copyright ( C ) 2009 Blender Foundation .
* All rights reserved .
*
*
* Contributor ( s ) : Blender Foundation , Joshua Leung
*
* * * * * * END GPL LICENSE BLOCK * * * * *
*/
# include <string.h>
# include <stdio.h>
# include <math.h>
# include <float.h>
# include "DNA_anim_types.h"
# include "DNA_action_types.h"
# include "DNA_object_types.h"
# include "DNA_space_types.h"
# include "DNA_scene_types.h"
# include "DNA_screen_types.h"
# include "DNA_userdef_types.h"
# include "MEM_guardedalloc.h"
# include "BLI_arithb.h"
# include "BLI_blenlib.h"
# include "BLI_editVert.h"
# include "BLI_rand.h"
# include "BKE_action.h"
2009-09-16 19:36:17 +00:00
# include "BKE_animsys.h"
2009-02-21 10:38:58 +00:00
# include "BKE_context.h"
# include "BKE_curve.h"
# include "BKE_customdata.h"
# include "BKE_depsgraph.h"
2009-03-15 10:39:02 +00:00
# include "BKE_fcurve.h"
2009-09-16 19:36:17 +00:00
# include "BKE_library.h"
# include "BKE_main.h"
2009-02-21 10:38:58 +00:00
# include "BKE_object.h"
# include "BKE_scene.h"
# include "BKE_screen.h"
# include "BKE_utildefines.h"
# include "BIF_gl.h"
# include "WM_api.h"
# include "WM_types.h"
# include "RNA_access.h"
# include "RNA_define.h"
# include "ED_anim_api.h"
# include "ED_keyframing.h"
# include "ED_screen.h"
# include "ED_types.h"
# include "ED_util.h"
# include "UI_interface.h"
# include "UI_resources.h"
# include "UI_view2d.h"
# include "graph_intern.h" // own include
2009-04-19 12:26:31 +00:00
/* XXX */
/* temporary definition for limits of float number buttons (FLT_MAX tends to infinity with old system) */
# define UI_FLT_MAX 10000.0f
2009-02-21 10:38:58 +00:00
2009-02-22 09:30:18 +00:00
/* ******************* graph editor space & buttons ************** */
2009-02-21 10:38:58 +00:00
# define B_NOP 1
# define B_REDR 2
2009-02-22 09:30:18 +00:00
/* -------------- */
2009-02-21 10:38:58 +00:00
static void do_graph_region_buttons ( bContext * C , void * arg , int event )
{
//Scene *scene= CTX_data_scene(C);
switch ( event ) {
}
/* default for now */
//WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
}
2009-05-19 17:13:33 +00:00
static int graph_panel_context ( const bContext * C , bAnimListElem * * ale , FCurve * * fcu )
2009-02-21 10:38:58 +00:00
{
2009-05-19 17:13:33 +00:00
bAnimContext ac ;
bAnimListElem * elem = NULL ;
/* for now, only draw if we could init the anim-context info (necessary for all animation-related tools)
* to work correctly is able to be correctly retrieved . There ' s no point showing empty panels ?
*/
if ( ANIM_animdata_get_context ( C , & ac ) = = 0 )
return 0 ;
/* try to find 'active' F-Curve */
elem = get_active_fcurve_channel ( & ac ) ;
if ( elem = = NULL )
return 0 ;
if ( fcu )
* fcu = ( FCurve * ) elem - > data ;
if ( ale )
* ale = elem ;
else
MEM_freeN ( elem ) ;
return 1 ;
}
static int graph_panel_poll ( const bContext * C , PanelType * pt )
{
return graph_panel_context ( C , NULL , NULL ) ;
}
static void graph_panel_properties ( const bContext * C , Panel * pa )
{
bAnimListElem * ale ;
FCurve * fcu ;
2009-02-21 10:38:58 +00:00
uiBlock * block ;
2009-02-21 11:22:06 +00:00
char name [ 128 ] ;
2009-02-21 10:38:58 +00:00
2009-05-19 17:13:33 +00:00
if ( ! graph_panel_context ( C , & ale , & fcu ) )
return ;
2009-02-21 10:38:58 +00:00
2009-10-09 10:45:11 +00:00
block = uiLayoutAbsoluteBlock ( pa - > layout ) ;
2009-05-19 17:13:33 +00:00
uiBlockSetHandleFunc ( block , do_graph_region_buttons , NULL ) ;
2009-02-21 10:38:58 +00:00
2009-02-22 09:30:18 +00:00
/* Info - Active F-Curve */
2009-02-21 11:22:06 +00:00
uiDefBut ( block , LABEL , 1 , " Active F-Curve: " , 10 , 200 , 150 , 19 , NULL , 0.0 , 0.0 , 0 , 0 , " " ) ;
2009-02-22 09:30:18 +00:00
if ( ale - > id ) {
// icon of active blocktype - is this really necessary?
int icon = geticon_anim_blocktype ( GS ( ale - > id - > name ) ) ;
// xxx type of icon-but is currently "LABEL", as that one is plain...
uiDefIconBut ( block , LABEL , 1 , icon , 10 , 180 , 20 , 19 , NULL , 0 , 0 , 0 , 0 , " ID-type that F-Curve belongs to " ) ;
}
getname_anim_fcurve ( name , ale - > id , fcu ) ;
2009-08-14 11:09:19 +00:00
uiDefBut ( block , LABEL , 1 , name , 40 , 180 , 300 , 19 , NULL , 0.0 , 0.0 , 0 , 0 , " Name of Active F-Curve " ) ;
2009-02-21 11:22:06 +00:00
2009-02-22 09:30:18 +00:00
/* TODO: the following settings could be added here
* - F - Curve coloring mode - mode selector + color selector
* - Access details ( ID - block + RNA - Path + Array Index )
* - . . .
*/
2009-05-19 17:13:33 +00:00
MEM_freeN ( ale ) ;
2009-02-21 10:38:58 +00:00
}
2009-03-21 03:49:22 +00:00
/* ******************* drivers ******************************** */
2009-02-22 09:30:18 +00:00
# define B_IPO_DEPCHANGE 10
static void do_graph_region_driver_buttons ( bContext * C , void * arg , int event )
{
Scene * scene = CTX_data_scene ( C ) ;
2009-04-11 00:29:21 +00:00
switch ( event ) {
2009-02-22 09:30:18 +00:00
case B_IPO_DEPCHANGE :
{
/* rebuild depsgraph for the new deps */
DAG_scene_sort ( scene ) ;
2009-04-13 00:46:32 +00:00
/* force an update of depsgraph */
ED_anim_dag_flush_update ( C ) ;
2009-02-22 09:30:18 +00:00
}
break ;
}
/* default for now */
2009-04-17 02:13:35 +00:00
WM_event_add_notifier ( C , NC_SCENE , scene ) ;
2009-02-22 09:30:18 +00:00
}
2009-04-11 06:48:39 +00:00
/* callback to remove the active driver */
static void driver_remove_cb ( bContext * C , void * ale_v , void * dummy_v )
{
bAnimListElem * ale = ( bAnimListElem * ) ale_v ;
ID * id = ale - > id ;
FCurve * fcu = ale - > data ;
/* try to get F-Curve that driver lives on, and ID block which has this AnimData */
if ( ELEM ( NULL , id , fcu ) )
return ;
/* call API method to remove this driver */
ANIM_remove_driver ( id , fcu - > rna_path , fcu - > array_index , 0 ) ;
}
2009-04-16 12:38:44 +00:00
/* callback to add a target variable to the active driver */
static void driver_add_var_cb ( bContext * C , void * driver_v , void * dummy_v )
{
ChannelDriver * driver = ( ChannelDriver * ) driver_v ;
/* add a new var */
driver_add_new_target ( driver ) ;
}
/* callback to remove target variable from active driver */
static void driver_delete_var_cb ( bContext * C , void * driver_v , void * dtar_v )
{
ChannelDriver * driver = ( ChannelDriver * ) driver_v ;
DriverTarget * dtar = ( DriverTarget * ) dtar_v ;
/* add a new var */
driver_free_target ( driver , dtar ) ;
}
2009-04-11 06:48:39 +00:00
/* callback to reset the driver's flags */
static void driver_update_flags_cb ( bContext * C , void * fcu_v , void * dummy_v )
{
FCurve * fcu = ( FCurve * ) fcu_v ;
ChannelDriver * driver = fcu - > driver ;
/* clear invalid flags */
driver - > flag & = ~ DRIVER_FLAG_INVALID ;
}
2009-05-19 17:13:33 +00:00
/* drivers panel poll */
static int graph_panel_drivers_poll ( const bContext * C , PanelType * pt )
{
2009-07-28 16:33:02 +00:00
SpaceIpo * sipo = CTX_wm_space_graph ( C ) ;
2009-05-19 17:13:33 +00:00
if ( sipo - > mode ! = SIPO_MODE_DRIVERS )
return 0 ;
return graph_panel_context ( C , NULL , NULL ) ;
}
Animato - Drivers with Multiple Targets:
Drivers now support multiple targets which act as 'variables'. The targets have a short 'name' (see later), and reference some property (in much the same way as F-Curves do, using RNA-Paths) which acts as the 'value'.
These named variables can then be used in a Python Expression which relates them to each other for more fine-grained control over the result of the driver. By using only the names of these variables in the expressions, we are able to define expressions/relationships in a much more readable way, as data access is separated from data use. This makes the underlying relationships easier to understand.
By default, if no Python Expression is given, the variables are simply averaged together, so old files won't break. :)
For example, check the following diagram (thanks Cessen/Nathan V from Peach team):
http://download.blender.org/ftp/incoming/250_drivers_mockup_cessen.png
TODO List:
* Depsgraph building for new driver relationships doesn't work yet. This needs to be recoded again, but this new system makes this much easier, since the targets are clearly defined (i.e. no need to parse py expressions to get list of objects)
* Graph Editor interface for editing these needs to be rewritten
* Python function for evaluating these expressions is needed (Campbell?)
2009-04-16 07:37:06 +00:00
2009-09-16 19:36:17 +00:00
static void test_obpoin_but ( struct bContext * C , char * name , ID * * idpp )
{
ID * id ;
id = CTX_data_main ( C ) - > object . first ;
while ( id ) {
if ( strcmp ( name , id - > name + 2 ) = = 0 ) {
* idpp = id ;
id_lib_extern ( id ) ; /* checks lib data, sets correct flag for saving then */
return ;
}
id = id - > next ;
}
* idpp = NULL ;
}
2009-05-19 17:13:33 +00:00
/* driver settings for active F-Curve (only for 'Drivers' mode) */
static void graph_panel_drivers ( const bContext * C , Panel * pa )
2009-02-22 09:30:18 +00:00
{
2009-05-19 17:13:33 +00:00
bAnimListElem * ale ;
FCurve * fcu ;
ChannelDriver * driver ;
2009-04-16 12:38:44 +00:00
DriverTarget * dtar ;
PointerRNA rna_ptr ;
2009-02-22 09:30:18 +00:00
uiBlock * block ;
uiBut * but ;
2009-04-16 12:38:44 +00:00
int yco = 85 , i = 0 ;
2009-02-22 09:30:18 +00:00
2009-05-19 17:13:33 +00:00
if ( ! graph_panel_context ( C , & ale , & fcu ) )
return ;
2009-02-22 09:30:18 +00:00
2009-05-19 17:13:33 +00:00
driver = fcu - > driver ;
2009-10-09 10:45:11 +00:00
block = uiLayoutAbsoluteBlock ( pa - > layout ) ;
2009-05-19 17:13:33 +00:00
uiBlockSetHandleFunc ( block , do_graph_region_driver_buttons , NULL ) ;
2009-02-22 09:30:18 +00:00
2009-04-11 06:48:39 +00:00
/* general actions */
but = uiDefBut ( block , BUT , B_IPO_DEPCHANGE , " Update Dependencies " , 10 , 200 , 180 , 22 , NULL , 0.0 , 0.0 , 0 , 0 , " Force updates of dependencies " ) ;
uiButSetFunc ( but , driver_update_flags_cb , fcu , NULL ) ;
but = uiDefBut ( block , BUT , B_IPO_DEPCHANGE , " Remove Driver " , 200 , 200 , 110 , 18 , NULL , 0.0 , 0.0 , 0 , 0 , " Remove this driver " ) ;
uiButSetFunc ( but , driver_remove_cb , ale , NULL ) ;
2009-02-22 09:30:18 +00:00
/* type */
2009-04-11 06:48:39 +00:00
uiDefBut ( block , LABEL , 1 , " Type: " , 10 , 170 , 60 , 20 , NULL , 0.0 , 0.0 , 0 , 0 , " " ) ;
2009-02-22 09:30:18 +00:00
uiDefButI ( block , MENU , B_IPO_DEPCHANGE ,
2009-04-16 12:38:44 +00:00
" Driver Type%t|Normal%x0|Scripted Expression%x1|Rotational Difference%x2 " ,
70 , 170 , 240 , 20 , & driver - > type , 0 , 0 , 0 , 0 , " Driver type " ) ;
/* show expression box if doing scripted drivers */
if ( driver - > type = = DRIVER_TYPE_PYTHON ) {
uiDefBut ( block , TEX , B_REDR , " Expr: " , 10 , 150 , 300 , 20 , driver - > expression , 0 , 255 , 0 , 0 , " One-liner Python Expression to use as Scripted Expression " ) ;
2009-02-22 09:30:18 +00:00
2009-04-11 06:48:39 +00:00
/* errors */
2009-02-22 09:30:18 +00:00
if ( driver - > flag & DRIVER_FLAG_INVALID ) {
2009-04-16 12:38:44 +00:00
uiDefIconBut ( block , LABEL , 1 , ICON_ERROR , 10 , 130 , 48 , 48 , NULL , 0 , 0 , 0 , 0 , " " ) ; // a bit larger
2009-02-22 09:30:18 +00:00
uiDefBut ( block , LABEL , 0 , " Error: invalid Python expression " ,
2009-04-16 12:38:44 +00:00
50 , 110 , 230 , 19 , NULL , 0 , 0 , 0 , 0 , " " ) ;
}
}
else {
/* errors */
if ( driver - > flag & DRIVER_FLAG_INVALID ) {
uiDefIconBut ( block , LABEL , 1 , ICON_ERROR , 10 , 130 , 48 , 48 , NULL , 0 , 0 , 0 , 0 , " " ) ; // a bit larger
uiDefBut ( block , LABEL , 0 , " Error: invalid target channel(s) " ,
50 , 130 , 230 , 19 , NULL , 0 , 0 , 0 , 0 , " " ) ;
2009-02-22 09:30:18 +00:00
}
}
2009-04-16 12:38:44 +00:00
but = uiDefBut ( block , BUT , B_IPO_DEPCHANGE , " Add Variable " , 10 , 110 , 300 , 20 , NULL , 0.0 , 0.0 , 0 , 0 , " Add a new target variable for this Driver " ) ;
uiButSetFunc ( but , driver_add_var_cb , driver , NULL ) ;
/* loop over targets, drawing them */
for ( dtar = driver - > targets . first ; dtar ; dtar = dtar - > next ) {
2009-04-17 02:13:35 +00:00
short height = ( dtar - > id ) ? 80 : 60 ;
/* panel behind buttons */
uiDefBut ( block , ROUNDBOX , B_REDR , " " , 5 , yco - height + 25 , 310 , height , NULL , 5.0 , 0.0 , 12.0 , 0 , " " ) ;
2009-02-22 09:30:18 +00:00
2009-04-16 12:38:44 +00:00
/* variable name */
2009-04-16 13:10:08 +00:00
uiDefButC ( block , TEX , B_REDR , " Name: " , 10 , yco , 280 , 20 , dtar - > name , 0 , 63 , 0 , 0 , " Name of target variable (No spaces or dots are allowed. Also, must not start with a symbol or digit). " ) ;
2009-04-11 00:29:21 +00:00
2009-04-16 12:38:44 +00:00
/* remove button */
but = uiDefIconBut ( block , BUT , B_REDR , ICON_X , 290 , yco , 20 , 20 , NULL , 0.0 , 0.0 , 0.0 , 0.0 , " Delete target variable. " ) ;
uiButSetFunc ( but , driver_delete_var_cb , driver , dtar ) ;
2009-02-22 09:30:18 +00:00
2009-04-11 06:48:39 +00:00
2009-04-16 12:38:44 +00:00
/* Target Object */
uiDefBut ( block , LABEL , 1 , " Value: " , 10 , yco - 30 , 60 , 20 , NULL , 0.0 , 0.0 , 0 , 0 , " " ) ;
uiDefIDPoinBut ( block , test_obpoin_but , ID_OB , B_REDR , " Ob: " , 70 , yco - 30 , 240 , 20 , & dtar - > id , " Object to use as Driver target " ) ;
// XXX should we hide these technical details?
if ( dtar - > id ) {
uiBlockBeginAlign ( block ) ;
/* RNA Path */
RNA_pointer_create ( ale - > id , & RNA_DriverTarget , dtar , & rna_ptr ) ;
uiDefButR ( block , TEX , 0 , " Path: " , 10 , yco - 50 , 250 , 20 , & rna_ptr , " rna_path " , 0 , 0 , 0 , - 1 , - 1 , " RNA Path (from Driver Object) to property used as Driver. " ) ;
/* Array Index */
uiDefButI ( block , NUM , B_REDR , " " , 260 , yco - 50 , 50 , 20 , & dtar - > array_index , 0 , INT_MAX , 0 , 0 , " Index to the specific property used as Driver if applicable. " ) ;
uiBlockEndAlign ( block ) ;
2009-04-11 06:48:39 +00:00
}
2009-04-16 12:38:44 +00:00
/* adjust y-coordinate for next target */
yco - = height ;
i + + ;
2009-02-22 09:30:18 +00:00
}
2009-05-19 17:13:33 +00:00
MEM_freeN ( ale ) ;
2009-02-22 09:30:18 +00:00
}
2009-03-21 03:49:22 +00:00
/* ******************* f-modifiers ******************************** */
2009-07-02 04:47:36 +00:00
/* all the drawing code is in editors/animation/fmodifier_ui.c */
2009-02-22 09:30:18 +00:00
2009-03-15 10:39:02 +00:00
# define B_FMODIFIER_REDRAW 20
2009-02-22 09:30:18 +00:00
static void do_graph_region_modifier_buttons ( bContext * C , void * arg , int event )
{
2009-03-16 11:11:44 +00:00
switch ( event ) {
2009-03-15 10:39:02 +00:00
case B_REDR :
2009-03-16 11:11:44 +00:00
case B_FMODIFIER_REDRAW : // XXX this should send depsgraph updates too
2009-03-15 10:39:02 +00:00
ED_area_tag_redraw ( CTX_wm_area ( C ) ) ;
return ; /* no notifier! */
}
}
2009-05-19 17:13:33 +00:00
static void graph_panel_modifiers ( const bContext * C , Panel * pa )
2009-02-22 09:30:18 +00:00
{
2009-05-19 17:13:33 +00:00
bAnimListElem * ale ;
FCurve * fcu ;
2009-03-15 10:39:02 +00:00
FModifier * fcm ;
2009-07-02 12:41:03 +00:00
uiLayout * col , * row ;
2009-02-22 09:30:18 +00:00
uiBlock * block ;
2009-03-16 11:11:44 +00:00
2009-07-02 12:41:03 +00:00
if ( ! graph_panel_context ( C , & ale , & fcu ) )
2009-05-19 17:13:33 +00:00
return ;
2009-02-22 09:30:18 +00:00
2009-07-02 12:41:03 +00:00
block = uiLayoutGetBlock ( pa - > layout ) ;
2009-05-19 17:13:33 +00:00
uiBlockSetHandleFunc ( block , do_graph_region_modifier_buttons , NULL ) ;
2009-03-16 11:11:44 +00:00
2009-03-15 10:39:02 +00:00
/* 'add modifier' button at top of panel */
2009-07-02 12:41:03 +00:00
{
row = uiLayoutRow ( pa - > layout , 0 ) ;
block = uiLayoutGetBlock ( row ) ;
// XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
uiDefButO ( block , BUT , " GRAPH_OT_fmodifier_add " , WM_OP_INVOKE_REGION_WIN , " Add Modifier " , 10 , 0 , 150 , 20 , " Adds a new F-Curve Modifier for the active F-Curve " ) ;
}
2009-03-15 10:39:02 +00:00
/* draw each modifier */
2009-07-02 12:41:03 +00:00
for ( fcm = fcu - > modifiers . first ; fcm ; fcm = fcm - > next ) {
col = uiLayoutColumn ( pa - > layout , 1 ) ;
2009-07-03 01:10:46 +00:00
ANIM_uiTemplate_fmodifier_draw ( col , ale - > id , & fcu - > modifiers , fcm ) ;
2009-07-02 12:41:03 +00:00
}
2009-05-19 17:13:33 +00:00
MEM_freeN ( ale ) ;
2009-02-22 09:30:18 +00:00
}
2009-03-21 03:49:22 +00:00
/* ******************* general ******************************** */
2009-02-22 09:30:18 +00:00
2009-05-19 17:13:33 +00:00
void graph_buttons_register ( ARegionType * art )
2009-02-21 10:38:58 +00:00
{
2009-05-19 17:13:33 +00:00
PanelType * pt ;
pt = MEM_callocN ( sizeof ( PanelType ) , " spacetype graph panel properties " ) ;
strcpy ( pt - > idname , " GRAPH_PT_properties " ) ;
strcpy ( pt - > label , " Properties " ) ;
pt - > draw = graph_panel_properties ;
pt - > poll = graph_panel_poll ;
BLI_addtail ( & art - > paneltypes , pt ) ;
pt = MEM_callocN ( sizeof ( PanelType ) , " spacetype graph panel drivers " ) ;
strcpy ( pt - > idname , " GRAPH_PT_drivers " ) ;
strcpy ( pt - > label , " Drivers " ) ;
pt - > draw = graph_panel_drivers ;
pt - > poll = graph_panel_drivers_poll ;
BLI_addtail ( & art - > paneltypes , pt ) ;
pt = MEM_callocN ( sizeof ( PanelType ) , " spacetype graph panel modifiers " ) ;
strcpy ( pt - > idname , " GRAPH_PT_modifiers " ) ;
strcpy ( pt - > label , " Modifiers " ) ;
pt - > draw = graph_panel_modifiers ;
pt - > poll = graph_panel_poll ;
BLI_addtail ( & art - > paneltypes , pt ) ;
2009-02-21 10:38:58 +00:00
}
static int graph_properties ( bContext * C , wmOperator * op )
{
ScrArea * sa = CTX_wm_area ( C ) ;
ARegion * ar = graph_has_buttons_region ( sa ) ;
2009-09-14 19:12:29 +00:00
if ( ar )
ED_region_toggle_hidden ( C , ar ) ;
2009-02-21 10:38:58 +00:00
return OPERATOR_FINISHED ;
}
2009-06-22 04:23:06 +00:00
void GRAPH_OT_properties ( wmOperatorType * ot )
2009-02-21 10:38:58 +00:00
{
ot - > name = " Properties " ;
2009-06-22 04:23:06 +00:00
ot - > idname = " GRAPH_OT_properties " ;
2009-02-21 10:38:58 +00:00
ot - > exec = graph_properties ;
ot - > poll = ED_operator_ipo_active ; // xxx
/* flags */
ot - > flag = 0 ;
}