UI: Add 'Copy Python Command' to menu
authorCampbell Barton <ideasman42@gmail.com>
Fri, 26 Feb 2016 00:57:35 +0000 (11:57 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 26 Feb 2016 01:00:36 +0000 (12:00 +1100)
This feature wasn't exposed anywhere in the interface.

source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_ops.c

index b8e3f6dc8210dc6346112e5cf2f8ea171e351a38..0ae505777a93a55a46ad8154613649374ecfb0d0 100644 (file)
@@ -6866,6 +6866,11 @@ static bool ui_but_menu(bContext *C, uiBut *but)
                }
        }
 
+       if (but->optype) {
+               uiItemO(layout, NULL,
+                       ICON_NONE, "UI_OT_copy_python_command_button");
+       }
+
        /* perhaps we should move this into (G.debug & G_DEBUG) - campbell */
        if (ui_block_is_menu(but->block) == false) {
                uiItemFullO(layout, "UI_OT_editsource", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0);
index 346c0879a787bc044fc7c3af2e5ee354518a5f1c..c707ac9eb523ac067735c2902b9c2f94c5159b01 100644 (file)
@@ -151,6 +151,53 @@ static void UI_OT_copy_data_path_button(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER;
 }
 
+static int copy_python_command_button_poll(bContext *C)
+{
+       uiBut *but = UI_context_active_but_get(C);
+
+       if (but && (but->optype != NULL)) {
+               return 1;
+       }
+
+       return 0;
+}
+
+static int copy_python_command_button_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       uiBut *but = UI_context_active_but_get(C);
+
+       if (but && (but->optype != NULL)) {
+               PointerRNA *opptr;
+               char *str;
+               opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */
+
+               str = WM_operator_pystring_ex(C, NULL, false, true, but->optype, opptr);
+
+               WM_clipboard_text_set(str, 0);
+
+               MEM_freeN(str);
+
+               return OPERATOR_FINISHED;
+       }
+
+       return OPERATOR_CANCELLED;
+}
+
+static void UI_OT_copy_python_command_button(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Copy Python Command";
+       ot->idname = "UI_OT_copy_python_command_button";
+       ot->description = "Copy the Python command matching this button";
+
+       /* callbacks */
+       ot->exec = copy_python_command_button_exec;
+       ot->poll = copy_python_command_button_poll;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER;
+}
+
 /* Reset to Default Values Button Operator ------------------------ */
 
 static int operator_button_property_finish(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
@@ -1044,6 +1091,7 @@ void ED_button_operatortypes(void)
 {
        WM_operatortype_append(UI_OT_reset_default_theme);
        WM_operatortype_append(UI_OT_copy_data_path_button);
+       WM_operatortype_append(UI_OT_copy_python_command_button);
        WM_operatortype_append(UI_OT_reset_default_button);
        WM_operatortype_append(UI_OT_unset_property_button);
        WM_operatortype_append(UI_OT_copy_to_selected_button);