[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [23269] trunk/blender: Operator cheat sheet (from the help menu)

Campbell Barton ideasman42 at gmail.com
Wed Sep 16 08:02:56 CEST 2009


Revision: 23269
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=23269
Author:   campbellbarton
Date:     2009-09-16 08:02:56 +0200 (Wed, 16 Sep 2009)

Log Message:
-----------
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) 

Modified Paths:
--------------
    trunk/blender/release/ui/bpy_ops.py
    trunk/blender/release/ui/space_info.py
    trunk/blender/source/blender/blenkernel/BKE_text.h
    trunk/blender/source/blender/blenkernel/intern/text.c
    trunk/blender/source/blender/makesrna/intern/rna_internal.h
    trunk/blender/source/blender/makesrna/intern/rna_text.c
    trunk/blender/source/blender/python/intern/bpy_operator.c

Added Paths:
-----------
    trunk/blender/source/blender/makesrna/intern/rna_text_api.c

Modified: trunk/blender/release/ui/bpy_ops.py
===================================================================
--- trunk/blender/release/ui/bpy_ops.py	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/release/ui/bpy_ops.py	2009-09-16 06:02:56 UTC (rev 23269)
@@ -3,6 +3,7 @@
 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 @@
 		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

Modified: trunk/blender/release/ui/space_info.py
===================================================================
--- trunk/blender/release/ui/space_info.py	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/release/ui/space_info.py	2009-09-16 06:02:56 UTC (rev 23269)
@@ -188,6 +188,9 @@
 		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 @@
 	__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)
 

Modified: trunk/blender/source/blender/blenkernel/BKE_text.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_text.h	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/source/blender/blenkernel/BKE_text.h	2009-09-16 06:02:56 UTC (rev 23269)
@@ -48,6 +48,8 @@
 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_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);

Modified: trunk/blender/source/blender/blenkernel/intern/text.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/text.c	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/source/blender/blenkernel/intern/text.c	2009-09-16 06:02:56 UTC (rev 23269)
@@ -130,8 +130,10 @@
 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,7 +539,31 @@
 	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 @@
 	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 @@
 	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;
 	

Modified: trunk/blender/source/blender/makesrna/intern/rna_internal.h
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_internal.h	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/source/blender/makesrna/intern/rna_internal.h	2009-09-16 06:02:56 UTC (rev 23269)
@@ -205,6 +205,7 @@
 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);
 

Modified: trunk/blender/source/blender/makesrna/intern/rna_text.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_text.c	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/source/blender/makesrna/intern/rna_text.c	2009-09-16 06:02:56 UTC (rev 23269)
@@ -223,6 +223,8 @@
 	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)

Added: trunk/blender/source/blender/makesrna/intern/rna_text_api.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_text_api.c	                        (rev 0)
+++ trunk/blender/source/blender/makesrna/intern/rna_text_api.c	2009-09-16 06:02:56 UTC (rev 23269)
@@ -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

Modified: trunk/blender/source/blender/python/intern/bpy_operator.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_operator.c	2009-09-16 03:10:25 UTC (rev 23268)
+++ trunk/blender/source/blender/python/intern/bpy_operator.c	2009-09-16 06:02:56 UTC (rev 23269)
@@ -118,6 +118,58 @@
 	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 @@
 PyObject *BPY_operator_module( void )
 {
 	static PyMethodDef pyop_call_meth =		{"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list