Operator cheat sheet (from the help menu)

writes all operators (including PyOperators) and their default values into a textblock.
Useful for an overview and checking consistancy.

eg. http://www.pasteall.org/7918/python

added rna functions text.clear() and text.write(str)
This commit is contained in:
Campbell Barton 2009-09-16 06:02:56 +00:00
parent 8df1bb99f9
commit cc100eadc5
8 changed files with 173 additions and 5 deletions

@ -3,6 +3,7 @@ from bpy.__ops__ import add as op_add
from bpy.__ops__ import remove as op_remove
from bpy.__ops__ import dir as op_dir
from bpy.__ops__ import call as op_call
from bpy.__ops__ import as_string as op_as_string
from bpy.__ops__ import get_rna as op_get_rna
# Keep in sync with WM_types.h
@ -130,7 +131,10 @@ class bpy_ops_submodule_op(object):
return op_get_rna(self.idname())
def __repr__(self):
def __repr__(self): # useful display, repr(op)
return op_as_string(self.idname())
def __str__(self): # used for print(...)
return "<function bpy.ops.%s.%s at 0x%x'>" % (self.module, self.func, id(self))
import bpy

@ -188,6 +188,9 @@ class INFO_MT_help(bpy.types.Menu):
layout.itemO("help.blender_eshop")
layout.itemO("help.developer_community")
layout.itemO("help.user_community")
layout.itemS()
layout.itemO("help.operator_cheat_sheet")
bpy.types.register(INFO_HT_header)
bpy.types.register(INFO_MT_file)
@ -246,10 +249,37 @@ class HELP_OT_user_community(HelpOperator):
__label__ = "User Community"
__URL__ = 'http://www.blender.org/community/user-community/'
class HELP_OT_operator_cheat_sheet(bpy.types.Operator):
__idname__ = "help.operator_cheat_sheet"
__label__ = "Operator Cheet Sheet (new textblock)"
def execute(self, context):
op_strings = []
tot = 0
for op_module_name in dir(bpy.ops):
op_module = getattr(bpy.ops, op_module_name)
for op_submodule_name in dir(op_module):
op = getattr(op_module, op_submodule_name)
text = repr(op)
if text.startswith('bpy.ops.'):
op_strings.append(text)
tot += 1
op_strings.append('')
bpy.ops.text.new() # XXX - assumes new text is always at the end!
textblock = bpy.data.texts[-1]
textblock.write('# %d Operators\n\n' % tot)
textblock.write('\n'.join(op_strings))
textblock.name = "OperatorList.txt"
print("See OperatorList.txt textblock")
return ('FINISHED',)
bpy.ops.add(HELP_OT_manual)
bpy.ops.add(HELP_OT_release_logs)
bpy.ops.add(HELP_OT_blender_website)
bpy.ops.add(HELP_OT_blender_eshop)
bpy.ops.add(HELP_OT_developer_community)
bpy.ops.add(HELP_OT_user_community)
bpy.ops.add(HELP_OT_operator_cheat_sheet)

@ -48,6 +48,8 @@ int reopen_text (struct Text *text);
struct Text* add_text (char *file, const char *relpath);
struct Text* copy_text (struct Text *ta);
void unlink_text (struct Main *bmain, struct Text *text);
void clear_text(struct Text *text);
void write_text(struct Text *text, char *str);
char* txt_to_buf (struct Text *text);
void txt_clean_text (struct Text *text);
@ -74,7 +76,7 @@ void txt_delete_selected (struct Text *text);
void txt_sel_all (struct Text *text);
void txt_sel_line (struct Text *text);
char* txt_sel_to_buf (struct Text *text);
void txt_insert_buf (struct Text *text, char *in_buffer);
void txt_insert_buf (struct Text *text, const char *in_buffer);
void txt_print_undo (struct Text *text);
void txt_undo_add_toop (struct Text *text, int op, unsigned int froml, unsigned short fromc, unsigned int tol, unsigned short toc);
void txt_do_undo (struct Text *text);

@ -130,8 +130,10 @@ undo position
static void txt_pop_first(Text *text);
static void txt_pop_last(Text *text);
static void txt_undo_add_op(Text *text, int op);
static void txt_undo_add_block(Text *text, int op, char *buf);
static void txt_undo_add_block(Text *text, int op, const char *buf);
static void txt_delete_line(Text *text, TextLine *line);
static void txt_delete_sel (Text *text);
static void txt_make_dirty (Text *text);
/***/
@ -537,6 +539,30 @@ void unlink_text(Main *bmain, Text *text)
text->id.us= 0;
}
void clear_text(Text *text) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate( );
txt_set_undostate( 1 );
txt_sel_all( text );
txt_delete_sel(text);
txt_set_undostate( oldstate );
txt_make_dirty(text);
}
void write_text(Text *text, char *str) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate( );
txt_insert_buf( text, str );
txt_move_eof( text, 0 );
txt_set_undostate( oldstate );
txt_make_dirty(text);
}
/*****************************/
/* Editing utility functions */
@ -1315,7 +1341,7 @@ char *txt_sel_to_buf (Text *text)
return buf;
}
void txt_insert_buf(Text *text, char *in_buffer)
void txt_insert_buf(Text *text, const char *in_buffer)
{
int i=0, l=0, j, u, len;
TextLine *add;
@ -1544,7 +1570,7 @@ static void txt_undo_add_op(Text *text, int op)
text->undo_buf[text->undo_pos+1]= 0;
}
static void txt_undo_add_block(Text *text, int op, char *buf)
static void txt_undo_add_block(Text *text, int op, const char *buf)
{
int length;

@ -205,6 +205,7 @@ void RNA_api_main(struct StructRNA *srna);
void RNA_api_mesh(struct StructRNA *srna);
void RNA_api_object(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);
void RNA_api_wm(struct StructRNA *srna);

@ -223,6 +223,8 @@ static void rna_def_text(BlenderRNA *brna)
prop= RNA_def_property(srna, "markers", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_type(prop, "TextMarker");
RNA_def_property_ui_text(prop, "Markers", "Text markers highlighting part of the text.");
RNA_api_text(srna);
}
void RNA_def_text(BlenderRNA *brna)

@ -0,0 +1,49 @@
/**
*
* ***** 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.
*
* Contributor(s): Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include "RNA_define.h"
#include "RNA_types.h"
#ifdef RNA_RUNTIME
#else
void RNA_api_text(StructRNA *srna)
{
FunctionRNA *func;
PropertyRNA *prop;
func= RNA_def_function(srna, "clear", "clear_text");
RNA_def_function_ui_description(func, "clear the text block.");
func= RNA_def_function(srna, "write", "write_text");
RNA_def_function_ui_description(func, "write text at the cursor location and advance to the end of the text block.");
prop= RNA_def_string(func, "text", "Text", 0, "", "New text for this datablock.");
RNA_def_property_flag(prop, PROP_REQUIRED);
}
#endif

@ -118,6 +118,58 @@ static PyObject *pyop_call( PyObject * self, PyObject * args)
Py_RETURN_NONE;
}
static PyObject *pyop_as_string( PyObject * self, PyObject * args)
{
wmOperatorType *ot;
PointerRNA ptr;
char *opname;
PyObject *kw= NULL; /* optional args */
int all_args = 1;
int error_val= 0;
char *buf;
PyObject *pybuf;
bContext *C = BPy_GetContext();
if (!PyArg_ParseTuple(args, "s|O!i:bpy.__ops__.as_string", &opname, &PyDict_Type, &kw, &all_args))
return NULL;
ot= WM_operatortype_find(opname, TRUE);
if (ot == NULL) {
PyErr_Format( PyExc_SystemError, "bpy.__ops__.as_string: operator \"%s\"could not be found", opname);
return NULL;
}
/* WM_operator_properties_create(&ptr, opname); */
/* Save another lookup */
RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
if(kw && PyDict_Size(kw))
error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
if (error_val==0)
buf= WM_operator_pystring(C, ot, &ptr, all_args);
WM_operator_properties_free(&ptr);
if (error_val==-1) {
return NULL;
}
if(buf) {
pybuf= PyUnicode_FromString(buf);
MEM_freeN(buf);
}
else {
pybuf= PyUnicode_FromString("");
}
return pybuf;
}
static PyObject *pyop_dir(PyObject *self)
{
PyObject *list = PyList_New(0), *name;
@ -162,6 +214,7 @@ static PyObject *pyop_getrna(PyObject *self, PyObject *value)
PyObject *BPY_operator_module( void )
{
static PyMethodDef pyop_call_meth = {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
static PyMethodDef pyop_as_string_meth ={"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL};
static PyMethodDef pyop_dir_meth = {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
static PyMethodDef pyop_getrna_meth = {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
static PyMethodDef pyop_add_meth = {"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
@ -171,6 +224,7 @@ PyObject *BPY_operator_module( void )
PyDict_SetItemString(PySys_GetObject("modules"), "bpy.__ops__", submodule);
PyModule_AddObject( submodule, "call", PyCFunction_New(&pyop_call_meth, NULL) );
PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) );
PyModule_AddObject( submodule, "dir", PyCFunction_New(&pyop_dir_meth, NULL) );
PyModule_AddObject( submodule, "get_rna", PyCFunction_New(&pyop_getrna_meth, NULL) );
PyModule_AddObject( submodule, "add", PyCFunction_New(&pyop_add_meth, NULL) );