From BlenderWiki

Jump to: navigation, search
Note: This is an archived version of the Blender Developer Wiki. The current and active wiki is available on wiki.blender.org.

Add single to active driver expression

ANIM_add_single_to_active_driver_expression().

Page

This is an operator I would like to try code.
This will make it so after you add a driver to a property, you can then add a property to the active driver expression, by pressing RMB on a property in the UI.
I think most the big problems are currently solved in blender.

Todo 00

Todo 01.
Code.
Test.
Document problems.

ANIM_add_driver

/* Main Driver Management API calls:
 * Add a new driver for the specified property on the given ID block
 */
short ANIM_add_driver(ReportList *reports, ID *id, const char rna_path[], int array_index, short flag, int type)

	ReportList *reports
	const char rna_path[]	// rna path to property
	int array_index		// property index
				// -1: Add to all properties, later changed to 0
				// will only loop once unless the array index was -1
	short flag		// options, check DNA
				// CREATEDRIVER_WITH_FMODIFIER
	int type		// Driver type used for driver variables
				// scripted expression, MIN, MAX, SUM, average

Design 00

It may be better to call the operator ANIM_add_prop_to_active_driver.
For transform properties, like "cube world x location", later the driver variable could be built by code using the driver variable UI interface.
This would require properties to have a differnt routines.
Add property set could be added later.
Also and property or properties to multiple drivers at the same time.
Seems this operator could be very elaborate.

Code 00

Test operator callback.
/* Get property path from UI, check if ok, call ANIM_add_prop, update UI, return. */
static int add_prop_to_driver_exec(bContext *C, wmOperator *op)
{
	PointerRNA ptr = {{NULL}};
	PropertyRNA *prop = NULL;
	short success = 0;
	int index;
	
	/* try using property retrieved from UI */
	uiContextActiveProperty(C, &ptr, &prop, &index);
	
	// check later, may be able to add more properties
	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
		char *path = get_driver_path_hack(C, &ptr, prop);
		
		if (path) {
			success += ANIM_add_prop_to_active_driver(op->reports, ptr.id.data, path, index);
			
			MEM_freeN(path);
		}
	}
	
	if (success) {				// check later, some may not be required
		/* send updates */
		uiContextAnimUpdate(C);
		
		WM_event_add_notifier(C, NC_ANIMATION | ND_FCURVES_ORDER, NULL); // XXX
	}
	
	return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
}
Test operator.
void ANIM_OT_add_prop_to_driver(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Single to Driver";
	ot->idname = "ANIM_OT_add_prop_to_driver";
	ot->description = "Adds single property to the active Driver.";
	
	/* callbacks */
	ot->exec = add_prop_to_driver_exec;
	
	/* flags */
	ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
}
Abstract operator funtion.
short ANIM_add_prop_to_active_driver(ReportList *reports, ID *id, const char rna_path[], int array_index)
{	
	// Get active driver from context.
	// Add rna property data path to active driver scripted expression.
}

Todo 01

Check if possible to write in python. Probably not.
Active driver context.
Learn how ANIM_add_driver works.
Check what calls this function.
Check what this function calls.
Document other important things that are used in the process.

Active Driver Context

source/blender/editors/animation/anim_channels_edit.c
Functions here seem to allocate the context from AnimData.
The AnimData pointer is then filtered.
Channels are filtered by making a bitmask 'filter' with enum flags.
Then passing the filter to ANIM_animdata_filter().
	filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
	ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype)
This will update the context for the anim_data pointer.
The anim_data pointers are then looped using the listbase pointers.
This is the filter for the active channel.
	/* for its type, channel should be "active" one */
	ANIMFILTER_ACTIVE = (1 << 4),
The eAnimFilter_Flags are found in source/blender/editors/include/ED_anim_api.h
There are different types, so the correct set will be required.
Im not sure yet if a new driver is active when added to a property.