PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args, PyObject *kw) { /* args, pyrna_struct_keyframe_parse handles these */ const char *path_full = NULL; int index = -1; float cfra = FLT_MAX; const char *group_name = NULL; char keytype = BEZT_KEYTYPE_KEYFRAME; /* XXX: Expose this as a one-off option... */ int options = 0; PYRNA_STRUCT_CHECK_OBJ(self); if (pyrna_struct_keyframe_parse(&self->ptr, args, kw, "s|ifsO!:bpy_struct.keyframe_insert()", "bpy_struct.keyframe_insert()", &path_full, &index, &cfra, &group_name, &options) == -1) { return NULL; } else { short result; ReportList reports; BKE_reports_init(&reports, RPT_STORE); result = insert_keyframe(&reports, (ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, keytype, options); MEM_freeN((void *)path_full); if (BPy_reports_to_error(&reports, PyExc_RuntimeError, true) == -1) return NULL; return PyBool_FromLong(result); } }
PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args) { const char *path, *path_full; int index = -1; PYRNA_STRUCT_CHECK_OBJ(self); if (!PyArg_ParseTuple(args, "s|i:driver_remove", &path, &index)) return NULL; if (pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_remove():", path, &path_full, &index) == -1) { return NULL; } else { short result; ReportList reports; BKE_reports_init(&reports, RPT_STORE); result = ANIM_remove_driver(&reports, (ID *)self->ptr.id.data, path_full, index, 0); MEM_freeN((void *)path_full); if (BPy_reports_to_error(&reports, PyExc_RuntimeError, true) == -1) return NULL; WM_event_add_notifier(BPy_GetContext(), NC_ANIMATION | ND_FCURVES_ORDER, NULL); return PyBool_FromLong(result); } }
static BlendFileData *load_game_data(char *progname, char *filename = NULL, char *relativename = NULL) { ReportList reports; BlendFileData *bfd = NULL; BKE_reports_init(&reports, RPT_STORE); /* try to load ourself, will only work if we are a runtime */ if (BLO_is_a_runtime(progname)) { bfd= BLO_read_runtime(progname, &reports); if (bfd) { bfd->type= BLENFILETYPE_RUNTIME; strcpy(bfd->main->name, progname); } } else { bfd= BLO_read_from_file(progname, &reports); } if (!bfd && filename) { bfd = load_game_data(filename); if (!bfd) { printf("Loading %s failed: ", filename); BKE_reports_print(&reports, RPT_ERROR); } } BKE_reports_clear(&reports); return bfd; }
void thumbnails_start(struct FileList* filelist, const struct bContext* C) { wmJob *steve; ThumbnailJob *tj; int idx; /* prepare job data */ tj= MEM_callocN(sizeof(ThumbnailJob), "thumbnails\n"); tj->filelist = filelist; for (idx = 0; idx < filelist->numfiles;idx++) { if (!filelist->filelist[idx].image) { if ( (filelist->filelist[idx].flags & (IMAGEFILE|MOVIEFILE|BLENDERFILE)) ) { FileImage* limg = MEM_callocN(sizeof(struct FileImage), "loadimage"); BLI_strncpy(limg->path, filelist->filelist[idx].path, FILE_MAX); limg->index= idx; limg->flags= filelist->filelist[idx].flags; BLI_addtail(&tj->loadimages, limg); } } } BKE_reports_init(&tj->reports, RPT_PRINT); /* setup job */ steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails", 0); WM_jobs_customdata(steve, tj, thumbnails_free); WM_jobs_timer(steve, 0.5, NC_WINDOW, NC_WINDOW); WM_jobs_callbacks(steve, thumbnails_startjob, NULL, thumbnails_update, NULL); /* start the job */ WM_jobs_start(CTX_wm_manager(C), steve); }
PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args, PyObject *kw) { /* args, pyrna_struct_keyframe_parse handles these */ const char *path_full = NULL; int index = -1; float cfra = FLT_MAX; const char *group_name = NULL; PYRNA_STRUCT_CHECK_OBJ(self); if (pyrna_struct_keyframe_parse(&self->ptr, args, kw, "s|ifsO!:bpy_struct.keyframe_delete()", "bpy_struct.keyframe_insert()", &path_full, &index, &cfra, &group_name, NULL) == -1) { return NULL; } else { short result; ReportList reports; BKE_reports_init(&reports, RPT_STORE); result = delete_keyframe(&reports, (ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0); MEM_freeN((void *)path_full); if (BPy_reports_to_error(&reports, PyExc_RuntimeError, true) == -1) return NULL; return PyBool_FromLong(result); } }
PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args) { const char *path, *path_full; int index = -1; PYRNA_STRUCT_CHECK_OBJ(self); if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index)) return NULL; if (pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_add():", path, &path_full, &index) == -1) { return NULL; } else { PyObject *ret = NULL; ReportList reports; int result; BKE_reports_init(&reports, RPT_STORE); result = ANIM_add_driver(&reports, (ID *)self->ptr.id.data, path_full, index, CREATEDRIVER_WITH_FMODIFIER, DRIVER_TYPE_PYTHON); if (BPy_reports_to_error(&reports, PyExc_RuntimeError, true) == -1) return NULL; if (result) { ID *id = self->ptr.id.data; AnimData *adt = BKE_animdata_from_id(id); FCurve *fcu; PointerRNA tptr; if (index == -1) { /* all, use a list */ int i = 0; ret = PyList_New(0); while ((fcu = list_find_fcurve(&adt->drivers, path_full, i++))) { RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr); PyList_APPEND(ret, pyrna_struct_CreatePyObject(&tptr)); } } else { fcu = list_find_fcurve(&adt->drivers, path_full, index); RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr); ret = pyrna_struct_CreatePyObject(&tptr); } WM_event_add_notifier(BPy_GetContext(), NC_ANIMATION | ND_FCURVES_ORDER, NULL); } else { /* XXX, should be handled by reports, */ PyErr_SetString(PyExc_TypeError, "bpy_struct.driver_add(): failed because of an internal error"); return NULL; } MEM_freeN((void *)path_full); return ret; } }
static void wm_init_reports(bContext *C) { ReportList *reports = CTX_wm_reports(C); BLI_assert(!reports || BLI_listbase_is_empty(&reports->list)); BKE_reports_init(reports, RPT_STORE); }
static PyObject *bpy_lib_enter(BPy_Library *self) { PyObject *ret; BPy_Library *self_from; PyObject *from_dict = _PyDict_NewPresized(MAX_LIBARRAY); ReportList reports; BKE_reports_init(&reports, RPT_STORE); self->blo_handle = BLO_blendhandle_from_file(self->abspath, &reports); if (self->blo_handle == NULL) { if (BPy_reports_to_error(&reports, PyExc_IOError, true) != -1) { PyErr_Format(PyExc_IOError, "load: %s failed to open blend file", self->abspath); } return NULL; } else { int i = 0, code; while ((code = BKE_idcode_iter_step(&i))) { if (BKE_idcode_is_linkable(code)) { const char *name_plural = BKE_idcode_to_name_plural(code); PyObject *str = PyUnicode_FromString(name_plural); PyObject *item; PyDict_SetItem(self->dict, str, item = PyList_New(0)); Py_DECREF(item); PyDict_SetItem(from_dict, str, item = _bpy_names(self, code)); Py_DECREF(item); Py_DECREF(str); } } } /* create a dummy */ self_from = PyObject_New(BPy_Library, &bpy_lib_Type); BLI_strncpy(self_from->relpath, self->relpath, sizeof(self_from->relpath)); BLI_strncpy(self_from->abspath, self->abspath, sizeof(self_from->abspath)); self_from->blo_handle = NULL; self_from->flag = 0; self_from->dict = from_dict; /* owns the dict */ /* return pair */ ret = PyTuple_New(2); PyTuple_SET_ITEMS(ret, (PyObject *)self_from, (PyObject *)self); Py_INCREF(self); BKE_reports_clear(&reports); return ret; }
static int screencast_exec(bContext *C, wmOperator *op) { wmWindowManager *wm = CTX_wm_manager(C); wmWindow *win = CTX_wm_window(C); bScreen *screen = CTX_wm_screen(C); wmJob *wm_job; ScreenshotJob *sj; /* if called again, stop the running job */ if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST)) WM_jobs_stop(wm, screen, screenshot_startjob); wm_job = WM_jobs_get(wm, win, screen, "Screencast", 0, WM_JOB_TYPE_SCREENCAST); sj = MEM_callocN(sizeof(ScreenshotJob), "screenshot job"); /* setup sj */ if (RNA_boolean_get(op->ptr, "full")) { sj->x = 0; sj->y = 0; sj->dumpsx = WM_window_pixels_x(win); sj->dumpsy = WM_window_pixels_y(win); } else { ScrArea *curarea = CTX_wm_area(C); sj->x = curarea->totrct.xmin; sj->y = curarea->totrct.ymin; sj->dumpsx = curarea->totrct.xmax - sj->x; sj->dumpsy = curarea->totrct.ymax - sj->y; } sj->bmain = CTX_data_main(C); sj->scene = CTX_data_scene(C); sj->wm = wm; BKE_reports_init(&sj->reports, RPT_PRINT); /* setup job */ WM_jobs_customdata_set(wm_job, sj, screenshot_freejob); WM_jobs_timer(wm_job, 0.1, 0, NC_SCREEN | ND_SCREENCAST); WM_jobs_callbacks(wm_job, screenshot_startjob, NULL, screenshot_updatejob, screenshot_endjob); WM_jobs_start(sj->wm, wm_job); screencast_cursor_toggle(sj->wm, 1); WM_event_add_notifier(C, NC_SCREEN | ND_SCREENCAST, screen); return OPERATOR_FINISHED; }
static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData, ModifierApplyFlag UNUSED(flag)) { DataTransferModifierData *dtmd = (DataTransferModifierData *) md; DerivedMesh *dm = derivedData; ReportList reports; /* Only used to check wehther we are operating on org data or not... */ Mesh *me = ob->data; MVert *mvert; const bool invert_vgroup = (dtmd->flags & MOD_DATATRANSFER_INVERT_VGROUP) != 0; const float max_dist = (dtmd->flags & MOD_DATATRANSFER_MAP_MAXDIST) ? dtmd->map_max_distance : FLT_MAX; SpaceTransform space_transform_data; SpaceTransform *space_transform = (dtmd->flags & MOD_DATATRANSFER_OBSRC_TRANSFORM) ? &space_transform_data : NULL; if (space_transform) { BLI_SPACE_TRANSFORM_SETUP(space_transform, ob, dtmd->ob_source); } mvert = dm->getVertArray(dm); if ((me->mvert == mvert) && (dtmd->data_types & DT_TYPES_AFFECT_MESH)) { /* We need to duplicate data here, otherwise setting custom normals, edges' shaprness, etc., could * modify org mesh, see T43671. */ dm = CDDM_copy(dm); } BKE_reports_init(&reports, RPT_STORE); /* Note: no islands precision for now here. */ BKE_object_data_transfer_dm(md->scene, dtmd->ob_source, ob, dm, dtmd->data_types, false, dtmd->vmap_mode, dtmd->emap_mode, dtmd->lmap_mode, dtmd->pmap_mode, space_transform, max_dist, dtmd->map_ray_radius, 0.0f, dtmd->layers_select_src, dtmd->layers_select_dst, dtmd->mix_mode, dtmd->mix_factor, dtmd->defgrp_name, invert_vgroup, &reports); if (BKE_reports_contain(&reports, RPT_ERROR)) { modifier_setError(md, "%s", BKE_reports_string(&reports, RPT_ERROR)); } else if (dm->getNumVerts(dm) > HIGH_POLY_WARNING || ((Mesh *)(dtmd->ob_source->data))->totvert > HIGH_POLY_WARNING) { modifier_setError(md, "You are using a rather high poly as source or destination, computation might be slow"); } return dm; }
static BlendFileData *load_game_data(char *filename) { ReportList reports; BlendFileData *bfd; BKE_reports_init(&reports, RPT_STORE); bfd= BLO_read_from_file(filename, &reports); if (!bfd) { printf("Loading %s failed: ", filename); BKE_reports_print(&reports, RPT_ERROR); } BKE_reports_clear(&reports); return bfd; }
static int render_animation(int UNUSED(argc), const char **UNUSED(argv), void *data) { bContext *C = data; if (CTX_data_scene(C)) { Main *bmain= CTX_data_main(C); Scene *scene= CTX_data_scene(C); Render *re= RE_NewRender(scene->id.name); ReportList reports; BKE_reports_init(&reports, RPT_PRINT); RE_SetReports(re, &reports); RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, scene->r.sfra, scene->r.efra, scene->r.frame_step); RE_SetReports(re, NULL); } else { printf("\nError: no blend loaded. cannot use '-a'.\n"); } return 0; }
static int render_frame(int argc, const char **argv, void *data) { bContext *C = data; Scene *scene = CTX_data_scene(C); if (scene) { Main *bmain = CTX_data_main(C); if (argc > 1) { Render *re = RE_NewRender(scene->id.name); int frame; ReportList reports; switch (*argv[1]) { case '+': frame = scene->r.sfra + atoi(argv[1] + 1); break; case '-': frame = (scene->r.efra - atoi(argv[1] + 1)) + 1; break; default: frame = atoi(argv[1]); break; } BLI_begin_threaded_malloc(); BKE_reports_init(&reports, RPT_PRINT); frame = CLAMPIS(frame, MINAFRAME, MAXFRAME); RE_SetReports(re, &reports); RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, frame, frame, scene->r.frame_step); RE_SetReports(re, NULL); BLI_end_threaded_malloc(); return 1; } else { printf("\nError: frame number must follow '-f / --render-frame'.\n"); return 0; } } else { printf("\nError: no blend loaded. cannot use '-f / --render-frame'.\n"); return 0; } }
bool GPC_Engine::Start(char *filename) { ReportList reports; BlendFileData *bfd; BKE_reports_init(&reports, RPT_STORE); bfd= BLO_read_from_file(filename, &reports); BKE_reports_clear(&reports); if (!bfd) { // XXX, deal with error here cout << "Unable to load: " << filename << endl; return false; } StartKetsji(); if(bfd->type == BLENFILETYPE_PUB) m_canvas->SetBannerDisplayEnabled(false); return true; }
void thumbnails_start(FileList *filelist, const bContext *C) { wmJob *wm_job; ThumbnailJob *tj; int idx; /* prepare job data */ tj = MEM_callocN(sizeof(*tj), __func__); tj->filelist = filelist; for (idx = 0; idx < filelist->numfiles; idx++) { if (!filelist->filelist[idx].path) { continue; } if (!filelist->filelist[idx].image) { if (filelist->filelist[idx].flags & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | FILE_TYPE_FTFONT | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { FileImage *limg = MEM_callocN(sizeof(*limg), __func__); BLI_strncpy(limg->path, filelist->filelist[idx].path, sizeof(limg->path)); limg->index = idx; limg->flags = filelist->filelist[idx].flags; BLI_addtail(&tj->loadimages, limg); } } } BKE_reports_init(&tj->reports, RPT_PRINT); /* setup job */ wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), filelist, "Thumbnails", 0, WM_JOB_TYPE_FILESEL_THUMBNAIL); WM_jobs_customdata_set(wm_job, tj, thumbnails_free); WM_jobs_timer(wm_job, 0.5, NC_WINDOW, NC_WINDOW); WM_jobs_callbacks(wm_job, thumbnails_startjob, NULL, thumbnails_update, thumbnails_endjob); /* start the job */ WM_jobs_start(CTX_wm_manager(C), wm_job); }
bool GPC_Engine::Start(unsigned char *blenderDataBuffer, unsigned int blenderDataBufferSize) { ReportList reports; BlendFileData *bfd; BKE_reports_init(&reports, RPT_STORE); bfd= BLO_read_from_memory(blenderDataBuffer, blenderDataBufferSize, &reports); BKE_reports_clear(&reports); if (!bfd) { // XXX, deal with error here cout << "Unable to load. " << endl; return false; } StartKetsji(); if(bfd->type == BLENFILETYPE_PUB) m_canvas->SetBannerDisplayEnabled(false); return true; }
static int screencast_exec(bContext *C, wmOperator *op) { bScreen *screen = CTX_wm_screen(C); wmJob *steve = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0); ScreenshotJob *sj = MEM_callocN(sizeof(ScreenshotJob), "screenshot job"); /* setup sj */ if (RNA_boolean_get(op->ptr, "full")) { wmWindow *win = CTX_wm_window(C); sj->x = 0; sj->y = 0; sj->dumpsx = win->sizex; sj->dumpsy = win->sizey; } else { ScrArea *curarea = CTX_wm_area(C); sj->x = curarea->totrct.xmin; sj->y = curarea->totrct.ymin; sj->dumpsx = curarea->totrct.xmax - sj->x; sj->dumpsy = curarea->totrct.ymax - sj->y; } sj->bmain = CTX_data_main(C); sj->scene = CTX_data_scene(C); BKE_reports_init(&sj->reports, RPT_PRINT); /* setup job */ WM_jobs_customdata(steve, sj, screenshot_freejob); WM_jobs_timer(steve, 0.1, 0, NC_SCREEN | ND_SCREENCAST); WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL); WM_jobs_start(CTX_wm_manager(C), steve); WM_event_add_notifier(C, NC_SCREEN | ND_SCREENCAST, screen); return OPERATOR_FINISHED; }
static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds) { static const char *kwlist[] = { "filepath", "datablocks", /* optional */ "relative_remap", "fake_user", "compress", NULL, }; /* args */ const char *filepath; char filepath_abs[FILE_MAX]; PyObject *datablocks = NULL; bool use_relative_remap = false, use_fake_user = false, use_compress = false; if (!PyArg_ParseTupleAndKeywords( args, kwds, "sO!|$O&O&O&:write", (char **)kwlist, &filepath, &PySet_Type, &datablocks, PyC_ParseBool, &use_relative_remap, PyC_ParseBool, &use_fake_user, PyC_ParseBool, &use_compress)) { return NULL; } Main *bmain_src = G.main; int write_flags = 0; if (use_relative_remap) { write_flags |= G_FILE_RELATIVE_REMAP; } if (use_compress) { write_flags |= G_FILE_COMPRESS; } BLI_strncpy(filepath_abs, filepath, FILE_MAX); BLI_path_abs(filepath_abs, G.main->name); BKE_blendfile_write_partial_begin(bmain_src); /* array of ID's and backup any data we modify */ struct { ID *id; /* original values */ short id_flag; short id_us; } *id_store_array, *id_store; int id_store_len = 0; PyObject *ret; /* collect all id data from the set and store in 'id_store_array' */ { Py_ssize_t pos, hash; PyObject *key; id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__); id_store = id_store_array; pos = hash = 0; while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) { if (!pyrna_id_FromPyObject(key, &id_store->id)) { PyErr_Format(PyExc_TypeError, "Expected and ID type, not %.200s", Py_TYPE(key)->tp_name); ret = NULL; goto finally; } else { id_store->id_flag = id_store->id->flag; id_store->id_us = id_store->id->us; if (use_fake_user) { id_store->id->flag |= LIB_FAKEUSER; } id_store->id->us = 1; BKE_blendfile_write_partial_tag_ID(id_store->id, true); id_store_len += 1; id_store++; } } } /* write blend */ int retval = 0; ReportList reports; BKE_reports_init(&reports, RPT_STORE); retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports); /* cleanup state */ BKE_blendfile_write_partial_end(bmain_src); if (retval) { BKE_reports_print(&reports, RPT_ERROR_ALL); BKE_reports_clear(&reports); ret = Py_None; Py_INCREF(ret); } else { if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) { PyErr_SetString(PyExc_IOError, "Unknown error writing library data"); } ret = NULL; } finally: /* clear all flags for ID's added to the store (may run on error too) */ id_store = id_store_array; for (int i = 0; i < id_store_len; id_store++, i++) { if (use_fake_user) { if ((id_store->id_flag & LIB_FAKEUSER) == 0) { id_store->id->flag &= ~LIB_FAKEUSER; } } id_store->id->us = id_store->id_us; BKE_blendfile_write_partial_tag_ID(id_store->id, false); } MEM_freeN(id_store_array); return ret; }
static int load_file(int UNUSED(argc), const char **argv, void *data) { bContext *C = data; /* Make the path absolute because its needed for relative linked blends to be found */ char filename[FILE_MAX]; /* note, we could skip these, but so far we always tried to load these files */ if (argv[0][0] == '-') { fprintf(stderr, "unknown argument, loading as file: %s\n", argv[0]); } BLI_strncpy(filename, argv[0], sizeof(filename)); BLI_path_cwd(filename); if (G.background) { int retval = BKE_read_file(C, filename, NULL); /* we successfully loaded a blend file, get sure that * pointcache works */ if (retval != BKE_READ_FILE_FAIL) { wmWindowManager *wm = CTX_wm_manager(C); /* special case, 2.4x files */ if (wm == NULL && CTX_data_main(C)->wm.first == NULL) { extern void wm_add_default(bContext *C); /* wm_add_default() needs the screen to be set. */ CTX_wm_screen_set(C, CTX_data_main(C)->screen.first); wm_add_default(C); } CTX_wm_manager_set(C, NULL); /* remove wm to force check */ WM_check(C); G.relbase_valid = 1; if (CTX_wm_manager(C) == NULL) CTX_wm_manager_set(C, wm); /* reset wm */ DAG_on_visible_update(CTX_data_main(C), TRUE); } else { /* failed to load file, stop processing arguments */ return -1; } /* WM_file_read() runs normally but since we're in background mode do here */ #ifdef WITH_PYTHON /* run any texts that were loaded in and flagged as modules */ BPY_python_reset(C); #endif /* happens for the UI on file reading too (huh? (ton))*/ // XXX BKE_reset_undo(); // BKE_write_undo("original"); /* save current state */ } else { /* we are not running in background mode here, but start blender in UI mode with * a file - this should do everything a 'load file' does */ ReportList reports; BKE_reports_init(&reports, RPT_PRINT); WM_file_autoexec_init(filename); WM_file_read(C, filename, &reports); BKE_reports_clear(&reports); } G.file_loaded = 1; return 0; }
static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args) { wmOperatorType *ot; int error_val = 0; PointerRNA ptr; int operator_ret = OPERATOR_CANCELLED; char *opname; char *context_str = NULL; PyObject *kw = NULL; /* optional args */ PyObject *context_dict = NULL; /* optional args */ PyObject *context_dict_back; /* note that context is an int, python does the conversion in this case */ int context = WM_OP_EXEC_DEFAULT; int is_undo = false; /* XXX Todo, work out a better solution for passing on context, * could make a tuple from self and pack the name and Context into it... */ bContext *C = (bContext *)BPy_GetContext(); if (C == NULL) { PyErr_SetString(PyExc_RuntimeError, "Context is None, cant poll any operators"); return NULL; } if (!PyArg_ParseTuple(args, "sO|O!si:_bpy.ops.call", &opname, &context_dict, &PyDict_Type, &kw, &context_str, &is_undo)) { return NULL; } ot = WM_operatortype_find(opname, true); if (ot == NULL) { PyErr_Format(PyExc_AttributeError, "Calling operator \"bpy.ops.%s\" error, " "could not be found", opname); return NULL; } if (!pyrna_write_check()) { PyErr_Format(PyExc_RuntimeError, "Calling operator \"bpy.ops.%s\" error, " "can't modify blend data in this state (drawing/rendering)", opname); return NULL; } if (context_str) { if (RNA_enum_value_from_id(operator_context_items, context_str, &context) == 0) { char *enum_str = BPy_enum_as_string(operator_context_items); PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s\" error, " "expected a string enum in (%.200s)", opname, enum_str); MEM_freeN(enum_str); return NULL; } } if (context_dict == NULL || context_dict == Py_None) { context_dict = NULL; } else if (!PyDict_Check(context_dict)) { PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s\" error, " "custom context expected a dict or None, got a %.200s", opname, Py_TYPE(context_dict)->tp_name); return NULL; } context_dict_back = CTX_py_dict_get(C); CTX_py_dict_set(C, (void *)context_dict); Py_XINCREF(context_dict); /* so we done loose it */ if (WM_operator_poll_context((bContext *)C, ot, context) == false) { const char *msg = CTX_wm_operator_poll_msg_get(C); PyErr_Format(PyExc_RuntimeError, "Operator bpy.ops.%.200s.poll() %.200s", opname, msg ? msg : "failed, context is incorrect"); CTX_wm_operator_poll_msg_set(C, NULL); /* better set to NULL else it could be used again */ error_val = -1; } else { WM_operator_properties_create_ptr(&ptr, ot); WM_operator_properties_sanitize(&ptr, 0); if (kw && PyDict_Size(kw)) error_val = pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: "); if (error_val == 0) { ReportList *reports; reports = MEM_mallocN(sizeof(ReportList), "wmOperatorReportList"); BKE_reports_init(reports, RPT_STORE | RPT_OP_HOLD); /* own so these don't move into global reports */ #ifdef BPY_RELEASE_GIL /* release GIL, since a thread could be started from an operator * that updates a driver */ /* note: I have not seen any examples of code that does this * so it may not be officially supported but seems to work ok. */ { PyThreadState *ts = PyEval_SaveThread(); #endif operator_ret = WM_operator_call_py(C, ot, context, &ptr, reports, is_undo); #ifdef BPY_RELEASE_GIL /* regain GIL */ PyEval_RestoreThread(ts); } #endif error_val = BPy_reports_to_error(reports, PyExc_RuntimeError, false); /* operator output is nice to have in the terminal/console too */ if (reports->list.first) { char *report_str = BKE_reports_string(reports, 0); /* all reports */ if (report_str) { PySys_WriteStdout("%s\n", report_str); MEM_freeN(report_str); } } BKE_reports_clear(reports); if ((reports->flag & RPT_FREE) == 0) { MEM_freeN(reports); } } WM_operator_properties_free(&ptr); #if 0 /* if there is some way to know an operator takes args we should use this */ { /* no props */ if (kw != NULL) { PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname); return NULL; } WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL); } #endif } /* restore with original context dict, probably NULL but need this for nested operator calls */ Py_XDECREF(context_dict); CTX_py_dict_set(C, (void *)context_dict_back); if (error_val == -1) { return NULL; } /* when calling bpy.ops.wm.read_factory_settings() bpy.data's main pointer is freed by clear_globals(), * further access will crash blender. setting context is not needed in this case, only calling because this * function corrects bpy.data (internal Main pointer) */ BPY_modules_update(C); /* needed for when WM_OT_read_factory_settings us called from within a script */ bpy_import_main_set(CTX_data_main(C)); /* return operator_ret as a bpy enum */ return pyrna_enum_bitfield_to_py(operator_return_items, operator_ret); }
bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options) { Main *main_newlib; /* stored as a dynamic 'main' until we free it */ Main *main_tmp= NULL; /* created only for linking, then freed */ LinkNode *names = NULL; int idcode= BKE_idcode_from_name(group); short flag= 0; /* don't need any special options */ ReportList reports; static char err_local[255]; /* only scene and mesh supported right now */ if (idcode!=ID_SCE && idcode!=ID_ME &&idcode!=ID_AC) { snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group); *err_str= err_local; BLO_blendhandle_close(bpy_openlib); return false; } if (GetMainDynamicPath(path)) { snprintf(err_local, sizeof(err_local), "blend file already open \"%s\"\n", path); *err_str= err_local; BLO_blendhandle_close(bpy_openlib); return false; } if (bpy_openlib==NULL) { snprintf(err_local, sizeof(err_local), "could not open blendfile \"%s\"\n", path); *err_str= err_local; return false; } main_newlib= (Main *)MEM_callocN( sizeof(Main), "BgeMain"); BKE_reports_init(&reports, RPT_STORE); /* here appending/linking starts */ main_tmp = BLO_library_append_begin(main_newlib, &bpy_openlib, (char *)path); int totnames_dummy; names = BLO_blendhandle_get_datablock_names( bpy_openlib, idcode, &totnames_dummy); int i=0; LinkNode *n= names; while(n) { BLO_library_append_named_part(main_tmp, &bpy_openlib, (char *)n->link, idcode); n= (LinkNode *)n->next; i++; } BLI_linklist_free(names, free); /* free linklist *and* each node's data */ BLO_library_append_end(NULL, main_tmp, &bpy_openlib, idcode, flag); /* now do another round of linking for Scenes so all actions are properly loaded */ if (idcode==ID_SCE && options & LIB_LOAD_LOAD_ACTIONS) { main_tmp = BLO_library_append_begin(main_newlib, &bpy_openlib, (char *)path); int totnames_dummy; names = BLO_blendhandle_get_datablock_names( bpy_openlib, ID_AC, &totnames_dummy); int i=0; LinkNode *n= names; while(n) { BLO_library_append_named_part(main_tmp, &bpy_openlib, (char *)n->link, ID_AC); n= (LinkNode *)n->next; i++; } BLI_linklist_free(names, free); /* free linklist *and* each node's data */ BLO_library_append_end(NULL, main_tmp, &bpy_openlib, ID_AC, flag); } BLO_blendhandle_close(bpy_openlib); BKE_reports_clear(&reports); /* done linking */ /* needed for lookups*/ GetMainDynamic().push_back(main_newlib); strncpy(main_newlib->name, path, sizeof(main_newlib->name)); if (idcode==ID_ME) { /* Convert all new meshes into BGE meshes */ ID* mesh; for (mesh= (ID *)main_newlib->mesh.first; mesh; mesh= (ID *)mesh->next ) { if (options & LIB_LOAD_VERBOSE) printf("MeshName: %s\n", mesh->name+2); RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)mesh, NULL, scene_merge, this); scene_merge->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj); } } else if (idcode==ID_AC) { /* Convert all actions */ ID *action; for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) { if (options & LIB_LOAD_VERBOSE) printf("ActionName: %s\n", action->name+2); scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action); } } else if (idcode==ID_SCE) { /* Merge all new linked in scene into the existing one */ ID *scene; for (scene= (ID *)main_newlib->scene.first; scene; scene= (ID *)scene->next ) { if (options & LIB_LOAD_VERBOSE) printf("SceneName: %s\n", scene->name+2); /* merge into the base scene */ KX_Scene* other= m_ketsjiEngine->CreateScene((Scene *)scene); scene_merge->MergeScene(other); // RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene delete other; } /* Now handle all the actions */ if (options & LIB_LOAD_LOAD_ACTIONS) { ID *action; for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) { if (options & LIB_LOAD_VERBOSE) printf("ActionName: %s\n", action->name+2); scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action); } } } return true; }
KX_LibLoadStatus *KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options) { Main *main_newlib; /* stored as a dynamic 'main' until we free it */ const int idcode = BKE_idcode_from_name(group); ReportList reports; static char err_local[255]; // TIMEIT_START(bge_link_blend_file); KX_LibLoadStatus *status; /* only scene and mesh supported right now */ if (idcode != ID_SCE && idcode != ID_ME && idcode != ID_AC) { snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group); *err_str = err_local; BLO_blendhandle_close(bpy_openlib); return NULL; } if (GetMainDynamicPath(path)) { snprintf(err_local, sizeof(err_local), "blend file already open \"%s\"\n", path); *err_str = err_local; BLO_blendhandle_close(bpy_openlib); return NULL; } if (bpy_openlib == NULL) { snprintf(err_local, sizeof(err_local), "could not open blendfile \"%s\"\n", path); *err_str = err_local; return NULL; } main_newlib = BKE_main_new(); BKE_reports_init(&reports, RPT_STORE); short flag = 0; /* don't need any special options */ /* created only for linking, then freed */ Main *main_tmp = BLO_library_link_begin(main_newlib, &bpy_openlib, (char *)path); load_datablocks(main_tmp, bpy_openlib, path, idcode); if (idcode == ID_SCE && options & LIB_LOAD_LOAD_SCRIPTS) { load_datablocks(main_tmp, bpy_openlib, path, ID_TXT); } /* now do another round of linking for Scenes so all actions are properly loaded */ if (idcode == ID_SCE && options & LIB_LOAD_LOAD_ACTIONS) { load_datablocks(main_tmp, bpy_openlib, path, ID_AC); } BLO_library_link_end(main_tmp, &bpy_openlib, flag, NULL, NULL); BLO_blendhandle_close(bpy_openlib); BKE_reports_clear(&reports); /* done linking */ /* needed for lookups*/ GetMainDynamic().push_back(main_newlib); BLI_strncpy(main_newlib->name, path, sizeof(main_newlib->name)); status = new KX_LibLoadStatus(this, m_ketsjiEngine, scene_merge, path); if (idcode == ID_ME) { /* Convert all new meshes into BGE meshes */ ID *mesh; for (mesh = (ID *)main_newlib->mesh.first; mesh; mesh = (ID *)mesh->next ) { if (options & LIB_LOAD_VERBOSE) printf("MeshName: %s\n", mesh->name + 2); RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)mesh, NULL, scene_merge, this, false); // For now only use the libloading option for scenes, which need to handle materials/shaders scene_merge->GetLogicManager()->RegisterMeshName(meshobj->GetName(), meshobj); } } else if (idcode == ID_AC) { /* Convert all actions */ ID *action; for (action= (ID *)main_newlib->action.first; action; action = (ID *)action->next) { if (options & LIB_LOAD_VERBOSE) printf("ActionName: %s\n", action->name + 2); scene_merge->GetLogicManager()->RegisterActionName(action->name + 2, action); } } else if (idcode == ID_SCE) { /* Merge all new linked in scene into the existing one */ ID *scene; // scenes gets deleted by the thread when it's done using it (look in async_convert()) vector<Scene *> *scenes = (options & LIB_LOAD_ASYNC) ? new vector<Scene *>() : NULL; for (scene = (ID *)main_newlib->scene.first; scene; scene = (ID *)scene->next ) { if (options & LIB_LOAD_VERBOSE) printf("SceneName: %s\n", scene->name + 2); if (options & LIB_LOAD_ASYNC) { scenes->push_back((Scene *)scene); } else { /* merge into the base scene */ KX_Scene* other = m_ketsjiEngine->CreateScene((Scene *)scene, true); scene_merge->MergeScene(other); // RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene delete other; } } if (options & LIB_LOAD_ASYNC) { status->SetData(scenes); BLI_task_pool_push(m_threadinfo->m_pool, async_convert, (void *)status, false, TASK_PRIORITY_LOW); } #ifdef WITH_PYTHON /* Handle any text datablocks */ if (options & LIB_LOAD_LOAD_SCRIPTS) addImportMain(main_newlib); #endif /* Now handle all the actions */ if (options & LIB_LOAD_LOAD_ACTIONS) { ID *action; for (action = (ID *)main_newlib->action.first; action; action = (ID *)action->next) { if (options & LIB_LOAD_VERBOSE) printf("ActionName: %s\n", action->name + 2); scene_merge->GetLogicManager()->RegisterActionName(action->name + 2, action); } } } if (!(options & LIB_LOAD_ASYNC)) status->Finish(); // TIMEIT_END(bge_link_blend_file); m_status_map[main_newlib->name] = status; return status; }
static void wm_init_reports(bContext *C) { BKE_reports_init(CTX_wm_reports(C), RPT_STORE); }