Example #1
0
static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
{
	Main *bmain = CTX_data_main(BPy_GetContext());
	BPy_Library *ret;
	const char *filename = NULL;
	bool is_rel = false, is_link = false;

	static const char *_keywords[] = {"filepath", "link", "relative", NULL};
	static _PyArg_Parser _parser = {"s|O&O&:load", _keywords, 0};
	if (!_PyArg_ParseTupleAndKeywordsFast(
	        args, kw, &_parser,
	        &filename,
	        PyC_ParseBool, &is_link,
	        PyC_ParseBool, &is_rel))
	{
		return NULL;
	}

	ret = PyObject_New(BPy_Library, &bpy_lib_Type);

	BLI_strncpy(ret->relpath, filename, sizeof(ret->relpath));
	BLI_strncpy(ret->abspath, filename, sizeof(ret->abspath));
	BLI_path_abs(ret->abspath, BKE_main_blendfile_path(bmain));

	ret->blo_handle = NULL;
	ret->flag = ((is_link ? FILE_LINK : 0) |
	             (is_rel ? FILE_RELPATH : 0));

	ret->dict = _PyDict_NewPresized(MAX_LIBARRAY);

	return (PyObject *)ret;
}
Example #2
0
static PyObject *bpygpu_offscreen_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
{
  BPYGPU_IS_INIT_OR_ERROR_OBJ;

  GPUOffScreen *ofs;
  int width, height, samples = 0;
  char err_out[256];

  static const char *_keywords[] = {"width", "height", "samples", NULL};
  static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0};
  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &width, &height, &samples)) {
    return NULL;
  }

  ofs = GPU_offscreen_create(width, height, samples, true, false, err_out);

  if (ofs == NULL) {
    PyErr_Format(PyExc_RuntimeError,
                 "gpu.offscreen.new(...) failed with '%s'",
                 err_out[0] ? err_out : "unknown error");
    return NULL;
  }

  return BPyGPUOffScreen_CreatePyObject(ofs);
}
Example #3
0
static PyObject *blender_import(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
{
	PyObject *exception, *err, *tb;
	const char *name;
	int found = 0;
	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
	int level = 0; /* relative imports */
	PyObject *newmodule;

	static const char *_keywords[] = {"name", "globals", "locals", "fromlist", "level", NULL};
	static _PyArg_Parser _parser = {"s|OOOi:bpy_import_meth", _keywords, 0};
	if (!_PyArg_ParseTupleAndKeywordsFast(
	        args, kw, &_parser,
	        &name, &globals, &locals, &fromlist, &level))
	{
		return NULL;
	}

	/* import existing builtin modules or modules that have been imported already */
	newmodule = PyImport_ImportModuleLevel(name, globals, locals, fromlist, level);
	
	if (newmodule)
		return newmodule;
	
	PyErr_Fetch(&exception, &err, &tb); /* get the python error in case we cant import as blender text either */
	
	/* importing from existing modules failed, see if we have this module as blender text */
	newmodule = bpy_text_import_name(name, &found);
	
	if (newmodule) { /* found module as blender text, ignore above exception */
		PyErr_Clear();
		Py_XDECREF(exception);
		Py_XDECREF(err);
		Py_XDECREF(tb);
		/* printf("imported from text buffer...\n"); */
	}
	else if (found == 1) { /* blender text module failed to execute but was found, use its error message */
		Py_XDECREF(exception);
		Py_XDECREF(err);
		Py_XDECREF(tb);
		return NULL;
	}
	else {
		/* no blender text was found that could import the module
		 * reuse the original error from PyImport_ImportModuleEx */
		PyErr_Restore(exception, err, tb);
	}
	return newmodule;
}
Example #4
0
static PyObject *bpygpu_offscreen_unbind(BPyGPUOffScreen *self, PyObject *args, PyObject *kwds)
{
  bool restore = true;

  BPY_GPU_OFFSCREEN_CHECK_OBJ(self);

  static const char *_keywords[] = {"restore", NULL};
  static _PyArg_Parser _parser = {"|O&:unbind", _keywords, 0};
  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, PyC_ParseBool, &restore)) {
    return NULL;
  }

  GPU_offscreen_unbind(self->ofs, restore);
  Py_RETURN_NONE;
}
Example #5
0
static PyObject *bpygpu_offscreen_bind(BPyGPUOffScreen *self, PyObject *args, PyObject *kwds)
{
  BPY_GPU_OFFSCREEN_CHECK_OBJ(self);
  bool save = true;

  static const char *_keywords[] = {"save", NULL};
  static _PyArg_Parser _parser = {"|O&:bind", _keywords, 0};
  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, PyC_ParseBool, &save)) {
    return NULL;
  }

  GPU_offscreen_bind(self->ofs, save);

  self->is_saved = save;
  Py_INCREF(self);

  return (PyObject *)self;
}
Example #6
0
static PyObject *bpygpu_offscreen_draw_view3d(BPyGPUOffScreen *self,
                                              PyObject *args,
                                              PyObject *kwds)
{
  MatrixObject *py_mat_view, *py_mat_projection;
  PyObject *py_scene, *py_view_layer, *py_region, *py_view3d;

  struct Depsgraph *depsgraph;
  struct Scene *scene;
  struct ViewLayer *view_layer;
  View3D *v3d;
  ARegion *ar;
  struct RV3DMatrixStore *rv3d_mats;

  BPY_GPU_OFFSCREEN_CHECK_OBJ(self);

  static const char *_keywords[] = {
      "scene", "view_layer", "view3d", "region", "view_matrix", "projection_matrix", NULL};

  static _PyArg_Parser _parser = {"OOOOO&O&:draw_view3d", _keywords, 0};
  if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                        kwds,
                                        &_parser,
                                        &py_scene,
                                        &py_view_layer,
                                        &py_view3d,
                                        &py_region,
                                        Matrix_Parse4x4,
                                        &py_mat_view,
                                        Matrix_Parse4x4,
                                        &py_mat_projection) ||
      (!(scene = PyC_RNA_AsPointer(py_scene, "Scene")) ||
       !(view_layer = PyC_RNA_AsPointer(py_view_layer, "ViewLayer")) ||
       !(v3d = PyC_RNA_AsPointer(py_view3d, "SpaceView3D")) ||
       !(ar = PyC_RNA_AsPointer(py_region, "Region")))) {
    return NULL;
  }

  BLI_assert(BKE_id_is_in_global_main(&scene->id));

  depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);

  rv3d_mats = ED_view3d_mats_rv3d_backup(ar->regiondata);

  GPU_offscreen_bind(self->ofs, true);

  ED_view3d_draw_offscreen(depsgraph,
                           scene,
                           v3d->shading.type,
                           v3d,
                           ar,
                           GPU_offscreen_width(self->ofs),
                           GPU_offscreen_height(self->ofs),
                           (float(*)[4])py_mat_view->matrix,
                           (float(*)[4])py_mat_projection->matrix,
                           false,
                           true,
                           "",
                           true,
                           self->ofs,
                           NULL);

  GPU_offscreen_unbind(self->ofs, true);

  ED_view3d_mats_rv3d_restore(ar->regiondata, rv3d_mats);
  MEM_freeN(rv3d_mats);

  Py_RETURN_NONE;
}
Example #7
0
static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
{
	/* args */
	const char *filepath;
	char filepath_abs[FILE_MAX];
	PyObject *datablocks = NULL;
	bool use_relative_remap = false, use_fake_user = false, use_compress = false;

	static const char *_keywords[] = {
		"filepath", "datablocks",
		/* optional */
		"relative_remap", "fake_user", "compress",
		NULL,
	};
	static _PyArg_Parser _parser = {"sO!|$O&O&O&:write", _keywords, 0};
	if (!_PyArg_ParseTupleAndKeywordsFast(
	        args, kw, &_parser,
	        &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;
}