== Sequencer ==
[plumiferos.git] / source / blender / python / api2_2x / Window.c
blob1624d01f0e678ecf21ec0622678bc9f2c9f398e9
1 /*
2 * $Id: Window.c 12650 2007-11-22 10:10:57Z campbellbarton $
4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
12 * about this.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * This is a new part of Blender.
28 * Contributor(s): Willian P. Germano, Tom Musgrove, Michael Reimpell,
29 * Yann Vernier, Ken Hughes
31 * ***** END GPL/BL DUAL LICENSE BLOCK *****
34 #include <Python.h>
36 #include "BDR_editobject.h" /* enter / leave editmode */
37 #include "BKE_global.h"
38 #include "BKE_main.h"
39 #include "BKE_object.h" /* for during_script() and during_scriptlink() */
40 #include "BKE_scene.h" /* scene_find_camera() */
41 #include "BPI_script.h"
42 #include "BIF_mywindow.h"
43 #include "BSE_headerbuttons.h"
44 #include "BSE_filesel.h"
45 #include "BIF_editmesh.h" /* for undo_push_mesh() */
46 #include "BIF_screen.h"
47 #include "BIF_space.h"
48 #include "BIF_drawtext.h"
49 #include "BIF_poseobject.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_text_types.h"
54 #include "DNA_object_types.h"
55 #include "mydevice.h"
56 #include "blendef.h" /* OBACT */
57 #include "windowTheme.h"
58 #include "Mathutils.h"
59 #include "constant.h"
60 #include "gen_utils.h"
61 #include "Armature.h"
63 /* Pivot Types
64 -0 for Bounding Box Center; \n\
65 -1 for 3D Cursor\n\
66 -2 for Individual Centers\n\
67 -3 for Median Point\n\
68 -4 for Active Object"; */
70 #define PIVOT_BOUNDBOX 0
71 #define PIVOT_CURSOR 1
72 #define PIVOT_INDIVIDUAL 2
73 #define PIVOT_MEDIAN 3
74 #define PIVOT_ACTIVE 4
76 /* See Draw.c */
77 extern int EXPP_disable_force_draw;
78 extern void setcameratoview3d(void);
80 /*****************************************************************************/
81 /* Python API function prototypes for the Window module. */
82 /*****************************************************************************/
83 PyObject *M_Window_Redraw( PyObject * self, PyObject * args );
84 static PyObject *M_Window_RedrawAll( PyObject * self, PyObject * args );
85 static PyObject *M_Window_QRedrawAll( PyObject * self, PyObject * args );
86 static PyObject *M_Window_DrawProgressBar( PyObject * self, PyObject * args );
87 static PyObject *M_Window_GetCursorPos( PyObject * self );
88 static PyObject *M_Window_SetCursorPos( PyObject * self, PyObject * args );
89 static PyObject *M_Window_WaitCursor( PyObject * self, PyObject * args );
90 static PyObject *M_Window_GetViewVector( PyObject * self );
91 static PyObject *M_Window_GetActiveLayer( PyObject * self );
92 static PyObject *M_Window_SetActiveLayer( PyObject * self, PyObject * args );
93 static PyObject *M_Window_GetViewQuat( PyObject * self );
94 static PyObject *M_Window_SetViewQuat( PyObject * self, PyObject * args );
95 static PyObject *M_Window_GetViewOffset( PyObject * self );
96 static PyObject *M_Window_SetViewOffset( PyObject * self, PyObject * args );
97 static PyObject *M_Window_GetViewMatrix( PyObject * self );
98 static PyObject *M_Window_GetPerspMatrix( PyObject * self );
99 static PyObject *M_Window_FileSelector( PyObject * self, PyObject * args );
100 static PyObject *M_Window_ImageSelector( PyObject * self, PyObject * args );
101 static PyObject *M_Window_EditMode( PyObject * self, PyObject * args );
102 static PyObject *M_Window_PoseMode( PyObject * self, PyObject * args );
103 static PyObject *M_Window_ViewLayers( PyObject * self, PyObject * args );
104 static PyObject *M_Window_GetViewLayerLock( PyObject * self, PyObject * args );
105 static PyObject *M_Window_SetViewLayerLock( PyObject * self, PyObject * args );
106 static PyObject *M_Window_CameraView( PyObject * self, PyObject * args );
107 static PyObject *M_Window_QTest( PyObject * self );
108 static PyObject *M_Window_QRead( PyObject * self );
109 static PyObject *M_Window_QAdd( PyObject * self, PyObject * args );
110 static PyObject *M_Window_QHandle( PyObject * self, PyObject * args );
111 static PyObject *M_Window_GetMouseCoords( PyObject * self );
112 static PyObject *M_Window_SetMouseCoords( PyObject * self, PyObject * args );
113 static PyObject *M_Window_GetMouseButtons( PyObject * self );
114 static PyObject *M_Window_GetKeyQualifiers( PyObject * self );
115 static PyObject *M_Window_SetKeyQualifiers( PyObject * self, PyObject * args );
116 static PyObject *M_Window_GetAreaSize( PyObject * self );
117 static PyObject *M_Window_GetAreaID( PyObject * self );
118 static PyObject *M_Window_GetScreenSize( PyObject * self );
119 static PyObject *M_Window_GetScreens( PyObject * self );
120 static PyObject *M_Window_SetScreen( PyObject * self, PyObject * value );
121 static PyObject *M_Window_GetScreenInfo( PyObject * self, PyObject * args,
122 PyObject * kwords );
123 static PyObject *M_Window_GetPivot( PyObject * self );
124 static PyObject *M_Window_SetPivot( PyObject * self, PyObject * value );
126 PyObject *Window_Init( void );
129 /*****************************************************************************/
130 /* The following string definitions are used for documentation strings. */
131 /* In Python these will be written to the console when doing a */
132 /* Blender.Window.__doc__ */
133 /*****************************************************************************/
134 static char M_Window_doc[] = "The Blender Window module\n\n";
136 static char M_Window_Redraw_doc[] =
137 "() - Force a redraw of a specific Window Type (see Window.Types)";
139 static char M_Window_RedrawAll_doc[] = "() - Redraw all windows";
141 static char M_Window_QRedrawAll_doc[] =
142 "() - Redraw all windows by queue event";
144 static char M_Window_FileSelector_doc[] =
145 "(callback [, title, filename]) - Open a file selector window.\n\
146 The selected file name is used as argument to a function callback f(name)\n\
147 that you must provide. 'title' is optional and defaults to 'SELECT FILE'.\n\
148 'filename' is optional and defaults to Blender.Get('filename').\n\n\
149 Example:\n\n\
150 import Blender\n\n\
151 def my_function(filename):\n\
152 print 'The selected file was: ', filename\n\n\
153 Blender.Window.FileSelector(my_function, 'SAVE FILE')\n";
155 static char M_Window_ImageSelector_doc[] =
156 "(callback [, title, filename]) - Open an image selector window.\n\
157 The selected file name is used as argument to a function callback f(name)\n\
158 that you must provide. 'title' is optional and defaults to 'SELECT IMAGE'.\n\
159 'filename' is optional and defaults to Blender.Get('filename').\n\n\
160 Example:\n\n\
161 import Blender\n\n\
162 def my_function(filename):\n\
163 print 'The selected image file was: ', filename\n\n\
164 Blender.Window.ImageSelector(my_function, 'LOAD IMAGE')\n";
166 static char M_Window_DrawProgressBar_doc[] =
167 "(done, text) - Draw a progress bar.\n\
168 'done' is a float value <= 1.0, 'text' contains info about what is\n\
169 currently being done.";
171 static char M_Window_GetCursorPos_doc[] =
172 "() - Get the current 3d cursor position as a list of three floats.";
174 static char M_Window_SetCursorPos_doc[] =
175 "([f,f,f]) - Set the current 3d cursor position from a list of three floats.";
177 static char M_Window_WaitCursor_doc[] =
178 "(bool) - Set cursor to wait mode (nonzero bool) or normal mode (0).";
180 static char M_Window_GetViewVector_doc[] =
181 "() - Get the current 3d view vector as a list of three floats [x,y,z].";
183 static char M_Window_GetActiveLayer_doc[] =
184 "() - Get the current 3d views active layer where new objects are created.";
186 static char M_Window_SetActiveLayer_doc[] =
187 "(int) - Set the current 3d views active layer where new objects are created.";
189 static char M_Window_GetViewMatrix_doc[] =
190 "() - Get the current 3d view matrix.";
192 static char M_Window_GetPerspMatrix_doc[] =
193 "() - Get the current 3d Persp matrix.";
195 static char M_Window_EditMode_doc[] =
196 "() - Get the current status -- 0: not in edit mode; 1: in edit mode.\n\
197 (status) - if 1: enter edit mode; if 0: leave edit mode.\n\
198 Returns the current status. This function is mostly useful to leave\n\
199 edit mode before applying changes to a mesh (otherwise the changes will\n\
200 be lost) and then returning to it upon leaving.";
202 static char M_Window_PoseMode_doc[] =
203 "() - Get the current status -- 0: not in pose mode; 1: in edit mode";
205 static char M_Window_ViewLayers_doc[] =
206 "(layers = [], winid = None) - Get/set active layers in all 3d View windows.\n\
207 () - Make no changes, only return currently visible layers.\n\
208 (layers = []) - a list of integers, each in the range [1, 20].\n\
209 (layers = [], winid = int) - layers as above, winid is an optional.\n\
210 arg that makes the function only set layers for that view.\n\
211 This function returns the currently visible layers as a list of ints.";
213 static char M_Window_GetViewLayerLock_doc[] =
214 "(winid = None) - Get the scene lock 3d View status.\n\
215 () - Return currently scene lock status.\n\
216 (winid = int) - scene lock status as above, winid is an option.\n\
217 arg that makes the function only get scene lock of the specified view.\n\
218 This function returns the currently scene lock status as a int.";
220 static char M_Window_SetViewLayerLock_doc[] =
221 "(vd_lock = 1/0, winid = None) - Set the scene lock 3d View status.\n\
222 (vd_lock = 1/0) - Set the scene lock status for the currently 3d View.\n\
223 (vd_lock = 1/0, winid = int) - the same as above, windid is an optional.\n\
224 arg that makes the function only set scene lock of the specified view.";
226 static char M_Window_GetViewQuat_doc[] =
227 "() - Get the current VIEW3D view quaternion values.";
229 static char M_Window_SetViewQuat_doc[] =
230 "(quat) - Set the current VIEW3D view quaternion values.\n\
231 (quat) - [f,f,f,f] or f,f,f,f: the new float values.";
233 static char M_Window_GetViewOffset_doc[] =
234 "() - Get the current VIEW3D view offset values.";
236 static char M_Window_SetViewOffset_doc[] =
237 "(ofs) - Set the current VIEW3D view offset values.\n\
238 (ofs) - [f,f,f] or f,f,f: the new float values.";
240 static char M_Window_CameraView_doc[] =
241 "(camtov3d = 0) - Set the current VIEW3D view to the active camera's view.\n\
242 (camtov3d = 0) - bool: if nonzero it's the camera that gets positioned at the\n\
243 current view, instead of the view being changed to that of the camera.\n\n\
244 If no camera is the active object, the active camera for the current scene\n\
245 is used.";
247 static char M_Window_QTest_doc[] =
248 "() - Check if there are pending events in the event queue.";
250 static char M_Window_QRead_doc[] =
251 "() - Get the next pending event from the event queue.\n\
252 This function returns a list [event, val], where:\n\
253 event - int: the key or mouse event (see Blender.Draw module);\n\
254 val - int: if 1 it's a key or mouse button press, if 0 a release. For\n\
255 mouse movement events 'val' returns the new coordinates in x or y.";
257 static char M_Window_QAdd_doc[] =
258 "(win, evt, val, after = 0) - Add an event to some window's event queue.\n\
259 (win) - int: the win id, see Blender.Window.GetScreenInfo();\n\
260 (evt) - int: the event number, see events in Blender.Draw;\n\
261 (val) - bool: 1 for a key press, 0 for a release;\n\
262 (after) - bool: if 1 the event is put after the current queue and added later.";
264 static char M_Window_QHandle_doc[] =
265 "(win) - Process all events for the given window (area) now.\n\
266 (win) - int: the window id, see Blender.Window.GetScreenInfo().\n\n\
267 See Blender.Window.QAdd() for how to send events to a particular window.";
269 static char M_Window_GetMouseCoords_doc[] =
270 "() - Get mouse pointer's current screen coordinates.";
272 static char M_Window_SetMouseCoords_doc[] =
273 "(x, y) - Set mouse pointer's current screen coordinates.\n\
274 (x,y) - ints ([x, y] also accepted): the new x, y coordinates.";
276 static char M_Window_GetMouseButtons_doc[] =
277 "() - Get the current mouse button state (see Blender.Window.MButs dict).";
279 static char M_Window_GetKeyQualifiers_doc[] =
280 "() - Get the current qualifier keys state.\n\
281 An int is returned: or'ed combination of values in Blender.Window.Qual's dict.";
283 static char M_Window_SetKeyQualifiers_doc[] =
284 "(qual) - Fake qualifier keys state.\n\
285 (qual) - int: an or'ed combination of the values in Blender.Window.Qual dict.\n\
286 Note: remember to reset to 0 after handling the related event (see QAdd()).";
288 static char M_Window_GetAreaID_doc[] =
289 "() - Get the current window's (area) ID.";
291 static char M_Window_GetAreaSize_doc[] =
292 "() - Get the current window's (area) size as [width, height].";
294 static char M_Window_GetScreenSize_doc[] =
295 "() - Get the screen's size as [width, height].";
297 static char M_Window_GetScreens_doc[] =
298 "() - Get a list with the names of all available screens.";
300 static char M_Window_SetScreen_doc[] =
301 "(name) - Set current screen to the one with the given 'name'.";
303 static char M_Window_GetScreenInfo_doc[] =
304 "(type = -1, rect = 'win', screen = None)\n\
305 - Get info about the the areas in the current screen setup.\n\
306 (type = -1) - int: the space type (Blender.Window.Types) to restrict the\n\
307 results to, all if -1;\n\
308 (rect = 'win') - str: the rectangle of interest. This defines if the corner\n\
309 coordinates returned will refer to:\n\
310 - the whole area: 'total';\n\
311 - only the header: 'header';\n\
312 - only the window content (default): 'win'.\n\
313 (screen = None) - str: the screen name, current if not given.\n\n\
314 A list of dictionaries (one for each area) is returned.\n\
315 Each dictionary has keys:\n\
316 'vertices': [xmin, ymin, xmax, ymax] area corners;\n\
317 'win': window type, see Blender.Window.Types dict;\n\
318 'id': area's id.";
320 static char M_Window_SetPivot_doc[] =
321 "(Pivot) - Set Pivot Mode for 3D Viewport:\n\
322 Options are: \n\
323 -PivotTypes.BOUNDBOX for Bounding Box Center; \n\
324 -PivotTypes.CURSOR for 3D Cursor\n\
325 -PivotTypes.INDIVIDUAL for Individual Centers\n\
326 -PivotTypes.MEDIAN for Median Point\n\
327 -PivotTypes.ACTIVE for Active Object";
329 static char M_Window_GetPivot_doc[] =
330 "Return the pivot for the active 3d window";
332 /*****************************************************************************/
333 /* Python method structure definition for Blender.Window module: */
334 /*****************************************************************************/
335 struct PyMethodDef M_Window_methods[] = {
336 {"Redraw", M_Window_Redraw, METH_VARARGS, M_Window_Redraw_doc},
337 {"RedrawAll", M_Window_RedrawAll, METH_VARARGS,
338 M_Window_RedrawAll_doc},
339 {"QRedrawAll", M_Window_QRedrawAll, METH_VARARGS,
340 M_Window_QRedrawAll_doc},
341 {"FileSelector", M_Window_FileSelector, METH_VARARGS,
342 M_Window_FileSelector_doc},
343 {"ImageSelector", ( PyCFunction ) M_Window_ImageSelector, METH_VARARGS,
344 M_Window_ImageSelector_doc},
345 {"DrawProgressBar", M_Window_DrawProgressBar, METH_VARARGS,
346 M_Window_DrawProgressBar_doc},
347 {"drawProgressBar", M_Window_DrawProgressBar, METH_VARARGS,
348 M_Window_DrawProgressBar_doc},
349 {"GetCursorPos", ( PyCFunction ) M_Window_GetCursorPos, METH_NOARGS,
350 M_Window_GetCursorPos_doc},
351 {"SetCursorPos", M_Window_SetCursorPos, METH_VARARGS,
352 M_Window_SetCursorPos_doc},
353 {"WaitCursor", M_Window_WaitCursor, METH_VARARGS,
354 M_Window_WaitCursor_doc},
355 {"GetViewVector", ( PyCFunction ) M_Window_GetViewVector, METH_NOARGS,
356 M_Window_GetViewVector_doc},
357 {"GetActiveLayer", ( PyCFunction ) M_Window_GetActiveLayer, METH_NOARGS,
358 M_Window_GetActiveLayer_doc},
359 {"SetActiveLayer", ( PyCFunction ) M_Window_SetActiveLayer, METH_VARARGS,
360 M_Window_SetActiveLayer_doc},
361 {"GetViewQuat", ( PyCFunction ) M_Window_GetViewQuat, METH_NOARGS,
362 M_Window_GetViewQuat_doc},
363 {"SetViewQuat", ( PyCFunction ) M_Window_SetViewQuat, METH_VARARGS,
364 M_Window_SetViewQuat_doc},
365 {"GetViewOffset", ( PyCFunction ) M_Window_GetViewOffset, METH_NOARGS,
366 M_Window_GetViewOffset_doc},
367 {"SetViewOffset", ( PyCFunction ) M_Window_SetViewOffset, METH_VARARGS,
368 M_Window_SetViewOffset_doc},
369 {"GetViewMatrix", ( PyCFunction ) M_Window_GetViewMatrix, METH_NOARGS,
370 M_Window_GetViewMatrix_doc},
371 {"GetPerspMatrix", ( PyCFunction ) M_Window_GetPerspMatrix, METH_NOARGS,
372 M_Window_GetPerspMatrix_doc},
373 {"EditMode", ( PyCFunction ) M_Window_EditMode, METH_VARARGS,
374 M_Window_EditMode_doc},
375 {"PoseMode", ( PyCFunction ) M_Window_PoseMode, METH_VARARGS,
376 M_Window_PoseMode_doc},
377 {"ViewLayers", ( PyCFunction ) M_Window_ViewLayers, METH_VARARGS,
378 M_Window_ViewLayers_doc},
379 {"GetViewLayerLock", ( PyCFunction ) M_Window_GetViewLayerLock, METH_VARARGS,
380 M_Window_GetViewLayerLock_doc},
381 {"SetViewLayerLock", ( PyCFunction ) M_Window_SetViewLayerLock, METH_VARARGS,
382 M_Window_SetViewLayerLock_doc},
383 /* typo, deprecate someday: */
384 {"ViewLayer", ( PyCFunction ) M_Window_ViewLayers, METH_VARARGS,
385 M_Window_ViewLayers_doc},
386 {"CameraView", ( PyCFunction ) M_Window_CameraView, METH_VARARGS,
387 M_Window_CameraView_doc},
388 {"QTest", ( PyCFunction ) M_Window_QTest, METH_NOARGS,
389 M_Window_QTest_doc},
390 {"QRead", ( PyCFunction ) M_Window_QRead, METH_NOARGS,
391 M_Window_QRead_doc},
392 {"QAdd", ( PyCFunction ) M_Window_QAdd, METH_VARARGS,
393 M_Window_QAdd_doc},
394 {"QHandle", ( PyCFunction ) M_Window_QHandle, METH_VARARGS,
395 M_Window_QHandle_doc},
396 {"GetMouseCoords", ( PyCFunction ) M_Window_GetMouseCoords,
397 METH_NOARGS,
398 M_Window_GetMouseCoords_doc},
399 {"SetMouseCoords", ( PyCFunction ) M_Window_SetMouseCoords,
400 METH_VARARGS,
401 M_Window_SetMouseCoords_doc},
402 {"GetMouseButtons", ( PyCFunction ) M_Window_GetMouseButtons,
403 METH_NOARGS,
404 M_Window_GetMouseButtons_doc},
405 {"GetKeyQualifiers", ( PyCFunction ) M_Window_GetKeyQualifiers,
406 METH_NOARGS,
407 M_Window_GetKeyQualifiers_doc},
408 {"SetKeyQualifiers", ( PyCFunction ) M_Window_SetKeyQualifiers,
409 METH_VARARGS,
410 M_Window_SetKeyQualifiers_doc},
411 {"GetAreaSize", ( PyCFunction ) M_Window_GetAreaSize, METH_NOARGS,
412 M_Window_GetAreaSize_doc},
413 {"GetAreaID", ( PyCFunction ) M_Window_GetAreaID, METH_NOARGS,
414 M_Window_GetAreaID_doc},
415 {"GetScreenSize", ( PyCFunction ) M_Window_GetScreenSize, METH_NOARGS,
416 M_Window_GetScreenSize_doc},
417 {"GetScreens", ( PyCFunction ) M_Window_GetScreens, METH_NOARGS,
418 M_Window_GetScreens_doc},
419 {"SetScreen", ( PyCFunction ) M_Window_SetScreen, METH_O,
420 M_Window_SetScreen_doc},
421 {"GetScreenInfo", ( PyCFunction ) M_Window_GetScreenInfo,
422 METH_VARARGS | METH_KEYWORDS, M_Window_GetScreenInfo_doc},
423 {"GetPivot", ( PyCFunction ) M_Window_GetPivot, METH_NOARGS,
424 M_Window_GetPivot_doc},
425 {"SetPivot", ( PyCFunction ) M_Window_SetPivot, METH_O,
426 M_Window_SetPivot_doc},
427 {NULL, NULL, 0, NULL}
430 /*****************************************************************************/
431 /* Function: M_Window_Redraw */
432 /* Python equivalent: Blender.Window.Redraw */
433 /*****************************************************************************/
434 /* not static so py_slider_update in Draw.[ch] can use it */
435 PyObject *M_Window_Redraw( PyObject * self, PyObject * args )
437 ScrArea *tempsa, *sa;
438 int wintype = SPACE_VIEW3D;
439 short redraw_all = 0;
441 if( !PyArg_ParseTuple( args, "|i", &wintype ) )
442 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
443 "expected int argument (or nothing)" ) );
445 if( wintype < 0 )
446 redraw_all = 1;
448 if( !during_script( ) && !G.background ) {
449 tempsa = curarea;
450 sa = G.curscreen->areabase.first;
452 while( sa ) {
454 if( sa->spacetype == wintype || redraw_all ) {
455 if (sa->spacetype == SPACE_SCRIPT && EXPP_disable_force_draw) {
456 scrarea_queue_redraw(sa);
458 else {
459 /* do not call fancy hacks here like pop_space_text(st); (ton) */
460 scrarea_do_windraw( sa );
461 if( sa->headwin ) scrarea_do_headdraw( sa );
464 sa = sa->next;
467 if( curarea != tempsa )
468 areawinset( tempsa->win );
470 if( curarea ) { /* is null if Blender is in bg mode */
471 if( curarea->headwin )
472 scrarea_do_headdraw( curarea );
473 screen_swapbuffers( );
477 Py_RETURN_NONE;
480 /*****************************************************************************/
481 /* Function: M_Window_RedrawAll */
482 /* Python equivalent: Blender.Window.RedrawAll */
483 /*****************************************************************************/
484 static PyObject *M_Window_RedrawAll( PyObject * self, PyObject * args )
486 PyObject *arg = Py_BuildValue( "(i)", -1 );
487 PyObject *ret = M_Window_Redraw( self, arg );
488 Py_DECREF(arg);
489 return ret;
492 /*****************************************************************************/
493 /* Function: M_Window_QRedrawAll */
494 /* Python equivalent: Blender.Window.QRedrawAll */
495 /*****************************************************************************/
496 static PyObject *M_Window_QRedrawAll( PyObject * self, PyObject * args )
498 EXPP_allqueue( REDRAWALL, 0 );
499 Py_RETURN_NONE;
502 /*****************************************************************************/
503 /* Function: M_Window_FileSelector */
504 /* Python equivalent: Blender.Window.FileSelector */
505 /*****************************************************************************/
507 /* This is the callback to "activate_fileselect" below. It receives the
508 * selected filename and (using it as argument) calls the Python callback
509 * provided by the script writer and stored in EXPP_FS_PyCallback. */
511 static void getSelectedFile( char *name )
513 PyObject *pycallback;
514 PyObject *result;
515 Script *script;
517 /* let's find the script that owns this callback */
518 script = G.main->script.first;
519 while (script) {
520 if (script->flags & SCRIPT_RUNNING) break;
521 script = script->id.next;
524 if (!script) {
525 if (curarea->spacetype == SPACE_SCRIPT) {
526 SpaceScript *sc = curarea->spacedata.first;
527 script = sc->script;
531 pycallback = script->py_browsercallback;
533 if (pycallback) {
534 result = PyObject_CallFunction( pycallback, "s", name );
536 if (!result) {
537 if (G.f & G_DEBUG)
538 fprintf(stderr, "BPy error: Callback call failed!\n");
540 else Py_DECREF(result);
542 if (script->py_browsercallback == pycallback)
543 script->py_browsercallback = NULL;
544 /* else another call to selector was made inside pycallback */
546 Py_DECREF(pycallback);
549 return;
552 static PyObject *M_Window_FileSelector( PyObject * self, PyObject * args )
554 char *title = "SELECT FILE";
555 char *filename = G.sce;
556 SpaceScript *sc;
557 Script *script = NULL;
558 PyObject *pycallback = NULL;
559 int startspace = 0;
561 if (during_scriptlink())
562 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
563 "script links can't call the file selector");
565 if (G.background)
566 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
567 "the file selector is not available in background mode");
569 if((!PyArg_ParseTuple( args, "O|ss", &pycallback, &title, &filename))
570 || (!PyCallable_Check(pycallback)))
571 return EXPP_ReturnPyObjError( PyExc_AttributeError,
572 "\nexpected a callback function (and optionally one or two strings) "
573 "as argument(s)" );
575 Py_INCREF(pycallback);
577 /* trick: we move to a spacescript because then the fileselector will properly
578 * unset our SCRIPT_FILESEL flag when the user chooses a file or cancels the
579 * selection. This is necessary because when a user cancels, the
580 * getSelectedFile function above doesn't get called and so couldn't unset the
581 * flag. */
582 startspace = curarea->spacetype;
583 if( startspace != SPACE_SCRIPT )
584 newspace( curarea, SPACE_SCRIPT );
586 sc = curarea->spacedata.first;
588 /* let's find the script that called us */
589 script = G.main->script.first;
590 while (script) {
591 if (script->flags & SCRIPT_RUNNING) break;
592 script = script->id.next;
595 if( !script ) {
596 /* if not running, then we were already on a SpaceScript space, executing
597 * a registered callback -- aka: this script has a gui */
598 script = sc->script; /* this is the right script */
599 } else { /* still running, use the trick */
600 script->lastspace = startspace;
601 sc->script = script;
604 script->flags |= SCRIPT_FILESEL;
606 /* clear any previous callback (nested calls to selector) */
607 if (script->py_browsercallback) {
608 Py_DECREF((PyObject *)script->py_browsercallback);
610 script->py_browsercallback = pycallback;
612 activate_fileselect( FILE_BLENDER, title, filename, getSelectedFile );
614 Py_RETURN_NONE;
617 static PyObject *M_Window_ImageSelector( PyObject * self, PyObject * args )
619 char *title = "SELECT IMAGE";
620 char *filename = G.sce;
621 SpaceScript *sc;
622 Script *script = NULL;
623 PyObject *pycallback = NULL;
624 int startspace = 0;
626 if (during_scriptlink())
627 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
628 "script links can't call the image selector");
630 if (G.background)
631 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
632 "the image selector is not available in background mode");
634 if( !PyArg_ParseTuple( args, "O|ss", &pycallback, &title, &filename )
635 || (!PyCallable_Check(pycallback)))
636 return EXPP_ReturnPyObjError ( PyExc_AttributeError,
637 "\nexpected a callback function (and optionally one or two strings) "
638 "as argument(s)" );
640 Py_INCREF(pycallback);
642 /* trick: we move to a spacescript because then the fileselector will properly
643 * unset our SCRIPT_FILESEL flag when the user chooses a file or cancels the
644 * selection. This is necessary because when a user cancels, the
645 * getSelectedFile function above doesn't get called and so couldn't unset the
646 * flag. */
647 startspace = curarea->spacetype;
648 if( startspace != SPACE_SCRIPT )
649 newspace( curarea, SPACE_SCRIPT );
651 sc = curarea->spacedata.first;
653 /* let's find the script that called us */
654 script = G.main->script.first;
655 while (script) {
656 if (script->flags & SCRIPT_RUNNING) break;
657 script = script->id.next;
660 if( !script ) {
661 /* if not running, then we were already on a SpaceScript space, executing
662 * a registered callback -- aka: this script has a gui */
663 script = sc->script; /* this is the right script */
664 } else { /* still running, use the trick */
665 script->lastspace = startspace;
666 sc->script = script;
669 script->flags |= SCRIPT_FILESEL; /* same flag as filesel */
670 /* clear any previous callback (nested calls to selector) */
671 if (script->py_browsercallback) {
672 Py_DECREF((PyObject *)script->py_browsercallback);
674 script->py_browsercallback = pycallback;
676 activate_imageselect( FILE_BLENDER, title, filename, getSelectedFile );
678 Py_RETURN_NONE;
681 /*****************************************************************************/
682 /* Function: M_Window_DrawProgressBar */
683 /* Python equivalent: Blender.Window.DrawProgressBar */
684 /*****************************************************************************/
685 static PyObject *M_Window_DrawProgressBar( PyObject * self, PyObject * args )
687 float done;
688 char *info = NULL;
689 int retval = 0;
690 ScrArea *sa = curarea;
692 if (G.background)
693 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
694 "the progress bar is not available in background mode");
696 if( !PyArg_ParseTuple( args, "fs", &done, &info ) )
697 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
698 "expected a float and a string as arguments" ) );
700 retval = progress_bar( done, info );
702 areawinset(sa->win);
704 return Py_BuildValue( "i", retval );
707 /*****************************************************************************/
708 /* Function: M_Window_GetCursorPos */
709 /* Python equivalent: Blender.Window.GetCursorPos */
710 /*****************************************************************************/
711 static PyObject *M_Window_GetCursorPos( PyObject * self )
713 float *cursor = NULL;
714 PyObject *pylist;
716 if( G.vd && G.vd->localview )
717 cursor = G.vd->cursor;
718 else
719 cursor = G.scene->cursor;
721 pylist = Py_BuildValue( "[fff]", cursor[0], cursor[1], cursor[2] );
723 if( !pylist )
724 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
725 "GetCursorPos: couldn't create pylist" ) );
727 return pylist;
730 /*****************************************************************************/
731 /* Function: M_Window_SetCursorPos */
732 /* Python equivalent: Blender.Window.SetCursorPos */
733 /*****************************************************************************/
734 static PyObject *M_Window_SetCursorPos( PyObject * self, PyObject * args )
736 int ok = 0;
737 float val[3];
739 if( PyObject_Length( args ) == 3 )
740 ok = PyArg_ParseTuple( args, "fff", &val[0], &val[1],
741 &val[2] );
742 else
743 ok = PyArg_ParseTuple( args, "(fff)", &val[0], &val[1],
744 &val[2] );
746 if( !ok )
747 return EXPP_ReturnPyObjError( PyExc_TypeError,
748 "expected [f,f,f] or f,f,f as arguments" );
750 if( G.vd && G.vd->localview ) {
751 G.vd->cursor[0] = val[0];
752 G.vd->cursor[1] = val[1];
753 G.vd->cursor[2] = val[2];
754 } else {
755 G.scene->cursor[0] = val[0];
756 G.scene->cursor[1] = val[1];
757 G.scene->cursor[2] = val[2];
760 Py_RETURN_NONE;
763 static PyObject *M_Window_WaitCursor( PyObject * self, PyObject * args )
765 int bool;
767 if( !PyArg_ParseTuple( args, "i", &bool ) )
768 return EXPP_ReturnPyObjError( PyExc_TypeError,
769 "expected bool (0 or 1) or nothing as argument" );
771 waitcursor( bool ); /* nonzero bool sets, zero unsets */
773 Py_RETURN_NONE;
776 /*****************************************************************************/
777 /* Function: M_Window_GetViewVector */
778 /* Python equivalent: Blender.Window.GetViewVector */
779 /*****************************************************************************/
780 static PyObject *M_Window_GetViewVector( PyObject * self )
782 float *vec = NULL;
783 PyObject *pylist;
785 if( !G.vd )
786 Py_RETURN_NONE;
788 vec = G.vd->viewinv[2];
790 pylist = Py_BuildValue( "[fff]", vec[0], vec[1], vec[2] );
792 if( !pylist )
793 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
794 "GetViewVector: couldn't create pylist" ) );
796 return pylist;
799 /*****************************************************************************/
800 /* Function: M_Window_GetActiveLayer */
801 /* Python equivalent: Blender.Window.GetActiveLayer */
802 /*****************************************************************************/
803 static PyObject *M_Window_GetActiveLayer( PyObject * self )
805 if (!G.vd) {
806 return PyInt_FromLong(0);
807 } else {
808 return PyInt_FromLong( G.vd->layact );
812 static PyObject *M_Window_SetActiveLayer( PyObject * self, PyObject * args )
814 int layer, bit=1;
815 if( !PyArg_ParseTuple( args, "i", &layer ) )
816 return ( EXPP_ReturnPyObjError( PyExc_TypeError,
817 "expected an int" ) );
819 if (!G.vd)
820 Py_RETURN_FALSE;
822 bit= 0;
823 while(bit<32) {
824 if(layer & (1<<bit)) {
825 G.vd->layact= 1<<bit;
826 G.vd->lay |= G.vd->layact;
828 if (G.vd->scenelock) {
829 G.scene->lay |= G.vd->layact;
831 bit = -1; /* no error */
832 break;
834 bit++;
837 if (bit != -1)
838 return ( EXPP_ReturnPyObjError( PyExc_ValueError,
839 "The flag could not be used for the active layer" ) );
841 Py_RETURN_NONE;
844 static PyObject *M_Window_GetViewQuat( PyObject * self )
846 float *vec = NULL;
847 PyObject *pylist;
849 if( !G.vd )
850 Py_RETURN_NONE;
852 vec = G.vd->viewquat;
854 pylist = Py_BuildValue( "[ffff]", vec[0], vec[1], vec[2], vec[3] );
856 if( !pylist )
857 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
858 "GetViewQuat: couldn't create pylist" ) );
860 return pylist;
863 static PyObject *M_Window_SetViewQuat( PyObject * self, PyObject * args )
865 int ok = 0;
866 float val[4];
868 if( !G.vd )
869 Py_RETURN_NONE;
871 if( PyObject_Length( args ) == 4 )
872 ok = PyArg_ParseTuple( args, "ffff", &val[0], &val[1], &val[2],
873 &val[3] );
874 else
875 ok = PyArg_ParseTuple( args, "(ffff)", &val[0], &val[1],
876 &val[2], &val[3] );
878 if( !ok )
879 return EXPP_ReturnPyObjError( PyExc_TypeError,
880 "expected [f,f,f,f] or f,f,f,f as arguments" );
882 G.vd->viewquat[0] = val[0];
883 G.vd->viewquat[1] = val[1];
884 G.vd->viewquat[2] = val[2];
885 G.vd->viewquat[3] = val[3];
887 Py_RETURN_NONE;
890 static PyObject *M_Window_GetViewOffset( PyObject * self )
892 float *vec = NULL;
893 PyObject *pylist;
895 if( !G.vd )
896 Py_RETURN_NONE;
898 vec = G.vd->ofs;
900 pylist = Py_BuildValue( "[fff]", vec[0], vec[1], vec[2] );
902 if( !pylist )
903 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
904 "GetViewQuat: couldn't create pylist" ) );
906 return pylist;
909 static PyObject *M_Window_SetViewOffset( PyObject * self, PyObject * args )
911 int ok = 0;
912 float val[3];
914 if( !G.vd )
915 Py_RETURN_NONE;
917 if( PyObject_Length( args ) == 3 )
918 ok = PyArg_ParseTuple( args, "fff", &val[0], &val[1],
919 &val[2] );
920 else
921 ok = PyArg_ParseTuple( args, "(fff)", &val[0], &val[1],
922 &val[2] );
924 if( !ok )
925 return EXPP_ReturnPyObjError( PyExc_TypeError,
926 "expected [f,f,f] or f,f,f as arguments" );
928 G.vd->ofs[0] = val[0];
929 G.vd->ofs[1] = val[1];
930 G.vd->ofs[2] = val[2];
932 Py_RETURN_NONE;
936 /*****************************************************************************/
937 /* Function: M_Window_GetViewMatrix */
938 /* Python equivalent: Blender.Window.GetViewMatrix */
939 /*****************************************************************************/
940 static PyObject *M_Window_GetViewMatrix( PyObject * self )
942 PyObject *viewmat;
944 if( !G.vd )
945 Py_RETURN_NONE;
947 viewmat =
948 ( PyObject * ) newMatrixObject( ( float * ) G.vd->viewmat, 4,
949 4, Py_WRAP );
951 if( !viewmat )
952 return EXPP_ReturnPyObjError( PyExc_MemoryError,
953 "GetViewMatrix: couldn't create matrix pyobject" );
955 return viewmat;
958 /*****************************************************************************/
959 /* Function: M_Window_GetPerspMatrix */
960 /* Python equivalent: Blender.Window.GetPerspMatrix */
961 /*****************************************************************************/
962 static PyObject *M_Window_GetPerspMatrix( PyObject * self )
964 PyObject *perspmat;
966 if( !G.vd )
967 Py_RETURN_NONE;
969 perspmat =
970 ( PyObject * ) newMatrixObject( ( float * ) G.vd->persmat, 4,
971 4, Py_WRAP );
973 if( !perspmat )
974 return EXPP_ReturnPyObjError( PyExc_MemoryError,
975 "GetPerspMatrix: couldn't create matrix pyobject" );
977 return perspmat;
981 /* update_armature_weakrefs()
982 * helper function used in M_Window_EditMode.
983 * rebuilds list of Armature weakrefs in __main__
986 static int update_armature_weakrefs()
988 /* stuff for armature weak refs */
989 char *list_name = ARM_WEAKREF_LIST_NAME;
990 PyObject *maindict = NULL, *armlist = NULL;
991 PyObject *pyarmature = NULL;
992 int x;
994 maindict= PyModule_GetDict(PyImport_AddModule( "__main__"));
995 armlist = PyDict_GetItemString(maindict, list_name);
996 if( !armlist){
997 printf("Oops - update_armature_weakrefs()\n");
998 return 0;
1001 for (x = 0; x < PySequence_Size(armlist); x++){
1002 pyarmature = PyWeakref_GetObject(PySequence_GetItem( armlist, x));
1003 if (pyarmature != Py_None)
1004 Armature_RebuildEditbones(pyarmature);
1006 return 1;
1010 static PyObject *M_Window_EditMode( PyObject * self, PyObject * args )
1012 short status = -1;
1013 char *undo_str = "From script";
1014 int undo_str_len = 11;
1015 int do_undo = 1;
1017 if( !PyArg_ParseTuple( args,
1018 "|hs#i", &status, &undo_str, &undo_str_len, &do_undo ) )
1019 return EXPP_ReturnPyObjError( PyExc_TypeError,
1020 "expected optional int (bool), string and int (bool) as arguments" );
1022 if( status >= 0 ) {
1023 if( status ) {
1024 if( !G.obedit ){
1026 //update armatures
1027 if(! update_armature_weakrefs()){
1028 return EXPP_ReturnPyObjError(
1029 PyExc_RuntimeError,
1030 "internal error - update_armature_weakrefs");
1033 //enter editmode
1034 enter_editmode(0);
1036 } else if( G.obedit ) {
1037 if( undo_str_len > 63 )
1038 undo_str[63] = '\0'; /* 64 is max */
1039 BIF_undo_push( undo_str ); /* This checks user undo settings */
1040 exit_editmode( EM_FREEDATA );
1042 //update armatures
1043 if(! update_armature_weakrefs()){
1044 return EXPP_ReturnPyObjError(
1045 PyExc_RuntimeError,
1046 "internal error - update_armature_weakrefs");
1052 return Py_BuildValue( "h", G.obedit ? 1 : 0 );
1055 static PyObject *M_Window_PoseMode( PyObject * self, PyObject * args )
1057 short status = -1;
1058 short is_posemode = 0;
1059 Base *base;
1061 if( !PyArg_ParseTuple( args, "|h", &status ) )
1062 return EXPP_ReturnPyObjError( PyExc_TypeError,
1063 "expected optional int (bool) as argument" );
1065 if( status >= 0 ) {
1066 if( status ) {
1067 enter_posemode();
1068 } else if( G.obedit ) {
1069 exit_posemode();
1073 base= BASACT;
1074 if (base && base->object->flag & OB_POSEMODE) {
1075 is_posemode = 1;
1078 return Py_BuildValue( "h", is_posemode );
1081 static PyObject *M_Window_ViewLayers( PyObject * self, PyObject * args )
1083 PyObject *item = NULL;
1084 PyObject *list = NULL, *resl = NULL;
1085 int val, i, bit = 0, layer = 0, len_list;
1086 short winid = -1;
1088 if( !G.scene ) {
1089 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1090 "can't get pointer to global scene" );
1093 /* Pase Args, Nothing, One list, Or a list and an int */
1094 if (PyTuple_GET_SIZE(args)!=0) {
1095 if( !PyArg_ParseTuple ( args, "O!|h", &PyList_Type, &list, &winid) ) {
1096 return EXPP_ReturnPyObjError( PyExc_TypeError,
1097 "nothing or a list and optionaly a window ID argument" );
1101 if( list ) {
1102 len_list = PyList_Size(list);
1104 if (len_list == 0)
1105 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1106 "list can't be empty, at list one layer must be set" );
1108 for( i = 0; i < len_list; i++ ) {
1109 item = PyList_GetItem( list, i );
1110 if( !PyInt_Check( item ) )
1111 return EXPP_ReturnPyObjError
1112 ( PyExc_AttributeError,
1113 "list must contain only integer numbers" );
1115 val = ( int ) PyInt_AsLong( item );
1116 if( val < 1 || val > 20 )
1117 return EXPP_ReturnPyObjError
1118 ( PyExc_AttributeError,
1119 "layer values must be in the range [1, 20]" );
1121 layer |= 1 << ( val - 1 );
1124 if (winid==-1) {
1125 printf("Doing WINIID NOT!!!\n");
1126 /* set scene and viewport */
1127 G.scene->lay = layer;
1128 if (G.vd) {
1129 G.vd->lay = layer;
1131 while( bit < 20 ) {
1132 val = 1 << bit;
1133 if( layer & val ) {
1134 G.vd->layact = val;
1135 break;
1137 bit++;
1140 } else {
1141 /* only set the windows layer */
1142 ScrArea *sa;
1143 SpaceLink *sl;
1144 View3D *vd;
1146 if (G.curscreen) { /* can never be too careful */
1147 for (sa=G.curscreen->areabase.first; sa; sa= sa->next) {
1148 if (winid == sa->win) {
1150 for (sl= sa->spacedata.first; sl; sl= sl->next)
1151 if(sl->spacetype==SPACE_VIEW3D)
1152 break;
1154 if (!sl)
1155 return EXPP_ReturnPyObjError( PyExc_ValueError,
1156 "The window matching the winid has no 3d viewport" );
1158 vd= (View3D *) sl;
1159 vd->lay = layer;
1161 for(bit= 0; bit < 20; bit++) {
1162 val = 1 << bit;
1163 if( layer & val ) {
1164 vd->layact = val;
1165 break;
1169 winid = -1; /* so we know its done */
1170 break;
1173 if (winid!=-1)
1174 return EXPP_ReturnPyObjError( PyExc_TypeError,
1175 "The winid argument did not match any window" );
1180 resl = PyList_New( 0 );
1181 if( !resl )
1182 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
1183 "couldn't create pylist!" ) );
1185 layer = G.scene->lay;
1187 bit = 0;
1188 while( bit < 20 ) {
1189 val = 1 << bit;
1190 if( layer & val ) {
1191 item = Py_BuildValue( "i", bit + 1 );
1192 PyList_Append( resl, item );
1193 Py_DECREF( item );
1195 bit++;
1198 return resl;
1201 static PyObject *M_Window_SetViewLayerLock( PyObject * self, PyObject * args )
1203 SpaceLink *sl;
1204 ScrArea *sa;
1205 View3D *vd;
1206 short winid = -1;
1207 short vd_lock = 0;
1209 if( !G.scene ) {
1210 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1211 "can't get pointer to global scene" );
1214 /* Pase Args, windid and view3d lock status */
1215 if (PyTuple_GET_SIZE(args)!=0) {
1216 if( !PyArg_ParseTuple ( args, "hh", &vd_lock, &winid ) ) {
1217 return EXPP_ReturnPyObjError( PyExc_TypeError,
1218 "nothing or optionaly a window ID argument" );
1221 else {
1222 return EXPP_ReturnPyObjError( PyExc_TypeError,
1223 "nothing or optionaly a window ID argument" );
1226 vd= NULL;
1228 if (winid==-1)
1229 vd= G.vd;
1230 else if (G.curscreen) {
1231 for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1232 if(winid == sa->win) {
1233 for (sl= sa->spacedata.first; sl; sl= sl->next)
1234 if(sl->spacetype == SPACE_VIEW3D)
1235 break;
1237 if (!sl)
1238 return EXPP_ReturnPyObjError( PyExc_ValueError,
1239 "The window matching the winid has no 3d viewport" );
1241 vd= (View3D *) sl;
1242 break;
1247 if(!vd) {
1248 return EXPP_ReturnPyObjError( PyExc_ValueError,
1249 "The winid argument did not match any window" );
1252 if((vd_lock) && (!vd->scenelock))
1253 vd->scenelock= 1;
1254 else if (vd->scenelock)
1255 vd->scenelock= 0;
1257 Py_RETURN_NONE;
1260 static PyObject *M_Window_GetViewLayerLock( PyObject * self, PyObject * args )
1262 SpaceLink *sl;
1263 ScrArea *sa;
1264 View3D *vd;
1265 short winid = -1;
1267 if( !G.scene ) {
1268 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1269 "can't get pointer to global scene" );
1272 /* Pase Args, Nothing or int */
1273 if (PyTuple_GET_SIZE(args)!=0) {
1274 if( !PyArg_ParseTuple ( args, "h", &winid) ) {
1275 return EXPP_ReturnPyObjError( PyExc_TypeError,
1276 "nothing or optionaly a window ID argument" );
1280 if (winid==-1)
1281 return Py_BuildValue( "h", G.vd->scenelock );
1283 if (G.curscreen) {
1284 for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1285 if(winid == sa->win) {
1286 for (sl= sa->spacedata.first; sl; sl= sl->next)
1287 if(sl->spacetype == SPACE_VIEW3D)
1288 break;
1290 if (!sl)
1291 return EXPP_ReturnPyObjError( PyExc_ValueError,
1292 "The window matching the winid has no 3d viewport" );
1294 vd= (View3D *) sl;
1295 return Py_BuildValue( "h", vd->scenelock );
1300 return EXPP_ReturnPyObjError( PyExc_ValueError,
1301 "The winid argument did not match any window" );
1305 static PyObject *M_Window_CameraView( PyObject * self, PyObject * args )
1307 SpaceLink *sl;
1308 ScrArea *sa;
1309 View3D *vd;
1310 short camtov3d = 0;
1311 short winid = -1;
1313 if( !PyArg_ParseTuple( args, "|ih", &camtov3d, &winid ) )
1314 return EXPP_ReturnPyObjError( PyExc_TypeError,
1315 "expected an int (from Window.Views) as argument" );
1317 vd= NULL;
1318 if (winid==-1)
1319 vd= G.vd;
1320 else if (G.curscreen) {
1321 for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1322 if(winid == sa->win) {
1323 for (sl= sa->spacedata.first; sl; sl= sl->next)
1324 if(sl->spacetype == SPACE_VIEW3D)
1325 break;
1327 if (!sl)
1328 return EXPP_ReturnPyObjError( PyExc_ValueError,
1329 "The window matching the winid has no 3d viewport" );
1331 vd= (View3D *) sl;
1332 break;
1337 if( !vd )
1338 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1339 "The winid argument did not match any window or the G.vd is uninitialized" );
1342 if( !vd->camera ) {
1343 if( BASACT && OBACT->type == OB_CAMERA )
1344 vd->camera = OBACT;
1345 else
1346 vd->camera = scene_find_camera( G.scene );
1347 handle_view3d_lock( );
1350 vd->persp = 2;
1351 vd->view = 0;
1353 if( camtov3d )
1354 setcameratoview3d( );
1356 Py_RETURN_NONE;
1359 static PyObject *M_Window_QTest( PyObject * self )
1361 return Py_BuildValue( "h", qtest( ) );
1364 static PyObject *M_Window_QRead( PyObject * self )
1366 short val = 0;
1367 unsigned short event;
1369 if (G.background)
1370 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
1371 "QRead is not available in background mode");
1373 event = extern_qread( &val );
1375 return Py_BuildValue( "ii", event, val );
1378 static PyObject *M_Window_QAdd( PyObject * self, PyObject * args )
1380 short win;
1381 short evt; /* unsigned, we check below */
1382 short val;
1383 short after = 0;
1385 if (G.background)
1386 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
1387 "QAdd is not available in background mode");
1389 if( !PyArg_ParseTuple( args, "hhh|h", &win, &evt, &val, &after ) )
1390 return EXPP_ReturnPyObjError( PyExc_TypeError,
1391 "expected three or four ints as arguments" );
1393 if( evt < 0 ) /* evt is unsigned short */
1394 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1395 "event value must be a positive int, check events in Blender.Draw" );
1397 if( after )
1398 addafterqueue( win, evt, val );
1399 else
1400 addqueue( win, evt, val );
1402 Py_RETURN_NONE;
1405 static PyObject *M_Window_QHandle( PyObject * self, PyObject * args )
1407 short win;
1408 ScrArea *sa;
1409 ScrArea *oldsa = NULL;
1411 if (G.background)
1412 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
1413 "QHandle is not available in background mode");
1415 if (!G.curscreen)
1416 return EXPP_ReturnPyObjError(PyExc_RuntimeError,
1417 "No screens available");
1419 if( !PyArg_ParseTuple( args, "h", &win ) )
1420 return EXPP_ReturnPyObjError( PyExc_TypeError,
1421 "expected an int as argument" );
1423 for (sa= G.curscreen->areabase.first; sa; sa= sa->next)
1424 if( sa->win == win )
1425 break;
1427 if( sa ) {
1428 BWinEvent evt;
1429 short do_redraw = 0, do_change = 0;
1431 if( sa != curarea || sa->win != mywinget( ) ) {
1432 oldsa = curarea;
1433 areawinset( sa->win );
1434 set_g_activearea( sa );
1436 while( bwin_qread( sa->win, &evt ) ) {
1437 if( evt.event == REDRAW ) {
1438 do_redraw = 1;
1439 } else if( evt.event == CHANGED ) {
1440 sa->win_swap = 0;
1441 do_change = 1;
1442 do_redraw = 1;
1443 } else {
1444 scrarea_do_winhandle( sa, &evt );
1449 if( oldsa ) {
1450 areawinset( oldsa->win );
1451 set_g_activearea( oldsa );
1454 Py_RETURN_NONE;
1457 static PyObject *M_Window_GetMouseCoords( PyObject * self )
1459 short mval[2];
1461 getmouse( mval );
1463 return Py_BuildValue( "hh", mval[0], mval[1] );
1466 static PyObject *M_Window_SetMouseCoords( PyObject * self, PyObject * args )
1468 int ok, x, y;
1470 if( !G.curscreen )
1471 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1472 "no current screen to retrieve info from!" );
1474 x = G.curscreen->sizex / 2;
1475 y = G.curscreen->sizey / 2;
1477 if( PyObject_Length( args ) == 2 )
1478 ok = PyArg_ParseTuple( args, "hh", &x, &y );
1479 else
1480 ok = PyArg_ParseTuple( args, "|(hh)", &x, &y );
1482 if( !ok )
1483 return EXPP_ReturnPyObjError( PyExc_TypeError,
1484 "expected [i, i] or i,i as arguments (or nothing)." );
1486 warp_pointer( x, y );
1488 Py_RETURN_NONE;
1491 static PyObject *M_Window_GetMouseButtons( PyObject * self )
1493 short mbut = get_mbut( );
1495 return Py_BuildValue( "h", mbut );
1498 static PyObject *M_Window_GetKeyQualifiers( PyObject * self )
1500 short qual = get_qual( );
1502 return Py_BuildValue( "h", qual );
1505 static PyObject *M_Window_SetKeyQualifiers( PyObject * self, PyObject * args )
1507 short qual = 0;
1509 if( !PyArg_ParseTuple( args, "|h", &qual ) )
1510 return EXPP_ReturnPyObjError( PyExc_TypeError,
1511 "expected nothing or an int (or'ed flags) as argument" );
1513 if( qual < 0 )
1514 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1515 "value must be a positive int, check Blender.Window.Qual" );
1517 G.qual = qual;
1519 return Py_BuildValue( "h", qual );
1522 static PyObject *M_Window_GetAreaSize( PyObject * self )
1524 ScrArea *sa = curarea;
1526 if( !sa )
1527 Py_RETURN_NONE;
1529 return Py_BuildValue( "hh", sa->winx, sa->winy );
1532 static PyObject *M_Window_GetAreaID( PyObject * self )
1534 ScrArea *sa = curarea;
1536 if( !sa )
1537 Py_RETURN_NONE;
1539 return Py_BuildValue( "h", sa->win );
1542 static PyObject *M_Window_GetScreenSize( PyObject * self )
1544 bScreen *scr = G.curscreen;
1546 if( !scr )
1547 Py_RETURN_NONE;
1549 return Py_BuildValue( "hh", scr->sizex, scr->sizey );
1553 static PyObject *M_Window_SetScreen( PyObject * self, PyObject * value )
1555 bScreen *scr = G.main->screen.first;
1556 char *name = PyString_AsString(value);
1558 if( !name )
1559 return EXPP_ReturnPyObjError( PyExc_TypeError,
1560 "expected string as argument" );
1562 while( scr ) {
1563 if( !strcmp( scr->id.name + 2, name ) ) {
1564 setscreen( scr );
1565 break;
1567 scr = scr->id.next;
1570 if( !scr )
1571 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1572 "no such screen, check Window.GetScreens() for valid names" );
1574 Py_RETURN_NONE;
1577 static PyObject *M_Window_GetScreens( PyObject * self )
1579 bScreen *scr = G.main->screen.first;
1580 PyObject *list = PyList_New( 0 );
1581 PyObject *str = NULL;
1583 if( !list )
1584 return EXPP_ReturnPyObjError( PyExc_MemoryError,
1585 "couldn't create py list!" );
1587 while( scr ) {
1588 str = PyString_FromString( scr->id.name + 2 );
1590 if( !str ) {
1591 Py_DECREF( list );
1592 return EXPP_ReturnPyObjError( PyExc_MemoryError,
1593 "couldn't create py string!" );
1596 PyList_Append( list, str ); /* incref's str */
1597 Py_DECREF( str );
1599 scr = scr->id.next;
1602 return list;
1605 static PyObject *M_Window_GetScreenInfo( PyObject * self, PyObject * args,
1606 PyObject * kwords )
1608 ScrArea *sa = G.curscreen->areabase.first;
1609 bScreen *scr = G.main->screen.first;
1610 PyObject *item, *list;
1611 rcti *rct;
1612 int type = -1;
1613 char *rect = "win";
1614 char *screen = "";
1615 static char *kwlist[] = { "type", "rect", "screen", NULL };
1616 int rctype = 0;
1618 if( !PyArg_ParseTupleAndKeywords( args, kwords, "|iss", kwlist, &type,
1619 &rect, &screen ) )
1620 return EXPP_ReturnPyObjError( PyExc_TypeError,
1621 "expected nothing or an int and two strings as arguments" );
1623 if( !strcmp( rect, "win" ) )
1624 rctype = 0;
1625 else if( !strcmp( rect, "total" ) )
1626 rctype = 1;
1627 else if( !strcmp( rect, "header" ) )
1628 rctype = 2;
1629 else
1630 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1631 "requested invalid type for area rectangle coordinates." );
1633 list = PyList_New( 0 );
1635 if( screen && screen[0] != '\0' ) {
1636 while( scr ) {
1637 if( !strcmp( scr->id.name + 2, screen ) ) {
1638 sa = scr->areabase.first;
1639 break;
1641 scr = scr->id.next;
1645 if( !scr ) {
1646 Py_DECREF( list );
1647 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1648 "no such screen, see existing ones with Window.GetScreens." );
1651 while( sa ) {
1652 if( type != -1 && sa->spacetype != type ) {
1653 sa = sa->next;
1654 continue;
1657 switch ( rctype ) {
1658 case 0:
1659 rct = &sa->winrct;
1660 break;
1661 case 1:
1662 rct = &sa->totrct;
1663 break;
1664 case 2:
1665 default:
1666 rct = &sa->headrct;
1669 item = Py_BuildValue( "{s:[h,h,h,h],s:h,s:h}",
1670 "vertices", rct->xmin, rct->ymin,
1671 rct->xmax, rct->ymax, "type",
1672 ( short ) sa->spacetype, "id",
1673 ( short ) sa->win );
1674 PyList_Append( list, item );
1675 Py_DECREF( item );
1677 sa = sa->next;
1680 return list;
1683 static PyObject *M_Window_GetPivot( PyObject * self )
1685 if (G.vd) {
1686 return PyInt_FromLong( G.vd->around );
1688 Py_RETURN_NONE;
1691 static PyObject *M_Window_SetPivot( PyObject * self, PyObject * value)
1694 short pivot;
1695 if (G.vd) {
1696 pivot = (short)PyInt_AsLong( value );
1698 if ( pivot > 4 || pivot < 0 )
1699 return EXPP_ReturnPyObjError( PyExc_AttributeError,
1700 "Expected a constant from Window.PivotTypes" );
1702 G.vd->around = pivot;
1704 Py_RETURN_NONE;
1709 /*****************************************************************************/
1710 /* Function: Window_Init */
1711 /*****************************************************************************/
1712 PyObject *Window_Init( void )
1714 PyObject *submodule, *Types, *Qual, *MButs, *PivotTypes, *dict;
1716 submodule =
1717 Py_InitModule3( "Blender.Window", M_Window_methods,
1718 M_Window_doc );
1720 dict = PyModule_GetDict( submodule );
1721 if( dict )
1722 PyDict_SetItemString( dict, "Theme", Theme_Init( ) );
1724 Types = PyConstant_New( );
1725 Qual = PyConstant_New( );
1726 MButs = PyConstant_New( );
1727 PivotTypes = PyConstant_New( );
1729 if( Types ) {
1730 BPy_constant *d = ( BPy_constant * ) Types;
1732 PyConstant_Insert( d, "VIEW3D", PyInt_FromLong( SPACE_VIEW3D ) );
1733 PyConstant_Insert( d, "IPO", PyInt_FromLong( SPACE_IPO ) );
1734 PyConstant_Insert( d, "OOPS", PyInt_FromLong( SPACE_OOPS ) );
1735 PyConstant_Insert( d, "BUTS", PyInt_FromLong( SPACE_BUTS ) );
1736 PyConstant_Insert( d, "FILE", PyInt_FromLong( SPACE_FILE ) );
1737 PyConstant_Insert( d, "IMAGE", PyInt_FromLong( SPACE_IMAGE ) );
1738 PyConstant_Insert( d, "INFO", PyInt_FromLong( SPACE_INFO ) );
1739 PyConstant_Insert( d, "SEQ", PyInt_FromLong( SPACE_SEQ ) );
1740 PyConstant_Insert( d, "IMASEL", PyInt_FromLong( SPACE_IMASEL ) );
1741 PyConstant_Insert( d, "SOUND", PyInt_FromLong( SPACE_SOUND ) );
1742 PyConstant_Insert( d, "ACTION", PyInt_FromLong( SPACE_ACTION ) );
1743 PyConstant_Insert( d, "TEXT", PyInt_FromLong( SPACE_TEXT ) );
1744 PyConstant_Insert( d, "NLA", PyInt_FromLong( SPACE_NLA ) );
1745 PyConstant_Insert( d, "SCRIPT", PyInt_FromLong( SPACE_SCRIPT ) );
1746 PyConstant_Insert( d, "TIME", PyInt_FromLong( SPACE_TIME ) );
1747 PyConstant_Insert( d, "NODE", PyInt_FromLong( SPACE_NODE ) );
1749 PyModule_AddObject( submodule, "Types", Types );
1752 if( Qual ) {
1753 BPy_constant *d = ( BPy_constant * ) Qual;
1755 PyConstant_Insert( d, "LALT", PyInt_FromLong( L_ALTKEY ) );
1756 PyConstant_Insert( d, "RALT", PyInt_FromLong( R_ALTKEY ) );
1757 PyConstant_Insert( d, "ALT", PyInt_FromLong( LR_ALTKEY ) );
1758 PyConstant_Insert( d, "LCTRL", PyInt_FromLong( L_CTRLKEY ) );
1759 PyConstant_Insert( d, "RCTRL", PyInt_FromLong( R_CTRLKEY ) );
1760 PyConstant_Insert( d, "CTRL", PyInt_FromLong( LR_CTRLKEY ) );
1761 PyConstant_Insert( d, "LSHIFT", PyInt_FromLong( L_SHIFTKEY ) );
1762 PyConstant_Insert( d, "RSHIFT", PyInt_FromLong( R_SHIFTKEY ) );
1763 PyConstant_Insert( d, "SHIFT", PyInt_FromLong( LR_SHIFTKEY ) );
1765 PyModule_AddObject( submodule, "Qual", Qual );
1768 if( MButs ) {
1769 BPy_constant *d = ( BPy_constant * ) MButs;
1771 PyConstant_Insert( d, "L", PyInt_FromLong( L_MOUSE ) );
1772 PyConstant_Insert( d, "M", PyInt_FromLong( M_MOUSE ) );
1773 PyConstant_Insert( d, "R", PyInt_FromLong( R_MOUSE ) );
1775 PyModule_AddObject( submodule, "MButs", MButs );
1778 if( PivotTypes ) {
1779 BPy_constant *d = ( BPy_constant * ) PivotTypes;
1781 PyConstant_Insert(d, "BOUNDBOX", PyInt_FromLong( PIVOT_BOUNDBOX ) );
1782 PyConstant_Insert(d, "CURSOR", PyInt_FromLong( PIVOT_CURSOR ) );
1783 PyConstant_Insert(d, "MEDIAN", PyInt_FromLong( PIVOT_MEDIAN ) );
1784 PyConstant_Insert(d, "ACTIVE", PyInt_FromLong( PIVOT_ACTIVE ) );
1785 PyConstant_Insert(d, "INDIVIDUAL", PyInt_FromLong( PIVOT_INDIVIDUAL ) );
1787 PyModule_AddObject( submodule, "PivotTypes", PivotTypes );
1789 return submodule;