Copy To Selected
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 17 Dec 2009 17:15:38 +0000 (17:15 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 17 Dec 2009 17:15:38 +0000 (17:15 +0000)
Until we have proper multi-object editing, this adds a Copy To Selected
option to the right mouse button menu for Object and Bone properties, to
copy the value from the active object to the selected objects.

Also includes some implementation changes to reset to default operator.

source/blender/editors/include/UI_interface.h
source/blender/editors/include/UI_view2d.h
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_ops.c
source/blender/editors/interface/view2d_ops.c
source/blender/editors/space_api/spacetypes.c
source/blender/makesrna/RNA_access.h
source/blender/makesrna/intern/rna_access.c

index f0e52a13d3fb23cacb239428fa1970d4a8bda665..8e44d7f386e8ddf53259a4c5344abf45acc88980 100644 (file)
@@ -689,7 +689,7 @@ void uiItemMenuEnumO(uiLayout *layout, char *name, int icon, char *opname, char
 void uiItemMenuEnumR(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname);
 
 /* UI Operators */
-void ui_buttons_operatortypes(void);
+void UI_buttons_operatortypes(void);
 
 /* Helpers for Operators */
 void uiAnimContextProperty(const struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index);
index 8fbff25589d422224df77affabf86628119b4b65..e3b00c21d045046a52c869302da2ea6d202bb8a6 100644 (file)
@@ -195,7 +195,7 @@ void UI_view2d_text_cache_rectf(struct View2D *v2d, struct rctf *rect, char *str
 void UI_view2d_text_cache_draw(struct ARegion *ar);
 
 /* operators */
-void ui_view2d_operatortypes(void);
+void UI_view2d_operatortypes(void);
 void UI_view2d_keymap(struct wmKeyConfig *keyconf);
 
 #endif /* UI_VIEW2D_H */
index 9604704dde838baf46bf076be8def612f655e9a2..88868a61592f9ccde75ec1485137f6d4ecace7c7 100644 (file)
@@ -3506,7 +3506,8 @@ static int ui_but_menu(bContext *C, uiBut *but)
                else
                        uiItemO(layout, "Reset to Default Value", 0, "UI_OT_reset_default_button");
                
-               uiItemO(layout, "Copy Data Path", 0, "UI_OT_copy_clipboard_button");
+               uiItemO(layout, "Copy Data Path", 0, "UI_OT_copy_data_path_button");
+               uiItemO(layout, "Copy To Selected", 0, "UI_OT_copy_to_selected_button");
 
                uiItemS(layout);
        }
index a5caf1c930b15b46a73437ea7da18c7b8e5c401d..d1204b41a2841373582896612ae9e252a229922d 100644 (file)
 
 /* ********************************************************** */
 
-/* Copy to Clipboard Button Operator ------------------------ */
+/* Copy Data Path Operator ------------------------ */
 
-static int copy_clipboard_button_exec(bContext *C, wmOperator *op)
+static int copy_data_path_button_exec(bContext *C, wmOperator *op)
 {
        PointerRNA ptr;
-       PropertyRNA *prop= NULL;
+       PropertyRNA *prop;
        char *path;
-       short success= 0;
+       int success= 0;
        int index;
 
        /* try to create driver using property retrieved from UI */
-       memset(&ptr, 0, sizeof(PointerRNA));
        uiAnimContextProperty(C, &ptr, &prop, &index);
 
        if (ptr.data && prop) {
@@ -86,15 +85,15 @@ static int copy_clipboard_button_exec(bContext *C, wmOperator *op)
        return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
 }
 
-void UI_OT_copy_clipboard_button(wmOperatorType *ot)
+void UI_OT_copy_data_path_button(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Copy Data Path";
-       ot->idname= "UI_OT_copy_clipboard_button";
+       ot->idname= "UI_OT_copy_data_path_button";
        ot->description= "Copy the RNA data path for this property to the clipboard.";
 
        /* callbacks */
-       ot->exec= copy_clipboard_button_exec;
+       ot->exec= copy_data_path_button_exec;
        //op->poll= ??? // TODO: need to have some valid property before this can be done
 
        /* flags */
@@ -103,103 +102,34 @@ void UI_OT_copy_clipboard_button(wmOperatorType *ot)
 
 /* Reset to Default Values Button Operator ------------------------ */
 
+static int reset_default_button_poll(bContext *C)
+{
+       PointerRNA ptr;
+       PropertyRNA *prop;
+       int index;
+
+       uiAnimContextProperty(C, &ptr, &prop, &index);
+       
+       return (ptr.data && prop && RNA_property_editable(&ptr, prop));
+}
+
 static int reset_default_button_exec(bContext *C, wmOperator *op)
 {
        PointerRNA ptr;
-       PropertyRNA *prop= NULL;
-       short success= 0;
-       int index, len;
-       int all = RNA_boolean_get(op->ptr, "all");
+       PropertyRNA *prop;
+       int success= 0;
+       int index, all = RNA_boolean_get(op->ptr, "all");
 
        /* try to reset the nominated setting to its default value */
-       memset(&ptr, 0, sizeof(PointerRNA));
        uiAnimContextProperty(C, &ptr, &prop, &index);
        
        /* if there is a valid property that is editable... */
        if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
-               /* get the length of the array to work with */
-               len= RNA_property_array_length(&ptr, prop);
-               
-               /* get and set the default values as appropriate for the various types */
-               switch (RNA_property_type(prop)) {
-                       case PROP_BOOLEAN:
-                               if (len) {
-                                       if (all) {
-                                               int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - boolean");
-                                               
-                                               RNA_property_boolean_get_default_array(&ptr, prop, tmparray);
-                                               RNA_property_boolean_set_array(&ptr, prop, tmparray);
-                                               
-                                               MEM_freeN(tmparray);
-                                       }
-                                       else {
-                                               int value= RNA_property_boolean_get_default_index(&ptr, prop, index);
-                                               RNA_property_boolean_set_index(&ptr, prop, index, value);
-                                       }
-                               }
-                               else {
-                                       int value= RNA_property_boolean_get_default(&ptr, prop);
-                                       RNA_property_boolean_set(&ptr, prop, value);
-                               }
-                               break;
-                       case PROP_INT:
-                               if (len) {
-                                       if (all) {
-                                               int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - int");
-                                               
-                                               RNA_property_int_get_default_array(&ptr, prop, tmparray);
-                                               RNA_property_int_set_array(&ptr, prop, tmparray);
-                                               
-                                               MEM_freeN(tmparray);
-                                       }
-                                       else {
-                                               int value= RNA_property_int_get_default_index(&ptr, prop, index);
-                                               RNA_property_int_set_index(&ptr, prop, index, value);
-                                       }
-                               }
-                               else {
-                                       int value= RNA_property_int_get_default(&ptr, prop);
-                                       RNA_property_int_set(&ptr, prop, value);
-                               }
-                               break;
-                       case PROP_FLOAT:
-                               if (len) {
-                                       if (all) {
-                                               float *tmparray= MEM_callocN(sizeof(float)*len, "reset_defaults - float");
-                                               
-                                               RNA_property_float_get_default_array(&ptr, prop, tmparray);
-                                               RNA_property_float_set_array(&ptr, prop, tmparray);
-                                               
-                                               MEM_freeN(tmparray);
-                                       }
-                                       else {
-                                               float value= RNA_property_float_get_default_index(&ptr, prop, index);
-                                               RNA_property_float_set_index(&ptr, prop, index, value);
-                                       }
-                               }
-                               else {
-                                       float value= RNA_property_float_get_default(&ptr, prop);
-                                       RNA_property_float_set(&ptr, prop, value);
-                               }
-                               break;
-                       case PROP_ENUM:
-                       {
-                               int value= RNA_property_enum_get_default(&ptr, prop);
-                               RNA_property_enum_set(&ptr, prop, value);
-                       }
-                               break;
-                       
-                       //case PROP_POINTER:
-                       //case PROP_STRING:
-                       default: 
-                               // FIXME: many of the other types such as strings and pointers need this implemented too!
-                               break;
+               if(RNA_property_reset(&ptr, prop, (all)? -1: index)) {
+                       /* perform updates required for this property */
+                       RNA_property_update(C, &ptr, prop);
+                       success= 1;
                }
-               
-               /* perform updates required for this property */
-               RNA_property_update(C, &ptr, prop);
-               
-               success= 1;
        }
        
        return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
@@ -213,12 +143,102 @@ void UI_OT_reset_default_button(wmOperatorType *ot)
        ot->description= "Copy the RNA data path for this property to the clipboard.";
 
        /* callbacks */
+       ot->poll= reset_default_button_poll;
        ot->exec= reset_default_button_exec;
-       //op->poll= ??? // TODO: need to have some valid property before this can be done
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array.");
+}
+
+/* Copy To Selected Operator ------------------------ */
+
+static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb)
+{
+       if(RNA_struct_is_a(ptr->type, &RNA_Object))
+               *lb = CTX_data_collection_get(C, "selected_editable_objects");
+       else if(RNA_struct_is_a(ptr->type, &RNA_EditBone))
+               *lb = CTX_data_collection_get(C, "selected_editable_bones");
+       else if(RNA_struct_is_a(ptr->type, &RNA_PoseBone))
+               *lb = CTX_data_collection_get(C, "selected_pose_bones");
+       else
+               return 0;
        
+       return 1;
+}
+
+static int copy_to_selected_button_poll(bContext *C)
+{
+       PointerRNA ptr;
+       PropertyRNA *prop;
+       int index, success= 0;
+
+       uiAnimContextProperty(C, &ptr, &prop, &index);
+
+       if (ptr.data && prop) {
+               CollectionPointerLink *link;
+               ListBase lb;
+
+               if(copy_to_selected_list(C, &ptr, &lb)) {
+                       for(link= lb.first; link; link=link->next)
+                               if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop))
+                                       success= 1;
+
+                       BLI_freelistN(&lb);
+               }
+       }
+
+       return success;
+}
+
+static int copy_to_selected_button_exec(bContext *C, wmOperator *op)
+{
+       PointerRNA ptr;
+       PropertyRNA *prop;
+       int success= 0;
+       int index, all = RNA_boolean_get(op->ptr, "all");
+
+       /* try to reset the nominated setting to its default value */
+       uiAnimContextProperty(C, &ptr, &prop, &index);
+       
+       /* if there is a valid property that is editable... */
+       if (ptr.data && prop) {
+               CollectionPointerLink *link;
+               ListBase lb;
+
+               if(copy_to_selected_list(C, &ptr, &lb)) {
+                       for(link= lb.first; link; link=link->next) {
+                               if(link->ptr.data != ptr.data && RNA_property_editable(&link->ptr, prop)) {
+                                       if(RNA_property_copy(&link->ptr, &ptr, prop, (all)? -1: index)) {
+                                               RNA_property_update(C, &link->ptr, prop);
+                                               success= 1;
+                                       }
+                               }
+                       }
+
+                       BLI_freelistN(&lb);
+               }
+       }
+       
+       return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
+}
+
+void UI_OT_copy_to_selected_button(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Copy To Selected";
+       ot->idname= "UI_OT_copy_to_selected_button";
+       ot->description= "Copy property from this object to selected objects or bones.";
+
+       /* callbacks */
+       ot->poll= copy_to_selected_button_poll;
+       ot->exec= copy_to_selected_button_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
        /* properties */
        RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array.");
 }
@@ -226,9 +246,10 @@ void UI_OT_reset_default_button(wmOperatorType *ot)
 /* ********************************************************* */
 /* Registration */
 
-void ui_buttons_operatortypes(void)
+void UI_buttons_operatortypes(void)
 {
-       WM_operatortype_append(UI_OT_copy_clipboard_button);
+       WM_operatortype_append(UI_OT_copy_data_path_button);
        WM_operatortype_append(UI_OT_reset_default_button);
+       WM_operatortype_append(UI_OT_copy_to_selected_button);
 }
 
index 8bd840f7e84cc880590f3b32020452eada6b21f7..6742df351d1ace7d50554359944cf19458438293 100644 (file)
@@ -1403,7 +1403,7 @@ void VIEW2D_OT_reset(wmOperatorType *ot)
 /* ********************************************************* */
 /* Registration */
 
-void ui_view2d_operatortypes(void)
+void UI_view2d_operatortypes(void)
 {
        WM_operatortype_append(VIEW2D_OT_pan);
        
index 8cf56ae3eda53b9e2319ba722c6a3d0c39ab0b08..90457c05fa794f8dfff5f9b7ae44fd0d4190435a 100644 (file)
@@ -101,7 +101,8 @@ void ED_spacetypes_init(void)
        ED_operatortypes_sound();
        ED_operatortypes_render();
        
-       ui_view2d_operatortypes();
+       UI_view2d_operatortypes();
+       UI_buttons_operatortypes();
        
        spacetypes = BKE_spacetypes_list();
        for(type=spacetypes->first; type; type=type->next)
index c2b36ddc915f3e5a248c8c9ebb467e4778aafe4f..c3f23e799d5c671265a67f12866a67582afb22c5 100644 (file)
@@ -722,6 +722,10 @@ void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA
 int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key);
 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop);
 
+/* copy/reset */
+int RNA_property_copy(PointerRNA *ptr, PointerRNA *fromptr, PropertyRNA *prop, int index);
+int RNA_property_reset(PointerRNA *ptr, PropertyRNA *prop, int index);
+
 /* Path
  *
  * Experimental method to refer to structs and properties with a string,
index 53f5407e521c389c84f09eaec2245287a63cc488..8ae0ebacff7a8f9479f2753571a81b71e3839d80 100644 (file)
@@ -4088,4 +4088,186 @@ int RNA_function_call_direct_va_lookup(bContext *C, ReportList *reports, Pointer
        return 0;
 }
 
+int RNA_property_reset(PointerRNA *ptr, PropertyRNA *prop, int index)
+{
+       int len;
+
+       /* get the length of the array to work with */
+       len= RNA_property_array_length(ptr, prop);
+       
+       /* get and set the default values as appropriate for the various types */
+       switch (RNA_property_type(prop)) {
+               case PROP_BOOLEAN:
+                       if (len) {
+                               if (index == -1) {
+                                       int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - boolean");
+                                       
+                                       RNA_property_boolean_get_default_array(ptr, prop, tmparray);
+                                       RNA_property_boolean_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       int value= RNA_property_boolean_get_default_index(ptr, prop, index);
+                                       RNA_property_boolean_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               int value= RNA_property_boolean_get_default(ptr, prop);
+                               RNA_property_boolean_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_INT:
+                       if (len) {
+                               if (index == -1) {
+                                       int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - int");
+                                       
+                                       RNA_property_int_get_default_array(ptr, prop, tmparray);
+                                       RNA_property_int_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       int value= RNA_property_int_get_default_index(ptr, prop, index);
+                                       RNA_property_int_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               int value= RNA_property_int_get_default(ptr, prop);
+                               RNA_property_int_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_FLOAT:
+                       if (len) {
+                               if (index == -1) {
+                                       float *tmparray= MEM_callocN(sizeof(float)*len, "reset_defaults - float");
+                                       
+                                       RNA_property_float_get_default_array(ptr, prop, tmparray);
+                                       RNA_property_float_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       float value= RNA_property_float_get_default_index(ptr, prop, index);
+                                       RNA_property_float_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               float value= RNA_property_float_get_default(ptr, prop);
+                               RNA_property_float_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_ENUM:
+               {
+                       int value= RNA_property_enum_get_default(ptr, prop);
+                       RNA_property_enum_set(ptr, prop, value);
+                       return 1;
+               }
+               
+               //case PROP_POINTER:
+               //case PROP_STRING:
+               default: 
+                       // FIXME: many of the other types such as strings and pointers need this implemented too!
+                       return 0;
+       }
+}
+       
+int RNA_property_copy(PointerRNA *ptr, PointerRNA *fromptr, PropertyRNA *prop, int index)
+{
+       int len, fromlen;
+
+       /* get the length of the array to work with */
+       len= RNA_property_array_length(ptr, prop);
+       fromlen= RNA_property_array_length(ptr, prop);
+
+       if(len != fromlen)
+               return 0;
+       
+       /* get and set the default values as appropriate for the various types */
+       switch (RNA_property_type(prop)) {
+               case PROP_BOOLEAN:
+                       if (len) {
+                               if (index == -1) {
+                                       int *tmparray= MEM_callocN(sizeof(int)*len, "copy - boolean");
+                                       
+                                       RNA_property_boolean_get_array(fromptr, prop, tmparray);
+                                       RNA_property_boolean_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       int value= RNA_property_boolean_get_index(fromptr, prop, index);
+                                       RNA_property_boolean_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               int value= RNA_property_boolean_get(fromptr, prop);
+                               RNA_property_boolean_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_INT:
+                       if (len) {
+                               if (index == -1) {
+                                       int *tmparray= MEM_callocN(sizeof(int)*len, "copy - int");
+                                       
+                                       RNA_property_int_get_array(fromptr, prop, tmparray);
+                                       RNA_property_int_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       int value= RNA_property_int_get_index(fromptr, prop, index);
+                                       RNA_property_int_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               int value= RNA_property_int_get(fromptr, prop);
+                               RNA_property_int_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_FLOAT:
+                       if (len) {
+                               if (index == -1) {
+                                       float *tmparray= MEM_callocN(sizeof(float)*len, "copy - float");
+                                       
+                                       RNA_property_float_get_array(fromptr, prop, tmparray);
+                                       RNA_property_float_set_array(ptr, prop, tmparray);
+                                       
+                                       MEM_freeN(tmparray);
+                               }
+                               else {
+                                       float value= RNA_property_float_get_index(fromptr, prop, index);
+                                       RNA_property_float_set_index(ptr, prop, index, value);
+                               }
+                       }
+                       else {
+                               float value= RNA_property_float_get(fromptr, prop);
+                               RNA_property_float_set(ptr, prop, value);
+                       }
+                       return 1;
+               case PROP_ENUM:
+               {
+                       int value= RNA_property_enum_get(fromptr, prop);
+                       RNA_property_enum_set(ptr, prop, value);
+                       return 1;
+               }
+               case PROP_POINTER:
+               {
+                       PointerRNA value= RNA_property_pointer_get(fromptr, prop);
+                       RNA_property_pointer_set(ptr, prop, value);
+                       return 1;
+               }
+               case PROP_STRING:
+               {
+                       char *value= RNA_property_string_get_alloc(fromptr, prop, NULL, 0);
+                       RNA_property_string_set(ptr, prop, value);
+                       MEM_freeN(value);
+                       return 1;
+               }
+               default:
+                       return 0;
+       }
+
+       return 0;
+}