object.constraints.add()/remove()/active, same for PoseChannel

modified internal api for minimal rna wrapper functions.

TODO
- missing updates for pose channels
- typecheck for pose/object constraints
This commit is contained in:
Campbell Barton 2009-11-11 19:58:30 +00:00
parent 047ee04418
commit 53250f85db
12 changed files with 285 additions and 96 deletions

View File

@ -102,6 +102,9 @@ typedef struct bConstraintTypeInfo {
bConstraintTypeInfo *constraint_get_typeinfo(struct bConstraint *con);
bConstraintTypeInfo *get_constraint_typeinfo(int type);
struct bConstraint *add_ob_constraint(struct Object *ob, const char *name, short type);
struct bConstraint *add_pose_constraint(struct Object *ob, struct bPoseChannel *pchan, const char *name, short type);
/* ---------------------------------------------------------------------------- */
/* Useful macros for testing various common flag combinations */

View File

@ -3601,6 +3601,91 @@ bConstraintTypeInfo *constraint_get_typeinfo (bConstraint *con)
return NULL;
}
/* Creates a new constraint, initialises its data, and returns it */
static bConstraint *add_new_constraint_internal(const char *name, short type)
{
bConstraint *con;
bConstraintTypeInfo *cti;
con = MEM_callocN(sizeof(bConstraint), "Constraint");
/* Set up a generic constraint datablock */
con->type = type;
con->flag |= CONSTRAINT_EXPAND;
con->enforce = 1.0f;
/* Load the data for it */
cti = constraint_get_typeinfo(con);
if (cti) {
con->data = MEM_callocN(cti->size, cti->structName);
/* only constraints that change any settings need this */
if (cti->new_data)
cti->new_data(con->data);
/* set the name based on the type of constraint */
name= name ? name : cti->name;
}
else
name= name ? name : "Const";
strcpy(con->name, name);
return con;
}
/* if pchan is not NULL then assume we're adding a pose constraint */
static bConstraint *add_new_constraint(Object *ob, bPoseChannel *pchan, const char *name, short type)
{
bConstraint *con;
ListBase *list;
con= add_new_constraint_internal(name, type);
if(pchan) list= &pchan->constraints;
else list= &ob->constraints;
if (list) {
bConstraint *coniter;
/* add new constraint to end of list of constraints before ensuring that it has a unique name
* (otherwise unique-naming code will fail, since it assumes element exists in list)
*/
BLI_addtail(list, con);
unique_constraint_name(con, list);
/* if the target list is a list on some PoseChannel belonging to a proxy-protected
* Armature layer, we must tag newly added constraints with a flag which allows them
* to persist after proxy syncing has been done
*/
if (proxylocked_constraints_owner(ob, pchan))
con->flag |= CONSTRAINT_PROXY_LOCAL;
/* make this constraint the active one
* - since constraint was added at end of stack, we can just go
* through deactivating all previous ones
*/
con->flag |= CONSTRAINT_ACTIVE;
for (coniter= con->prev; coniter; coniter= coniter->prev)
coniter->flag &= ~CONSTRAINT_ACTIVE;
}
return con;
}
bConstraint *add_pose_constraint(Object *ob, bPoseChannel *pchan, const char *name, short type)
{
if(pchan==NULL)
return NULL;
return add_new_constraint(ob, pchan, name, type);
}
bConstraint *add_ob_constraint(Object *ob, const char *name, short type)
{
return add_new_constraint(ob, NULL, name, type);
}
/* ************************* General Constraints API ************************** */
/* The functions here are called by various parts of Blender. Very few (should be none if possible)
* constraint-specific code should occur here.

View File

@ -91,9 +91,6 @@ int object_is_libdata(struct Object *ob);
int object_data_is_libdata(struct Object *ob);
/* constraints */
struct bConstraint *add_new_constraint(short type);
void add_constraint_to_object(struct bConstraint *con, struct Object *ob);
struct ListBase *get_active_constraints(struct Object *ob);
struct bConstraint *get_active_constraint(struct Object *ob);

View File

@ -187,56 +187,6 @@ void update_pyconstraint_cb (void *arg1, void *arg2)
#endif
}
/* Creates a new constraint, initialises its data, and returns it */
bConstraint *add_new_constraint (short type)
{
bConstraint *con;
bConstraintTypeInfo *cti;
con = MEM_callocN(sizeof(bConstraint), "Constraint");
/* Set up a generic constraint datablock */
con->type = type;
con->flag |= CONSTRAINT_EXPAND;
con->enforce = 1.0f;
/* Load the data for it */
cti = constraint_get_typeinfo(con);
if (cti) {
con->data = MEM_callocN(cti->size, cti->structName);
/* only constraints that change any settings need this */
if (cti->new_data)
cti->new_data(con->data);
/* set the name based on the type of constraint */
strcpy(con->name, cti->name);
}
else
strcpy(con->name, "Const");
return con;
}
/* Adds the given constraint to the Object-level set of constraints for the given Object */
void add_constraint_to_object (bConstraint *con, Object *ob)
{
ListBase *list;
list = &ob->constraints;
if (list) {
unique_constraint_name(con, list);
BLI_addtail(list, con);
if (proxylocked_constraints_owner(ob, NULL))
con->flag |= CONSTRAINT_PROXY_LOCAL;
con->flag |= CONSTRAINT_ACTIVE;
for (con= con->prev; con; con= con->prev)
con->flag &= ~CONSTRAINT_ACTIVE;
}
}
/* helper function for add_constriant - sets the last target for the active constraint */
static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index)
{
@ -1076,9 +1026,14 @@ static short get_new_constraint_target(bContext *C, int con_type, Object **tar_o
static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget)
{
Scene *scene= CTX_data_scene(C);
bPoseChannel *pchan= get_active_posechannel(ob);
bPoseChannel *pchan;
bConstraint *con;
if(list == &ob->constraints)
pchan= NULL;
else
pchan= get_active_posechannel(ob);
/* check if constraint to be added is valid for the given constraints stack */
if (type == CONSTRAINT_TYPE_NULL) {
return OPERATOR_CANCELLED;
@ -1097,32 +1052,10 @@ static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase
}
/* create a new constraint of the type requried, and add it to the active/given constraints list */
con = add_new_constraint(type);
if (list) {
bConstraint *coniter;
/* add new constraint to end of list of constraints before ensuring that it has a unique name
* (otherwise unique-naming code will fail, since it assumes element exists in list)
*/
BLI_addtail(list, con);
unique_constraint_name(con, list);
/* if the target list is a list on some PoseChannel belonging to a proxy-protected
* Armature layer, we must tag newly added constraints with a flag which allows them
* to persist after proxy syncing has been done
*/
if (proxylocked_constraints_owner(ob, pchan))
con->flag |= CONSTRAINT_PROXY_LOCAL;
/* make this constraint the active one
* - since constraint was added at end of stack, we can just go
* through deactivating all previous ones
*/
con->flag |= CONSTRAINT_ACTIVE;
for (coniter= con->prev; coniter; coniter= coniter->prev)
coniter->flag &= ~CONSTRAINT_ACTIVE;
}
if(pchan)
con = add_pose_constraint(ob, pchan, NULL, type);
else
con = add_ob_constraint(ob, NULL, type);
/* get the first selected object/bone, and make that the target
* - apart from the buttons-window add buttons, we shouldn't add in this way

View File

@ -608,14 +608,11 @@ static int parent_set_exec(bContext *C, wmOperator *op)
bFollowPathConstraint *data;
float cmat[4][4], vec[3];
con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
strcpy (con->name, "AutoPath");
con = add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
data = con->data;
data->tar = par;
add_constraint_to_object(con, ob);
get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob));
sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
@ -923,8 +920,7 @@ static int track_set_exec(bContext *C, wmOperator *op)
CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
if(ob!=obact) {
con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
strcpy (con->name, "AutoTrack");
con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
data = con->data;
data->tar = obact;
@ -935,8 +931,6 @@ static int track_set_exec(bContext *C, wmOperator *op)
data->reserved1 = TRACK_nZ;
data->reserved2 = UP_Y;
}
add_constraint_to_object(con, ob);
}
}
CTX_DATA_END;
@ -947,8 +941,7 @@ static int track_set_exec(bContext *C, wmOperator *op)
CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
if(ob!=obact) {
con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
strcpy (con->name, "AutoTrack");
con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
data = con->data;
data->tar = obact;
@ -959,8 +952,6 @@ static int track_set_exec(bContext *C, wmOperator *op)
data->trackflag = TRACK_nZ;
data->lockflag = LOCK_Y;
}
add_constraint_to_object(con, ob);
}
}
CTX_DATA_END;

View File

@ -878,7 +878,7 @@ static short pose_grab_with_ik_add(bPoseChannel *pchan)
}
}
con = add_new_constraint(CONSTRAINT_TYPE_KINEMATIC);
con = add_pose_constraint(NULL, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
BLI_addtail(&pchan->constraints, con);
pchan->constflag |= (PCHAN_HAS_IK|PCHAN_HAS_TARGET); /* for draw, but also for detecting while pose solving */
data= con->data;

View File

@ -213,6 +213,7 @@ void RNA_api_main(struct StructRNA *srna);
void RNA_api_material(StructRNA *srna);
void RNA_api_mesh(struct StructRNA *srna);
void RNA_api_object(struct StructRNA *srna);
void RNA_api_pose_channel(struct StructRNA *srna);
void RNA_api_scene(struct StructRNA *srna);
void RNA_api_text(struct StructRNA *srna);
void RNA_api_ui_layout(struct StructRNA *srna);

View File

@ -86,6 +86,7 @@ EnumPropertyItem object_type_items[] = {
#include "BLI_math.h"
#include "DNA_key_types.h"
#include "DNA_constraint_types.h"
#include "BKE_armature.h"
#include "BKE_bullet.h"
@ -859,6 +860,30 @@ static PointerRNA rna_Object_collision_get(PointerRNA *ptr)
return rna_pointer_inherit_refine(ptr, &RNA_CollisionSettings, ob->pd);
}
static PointerRNA rna_Object_active_constraint_get(PointerRNA *ptr)
{
Object *ob= (Object*)ptr->id.data;
bConstraint *con;
for(con= ob->constraints.first; con; con= con->next) {
if(con->flag & CONSTRAINT_ACTIVE)
break;
}
return rna_pointer_inherit_refine(ptr, &RNA_Constraint, con);
}
static void rna_Object_active_constraint_set(PointerRNA *ptr, PointerRNA value)
{
Object *ob= (Object*)ptr->id.data;
bConstraint *con;
for(con= ob->constraints.first; con; con= con->next) {
if(value.data==con)
con->flag |= CONSTRAINT_ACTIVE;
else
con->flag &= ~CONSTRAINT_ACTIVE;
}
}
#else
static void rna_def_vertex_group(BlenderRNA *brna)
@ -1428,6 +1453,16 @@ static void rna_def_object(BlenderRNA *brna)
prop= RNA_def_property(srna, "constraints", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_type(prop, "Constraint");
RNA_def_property_ui_text(prop, "Constraints", "Constraints of the object.");
RNA_def_property_collection_funcs(prop, 0, 0, 0, 0, 0, 0, 0, "constraints__add", "constraints__remove");
{ /* Collection active property */
PropertyRNA *prop_act= RNA_def_property(srna, "constraints__active", PROP_POINTER, PROP_NONE);
RNA_def_property_struct_type(prop_act, "Constraint");
RNA_def_property_pointer_funcs(prop_act, "rna_Object_active_constraint_get", "rna_Object_active_constraint_set", NULL);
RNA_def_property_flag(prop_act, PROP_EDITABLE);
RNA_def_property_ui_text(prop_act, "Active Constraint", "Active Object constraint.");
RNA_def_property_collection_active(prop, prop_act);
}
prop= RNA_def_property(srna, "modifiers", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_type(prop, "Modifier");

View File

@ -33,6 +33,7 @@
#include "RNA_define.h"
#include "RNA_types.h"
#include "RNA_enum_types.h"
#include "DNA_object_types.h"
@ -50,6 +51,7 @@
#include "BKE_mesh.h"
#include "BKE_DerivedMesh.h"
#include "BKE_constraint.h"
#include "BKE_customdata.h"
#include "BKE_anim.h"
#include "BKE_depsgraph.h"
@ -64,6 +66,7 @@
#include "DNA_meshdata_types.h"
#include "DNA_curve_types.h"
#include "DNA_modifier_types.h"
#include "DNA_constraint_types.h"
#include "MEM_guardedalloc.h"
@ -349,6 +352,30 @@ static void rna_Mesh_assign_verts_to_group(Object *ob, bDeformGroup *group, int
}
*/
static bConstraint *rna_Object_constraints_add(Object *object, bContext *C, int type)
{
WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, object);
return add_ob_constraint(object, NULL, type);
}
static int rna_Object_constraints_remove(Object *object, bContext *C, int index)
{
bConstraint *con= BLI_findlink(&object->constraints, index);
if(con) {
free_constraint_data(con);
BLI_freelinkN(&object->constraints, con);
ED_object_constraint_set_active(object, NULL);
WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, object);
return 1;
}
else {
return 0;
}
}
#else
void RNA_api_object(StructRNA *srna)
@ -424,6 +451,27 @@ void RNA_api_object(StructRNA *srna)
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
parm= RNA_def_boolean(func, "is_visible", 0, "", "Object visibility.");
RNA_def_function_return(func, parm);
/* Constraint collection */
func= RNA_def_function(srna, "constraints__add", "rna_Object_constraints_add");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
RNA_def_function_ui_description(func, "Add a constraint to this object");
/* return type */
parm= RNA_def_pointer(func, "constraint", "Constraint", "", "New constraint.");
RNA_def_function_return(func, parm);
/* object to add */
parm= RNA_def_enum(func, "type", constraint_type_items, 1, "", "Constraint type to add.");
RNA_def_property_flag(parm, PROP_REQUIRED);
func= RNA_def_function(srna, "constraints__remove", "rna_Object_constraints_remove");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
RNA_def_function_ui_description(func, "Remove a constraint from this object.");
/* return type */
parm= RNA_def_boolean(func, "success", 0, "Success", "Removed the constraint successfully.");
RNA_def_function_return(func, parm);
/* object to add */
parm= RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
RNA_def_property_flag(parm, PROP_REQUIRED);
}
#endif

View File

@ -414,6 +414,32 @@ static void rna_pose_pgroup_name_set(PointerRNA *ptr, const char *value, char *r
}
#endif
static PointerRNA rna_PoseChannel_active_constraint_get(PointerRNA *ptr)
{
bPoseChannel *pchan= (bPoseChannel*)ptr->data;
bConstraint *con;
for(con= pchan->constraints.first; con; con= con->next) {
if(con->flag & CONSTRAINT_ACTIVE)
break;
}
return rna_pointer_inherit_refine(ptr, &RNA_Constraint, con);
}
static void rna_PoseChannel_active_constraint_set(PointerRNA *ptr, PointerRNA value)
{
bPoseChannel *pchan= (bPoseChannel*)ptr->data;
bConstraint *con;
for(con= pchan->constraints.first; con; con= con->next) {
if(value.data==con)
con->flag |= CONSTRAINT_ACTIVE;
else
con->flag &= ~CONSTRAINT_ACTIVE;
}
}
#else
static void rna_def_bone_group(BlenderRNA *brna)
@ -512,7 +538,17 @@ static void rna_def_pose_channel(BlenderRNA *brna)
/* Bone Constraints */
prop= RNA_def_property(srna, "constraints", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_type(prop, "Constraint");
RNA_def_property_ui_text(prop, "Constraints", "Constraints that act on this PoseChannel.");
RNA_def_property_ui_text(prop, "Constraints", "Constraints that act on this PoseChannel.");
RNA_def_property_collection_funcs(prop, 0, 0, 0, 0, 0, 0, 0, "constraints__add", "constraints__remove");
{ /* Collection active property */
PropertyRNA *prop_act= RNA_def_property(srna, "constraints__active", PROP_POINTER, PROP_NONE);
RNA_def_property_struct_type(prop_act, "Constraint");
RNA_def_property_pointer_funcs(prop_act, "rna_PoseChannel_active_constraint_get", "rna_PoseChannel_active_constraint_set", NULL);
RNA_def_property_flag(prop_act, PROP_EDITABLE);
RNA_def_property_ui_text(prop_act, "Active Constraint", "Active PoseChannel constraint.");
RNA_def_property_collection_active(prop, prop_act);
}
/* Name + Selection Status */
prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
@ -791,6 +827,8 @@ static void rna_def_pose_channel(BlenderRNA *brna)
RNA_def_property_array(prop, 3);
RNA_def_property_ui_text(prop, "Lock Scale", "Lock editing of scale in the interface.");
RNA_def_property_update(prop, NC_OBJECT|ND_POSE, "rna_Pose_update");
RNA_api_pose_channel(srna);
}
static void rna_def_pose_itasc(BlenderRNA *brna)

View File

@ -33,6 +33,7 @@
#include "RNA_define.h"
#include "RNA_types.h"
#include "RNA_enum_types.h"
#include "DNA_object_types.h"
@ -43,14 +44,71 @@
/* #include "DNA_anim_types.h" */
#include "DNA_action_types.h" /* bPose */
#include "BKE_constraint.h" /* bPose */
static bConstraint *rna_PoseChannel_constraints_add(bPoseChannel *pchan, bContext *C, int type)
{
//WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, object);
// TODO, pass object also
// TODO, new pose bones don't have updated draw flags
return add_pose_constraint(NULL, pchan, NULL, type);
}
static int rna_PoseChannel_constraints_remove(bPoseChannel *pchan, bContext *C, int index)
{
bConstraint *con= BLI_findlink(&pchan->constraints, index);
if(con) {
free_constraint_data(con);
BLI_freelinkN(&pchan->constraints, con);
//ED_object_constraint_set_active(object, NULL);
//WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, object);
return 1;
}
else {
return 0;
}
}
#else
void RNA_api_pose(StructRNA *srna)
{
/* FunctionRNA *func; */
/* PropertyRNA *parm; */
}
void RNA_api_pose_channel(StructRNA *srna)
{
FunctionRNA *func;
PropertyRNA *parm;
/* Constraint collection */
func= RNA_def_function(srna, "constraints__add", "rna_PoseChannel_constraints_add");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
RNA_def_function_ui_description(func, "Add a constraint to this object");
/* return type */
parm= RNA_def_pointer(func, "constraint", "Constraint", "", "New constraint.");
RNA_def_function_return(func, parm);
/* object to add */
parm= RNA_def_enum(func, "type", constraint_type_items, 1, "", "Constraint type to add.");
RNA_def_property_flag(parm, PROP_REQUIRED);
func= RNA_def_function(srna, "constraints__remove", "rna_PoseChannel_constraints_remove");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
RNA_def_function_ui_description(func, "Remove a constraint from this object.");
/* return type */
parm= RNA_def_boolean(func, "success", 0, "Success", "Removed the constraint successfully.");
RNA_def_function_return(func, parm);
/* object to add */
parm= RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
RNA_def_property_flag(parm, PROP_REQUIRED);
}
#endif

View File

@ -2229,7 +2229,7 @@ void RNA_def_scene(BlenderRNA *brna)
RNA_def_property_struct_type(prop_act, "ObjectBase");
RNA_def_property_pointer_sdna(prop_act, NULL, "basact");
RNA_def_property_flag(prop_act, PROP_EDITABLE);
RNA_def_property_ui_text(prop_act, "Active Base", "Active object in the scene.");
RNA_def_property_ui_text(prop_act, "Active Base", "Active object base in the scene.");
RNA_def_property_update(prop_act, NC_SCENE|ND_OB_ACTIVE, NULL);
RNA_def_property_collection_active(prop, prop_act);
}
@ -2245,7 +2245,7 @@ void RNA_def_scene(BlenderRNA *brna)
RNA_def_property_struct_type(prop_act, "Object");
RNA_def_property_pointer_funcs(prop_act, "rna_Scene_active_object_get", "rna_Scene_active_object_set", NULL);
RNA_def_property_flag(prop_act, PROP_EDITABLE);
RNA_def_property_ui_text(prop_act, "Object", "Object to use as projector transform.");
RNA_def_property_ui_text(prop_act, "Active Object", "Active object for this scene.");
/* Could call: ED_base_object_activate(C, scene->basact);
* but would be a bad level call and it seems the notifier is enough */
RNA_def_property_update(prop_act, NC_SCENE|ND_OB_ACTIVE, NULL);