adding menu slot Armature

adding menu slot ScriptTemplate
new script scripttemplate_mesh_edit is a template for an editmesh script.

The function Text makeCurrent() is a dummy until I can get it working when the script runs from a menu.
This commit is contained in:
Campbell Barton 2007-02-21 11:17:17 +00:00
parent 6831c04533
commit f71458b904
9 changed files with 213 additions and 5 deletions

@ -3,8 +3,8 @@
"""
Name: 'Armature Symmetry'
Blender: 242
Group: 'Animation'
Tooltip: 'Make an Armature symetrical'
Group: 'Armature'
Tooltip: 'Make an Armature symmetrical'
"""
__author__ = "Campbell Barton"

@ -0,0 +1,93 @@
#!BPY
"""
Name: 'Mesh Editing'
Blender: 243
Group: 'ScriptTemplate'
Tooltip: 'Add a new text for editing a mesh'
"""
from Blender import Text, Window
script_data = '''
#!BPY
"""
Name: 'My Mesh Script'
Blender: 243
Group: 'Mesh'
Tooltip: 'Put some useful info here'
"""
# Add a licence here if you wish to re-distribute, we recommend the GPL
from Blender import Scene, Mesh, Window, sys
import BPyMessages
def my_mesh_util(me):
# This function runs out of editmode with a mesh
# error cases are alredy checked for
# Remove these when writing your own tool
print me.name
print 'vert count', len(me.verts)
print 'edge count', len(me.edges)
print 'face count', len(me.faces)
# Examples
# Move selected verts on the x axis
"""
for v in me.verts:
if v.sel:
v.co.x += 1.0
"""
# Shrink selected faces
"""
for f in me.faces:
if f.sel:
c = f.cent
for v in f:
v.co = (c+v.co)/2
"""
def main():
# Gets the current scene, there can be many scenes in 1 blend file.
sce = Scene.GetCurrent()
# Get the active object, there can only ever be 1
# and the active object is always the editmode object.
ob_act = sce.objects.active
if not ob_act or ob_act.type != 'Mesh':
BPyMessages.Error_NoMeshActive()
return
# Saves the editmode state and go's out of
# editmode if its enabled, we cant make
# changes to the mesh data while in editmode.
is_editmode = Window.EditMode()
if is_editmode: Window.EditMode(1)
Window.WaitCursor(1)
me = ob_act.getData(mesh=1)
t = sys.time()
# Run the mesh editing function
my_mesh_util(me)
# Timing the script is a good way to be aware on any speed hits when scripting
print 'My Script finished in %.2f seconds' % (sys.time()-t)
Window.WaitCursor(0)
# This lets you can import the script without running it
if __name__ == '__main__':
main()
'''
new_text = Text.New('mesh_template.py')
new_text.write(script_data)
new_text.makeCurrent()
Window.RedrawAll()

@ -105,6 +105,10 @@ static int bpymenu_group_atoi( char *str )
return PYMENU_VERTEXPAINT;
else if( !strcmp( str, "UVCalculation" ) )
return PYMENU_UVCALCULATION;
else if( !strcmp( str, "Armature" ) )
return PYMENU_ARMATURE;
else if( !strcmp( str, "ScriptTemplate" ) )
return PYMENU_SCRIPTTEMPLATE;
/* "Misc" or an inexistent group name: use misc */
else
return PYMENU_MISC;
@ -173,6 +177,12 @@ char *BPyMenu_group_itoa( short menugroup )
case PYMENU_UVCALCULATION:
return "UVCalculation";
break;
case PYMENU_ARMATURE:
return "Armature";
break;
case PYMENU_SCRIPTTEMPLATE:
return "ScriptTemplate";
break;
case PYMENU_MISC:
return "Misc";
break;

@ -100,6 +100,8 @@ typedef enum {
PYMENU_WEIGHTPAINT,
PYMENU_VERTEXPAINT,
PYMENU_UVCALCULATION,
PYMENU_ARMATURE,
PYMENU_SCRIPTTEMPLATE,
PYMENU_HELP,/*Main Help menu items - prob best to leave for 'official' ones*/
PYMENU_HELPSYSTEM,/* Resources, troubleshooting, system tools */
PYMENU_HELPWEBSITES,/* Help -> Websites submenu */

@ -43,6 +43,11 @@
#include "gen_utils.h"
#include "../BPY_extern.h"
/* used only for makeCurrent, this may be deprecated when Blender.Base is implimented */
#include "BIF_screen.h"
#include "DNA_space_types.h"
#include "DNA_screen_types.h"
#define EXPP_TEXT_MODE_FOLLOW TXT_FOLLOW
/*****************************************************************************/
@ -106,6 +111,7 @@ static PyObject *Text_clear( BPy_Text * self );
static PyObject *Text_write( BPy_Text * self, PyObject * args );
static PyObject *Text_set( BPy_Text * self, PyObject * args );
static PyObject *Text_asLines( BPy_Text * self );
static PyObject *Text_makeCurrent( BPy_Text * self );
/*****************************************************************************/
/* Python BPy_Text methods table: */
@ -128,6 +134,8 @@ static PyMethodDef BPy_Text_methods[] = {
"(name, val) - Set attribute 'name' to value 'val'"},
{"asLines", ( PyCFunction ) Text_asLines, METH_NOARGS,
"() - Return text buffer as a list of lines"},
{"makeCurrent", ( PyCFunction ) Text_makeCurrent, METH_NOARGS,
"() - display this text."},
{NULL, NULL, 0, NULL}
};
@ -528,6 +536,23 @@ static PyObject *Text_asLines( BPy_Text * self )
return list;
}
static PyObject *Text_makeCurrent( BPy_Text * self )
{
/*
SpaceText *st= curarea->spacedata.first;
if( !self->text )
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"This object isn't linked to a Blender Text Object" );
if(st->spacetype!=SPACE_TEXT)
Py_RETURN_NONE;
st->text = self->text;
*/
Py_RETURN_NONE;
}
/*****************************************************************************/
/* Function: Text_dealloc */
/* Description: This is a callback function for the BPy_Text type. It is */

@ -363,7 +363,6 @@ struct PyMethodDef M_Window_methods[] = {
PyObject *M_Window_Redraw( PyObject * self, PyObject * args )
{
ScrArea *tempsa, *sa;
SpaceText *st;
int wintype = SPACE_VIEW3D;
short redraw_all = 0;

@ -125,3 +125,10 @@ class Text:
@rtype: list of strings
@return: A list of strings, one for each line in the buffer
"""
def makeCurrent():
"""
Display this text in the current 3d view if any
@rtype: None
@return: None
"""

@ -67,6 +67,7 @@
#include "BSE_filesel.h"
#include "BPY_extern.h"
#include "BPY_menus.h"
#include "blendef.h"
#include "mydevice.h"
@ -197,6 +198,37 @@ void do_text_buttons(unsigned short event)
}
}
static void do_text_template_scriptsmenu(void *arg, int event)
{
BPY_menu_do_python(PYMENU_SCRIPTTEMPLATE, event);
allqueue(REDRAWIMAGE, 0);
}
static uiBlock *text_template_scriptsmenu (void *args_unused)
{
uiBlock *block;
BPyMenu *pym;
int i= 0;
short yco = 20, menuwidth = 120;
block= uiNewBlock(&curarea->uiblocks, "text_template_scriptsmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
uiBlockSetButmFunc(block, do_text_template_scriptsmenu, NULL);
/* note that we acount for the N previous entries with i+20: */
for (pym = BPyMenuTable[PYMENU_SCRIPTTEMPLATE]; pym; pym = pym->next, i++) {
uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19,
NULL, 0.0, 0.0, 1, i,
pym->tooltip?pym->tooltip:pym->filename);
}
uiBlockSetDirection(block, UI_RIGHT);
uiTextBoundsBlock(block, 60);
return block;
}
/* action executed after clicking in File menu */
static void do_text_filemenu(void *arg, int event)
{
@ -577,13 +609,15 @@ static uiBlock *text_filemenu(void *arg_unused)
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "New|Alt N", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Open...|Alt O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
if(text) {
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Reopen|Alt R", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 3, "");
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Reopen|Alt R", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 3, "");
uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Save|Alt S", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Save As...", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Run Python Script|Alt P", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
}
uiDefIconTextBlockBut(block, text_template_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Script Templates", 0, yco-=20, 120, 19, "");
if(curarea->headertype==HEADERTOP) {
uiBlockSetDirection(block, UI_DOWN);

@ -3576,6 +3576,40 @@ static void do_view3d_edit_armaturemenu(void *arg, int event)
allqueue(REDRAWVIEW3D, 0);
}
static void do_view3d_scripts_armaturemenu(void *arg, int event)
{
BPY_menu_do_python(PYMENU_SCRIPTTEMPLATE, event);
allqueue(REDRAWIMAGE, 0);
}
static uiBlock *view3d_scripts_armaturemenu(void *args_unused)
{
uiBlock *block;
BPyMenu *pym;
int i= 0;
short yco = 20, menuwidth = 120;
block= uiNewBlock(&curarea->uiblocks, "view3d_scripts_armaturemenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
uiBlockSetButmFunc(block, do_view3d_scripts_armaturemenu, NULL);
/* note that we acount for the N previous entries with i+20: */
for (pym = BPyMenuTable[PYMENU_ARMATURE]; pym; pym = pym->next, i++) {
uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19,
NULL, 0.0, 0.0, 1, i,
pym->tooltip?pym->tooltip:pym->filename);
}
uiBlockSetDirection(block, UI_RIGHT);
uiTextBoundsBlock(block, 60);
return block;
}
static uiBlock *view3d_edit_armaturemenu(void *arg_unused)
{
bArmature *arm= G.obedit->data;
@ -3613,7 +3647,11 @@ static uiBlock *view3d_edit_armaturemenu(void *arg_unused)
uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
uiDefIconTextBlockBut(block, view3d_edit_armature_parentmenu, NULL, ICON_RIGHTARROW_THIN, "Parent", 0, yco-=20, 120, 19, "");
uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
uiDefIconTextBlockBut(block, view3d_scripts_armaturemenu, NULL, ICON_RIGHTARROW_THIN, "Scripts", 0, yco-=20, 120, 19, "");
if(curarea->headertype==HEADERTOP) {
uiBlockSetDirection(block, UI_DOWN);
}