13#define PY_SSIZE_T_CLEAN
16#include <structmember.h>
65 "`bpyunits_ucategories_items` should match `B_UNIT_` enum items in `BKE_units.h`")
76 "bpy.utils.units.systems",
77 "This named tuple contains all predefined unit systems",
78 bpyunits_systems_fields,
82 "bpy.utils.units.categories",
83 "This named tuple contains all predefined unit names",
92 PyStructSequence_Desc *py_sseq_desc,
93 const char **str_items)
95 PyObject *py_struct_seq;
98 const char **str_iter;
99 PyStructSequence_Field *desc;
103 for (str_iter = str_items, desc = py_sseq_desc->fields; *str_iter; str_iter++, desc++) {
104 desc->name =
const_cast<char *
>(*str_iter);
108 desc->name = desc->doc =
nullptr;
110 PyStructSequence_InitType(py_type, py_sseq_desc);
113 py_struct_seq = PyStructSequence_New(py_type);
116 for (str_iter = str_items; *str_iter; str_iter++) {
117 PyStructSequence_SET_ITEM(py_struct_seq, pos++, PyUnicode_FromString(*str_iter));
120 return py_struct_seq;
123static bool bpyunits_validate(
const char *usys_str,
const char *ucat_str,
int *r_usys,
int *r_ucat)
127 PyErr_Format(PyExc_ValueError,
"Unknown unit system specified: %.200s.", usys_str);
133 PyErr_Format(PyExc_ValueError,
"Unknown unit category specified: %.200s.", ucat_str);
138 PyErr_Format(PyExc_ValueError,
139 "%.200s / %.200s unit system/category combination is not valid.",
150 bpyunits_to_value_doc,
151 ".. function:: to_value(unit_system, unit_category, str_input, *, str_ref_unit=None)\n"
153 " Convert a given input string into a float value.\n"
155 " :param unit_system: The unit system, from :attr:`bpy.utils.units.systems`.\n"
156 " :type unit_system: str\n"
157 " :param unit_category: The category of data we are converting (length, area, rotation, "
159 " from :attr:`bpy.utils.units.categories`.\n"
160 " :type unit_category: str\n"
161 " :param str_input: The string to convert to a float value.\n"
162 " :type str_input: str\n"
163 " :param str_ref_unit: A reference string from which to extract a default unit, if none is "
164 "found in ``str_input``.\n"
165 " :type str_ref_unit: str | None\n"
166 " :return: The converted/interpreted value.\n"
168 " :raises ValueError: if conversion fails to generate a valid Python float value.\n");
171 char *usys_str =
nullptr, *ucat_str =
nullptr, *inpt =
nullptr, *uref =
nullptr;
172 const float scale = 1.0f;
180 static const char *_keywords[] = {
187 static _PyArg_Parser _parser = {
197 if (!_PyArg_ParseTupleAndKeywordsFast(
198 args, kw, &_parser, &usys_str, &ucat_str, &inpt, &str_len, &uref))
207 const size_t str_maxncpy = str_len * 2 + 64;
208 str =
static_cast<char *
>(PyMem_MALLOC(
sizeof(*
str) * str_maxncpy));
214 if (PyErr_Occurred()) {
219 PyExc_ValueError,
"'%.200s' (converted as '%s') could not be evaluated.", inpt,
str);
232 bpyunits_to_string_doc,
233 ".. function:: to_string(unit_system, unit_category, value, *, precision=3, "
234 "split_unit=False, compatible_unit=False)\n"
236 " Convert a given input float value into a string with units.\n"
238 " :param unit_system: The unit system, from :attr:`bpy.utils.units.systems`.\n"
239 " :type unit_system: str\n"
240 " :param unit_category: The category of data we are converting (length, area, "
242 " from :attr:`bpy.utils.units.categories`.\n"
243 " :type unit_category: str\n"
244 " :param value: The value to convert to a string.\n"
245 " :type value: float\n"
246 " :param precision: Number of digits after the decimal point.\n"
247 " :type precision: int\n"
248 " :param split_unit: Whether to use several units if needed (1m1cm), or always only "
250 " :type split_unit: bool\n"
251 " :param compatible_unit: Whether to use keyboard-friendly units (1m2) or nicer "
253 " :type compatible_unit: bool\n"
254 " :return: The converted string.\n"
256 " :raises ValueError: if conversion fails to generate a valid Python string.\n");
259 char *usys_str =
nullptr, *ucat_str =
nullptr;
262 bool split_unit =
false, compatible_unit =
false;
266 static const char *_keywords[] = {
275 static _PyArg_Parser _parser = {
287 if (!_PyArg_ParseTupleAndKeywordsFast(args,
315 char buf1[64], buf2[64];
320 buf1,
sizeof(buf1), value, precision, usys, ucat, split_unit,
false,
true);
322 if (compatible_unit) {
338# pragma clang diagnostic push
339# pragma clang diagnostic ignored "-Wcast-function-type"
341# pragma GCC diagnostic push
342# pragma GCC diagnostic ignored "-Wcast-function-type"
349 METH_VARARGS | METH_KEYWORDS,
350 bpyunits_to_value_doc},
353 METH_VARARGS | METH_KEYWORDS,
354 bpyunits_to_string_doc},
355 {
nullptr,
nullptr, 0,
nullptr},
360# pragma clang diagnostic pop
362# pragma GCC diagnostic pop
369 "This module contains some data/methods regarding units handling.\n");
384 PyObject *submodule, *item;
387 PyDict_SetItemString(PyImport_GetModuleDict(),
bpyunits_module.m_name, submodule);
394 PyModule_AddObject(submodule,
"systems", item);
399 PyModule_AddObject(submodule,
"categories", item);
float3x3 scale(float3x3 mat, float2 scale)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value)
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
int PyC_ParseBool(PyObject *o, void *p)
static PyObject * bpyunits_to_string(PyObject *, PyObject *args, PyObject *kw)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyTypeObject BPyUnitsSystemsType
static const char * bpyunits_ucategories_items[]
static PyStructSequence_Desc bpyunits_systems_desc
static PyModuleDef bpyunits_module
static PyMethodDef bpyunits_methods[]
int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static PyObject * py_structseq_from_strings(PyTypeObject *py_type, PyStructSequence_Desc *py_sseq_desc, const char **str_items)
bool BKE_unit_replace_string(char *str, int str_maxncpy, const char *str_prev, double scale_pref, int system, int type)
static PyObject * bpyunits_to_value(PyObject *, PyObject *args, PyObject *kw)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static PyStructSequence_Field bpyunits_categories_fields[ARRAY_SIZE(bpyunits_ucategories_items)]
static PyTypeObject BPyUnitsCategoriesType
PyObject * BPY_utils_units()
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyStructSequence_Desc bpyunits_categories_desc
size_t BKE_unit_value_as_string_adaptive(char *str, int str_maxncpy, double value, int prec, int system, int type, bool split, bool pad, bool do_unit_suffix)
bool BKE_unit_is_valid(int system, int type)
static bool bpyunits_validate(const char *usys_str, const char *ucat_str, int *r_usys, int *r_ucat)
void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int system, int type)
static const char * bpyunits_usystem_items[]
header-only compatibility defines.