/**
 * \a check_prop callback filters functions to avoid drawing certain properties,
 * in cases where PROP_HIDDEN flag can't be used for a property.
 */
int uiDefAutoButsRNA(uiLayout *layout, PointerRNA *ptr,
                     int (*check_prop)(PointerRNA *, PropertyRNA *),
                     const char label_align)
{
	uiLayout *split, *col;
	int flag;
	const char *name;
	int tot = 0;

	assert(ELEM3(label_align, '\0', 'H', 'V'));

	RNA_STRUCT_BEGIN (ptr, prop)
	{
		flag = RNA_property_flag(prop);
		if (flag & PROP_HIDDEN || (check_prop && check_prop(ptr, prop) == FALSE))
			continue;

		if (label_align != '\0') {
			PropertyType type = RNA_property_type(prop);
			int is_boolean = (type == PROP_BOOLEAN && !RNA_property_array_check(prop));

			name = RNA_property_ui_name(prop);

			if (label_align == 'V') {
				col = uiLayoutColumn(layout, TRUE);

				if (!is_boolean)
					uiItemL(col, name, ICON_NONE);
			}
			else if (label_align == 'H') {
				split = uiLayoutSplit(layout, 0.5f, FALSE);

				col = uiLayoutColumn(split, FALSE);
				uiItemL(col, (is_boolean) ? "" : name, ICON_NONE);
				col = uiLayoutColumn(split, FALSE);
			}
			else {
				col = NULL;
			}

			/* may meed to add more cases here.
			 * don't override enum flag names */

			/* name is shown above, empty name for button below */
			name = (flag & PROP_ENUM_FLAG || is_boolean) ? NULL : "";
		}
		else {
			col = layout;
			name = NULL; /* no smart label alignment, show default name with button */
		}

		uiItemFullR(col, ptr, prop, -1, 0, 0, name, ICON_NONE);
		tot++;
	}
static int py_to_array(PyObject *seq, PointerRNA *ptr, PropertyRNA *prop,
                       char *param_data, ItemTypeCheckFunc check_item_type, const char *item_type_str, int item_size,
                       ItemConvertFunc convert_item, RNA_SetArrayFunc rna_set_array, const char *error_prefix)
{
	/*int totdim, dim_size[MAX_ARRAY_DIMENSION];*/
	int totitem;
	char *data = NULL;

	/*totdim = RNA_property_array_dimension(ptr, prop, dim_size);*/ /*UNUSED*/

	if (validate_array(seq, ptr, prop, 0, check_item_type, item_type_str, &totitem, error_prefix) == -1) {
		return -1;
	}

	if (totitem) {
		/* note: this code is confusing */
		if (param_data && RNA_property_flag(prop) & PROP_DYNAMIC) {
			/* not freeing allocated mem, RNA_parameter_list_free() will do this */
			ParameterDynAlloc *param_alloc = (ParameterDynAlloc *)param_data;
			param_alloc->array_tot = (int)totitem;
			param_alloc->array = MEM_callocN(item_size * totitem, "py_to_array dyn"); /* freeing param list will free */

			data = param_alloc->array;
		}
		else if (param_data) {
			data = param_data;
		}
		else {
			data = PyMem_MALLOC(item_size * totitem);
		}

		/* will only fail in very rare cases since we already validated the
		 * python data, the check here is mainly for completeness. */
		if (copy_values(seq, ptr, prop, 0, data, item_size, NULL, convert_item, NULL) != NULL) {
			if (param_data == NULL) {
				/* NULL can only pass through in case RNA property arraylength is 0 (impossible?) */
				rna_set_array(ptr, prop, data);
				PyMem_FREE(data);
			}
		}
		else {
			if (param_data == NULL) {
				PyMem_FREE(data);
			}

			PyErr_Format(PyExc_TypeError, "%s internal error parsing sequence of type '%s' after successful validation",
			             error_prefix, Py_TYPE(seq)->tp_name);
			return -1;
		}
	}

	return 0;
}
Exemple #3
0
static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)
{
	PyGILState_STATE gilstate;

	PyObject *py_func= RNA_property_enum_py_data_get(prop);
	PyObject *self= NULL;
	PyObject *args;
	PyObject *items; /* returned from the function call */

	EnumPropertyItem *eitems= NULL;
	int err= 0;

	bpy_context_set(C, &gilstate);

	args= PyTuple_New(2);
	self= pyrna_struct_as_instance(ptr);
	PyTuple_SET_ITEM(args, 0, self);

	/* now get the context */
	PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
	Py_INCREF(bpy_context_module);

	items= PyObject_CallObject(py_func, args);

	Py_DECREF(args);

	if(items==NULL) {
		err= -1;
	}
	else {
		PyObject *items_fast;
		int defvalue_dummy=0;

		if(!(items_fast= PySequence_Fast(items, "EnumProperty(...): return value from the callback was not a sequence"))) {
			err= -1;
		}
		else {
			eitems= enum_items_from_py(items_fast, NULL, &defvalue_dummy, (RNA_property_flag(prop) & PROP_ENUM_FLAG)!=0);

			Py_DECREF(items_fast);

			if(!eitems) {
				err= -1;
			}
		}

		Py_DECREF(items);
	}

	if(err != -1) { /* worked */
		*free= 1;
	}
	else {
		printf_func_error(py_func);

		eitems= DummyRNA_NULL_items;
	}


	bpy_context_clear(C, &gilstate);
	return eitems;
}
Exemple #4
0
uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int index, const char *name, int icon, int x1, int y1, int x2, int y2)
{
	uiBut *but = NULL;

	switch (RNA_property_type(prop)) {
		case PROP_BOOLEAN:
		{
			int arraylen = RNA_property_array_length(ptr, prop);

			if (arraylen && index == -1)
				return NULL;
			
			if (icon && name && name[0] == '\0')
				but = uiDefIconButR_prop(block, UI_BTYPE_ICON_TOGGLE, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else if (icon)
				but = uiDefIconTextButR_prop(block, UI_BTYPE_ICON_TOGGLE, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else
				but = uiDefButR_prop(block, UI_BTYPE_CHECKBOX, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			break;
		}
		case PROP_INT:
		case PROP_FLOAT:
		{
			int arraylen = RNA_property_array_length(ptr, prop);

			if (arraylen && index == -1) {
				if (ELEM(RNA_property_subtype(prop), PROP_COLOR, PROP_COLOR_GAMMA)) {
					but = uiDefButR_prop(block, UI_BTYPE_COLOR, 0, name, x1, y1, x2, y2, ptr, prop, -1, 0, 0, -1, -1, NULL);
				}
				else {
					return NULL;
				}
			}
			else if (RNA_property_subtype(prop) == PROP_PERCENTAGE || RNA_property_subtype(prop) == PROP_FACTOR)
				but = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else
				but = uiDefButR_prop(block, UI_BTYPE_NUM, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);

			if (RNA_property_flag(prop) & PROP_TEXTEDIT_UPDATE) {
				UI_but_flag_enable(but, UI_BUT_TEXTEDIT_UPDATE);
			}
			break;
		}
		case PROP_ENUM:
			if (icon && name && name[0] == '\0')
				but = uiDefIconButR_prop(block, UI_BTYPE_MENU, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else if (icon)
				but = uiDefIconTextButR_prop(block, UI_BTYPE_MENU, 0, icon, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else
				but = uiDefButR_prop(block, UI_BTYPE_MENU, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			break;
		case PROP_STRING:
			if (icon && name && name[0] == '\0')
				but = uiDefIconButR_prop(block, UI_BTYPE_TEXT, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else if (icon)
				but = uiDefIconTextButR_prop(block, UI_BTYPE_TEXT, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			else
				but = uiDefButR_prop(block, UI_BTYPE_TEXT, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);

			PropertySubType subtype = RNA_property_subtype(prop);
			if (!(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME) || (block->flag & UI_BLOCK_LIST_ITEM))) {
				UI_but_flag_enable(but, UI_BUT_VALUE_CLEAR);
			}
			if (RNA_property_flag(prop) & PROP_TEXTEDIT_UPDATE) {
				UI_but_flag_enable(but, UI_BUT_TEXTEDIT_UPDATE);
			}
			break;
		case PROP_POINTER:
		{
			PointerRNA pptr;

			pptr = RNA_property_pointer_get(ptr, prop);
			if (!pptr.type)
				pptr.type = RNA_property_pointer_type(ptr, prop);
			icon = RNA_struct_ui_icon(pptr.type);
			if (icon == ICON_DOT)
				icon = 0;

			but = uiDefIconTextButR_prop(block, UI_BTYPE_SEARCH_MENU, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
			break;
		}
		case PROP_COLLECTION:
		{
			char text[256];
			BLI_snprintf(text, sizeof(text), IFACE_("%d items"), RNA_property_collection_length(ptr, prop));
			but = uiDefBut(block, UI_BTYPE_LABEL, 0, text, x1, y1, x2, y2, NULL, 0, 0, 0, 0, NULL);
			UI_but_flag_enable(but, UI_BUT_DISABLED);
			break;
		}
		default:
			but = NULL;
			break;
	}

	return but;
}
/* Modifies property array length if needed and PROP_DYNAMIC flag is set. */
static int validate_array_length(PyObject *rvalue, PointerRNA *ptr, PropertyRNA *prop,
                                 int lvalue_dim, int *totitem, const char *error_prefix)
{
	int dimsize[MAX_ARRAY_DIMENSION];
	int tot, totdim, len;

	totdim = RNA_property_array_dimension(ptr, prop, dimsize);
	tot = count_items(rvalue, totdim - lvalue_dim);

	if (tot == -1) {
		PyErr_Format(PyExc_ValueError, "%s %.200s.%.200s, error validating the sequence length",
		             error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
		return -1;
	}
	else if ((RNA_property_flag(prop) & PROP_DYNAMIC) && lvalue_dim == 0) {
		if (RNA_property_array_length(ptr, prop) != tot) {
#if 0
			/* length is flexible */
			if (!RNA_property_dynamic_array_set_length(ptr, prop, tot)) {
				/* BLI_snprintf(error_str, error_str_size,
				 *              "%s.%s: array length cannot be changed to %d",
				 *              RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), tot); */
				PyErr_Format(PyExc_ValueError, "%s %s.%s: array length cannot be changed to %d",
				             error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), tot);
				return -1;
			}
#else
			*totitem = tot;
			return 0;

#endif
		}

		len = tot;
	}
	else {
		/* length is a constraint */
		if (!lvalue_dim) {
			len = RNA_property_array_length(ptr, prop);
		}
		/* array item assignment */
		else {
			int i;

			len = 1;

			/* arr[3][4][5]
			 *
			 *    arr[2] = x
			 *    dimsize = {4, 5}
			 *    dimsize[1] = 4
			 *    dimsize[2] = 5
			 *    lvalue_dim = 0, totdim = 3
			 * 
			 *    arr[2][3] = x
			 *    lvalue_dim = 1
			 * 
			 *    arr[2][3][4] = x
			 *    lvalue_dim = 2 */
			for (i = lvalue_dim; i < totdim; i++)
				len *= dimsize[i];
		}

		if (tot != len) {
			/* BLI_snprintf(error_str, error_str_size, "sequence must have length of %d", len); */
			PyErr_Format(PyExc_ValueError, "%s %.200s.%.200s, sequence must have %d items total, not %d",
			             error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), len, tot);
			return -1;
		}
	}

	*totitem = len;

	return 0;
}