diff options
Diffstat (limited to 'source/blender/python')
30 files changed, 1135 insertions, 325 deletions
diff --git a/source/blender/python/doc/epy/Geometry.py b/source/blender/python/doc/epy/Geometry.py index 237cd8c3443..717c147b215 100644 --- a/source/blender/python/doc/epy/Geometry.py +++ b/source/blender/python/doc/epy/Geometry.py @@ -101,7 +101,7 @@ def PolyFill(polylines): The example below creates 2 polylines and fills them in with faces, then makes a mesh in the current scene:: import Blender - Vector= Blender.Mathutils.Vector + Vector= Blender.mathutils.Vector # Outline of 5 points polyline1= [Vector(-2.0, 1.0, 1.0), Vector(-1.0, 2.0, 1.0), Vector(1.0, 2.0, 1.0), Vector(1.0, -1.0, 1.0), Vector(-1.0, -1.0, 1.0)] diff --git a/source/blender/python/doc/epy/Mathutils.py b/source/blender/python/doc/epy/Mathutils.py index b1bb039debe..17a227f729a 100644 --- a/source/blender/python/doc/epy/Mathutils.py +++ b/source/blender/python/doc/epy/Mathutils.py @@ -1,4 +1,4 @@ -# Blender.Mathutils module and its subtypes +# Blender.mathutils module and its subtypes diff --git a/source/blender/python/doc/examples/Mathutils.Euler.py b/source/blender/python/doc/examples/Mathutils.Euler.py deleted file mode 100644 index 0e7a62162d9..00000000000 --- a/source/blender/python/doc/examples/Mathutils.Euler.py +++ /dev/null @@ -1,3 +0,0 @@ -import Mathutils - -# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/Mathutils.Matrix.py b/source/blender/python/doc/examples/Mathutils.Matrix.py deleted file mode 100644 index 0e7a62162d9..00000000000 --- a/source/blender/python/doc/examples/Mathutils.Matrix.py +++ /dev/null @@ -1,3 +0,0 @@ -import Mathutils - -# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/Mathutils.Quaternion.py b/source/blender/python/doc/examples/Mathutils.Quaternion.py deleted file mode 100644 index 0e7a62162d9..00000000000 --- a/source/blender/python/doc/examples/Mathutils.Quaternion.py +++ /dev/null @@ -1,3 +0,0 @@ -import Mathutils - -# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/Mathutils.py b/source/blender/python/doc/examples/Mathutils.py deleted file mode 100644 index a00ca3dd1c8..00000000000 --- a/source/blender/python/doc/examples/Mathutils.py +++ /dev/null @@ -1,17 +0,0 @@ -import Mathutils - -vec = Mathutils.Vector(1.0, 2.0, 3.0) - -mat_rot = Mathutils.RotationMatrix(90, 4, 'X') -mat_trans = Mathutils.TranslationMatrix(vec) - -mat = mat_trans * mat_rot -mat.invert() - -mat3 = mat.rotation_part() -quat1 = mat.to_quat() -quat2 = mat3.to_quat() - -angle = quat1.difference(quat2) - -print(angle)
\ No newline at end of file diff --git a/source/blender/python/doc/examples/mathutils.Euler.py b/source/blender/python/doc/examples/mathutils.Euler.py new file mode 100644 index 00000000000..f8294ce5545 --- /dev/null +++ b/source/blender/python/doc/examples/mathutils.Euler.py @@ -0,0 +1,3 @@ +import mathutils + +# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/mathutils.Matrix.py b/source/blender/python/doc/examples/mathutils.Matrix.py new file mode 100644 index 00000000000..f8294ce5545 --- /dev/null +++ b/source/blender/python/doc/examples/mathutils.Matrix.py @@ -0,0 +1,3 @@ +import mathutils + +# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/mathutils.Quaternion.py b/source/blender/python/doc/examples/mathutils.Quaternion.py new file mode 100644 index 00000000000..f8294ce5545 --- /dev/null +++ b/source/blender/python/doc/examples/mathutils.Quaternion.py @@ -0,0 +1,3 @@ +import mathutils + +# todo
\ No newline at end of file diff --git a/source/blender/python/doc/examples/Mathutils.Vector.py b/source/blender/python/doc/examples/mathutils.Vector.py index 8b3dbfa5ee8..143ad234416 100644 --- a/source/blender/python/doc/examples/Mathutils.Vector.py +++ b/source/blender/python/doc/examples/mathutils.Vector.py @@ -1,20 +1,20 @@ -import Mathutils +import mathutils # zero length vector -vec = Mathutils.Vector(0, 0, 1) +vec = mathutils.Vector(0, 0, 1) # unit length vector vec_a = vec.copy().normalize() -vec_b = Mathutils.Vector(0, 1, 2) +vec_b = mathutils.Vector(0, 1, 2) -vec2d = Mathutils.Vector(1, 2) -vec3d = Mathutils.Vector([1, 0, 0]) +vec2d = mathutils.Vector(1, 2) +vec3d = mathutils.Vector([1, 0, 0]) vec4d = vec_a.copy().resize4D() # other mathutuls types -quat = Mathutils.Quaternion() -matrix = Mathutils.Matrix() +quat = mathutils.Quaternion() +matrix = mathutils.Matrix() # Comparison operators can be done on Vector classes: diff --git a/source/blender/python/doc/examples/mathutils.py b/source/blender/python/doc/examples/mathutils.py new file mode 100644 index 00000000000..dee8fa4d6bd --- /dev/null +++ b/source/blender/python/doc/examples/mathutils.py @@ -0,0 +1,17 @@ +import mathutils + +vec = mathutils.Vector(1.0, 2.0, 3.0) + +mat_rot = mathutils.RotationMatrix(90, 4, 'X') +mat_trans = mathutils.TranslationMatrix(vec) + +mat = mat_trans * mat_rot +mat.invert() + +mat3 = mat.rotation_part() +quat1 = mat.to_quat() +quat2 = mat3.to_quat() + +angle = quat1.difference(quat2) + +print(angle)
\ No newline at end of file diff --git a/source/blender/python/doc/sphinx_doc_gen.py b/source/blender/python/doc/sphinx_doc_gen.py index 9948b04136c..48c06fb47e9 100644 --- a/source/blender/python/doc/sphinx_doc_gen.py +++ b/source/blender/python/doc/sphinx_doc_gen.py @@ -43,9 +43,16 @@ import bpy import rna_info reload(rna_info) +# lame, python wont give some access +MethodDescriptorType = type(dict.get) +GetSetDescriptorType = type(int.real) +StaticMethodType = type(staticmethod(lambda: None)) + EXAMPLE_SET = set() EXAMPLE_SET_USED = set() +_BPY_STRUCT_FAKE = "bpy_struct" + def range_str(val): if val < -10000000: return '-inf' if val > 10000000: return 'inf' @@ -118,6 +125,23 @@ def pyfunc2sphinx(ident, fw, identifier, py_func, is_class=True): write_indented_lines(ident + " ", fw, py_func.__doc__.strip()) fw("\n") + +def py_descr2sphinx(ident, fw, descr, module_name, type_name, identifier): + doc = descr.__doc__ + if not doc: + doc = "Undocumented" + + if type(descr) == GetSetDescriptorType: + fw(ident + ".. attribute:: %s\n\n" % identifier) + write_indented_lines(ident, fw, doc, False) + elif type(descr) == MethodDescriptorType: # GetSetDescriptorType, GetSetDescriptorType's are not documented yet + write_indented_lines(ident, fw, doc, False) + else: + raise TypeError("type was not GetSetDescriptorType or MethodDescriptorType") + + write_example_ref(ident, fw, module_name + "." + type_name + "." + identifier) + fw("\n") + def py_c_func2sphinx(ident, fw, identifier, py_func, is_class=True): ''' c defined function to sphinx. @@ -144,13 +168,7 @@ def pyprop2sphinx(ident, fw, identifier, py_prop): def pymodule2sphinx(BASEPATH, module_name, module, title): import types - # lame, python wont give some access - MethodDescriptorType = type(dict.get) - GetSetDescriptorType = type(int.real) - StaticMethodType = type(staticmethod(lambda: None)) - - - + attribute_set = set() filepath = os.path.join(BASEPATH, module_name + ".rst") file = open(filepath, "w") @@ -171,18 +189,26 @@ def pymodule2sphinx(BASEPATH, module_name, module, title): # write members of the module # only tested with PyStructs which are not exactly modules - for attribute, descr in sorted(type(module).__dict__.items()): + for key, descr in sorted(type(module).__dict__.items()): if type(descr) == types.MemberDescriptorType: if descr.__doc__: - fw(".. data:: %s\n\n" % attribute) + fw(".. data:: %s\n\n" % key) write_indented_lines(" ", fw, descr.__doc__, False) + attribute_set.add(key) fw("\n") - + del key, descr classes = [] - for attribute in dir(module): + for attribute in sorted(dir(module)): if not attribute.startswith("_"): + + if attribute in attribute_set: + continue + + if attribute.startswith("n_"): # annoying exception, needed for bpy.app + continue + value = getattr(module, attribute) value_type = type(value) @@ -195,44 +221,44 @@ def pymodule2sphinx(BASEPATH, module_name, module, title): py_c_func2sphinx("", fw, attribute, value, is_class=False) elif value_type == type: classes.append((attribute, value)) + elif value_type in (bool, int, float, str, tuple): + # constant, not much fun we can do here except to list it. + # TODO, figure out some way to document these! + fw(".. data:: %s\n\n" % attribute) + write_indented_lines(" ", fw, "constant value %s" % repr(value), False) + fw("\n") + else: + print("\tnot documenting %s.%s" % (module_name, attribute)) + continue + + attribute_set.add(attribute) # TODO, more types... - + # write collected classes now - for (attribute, value) in classes: + for (type_name, value) in classes: # May need to be its own function - fw(".. class:: %s\n\n" % attribute) + fw(".. class:: %s\n\n" % type_name) if value.__doc__: write_indented_lines(" ", fw, value.__doc__, False) fw("\n") - write_example_ref(" ", fw, module_name + "." + attribute) + write_example_ref(" ", fw, module_name + "." + type_name) - for key in sorted(value.__dict__.keys()): - if key.startswith("__"): - continue - descr = value.__dict__[key] - if type(descr) == GetSetDescriptorType: - if descr.__doc__: - fw(" .. attribute:: %s\n\n" % key) - write_indented_lines(" ", fw, descr.__doc__, False) - write_example_ref(" ", fw, module_name + "." + attribute + "." + key) - fw("\n") - - for key in sorted(value.__dict__.keys()): - if key.startswith("__"): - continue - descr = value.__dict__[key] + descr_items = [(key, descr) for key, descr in sorted(value.__dict__.items()) if not key.startswith("__")] + + for key, descr in descr_items: if type(descr) == MethodDescriptorType: # GetSetDescriptorType, GetSetDescriptorType's are not documented yet - if descr.__doc__: - write_indented_lines(" ", fw, descr.__doc__, False) - write_example_ref(" ", fw, module_name + "." + attribute + "." + key) - fw("\n") - elif type(descr) == StaticMethodType: + py_descr2sphinx(" ", fw, descr, module_name, type_name, key) + + for key, descr in descr_items: + if type(descr) == GetSetDescriptorType: + py_descr2sphinx(" ", fw, descr, module_name, type_name, key) + + for key, descr in descr_items: + if type(descr) == StaticMethodType: descr = getattr(value, key) - if descr.__doc__: - write_indented_lines(" ", fw, descr.__doc__, False) - write_example_ref(" ", fw, module_name + "." + attribute + "." + key) - fw("\n") - + write_indented_lines(" ", fw, descr.__doc__ or "Undocumented", False) + fw("\n") + fw("\n\n") file.close() @@ -299,7 +325,7 @@ def rna2sphinx(BASEPATH): fw(" These parts of the API are relatively stable and are unlikely to change significantly\n") fw(" * data API, access to attributes of blender data such as mesh verts, material color, timeline frames and scene objects\n") fw(" * user interface functions for defining buttons, creation of menus, headers, panels\n") - fw(" * modules: bgl, Mathutils and Geometry\n") + fw(" * modules: bgl, mathutils and geometry\n") fw("\n") fw(".. toctree::\n") fw(" :maxdepth: 1\n\n") @@ -313,7 +339,7 @@ def rna2sphinx(BASEPATH): # C modules fw(" bpy.props.rst\n\n") - fw(" Mathutils.rst\n\n") + fw(" mathutils.rst\n\n") fw(" Freestyle.rst\n\n") fw(" blf.rst\n\n") file.close() @@ -353,8 +379,8 @@ def rna2sphinx(BASEPATH): from bpy import props as module pymodule2sphinx(BASEPATH, "bpy.props", module, "Property Definitions (bpy.props)") - import Mathutils as module - pymodule2sphinx(BASEPATH, "Mathutils", module, "Math Types & Utilities (Mathutils)") + import mathutils as module + pymodule2sphinx(BASEPATH, "mathutils", module, "Math Types & Utilities (mathutils)") del module import Freestyle as module @@ -405,8 +431,14 @@ def rna2sphinx(BASEPATH): file = open(filepath, "w") fw = file.write - if struct.base: - title = "%s(%s)" % (struct.identifier, struct.base.identifier) + base_id = getattr(struct.base, "identifier", "") + + if _BPY_STRUCT_FAKE: + if not base_id: + base_id = _BPY_STRUCT_FAKE + + if base_id: + title = "%s(%s)" % (struct.identifier, base_id) else: title = struct.identifier @@ -414,26 +446,34 @@ def rna2sphinx(BASEPATH): fw(".. module:: bpy.types\n\n") - bases = struct.get_bases() - if bases: - if len(bases) > 1: + base_ids = [base.identifier for base in struct.get_bases()] + + if _BPY_STRUCT_FAKE: + base_ids.append(_BPY_STRUCT_FAKE) + + base_ids.reverse() + + if base_ids: + if len(base_ids) > 1: fw("base classes --- ") else: fw("base class --- ") - fw(", ".join([(":class:`%s`" % base.identifier) for base in reversed(bases)])) + fw(", ".join([(":class:`%s`" % base_id) for base_id in base_ids])) fw("\n\n") - subclasses = [s for s in structs.values() if s.base is struct] + subclass_ids = [s.identifier for s in structs.values() if s.base is struct if not rna_info.rna_id_ignore(s.identifier)] + if subclass_ids: + fw("subclasses --- \n" + ", ".join([(":class:`%s`" % s) for s in subclass_ids]) + "\n\n") - if subclasses: - fw("subclasses --- \n") - fw(", ".join([(":class:`%s`" % s.identifier) for s in subclasses])) - fw("\n\n") - + base_id = getattr(struct.base, "identifier", "") + + if _BPY_STRUCT_FAKE: + if not base_id: + base_id = _BPY_STRUCT_FAKE - if struct.base: - fw(".. class:: %s(%s)\n\n" % (struct.identifier, struct.base.identifier)) + if base_id: + fw(".. class:: %s(%s)\n\n" % (struct.identifier, base_id)) else: fw(".. class:: %s\n\n" % struct.identifier) @@ -484,6 +524,62 @@ def rna2sphinx(BASEPATH): pyfunc2sphinx(" ", fw, identifier, py_func, is_class=True) del py_funcs, py_func + lines = [] + + if struct.base or _BPY_STRUCT_FAKE: + bases = list(reversed(struct.get_bases())) + + # props + lines[:] = [] + + if _BPY_STRUCT_FAKE: + descr_items = [(key, descr) for key, descr in sorted(bpy.types.Struct.__bases__[0].__dict__.items()) if not key.startswith("__")] + + if _BPY_STRUCT_FAKE: + for key, descr in descr_items: + if type(descr) == GetSetDescriptorType: + lines.append("* :class:`%s.%s`\n" % (_BPY_STRUCT_FAKE, key)) + + for base in bases: + for prop in base.properties: + lines.append("* :class:`%s.%s`\n" % (base.identifier, prop.identifier)) + + for identifier, py_prop in base.get_py_properties(): + lines.append("* :class:`%s.%s`\n" % (base.identifier, identifier)) + + for identifier, py_prop in base.get_py_properties(): + lines.append("* :class:`%s.%s`\n" % (base.identifier, identifier)) + + if lines: + fw(".. rubric:: Inherited Properties\n\n") + for line in lines: + fw(line) + fw("\n") + + + # funcs + lines[:] = [] + + if _BPY_STRUCT_FAKE: + for key, descr in descr_items: + if type(descr) == MethodDescriptorType: + lines.append("* :class:`%s.%s`\n" % (_BPY_STRUCT_FAKE, key)) + + for base in bases: + for func in base.functions: + lines.append("* :class:`%s.%s`\n" % (base.identifier, func.identifier)) + for identifier, py_func in base.get_py_functions(): + lines.append("* :class:`%s.%s`\n" % (base.identifier, identifier)) + + if lines: + fw(".. rubric:: Inherited Functions\n\n") + for line in lines: + fw(line) + fw("\n") + + lines[:] = [] + + if struct.references: # use this otherwise it gets in the index for a normal heading. fw(".. rubric:: References\n\n") @@ -501,6 +597,38 @@ def rna2sphinx(BASEPATH): if "_OT_" in struct.identifier: continue write_struct(struct) + + # special case, bpy_struct + if _BPY_STRUCT_FAKE: + filepath = os.path.join(BASEPATH, "bpy.types.%s.rst" % _BPY_STRUCT_FAKE) + file = open(filepath, "w") + fw = file.write + + fw("%s\n" % _BPY_STRUCT_FAKE) + fw("=" * len(_BPY_STRUCT_FAKE) + "\n") + fw("\n") + fw(".. module:: bpy.types\n") + fw("\n") + + subclass_ids = [s.identifier for s in structs.values() if s.base is None if not rna_info.rna_id_ignore(s.identifier)] + if subclass_ids: + fw("subclasses --- \n" + ", ".join([(":class:`%s`" % s) for s in sorted(subclass_ids)]) + "\n\n") + + fw(".. class:: %s\n" % _BPY_STRUCT_FAKE) + fw("\n") + fw(" built-in base class for all classes in bpy.types, note that bpy.types.%s is not actually available from within blender, it only exists for the purpose of documentation.\n" % _BPY_STRUCT_FAKE) + fw("\n") + + descr_items = [(key, descr) for key, descr in sorted(bpy.types.Struct.__bases__[0].__dict__.items()) if not key.startswith("__")] + + for key, descr in descr_items: + if type(descr) == MethodDescriptorType: # GetSetDescriptorType, GetSetDescriptorType's are not documented yet + py_descr2sphinx(" ", fw, descr, "bpy.types", _BPY_STRUCT_FAKE, key) + + for key, descr in descr_items: + if type(descr) == GetSetDescriptorType: + py_descr2sphinx(" ", fw, descr, "bpy.types", _BPY_STRUCT_FAKE, key) + # oeprators def write_ops(): @@ -521,7 +649,7 @@ def rna2sphinx(BASEPATH): fw(".. module:: bpy.ops.%s\n\n" % op.module_name) last_mod = op.module_name - + args_str = ", ".join([prop.get_arg_default(force=True) for prop in op.args]) fw(".. function:: %s(%s)\n\n" % (op.func_name, args_str)) if op.description: diff --git a/source/blender/python/generic/blf.c b/source/blender/python/generic/blf_api.c index eda13db57b5..f1cbd0f7e7e 100644 --- a/source/blender/python/generic/blf.c +++ b/source/blender/python/generic/blf_api.c @@ -23,7 +23,7 @@ */ #include <Python.h> -#include "blf.h" +#include "blf_api.h" #include "../../blenfont/BLF_api.h" diff --git a/source/blender/python/generic/blf.h b/source/blender/python/generic/blf_api.h index fae20ace996..fae20ace996 100644 --- a/source/blender/python/generic/blf.h +++ b/source/blender/python/generic/blf_api.h diff --git a/source/blender/python/generic/Geometry.c b/source/blender/python/generic/geometry.c index 158b07b1be5..18907bb3012 100644 --- a/source/blender/python/generic/Geometry.c +++ b/source/blender/python/generic/geometry.c @@ -27,7 +27,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "Geometry.h" +#include "geometry.h" /* Used for PolyFill */ #include "BKE_displist.h" @@ -44,7 +44,7 @@ /*-------------------------DOC STRINGS ---------------------------*/ -static char M_Geometry_doc[] = "The Blender Geometry module\n\n"; +static char M_Geometry_doc[] = "The Blender geometry module\n\n"; static char M_Geometry_Intersect_doc[] = "(v1, v2, v3, ray, orig, clip=1) - returns the intersection between a ray and a triangle, if possible, returns None otherwise"; static char M_Geometry_TriangleArea_doc[] = "(v1, v2, v3) - returns the area size of the 2D or 3D triangle defined"; static char M_Geometry_TriangleNormal_doc[] = "(v1, v2, v3) - returns the normal of the 3D triangle defined"; @@ -59,7 +59,7 @@ static char M_Geometry_BoxPack2D_doc[] = ""; static char M_Geometry_BezierInterp_doc[] = ""; //---------------------------------INTERSECTION FUNCTIONS-------------------- -//----------------------------------Mathutils.Intersect() ------------------- +//----------------------------------geometry.Intersect() ------------------- static PyObject *M_Geometry_Intersect( PyObject * self, PyObject * args ) { VectorObject *ray, *ray_off, *vec1, *vec2, *vec3; @@ -131,7 +131,7 @@ static PyObject *M_Geometry_Intersect( PyObject * self, PyObject * args ) return newVectorObject(pvec, 3, Py_NEW, NULL); } -//----------------------------------Mathutils.LineIntersect() ------------------- +//----------------------------------geometry.LineIntersect() ------------------- /* Line-Line intersection using algorithm from mathworld.wolfram.com */ static PyObject *M_Geometry_LineIntersect( PyObject * self, PyObject * args ) { @@ -200,7 +200,7 @@ static PyObject *M_Geometry_LineIntersect( PyObject * self, PyObject * args ) //---------------------------------NORMALS FUNCTIONS-------------------- -//----------------------------------Mathutils.QuadNormal() ------------------- +//----------------------------------geometry.QuadNormal() ------------------- static PyObject *M_Geometry_QuadNormal( PyObject * self, PyObject * args ) { VectorObject *vec1; @@ -251,7 +251,7 @@ static PyObject *M_Geometry_QuadNormal( PyObject * self, PyObject * args ) return newVectorObject(n1, 3, Py_NEW, NULL); } -//----------------------------Mathutils.TriangleNormal() ------------------- +//----------------------------geometry.TriangleNormal() ------------------- static PyObject *M_Geometry_TriangleNormal( PyObject * self, PyObject * args ) { VectorObject *vec1, *vec2, *vec3; @@ -288,7 +288,7 @@ static PyObject *M_Geometry_TriangleNormal( PyObject * self, PyObject * args ) } //--------------------------------- AREA FUNCTIONS-------------------- -//----------------------------------Mathutils.TriangleArea() ------------------- +//----------------------------------geometry.TriangleArea() ------------------- static PyObject *M_Geometry_TriangleArea( PyObject * self, PyObject * args ) { VectorObject *vec1, *vec2, *vec3; @@ -333,7 +333,7 @@ static PyObject *M_Geometry_TriangleArea( PyObject * self, PyObject * args ) } } -/*----------------------------------Geometry.PolyFill() -------------------*/ +/*----------------------------------geometry.PolyFill() -------------------*/ /* PolyFill function, uses Blenders scanfill to fill multiple poly lines */ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq ) { @@ -363,7 +363,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq ) if (!PySequence_Check(polyLine)) { freedisplist(&dispbase); Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/ - PyErr_SetString( PyExc_TypeError, "One or more of the polylines is not a sequence of Mathutils.Vector's" ); + PyErr_SetString( PyExc_TypeError, "One or more of the polylines is not a sequence of mathutils.Vector's" ); return NULL; } @@ -373,7 +373,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq ) if (EXPP_check_sequence_consistency( polyLine, &vector_Type ) != 1) { freedisplist(&dispbase); Py_DECREF(polyLine); - PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" ); + PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a mathutils.Vector type" ); return NULL; } #endif @@ -414,7 +414,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq ) if(ls_error) { freedisplist(&dispbase); /* possible some dl was allocated */ - PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" ); + PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a mathutils.Vector type" ); return NULL; } else if (totpoints) { @@ -428,7 +428,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq ) tri_list= PyList_New(dl->parts); if( !tri_list ) { freedisplist(&dispbase); - PyErr_SetString( PyExc_RuntimeError, "Geometry.PolyFill failed to make a new list" ); + PyErr_SetString( PyExc_RuntimeError, "geometry.PolyFill failed to make a new list" ); return NULL; } @@ -819,7 +819,7 @@ struct PyMethodDef M_Geometry_methods[] = { static struct PyModuleDef M_Geometry_module_def = { PyModuleDef_HEAD_INIT, - "Geometry", /* m_name */ + "geometry", /* m_name */ M_Geometry_doc, /* m_doc */ 0, /* m_size */ M_Geometry_methods, /* m_methods */ diff --git a/source/blender/python/generic/Geometry.h b/source/blender/python/generic/geometry.h index 1e17ca6bf27..401efcc7888 100644 --- a/source/blender/python/generic/Geometry.h +++ b/source/blender/python/generic/geometry.h @@ -32,7 +32,7 @@ #define EXPP_Geometry_H #include <Python.h> -#include "Mathutils.h" +#include "mathutils.h" PyObject *Geometry_Init(void); diff --git a/source/blender/python/generic/Mathutils.c b/source/blender/python/generic/mathutils.c index 542bf7a6ca9..e1e1cd2ae69 100644 --- a/source/blender/python/generic/Mathutils.c +++ b/source/blender/python/generic/mathutils.c @@ -47,7 +47,7 @@ * Moved to Geometry module: Intersect, TriangleArea, TriangleNormal, QuadNormal, LineIntersect */ -#include "Mathutils.h" +#include "mathutils.h" #include "BLI_math.h" @@ -124,7 +124,7 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2) } //----------------------------------MATRIX FUNCTIONS-------------------- -//----------------------------------Mathutils.RotationMatrix() ---------- +//----------------------------------mathutils.RotationMatrix() ---------- //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. static char M_Mathutils_RotationMatrix_doc[] = ".. function:: RotationMatrix(angle, size, axis)\n" @@ -150,14 +150,14 @@ static PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args) 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; if(!PyArg_ParseTuple(args, "fi|O", &angle, &matSize, &vec)) { - PyErr_SetString(PyExc_TypeError, "Mathutils.RotationMatrix(angle, size, axis): expected float int and a string or vector\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.RotationMatrix(angle, size, axis): expected float int and a string or vector\n"); return NULL; } if(vec && !VectorObject_Check(vec)) { axis= _PyUnicode_AsString((PyObject *)vec); if(axis==NULL || axis[0]=='\0' || axis[1]!='\0' || axis[0] < 'X' || axis[0] > 'Z') { - PyErr_SetString(PyExc_TypeError, "Mathutils.RotationMatrix(): 3rd argument axis value must be a 3D vector or a string in 'X', 'Y', 'Z'\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.RotationMatrix(): 3rd argument axis value must be a 3D vector or a string in 'X', 'Y', 'Z'\n"); return NULL; } else { @@ -172,20 +172,20 @@ static PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args) angle-=(Py_PI*2); if(matSize != 2 && matSize != 3 && matSize != 4) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.RotationMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); return NULL; } if(matSize == 2 && (vec != NULL)) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): cannot create a 2x2 rotation matrix around arbitrary axis\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.RotationMatrix(): cannot create a 2x2 rotation matrix around arbitrary axis\n"); return NULL; } if((matSize == 3 || matSize == 4) && (axis == NULL) && (vec == NULL)) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): please choose an axis of rotation for 3d and 4d matrices\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.RotationMatrix(): please choose an axis of rotation for 3d and 4d matrices\n"); return NULL; } if(vec) { if(vec->size != 3) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): the vector axis must be a 3D vector\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.RotationMatrix(): the vector axis must be a 3D vector\n"); return NULL; } @@ -228,7 +228,7 @@ static PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args) } else { /* should never get here */ - PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): unknown error\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.RotationMatrix(): unknown error\n"); return NULL; } @@ -263,11 +263,11 @@ static PyObject *M_Mathutils_TranslationMatrix(PyObject * self, VectorObject * v 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; if(!VectorObject_Check(vec)) { - PyErr_SetString(PyExc_TypeError, "Mathutils.TranslationMatrix(): expected vector\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.TranslationMatrix(): expected vector\n"); return NULL; } if(vec->size != 3 && vec->size != 4) { - PyErr_SetString(PyExc_TypeError, "Mathutils.TranslationMatrix(): vector must be 3D or 4D\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.TranslationMatrix(): vector must be 3D or 4D\n"); return NULL; } @@ -282,7 +282,7 @@ static PyObject *M_Mathutils_TranslationMatrix(PyObject * self, VectorObject * v return newMatrixObject(mat, 4, 4, Py_NEW, NULL); } -//----------------------------------Mathutils.ScaleMatrix() ------------- +//----------------------------------mathutils.ScaleMatrix() ------------- //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. static char M_Mathutils_ScaleMatrix_doc[] = ".. function:: ScaleMatrix(factor, size, axis)\n" @@ -307,16 +307,16 @@ static PyObject *M_Mathutils_ScaleMatrix(PyObject * self, PyObject * args) 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; if(!PyArg_ParseTuple(args, "fi|O!", &factor, &matSize, &vector_Type, &vec)) { - PyErr_SetString(PyExc_TypeError, "Mathutils.ScaleMatrix(): expected float int and optional vector\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.ScaleMatrix(): expected float int and optional vector\n"); return NULL; } if(matSize != 2 && matSize != 3 && matSize != 4) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.ScaleMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.ScaleMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); return NULL; } if(vec) { if(vec->size > 2 && matSize == 2) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.ScaleMatrix(): please use 2D vectors when scaling in 2D\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.ScaleMatrix(): please use 2D vectors when scaling in 2D\n"); return NULL; } @@ -373,7 +373,7 @@ static PyObject *M_Mathutils_ScaleMatrix(PyObject * self, PyObject * args) //pass to matrix creation return newMatrixObject(mat, matSize, matSize, Py_NEW, NULL); } -//----------------------------------Mathutils.OrthoProjectionMatrix() --- +//----------------------------------mathutils.OrthoProjectionMatrix() --- //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. static char M_Mathutils_OrthoProjectionMatrix_doc[] = ".. function:: OrthoProjectionMatrix(plane, size, axis)\n" @@ -398,16 +398,16 @@ static PyObject *M_Mathutils_OrthoProjectionMatrix(PyObject * self, PyObject * a 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; if(!PyArg_ParseTuple(args, "si|O!", &plane, &matSize, &vector_Type, &vec)) { - PyErr_SetString(PyExc_TypeError, "Mathutils.OrthoProjectionMatrix(): expected string and int and optional vector\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.OrthoProjectionMatrix(): expected string and int and optional vector\n"); return NULL; } if(matSize != 2 && matSize != 3 && matSize != 4) { - PyErr_SetString(PyExc_AttributeError,"Mathutils.OrthoProjectionMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); + PyErr_SetString(PyExc_AttributeError,"mathutils.OrthoProjectionMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); return NULL; } if(vec) { if(vec->size > 2 && matSize == 2) { - PyErr_SetString(PyExc_AttributeError, "Mathutils.OrthoProjectionMatrix(): please use 2D vectors when scaling in 2D\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.OrthoProjectionMatrix(): please use 2D vectors when scaling in 2D\n"); return NULL; } @@ -430,7 +430,7 @@ static PyObject *M_Mathutils_OrthoProjectionMatrix(PyObject * self, PyObject * a mat[4] = 1.0f; mat[8] = 1.0f; } else { - PyErr_SetString(PyExc_AttributeError, "Mathutils.OrthoProjectionMatrix(): unknown plane - expected: X, Y, XY, XZ, YZ\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.OrthoProjectionMatrix(): unknown plane - expected: X, Y, XY, XZ, YZ\n"); return NULL; } } else { //arbitrary plane @@ -458,7 +458,7 @@ static PyObject *M_Mathutils_OrthoProjectionMatrix(PyObject * self, PyObject * a mat[7] = -(vec->vec[1] * vec->vec[2]); mat[8] = 1 - (vec->vec[2] * vec->vec[2]); } else { - PyErr_SetString(PyExc_AttributeError, "Mathutils.OrthoProjectionMatrix(): unknown plane - expected: 'r' expected for axis designation\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.OrthoProjectionMatrix(): unknown plane - expected: 'r' expected for axis designation\n"); return NULL; } } @@ -500,11 +500,11 @@ static PyObject *M_Mathutils_ShearMatrix(PyObject * self, PyObject * args) 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; if(!PyArg_ParseTuple(args, "sfi", &plane, &factor, &matSize)) { - PyErr_SetString(PyExc_TypeError,"Mathutils.ShearMatrix(): expected string float and int\n"); + PyErr_SetString(PyExc_TypeError,"mathutils.ShearMatrix(): expected string float and int\n"); return NULL; } if(matSize != 2 && matSize != 3 && matSize != 4) { - PyErr_SetString(PyExc_AttributeError,"Mathutils.ShearMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); + PyErr_SetString(PyExc_AttributeError,"mathutils.ShearMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n"); return NULL; } @@ -535,7 +535,7 @@ static PyObject *M_Mathutils_ShearMatrix(PyObject * self, PyObject * args) mat[4] = 1.0f; mat[8] = 1.0f; } else { - PyErr_SetString(PyExc_AttributeError, "Mathutils.ShearMatrix(): expected: x, y, xy, xz, yz or wrong matrix size for shearing plane\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.ShearMatrix(): expected: x, y, xy, xz, yz or wrong matrix size for shearing plane\n"); return NULL; } if(matSize == 4) { @@ -683,7 +683,7 @@ struct PyMethodDef M_Mathutils_methods[] = { static struct PyModuleDef M_Mathutils_module_def = { PyModuleDef_HEAD_INIT, - "Mathutils", /* m_name */ + "mathutils", /* m_name */ M_Mathutils_doc, /* m_doc */ 0, /* m_size */ M_Mathutils_methods, /* m_methods */ @@ -705,7 +705,9 @@ PyObject *Mathutils_Init(void) return NULL; if( PyType_Ready( &quaternion_Type ) < 0 ) return NULL; - + if( PyType_Ready( &color_Type ) < 0 ) + return NULL; + submodule = PyModule_Create(&M_Mathutils_module_def); PyDict_SetItemString(PySys_GetObject("modules"), M_Mathutils_module_def.m_name, submodule); @@ -714,6 +716,7 @@ PyObject *Mathutils_Init(void) PyModule_AddObject( submodule, "Matrix", (PyObject *)&matrix_Type ); PyModule_AddObject( submodule, "Euler", (PyObject *)&euler_Type ); PyModule_AddObject( submodule, "Quaternion", (PyObject *)&quaternion_Type ); + PyModule_AddObject( submodule, "Color", (PyObject *)&color_Type ); mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb); diff --git a/source/blender/python/generic/Mathutils.h b/source/blender/python/generic/mathutils.h index 869ac4d70df..f5bbcfcf666 100644 --- a/source/blender/python/generic/Mathutils.h +++ b/source/blender/python/generic/mathutils.h @@ -33,25 +33,29 @@ #include <Python.h> -#include "vector.h" -#include "matrix.h" -#include "quat.h" -#include "euler.h" - /* Can cast different mathutils types to this, use for generic funcs */ extern char BaseMathObject_Wrapped_doc[]; extern char BaseMathObject_Owner_doc[]; +#define BASE_MATH_MEMBERS(_data) \ + PyObject_VAR_HEAD \ + float *_data; /* array of data (alias), wrapped status depends on wrapped status */ \ + PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ \ + unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ \ + unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ \ + unsigned char wrapped; /* wrapped data type? */ \ + typedef struct { - PyObject_VAR_HEAD - float *data; /*array of data (alias), wrapped status depends on wrapped status */ - PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ - unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ - unsigned char wrapped; /* wrapped data type? */ + BASE_MATH_MEMBERS(data) } BaseMathObject; +#include "mathutils_vector.h" +#include "mathutils_matrix.h" +#include "mathutils_quat.h" +#include "mathutils_euler.h" +#include "mathutils_color.h" + PyObject *BaseMathObject_getOwner( BaseMathObject * self, void * ); PyObject *BaseMathObject_getWrapped( BaseMathObject *self, void * ); void BaseMathObject_dealloc(BaseMathObject * self); diff --git a/source/blender/python/generic/mathutils_color.c b/source/blender/python/generic/mathutils_color.c new file mode 100644 index 00000000000..d44515d1551 --- /dev/null +++ b/source/blender/python/generic/mathutils_color.c @@ -0,0 +1,467 @@ +/* + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "mathutils.h" + +#include "BLI_math.h" +#include "BKE_utildefines.h" + +//----------------------------------mathutils.Color() ------------------- +//makes a new color for you to play with +static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwargs) +{ + PyObject *listObject = NULL; + int size, i; + float col[3]; + PyObject *e; + + + size = PyTuple_GET_SIZE(args); + if (size == 1) { + listObject = PyTuple_GET_ITEM(args, 0); + if (PySequence_Check(listObject)) { + size = PySequence_Length(listObject); + } else { // Single argument was not a sequence + PyErr_SetString(PyExc_TypeError, "mathutils.Color(): 3d numeric sequence expected\n"); + return NULL; + } + } else if (size == 0) { + //returns a new empty 3d color + return newColorObject(NULL, Py_NEW, NULL); + } else { + listObject = args; + } + + if (size != 3) { // Invalid color size + PyErr_SetString(PyExc_AttributeError, "mathutils.Color(): 3d numeric sequence expected\n"); + return NULL; + } + + for (i=0; i<size; i++) { + e = PySequence_GetItem(listObject, i); + if (e == NULL) { // Failed to read sequence + Py_DECREF(listObject); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Color(): 3d numeric sequence expected\n"); + return NULL; + } + + col[i]= (float)PyFloat_AsDouble(e); + Py_DECREF(e); + + if(col[i]==-1 && PyErr_Occurred()) { // parsed item is not a number + PyErr_SetString(PyExc_TypeError, "mathutils.Color(): 3d numeric sequence expected\n"); + return NULL; + } + } + return newColorObject(col, Py_NEW, NULL); +} + +//-----------------------------METHODS---------------------------- + +//----------------------------Color.rotate()----------------------- +// return a copy of the color + +static char Color_copy_doc[] = +".. function:: copy()\n" +"\n" +" Returns a copy of this color.\n" +"\n" +" :return: A copy of the color.\n" +" :rtype: :class:`Color`\n" +"\n" +" .. note:: use this to get a copy of a wrapped color with no reference to the original data.\n"; + +static PyObject *Color_copy(ColorObject * self, PyObject *args) +{ + if(!BaseMath_ReadCallback(self)) + return NULL; + + return newColorObject(self->col, Py_NEW, Py_TYPE(self)); +} + +//----------------------------print object (internal)-------------- +//print the object to screen +static PyObject *Color_repr(ColorObject * self) +{ + char str[64]; + + if(!BaseMath_ReadCallback(self)) + return NULL; + + sprintf(str, "[%.6f, %.6f, %.6f](color)", self->col[0], self->col[1], self->col[2]); + return PyUnicode_FromString(str); +} +//------------------------tp_richcmpr +//returns -1 execption, 0 false, 1 true +static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +{ + ColorObject *colA = NULL, *colB = NULL; + int result = 0; + + if(ColorObject_Check(objectA)) { + colA = (ColorObject*)objectA; + if(!BaseMath_ReadCallback(colA)) + return NULL; + } + if(ColorObject_Check(objectB)) { + colB = (ColorObject*)objectB; + if(!BaseMath_ReadCallback(colB)) + return NULL; + } + + if (!colA || !colB){ + if (comparison_type == Py_NE){ + Py_RETURN_TRUE; + }else{ + Py_RETURN_FALSE; + } + } + colA = (ColorObject*)objectA; + colB = (ColorObject*)objectB; + + switch (comparison_type){ + case Py_EQ: + result = EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1); + break; + case Py_NE: + result = !EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1); + break; + default: + printf("The result of the comparison could not be evaluated"); + break; + } + if (result == 1){ + Py_RETURN_TRUE; + }else{ + Py_RETURN_FALSE; + } +} + +//---------------------SEQUENCE PROTOCOLS------------------------ +//----------------------------len(object)------------------------ +//sequence length +static int Color_len(ColorObject * self) +{ + return 3; +} +//----------------------------object[]--------------------------- +//sequence accessor (get) +static PyObject *Color_item(ColorObject * self, int i) +{ + if(i<0) i= 3-i; + + if(i < 0 || i >= 3) { + PyErr_SetString(PyExc_IndexError, "color[attribute]: array index out of range"); + return NULL; + } + + if(!BaseMath_ReadIndexCallback(self, i)) + return NULL; + + return PyFloat_FromDouble(self->col[i]); + +} +//----------------------------object[]------------------------- +//sequence accessor (set) +static int Color_ass_item(ColorObject * self, int i, PyObject * value) +{ + float f = PyFloat_AsDouble(value); + + if(f == -1 && PyErr_Occurred()) { // parsed item not a number + PyErr_SetString(PyExc_TypeError, "color[attribute] = x: argument not a number"); + return -1; + } + + if(i<0) i= 3-i; + + if(i < 0 || i >= 3){ + PyErr_SetString(PyExc_IndexError, "color[attribute] = x: array assignment index out of range\n"); + return -1; + } + + self->col[i] = f; + + if(!BaseMath_WriteIndexCallback(self, i)) + return -1; + + return 0; +} +//----------------------------object[z:y]------------------------ +//sequence slice (get) +static PyObject *Color_slice(ColorObject * self, int begin, int end) +{ + PyObject *list = NULL; + int count; + + if(!BaseMath_ReadCallback(self)) + return NULL; + + CLAMP(begin, 0, 3); + if (end<0) end= 4+end; + CLAMP(end, 0, 3); + begin = MIN2(begin,end); + + list = PyList_New(end - begin); + for(count = begin; count < end; count++) { + PyList_SetItem(list, count - begin, + PyFloat_FromDouble(self->col[count])); + } + + return list; +} +//----------------------------object[z:y]------------------------ +//sequence slice (set) +static int Color_ass_slice(ColorObject * self, int begin, int end, + PyObject * seq) +{ + int i, y, size = 0; + float col[3]; + PyObject *e; + + if(!BaseMath_ReadCallback(self)) + return -1; + + CLAMP(begin, 0, 3); + if (end<0) end= 4+end; + CLAMP(end, 0, 3); + begin = MIN2(begin,end); + + size = PySequence_Length(seq); + if(size != (end - begin)){ + PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: size mismatch in slice assignment"); + return -1; + } + + for (i = 0; i < size; i++) { + e = PySequence_GetItem(seq, i); + if (e == NULL) { // Failed to read sequence + PyErr_SetString(PyExc_RuntimeError, "color[begin:end] = []: unable to read sequence"); + return -1; + } + + col[i] = (float)PyFloat_AsDouble(e); + Py_DECREF(e); + + if(col[i]==-1 && PyErr_Occurred()) { // parsed item not a number + PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: sequence argument not a number"); + return -1; + } + } + //parsed well - now set in vector + for(y = 0; y < 3; y++){ + self->col[begin + y] = col[y]; + } + + BaseMath_WriteCallback(self); + return 0; +} +//-----------------PROTCOL DECLARATIONS-------------------------- +static PySequenceMethods Color_SeqMethods = { + (lenfunc) Color_len, /* sq_length */ + (binaryfunc) 0, /* sq_concat */ + (ssizeargfunc) 0, /* sq_repeat */ + (ssizeargfunc) Color_item, /* sq_item */ + (ssizessizeargfunc) Color_slice, /* sq_slice */ + (ssizeobjargproc) Color_ass_item, /* sq_ass_item */ + (ssizessizeobjargproc) Color_ass_slice, /* sq_ass_slice */ +}; + + +/* color channel, vector.r/g/b */ +static PyObject *Color_getChannel( ColorObject * self, void *type ) +{ + return Color_item(self, GET_INT_FROM_POINTER(type)); +} + +static int Color_setChannel(ColorObject * self, PyObject * value, void * type) +{ + return Color_ass_item(self, GET_INT_FROM_POINTER(type), value); +} + +/* color channel (HSV), color.h/s/v */ +static PyObject *Color_getChannelHSV( ColorObject * self, void *type ) +{ + float hsv[3]; + int i= GET_INT_FROM_POINTER(type); + + if(!BaseMath_ReadCallback(self)) + return NULL; + + rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2])); + + return PyFloat_FromDouble(hsv[i]); +} + +static int Color_setChannelHSV(ColorObject * self, PyObject * value, void * type) +{ + float hsv[3]; + int i= GET_INT_FROM_POINTER(type); + float f = PyFloat_AsDouble(value); + + if(f == -1 && PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "color.h/s/v = value: argument not a number"); + return -1; + } + + if(!BaseMath_ReadCallback(self)) + return -1; + + rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2])); + CLAMP(f, 0.0f, 1.0f); + hsv[i] = f; + hsv_to_rgb(hsv[0], hsv[1], hsv[2], &(self->col[0]), &(self->col[1]), &(self->col[2])); + + if(!BaseMath_WriteCallback(self)) + return -1; + + return 0; +} + +/*****************************************************************************/ +/* Python attributes get/set structure: */ +/*****************************************************************************/ +static PyGetSetDef Color_getseters[] = { + {"r", (getter)Color_getChannel, (setter)Color_setChannel, "Red color channel. **type** float", (void *)0}, + {"g", (getter)Color_getChannel, (setter)Color_setChannel, "Green color channel. **type** float", (void *)1}, + {"b", (getter)Color_getChannel, (setter)Color_setChannel, "Blue color channel. **type** float", (void *)2}, + + {"h", (getter)Color_getChannelHSV, (setter)Color_setChannelHSV, "HSV Hue component in [0, 1]. **type** float", (void *)0}, + {"s", (getter)Color_getChannelHSV, (setter)Color_setChannelHSV, "HSV Saturation component in [0, 1]. **type** float", (void *)1}, + {"v", (getter)Color_getChannelHSV, (setter)Color_setChannelHSV, "HSV Value component in [0, 1]. **type** float", (void *)2}, + + {"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL}, + {"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL}, + {NULL,NULL,NULL,NULL,NULL} /* Sentinel */ +}; + + +//-----------------------METHOD DEFINITIONS ---------------------- +static struct PyMethodDef Color_methods[] = { + {"__copy__", (PyCFunction) Color_copy, METH_VARARGS, Color_copy_doc}, + {"copy", (PyCFunction) Color_copy, METH_VARARGS, Color_copy_doc}, + {NULL, NULL, 0, NULL} +}; + +//------------------PY_OBECT DEFINITION-------------------------- +static char color_doc[] = +"This object gives access to Colors in Blender."; + +PyTypeObject color_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "color", //tp_name + sizeof(ColorObject), //tp_basicsize + 0, //tp_itemsize + (destructor)BaseMathObject_dealloc, //tp_dealloc + 0, //tp_print + 0, //tp_getattr + 0, //tp_setattr + 0, //tp_compare + (reprfunc) Color_repr, //tp_repr + 0, //tp_as_number + &Color_SeqMethods, //tp_as_sequence + 0, //tp_as_mapping + 0, //tp_hash + 0, //tp_call + 0, //tp_str + 0, //tp_getattro + 0, //tp_setattro + 0, //tp_as_buffer + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags + color_doc, //tp_doc + 0, //tp_traverse + 0, //tp_clear + (richcmpfunc)Color_richcmpr, //tp_richcompare + 0, //tp_weaklistoffset + 0, //tp_iter + 0, //tp_iternext + Color_methods, //tp_methods + 0, //tp_members + Color_getseters, //tp_getset + 0, //tp_base + 0, //tp_dict + 0, //tp_descr_get + 0, //tp_descr_set + 0, //tp_dictoffset + 0, //tp_init + 0, //tp_alloc + Color_new, //tp_new + 0, //tp_free + 0, //tp_is_gc + 0, //tp_bases + 0, //tp_mro + 0, //tp_cache + 0, //tp_subclasses + 0, //tp_weaklist + 0 //tp_del +}; +//------------------------newColorObject (internal)------------- +//creates a new color object +/*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER + (i.e. it was allocated elsewhere by MEM_mallocN()) + pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON + (i.e. it must be created here with PyMEM_malloc())*/ +PyObject *newColorObject(float *col, int type, PyTypeObject *base_type) +{ + ColorObject *self; + int x; + + if(base_type) self = (ColorObject *)base_type->tp_alloc(base_type, 0); + else self = PyObject_NEW(ColorObject, &color_Type); + + /* init callbacks as NULL */ + self->cb_user= NULL; + self->cb_type= self->cb_subtype= 0; + + if(type == Py_WRAP){ + self->col = col; + self->wrapped = Py_WRAP; + }else if (type == Py_NEW){ + self->col = PyMem_Malloc(3 * sizeof(float)); + if(!col) { //new empty + for(x = 0; x < 3; x++) { + self->col[x] = 0.0f; + } + }else{ + VECCOPY(self->col, col); + } + self->wrapped = Py_NEW; + }else{ //bad type + return NULL; + } + + return (PyObject *)self; +} + +PyObject *newColorObject_cb(PyObject *cb_user, int cb_type, int cb_subtype) +{ + ColorObject *self= (ColorObject *)newColorObject(NULL, Py_NEW, NULL); + if(self) { + Py_INCREF(cb_user); + self->cb_user= cb_user; + self->cb_type= (unsigned char)cb_type; + self->cb_subtype= (unsigned char)cb_subtype; + } + + return (PyObject *)self; +} diff --git a/source/blender/python/generic/mathutils_color.h b/source/blender/python/generic/mathutils_color.h new file mode 100644 index 00000000000..02b27d86817 --- /dev/null +++ b/source/blender/python/generic/mathutils_color.h @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Joseph Gilbert + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#ifndef EXPP_color_h +#define EXPP_color_h + +#include <Python.h> + +extern PyTypeObject color_Type; +#define ColorObject_Check(_v) PyObject_TypeCheck((_v), &color_Type) + +typedef struct { + BASE_MATH_MEMBERS(col) +} ColorObject; + +/*struct data contains a pointer to the actual data that the +object uses. It can use either PyMem allocated data (which will +be stored in py_data) or be a wrapper for data allocated through +blender (stored in blend_data). This is an either/or struct not both*/ + +//prototypes +PyObject *newColorObject( float *col, int type, PyTypeObject *base_type); +PyObject *newColorObject_cb(PyObject *cb_user, int cb_type, int cb_subtype); + +#endif /* EXPP_color_h */ diff --git a/source/blender/python/generic/euler.c b/source/blender/python/generic/mathutils_euler.c index 892e42657b7..04f80dd4116 100644 --- a/source/blender/python/generic/euler.c +++ b/source/blender/python/generic/mathutils_euler.c @@ -26,7 +26,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "Mathutils.h" +#include "mathutils.h" #include "BLI_math.h" #include "BKE_utildefines.h" @@ -35,7 +35,7 @@ #include "BLO_sys_types.h" #endif -//----------------------------------Mathutils.Euler() ------------------- +//----------------------------------mathutils.Euler() ------------------- //makes a new euler for you to play with static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs) { @@ -51,7 +51,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar if (PySequence_Check(listObject)) { size = PySequence_Length(listObject); } else { // Single argument was not a sequence - PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Euler(): 3d numeric sequence expected\n"); return NULL; } } else if (size == 0) { @@ -62,7 +62,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar } if (size != 3) { // Invalid euler size - PyErr_SetString(PyExc_AttributeError, "Mathutils.Euler(): 3d numeric sequence expected\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Euler(): 3d numeric sequence expected\n"); return NULL; } @@ -70,7 +70,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar e = PySequence_GetItem(listObject, i); if (e == NULL) { // Failed to read sequence Py_DECREF(listObject); - PyErr_SetString(PyExc_RuntimeError, "Mathutils.Euler(): 3d numeric sequence expected\n"); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Euler(): 3d numeric sequence expected\n"); return NULL; } @@ -78,7 +78,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar Py_DECREF(e); if(eul[i]==-1 && PyErr_Occurred()) { // parsed item is not a number - PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Euler(): 3d numeric sequence expected\n"); return NULL; } } @@ -499,7 +499,7 @@ static PySequenceMethods Euler_SeqMethods = { /* - * vector axis, vector.x/y/z/w + * euler axis, euler.x/y/z */ static PyObject *Euler_getAxis( EulerObject * self, void *type ) { diff --git a/source/blender/python/generic/euler.h b/source/blender/python/generic/mathutils_euler.h index 994a5f1780e..b8523c3b661 100644 --- a/source/blender/python/generic/euler.h +++ b/source/blender/python/generic/mathutils_euler.h @@ -37,14 +37,7 @@ extern PyTypeObject euler_Type; #define EulerObject_Check(_v) PyObject_TypeCheck((_v), &euler_Type) typedef struct { - PyObject_VAR_HEAD - float *eul; /*1D array of data */ - PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ - unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ - unsigned char wrapped; /* wrapped data type? */ - /* end BaseMathObject */ - + BASE_MATH_MEMBERS(eul) unsigned char order; /* rotation order */ } EulerObject; diff --git a/source/blender/python/generic/matrix.c b/source/blender/python/generic/mathutils_matrix.c index 216139dc44f..0ce5641b07c 100644 --- a/source/blender/python/generic/matrix.c +++ b/source/blender/python/generic/mathutils_matrix.c @@ -25,7 +25,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "Mathutils.h" +#include "mathutils.h" #include "BKE_utildefines.h" #include "BLI_math.h" @@ -105,7 +105,7 @@ Mathutils_Callback mathutils_matrix_vector_cb = { }; /* matrix vector callbacks, this is so you can do matrix[i][j] = val */ -//----------------------------------Mathutils.Matrix() ----------------- +//----------------------------------mathutils.Matrix() ----------------- //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. //create a new matrix type static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) @@ -118,8 +118,8 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) float scalar; argSize = PyTuple_GET_SIZE(args); - if(argSize > 4){ //bad arg nums - PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); + if(argSize > MATRIX_MAX_DIM) { //bad arg nums + PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); return NULL; } else if (argSize == 0) { //return empty 4D matrix return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW, NULL); @@ -141,13 +141,13 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (PySequence_Check(argObject)) { //seq? if(seqSize){ //0 at first if(PySequence_Length(argObject) != seqSize){ //seq size not same - PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); return NULL; } } seqSize = PySequence_Length(argObject); }else{ //arg not a sequence - PyErr_SetString(PyExc_TypeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); return NULL; } } @@ -155,14 +155,14 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) for (i = 0; i < argSize; i++){ m = PyTuple_GET_ITEM(args, i); if (m == NULL) { // Failed to read sequence - PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n"); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Matrix(): failed to parse arguments...\n"); return NULL; } for (j = 0; j < seqSize; j++) { s = PySequence_GetItem(m, j); if (s == NULL) { // Failed to read sequence - PyErr_SetString(PyExc_RuntimeError, "Mathutils.Matrix(): failed to parse arguments...\n"); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Matrix(): failed to parse arguments...\n"); return NULL; } @@ -170,7 +170,7 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(s); if(scalar==-1 && PyErr_Occurred()) { // parsed item is not a number - PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); return NULL; } @@ -321,11 +321,6 @@ PyObject *Matrix_Resize4x4(MatrixObject * self) PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space"); return NULL; } - self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4)); - if(self->matrix == NULL) { - PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space"); - return NULL; - } /*set row pointers*/ for(x = 0; x < 4; x++) { self->matrix[x] = self->contigPtr + (x * 4); @@ -1425,12 +1420,6 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type, PyType if(type == Py_WRAP){ self->contigPtr = mat; - /*create pointer array*/ - self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); - if(self->matrix == NULL) { /*allocation failure*/ - PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space"); - return NULL; - } /*pointer array points to contigous memory*/ for(x = 0; x < rowSize; x++) { self->matrix[x] = self->contigPtr + (x * colSize); @@ -1442,13 +1431,6 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type, PyType PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n"); return NULL; } - /*create pointer array*/ - self->matrix = PyMem_Malloc(rowSize * sizeof(float *)); - if(self->matrix == NULL) { /*allocation failure*/ - PyMem_Free(self->contigPtr); - PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space"); - return NULL; - } /*pointer array points to contigous memory*/ for(x = 0; x < rowSize; x++) { self->matrix[x] = self->contigPtr + (x * colSize); diff --git a/source/blender/python/generic/matrix.h b/source/blender/python/generic/mathutils_matrix.h index b18a3e8e6fe..21538f8168e 100644 --- a/source/blender/python/generic/matrix.h +++ b/source/blender/python/generic/mathutils_matrix.h @@ -34,21 +34,14 @@ extern PyTypeObject matrix_Type; #define MatrixObject_Check(_v) PyObject_TypeCheck((_v), &matrix_Type) +#define MATRIX_MAX_DIM 4 -typedef float **ptRow; -typedef struct _Matrix { /* keep aligned with BaseMathObject in Mathutils.h */ - PyObject_VAR_HEAD - float *contigPtr; /*1D array of data (alias)*/ - PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ - unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ - unsigned char wrapped; /*is wrapped data?*/ - /* end BaseMathObject */ +typedef struct { + BASE_MATH_MEMBERS(contigPtr) unsigned char rowSize; unsigned int colSize; - ptRow matrix; /*ptr to the contigPtr (accessor)*/ - + float *matrix[MATRIX_MAX_DIM]; /* ptr to the contigPtr (accessor) */ } MatrixObject; /*struct data contains a pointer to the actual data that the diff --git a/source/blender/python/generic/quat.c b/source/blender/python/generic/mathutils_quat.c index 36d01e7aa9f..38fb2ae4903 100644 --- a/source/blender/python/generic/quat.c +++ b/source/blender/python/generic/mathutils_quat.c @@ -26,7 +26,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "Mathutils.h" +#include "mathutils.h" #include "BLI_math.h" #include "BKE_utildefines.h" @@ -712,7 +712,7 @@ static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type ) return (PyObject *) newVectorObject(vec, 3, Py_NEW, NULL); } -//----------------------------------Mathutils.Quaternion() -------------- +//----------------------------------mathutils.Quaternion() -------------- static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *listObject = NULL, *n, *q; @@ -728,13 +728,13 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw if ((size == 4 && PySequence_Length(args) !=1) || (size == 3 && PySequence_Length(args) !=2) || (size >4 || size < 3)) { // invalid args/size - PyErr_SetString(PyExc_AttributeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } if(size == 3){ //get angle in axis/angle n = PySequence_GetItem(args, 1); if(n == NULL) { // parsed item not a number or getItem fail - PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } @@ -742,7 +742,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw Py_DECREF(n); if (angle==-1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } } @@ -752,17 +752,17 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw size = PySequence_Length(listObject); if (size != 3) { // invalid args/size - PyErr_SetString(PyExc_AttributeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } angle = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0)); if (angle==-1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } } else { // argument was not a sequence - PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } } @@ -774,12 +774,12 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw if (size == 3) { // invalid quat size if(PySequence_Length(args) != 2){ - PyErr_SetString(PyExc_AttributeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } }else{ if(size != 4){ - PyErr_SetString(PyExc_AttributeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } } @@ -787,7 +787,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw for (i=0; i<size; i++) { //parse q = PySequence_GetItem(listObject, i); if (q == NULL) { // Failed to read sequence - PyErr_SetString(PyExc_RuntimeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } @@ -795,7 +795,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw Py_DECREF(q); if (quat[i]==-1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); return NULL; } } diff --git a/source/blender/python/generic/quat.h b/source/blender/python/generic/mathutils_quat.h index 6e0c9d6dd1f..c9ec12d6152 100644 --- a/source/blender/python/generic/quat.h +++ b/source/blender/python/generic/mathutils_quat.h @@ -36,15 +36,8 @@ extern PyTypeObject quaternion_Type; #define QuaternionObject_Check(_v) PyObject_TypeCheck((_v), &quaternion_Type) -typedef struct { /* keep aligned with BaseMathObject in Mathutils.h */ - PyObject_VAR_HEAD - float *quat; /* 1D array of data (alias) */ - PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ - unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ - unsigned char wrapped; /* wrapped data type? */ - /* end BaseMathObject */ - +typedef struct { + BASE_MATH_MEMBERS(quat) } QuaternionObject; /*struct data contains a pointer to the actual data that the diff --git a/source/blender/python/generic/vector.c b/source/blender/python/generic/mathutils_vector.c index fa26946a682..e013a358393 100644 --- a/source/blender/python/generic/vector.c +++ b/source/blender/python/generic/mathutils_vector.c @@ -25,7 +25,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "Mathutils.h" +#include "mathutils.h" #include "BLI_blenlib.h" #include "BKE_utildefines.h" @@ -41,7 +41,7 @@ static PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat); /* utility func */ -//----------------------------------Mathutils.Vector() ------------------ +//----------------------------------mathutils.Vector() ------------------ // Supports 2D, 3D, and 4D vector objects both int and float values // accepted. Mixed float and int values accepted. Ints are parsed to float static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds) @@ -57,7 +57,7 @@ static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (PySequence_Check(listObject)) { size = PySequence_Length(listObject); } else { // Single argument was not a sequence - PyErr_SetString(PyExc_TypeError, "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); return NULL; } } else if (size == 0) { @@ -68,21 +68,21 @@ static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } if (size<2 || size>4) { // Invalid vector size - PyErr_SetString(PyExc_AttributeError, "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); + PyErr_SetString(PyExc_AttributeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); return NULL; } for (i=0; i<size; i++) { v=PySequence_GetItem(listObject, i); if (v==NULL) { // Failed to read sequence - PyErr_SetString(PyExc_RuntimeError, "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); + PyErr_SetString(PyExc_RuntimeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); return NULL; } f= PyFloat_AsDouble(v); if(f==-1 && PyErr_Occurred()) { // parsed item not a number Py_DECREF(v); - PyErr_SetString(PyExc_TypeError, "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); + PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); return NULL; } @@ -643,7 +643,6 @@ static PyObject *Vector_Project(VectorObject * self, VectorObject * value) return newVectorObject(vec, size, Py_NEW, NULL); } -//----------------------------------Mathutils.MidpointVecs() ------------- static char Vector_Lerp_doc[] = ".. function:: lerp(other, factor)\n" "\n" diff --git a/source/blender/python/generic/vector.h b/source/blender/python/generic/mathutils_vector.h index fd95f5a6750..42b9849dd3f 100644 --- a/source/blender/python/generic/vector.h +++ b/source/blender/python/generic/mathutils_vector.h @@ -1,4 +1,5 @@ -/* $Id$ +/* + * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** * @@ -35,14 +36,8 @@ extern PyTypeObject vector_Type; #define VectorObject_Check(_v) PyObject_TypeCheck((_v), &vector_Type) -typedef struct { /* keep aligned with BaseMathObject in Mathutils.h */ - PyObject_VAR_HEAD - float *vec; /*1D array of data (alias), wrapped status depends on wrapped status */ - PyObject *cb_user; /* if this vector references another object, otherwise NULL, *Note* this owns its reference */ - unsigned char cb_type; /* which user funcs do we adhere to, RNA, GameObject, etc */ - unsigned char cb_subtype; /* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ - unsigned char wrapped; /* wrapped data type? */ - /* end BaseMathObject */ +typedef struct { + BASE_MATH_MEMBERS(vec) unsigned char size; /* vec size 2,3 or 4 */ } VectorObject; diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c index a14e8ee98c3..439607a414b 100644 --- a/source/blender/python/intern/bpy.c +++ b/source/blender/python/intern/bpy.c @@ -35,9 +35,9 @@ #include "BLI_path_util.h" /* external util modules */ -#include "../generic/Geometry.h" +#include "../generic/geometry.h" #include "../generic/bgl.h" -#include "../generic/blf.h" +#include "../generic/blf_api.h" #include "../generic/IDProp.h" #include "BPy_Freestyle.h" diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 4792d284ca7..89782978c85 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -1,4 +1,3 @@ - /** * $Id$ * @@ -51,7 +50,7 @@ #define USE_MATHUTILS #ifdef USE_MATHUTILS -#include "../generic/Mathutils.h" /* so we can have mathutils callbacks */ +#include "../generic/mathutils.h" /* so we can have mathutils callbacks */ #include "../generic/IDProp.h" /* for IDprop lookups */ @@ -67,6 +66,7 @@ static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */ #define MATHUTILS_CB_SUBTYPE_EUL 0 #define MATHUTILS_CB_SUBTYPE_VEC 1 #define MATHUTILS_CB_SUBTYPE_QUAT 2 +#define MATHUTILS_CB_SUBTYPE_COLOR 0 static int mathutils_rna_generic_check(BPy_PropertyRNA *self) { @@ -84,9 +84,19 @@ static int mathutils_rna_vector_get(BPy_PropertyRNA *self, int subtype, float *v static int mathutils_rna_vector_set(BPy_PropertyRNA *self, int subtype, float *vec_to) { + float min, max; if(self->prop==NULL) return 0; - /* TODO, clamp */ + + RNA_property_float_range(&self->ptr, self->prop, &min, &max); + + if(min != FLT_MIN || max != FLT_MAX) { + int i, len= RNA_property_array_length(&self->ptr, self->prop); + for(i=0; i<len; i++) { + CLAMP(vec_to[i], min, max); + } + } + RNA_property_float_set_array(&self->ptr, self->prop, vec_to); RNA_property_update(BPy_GetContext(), &self->ptr, self->prop); return 1; @@ -237,8 +247,8 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop) } else { PyObject *eul_cb= newEulerObject_cb(ret, 0, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA - Py_DECREF(ret); /* the matrix owns now */ - ret= eul_cb; /* return the matrix instead */ + Py_DECREF(ret); /* the euler owns now */ + ret= eul_cb; /* return the euler instead */ } } else if (len==4) { @@ -248,11 +258,23 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop) } else { PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_QUAT); - Py_DECREF(ret); /* the matrix owns now */ - ret= quat_cb; /* return the matrix instead */ + Py_DECREF(ret); /* the quat owns now */ + ret= quat_cb; /* return the quat instead */ } } break; + case PROP_COLOR: + if(len==3) { /* color */ + if(is_thick) { + ret= newColorObject(NULL, Py_NEW, NULL); // TODO, get order from RNA + RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col); + } + else { + PyObject *col_cb= newColorObject_cb(ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_COLOR); + Py_DECREF(ret); /* the color owns now */ + ret= col_cb; /* return the color instead */ + } + } default: break; } @@ -1077,6 +1099,9 @@ static int pyrna_py_to_prop_index(BPy_PropertyRNA *self, int index, PyObject *va break; } } + + /* Run rna property functions */ + RNA_property_update(BPy_GetContext(), ptr, prop); return ret; } @@ -1610,6 +1635,16 @@ static PyMappingMethods pyrna_struct_as_mapping = { ( objobjargproc ) pyrna_struct_ass_subscript, /* mp_ass_subscript */ }; +static char pyrna_struct_keys_doc[] = +".. method:: keys()\n" +"\n" +" Returns the keys of this objects custom properties (matches pythons dictionary function of the same name).\n" +"\n" +" :return: custom property keys.\n" +" :rtype: list of strings\n" +"\n" +" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n"; + static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self) { IDProperty *group; @@ -1627,6 +1662,16 @@ static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self) return BPy_Wrap_GetKeys(group); } +static char pyrna_struct_items_doc[] = +".. method:: items()\n" +"\n" +" Returns the items of this objects custom properties (matches pythons dictionary function of the same name).\n" +"\n" +" :return: custom property key, value pairs.\n" +" :rtype: list of key, value tuples\n" +"\n" +" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n"; + static PyObject *pyrna_struct_items(BPy_PropertyRNA *self) { IDProperty *group; @@ -1644,6 +1689,15 @@ static PyObject *pyrna_struct_items(BPy_PropertyRNA *self) return BPy_Wrap_GetItems(self->ptr.id.data, group); } +static char pyrna_struct_values_doc[] = +".. method:: values()\n" +"\n" +" Returns the values of this objects custom properties (matches pythons dictionary function of the same name).\n" +"\n" +" :return: custom property values.\n" +" :rtype: list\n" +"\n" +" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n"; static PyObject *pyrna_struct_values(BPy_PropertyRNA *self) { @@ -1662,36 +1716,46 @@ static PyObject *pyrna_struct_values(BPy_PropertyRNA *self) return BPy_Wrap_GetValues(self->ptr.id.data, group); } -/* internal use for insert and delete */ -int pyrna_struct_keyframe_parse(PointerRNA *ptr, PyObject *args, char *error_prefix, - char **path_full, int *index, float *cfra) /* return values */ +/* for keyframes and drivers */ +static int pyrna_struct_anim_args_parse(PointerRNA *ptr, char *error_prefix, char *path, + char **path_full, int *index) { - char *path; PropertyRNA *prop; - int array_len; - - if (!PyArg_ParseTuple(args, "s|if", &path, index, cfra)) { - PyErr_Format(PyExc_TypeError, "%.200s expected a string and optionally an int and float arguments", error_prefix); - return -1; - } - + if (ptr->data==NULL) { PyErr_Format(PyExc_TypeError, "%.200s this struct has no data, can't be animated", error_prefix); return -1; } - + prop = RNA_struct_find_property(ptr, path); - + if (prop==NULL) { PyErr_Format( PyExc_TypeError, "%.200s property \"%s\" not found", error_prefix, path); return -1; } if (!RNA_property_animateable(ptr, prop)) { - PyErr_Format( PyExc_TypeError, "%.200s property \"%s\" not animatable", error_prefix, path); + PyErr_Format(PyExc_TypeError, "%.200s property \"%s\" not animatable", error_prefix, path); return -1; } + if(RNA_property_array_check(ptr, prop) == 0) { + if((*index) == -1) { + *index= 0; + } + else { + PyErr_Format(PyExc_TypeError, "%.200s index %d was given while property \"%s\" is not an array", error_prefix, *index, path); + return -1; + } + } + else { + int array_len= RNA_property_array_length(ptr, prop); + if((*index) < -1 || (*index) >= array_len) { + PyErr_Format( PyExc_TypeError, "%.200s index out of range \"%s\", given %d, array length is %d", error_prefix, path, *index, array_len); + return -1; + } + } + *path_full= RNA_path_from_ID_to_property(ptr, prop); if (*path_full==NULL) { @@ -1699,18 +1763,45 @@ int pyrna_struct_keyframe_parse(PointerRNA *ptr, PyObject *args, char *error_pre return -1; } - array_len= RNA_property_array_length(ptr, prop); - if((*index) != -1 && (*index) >= array_len) { - PyErr_Format( PyExc_TypeError, "%.200s index out of range \"%s\", given %d, array length is %d", error_prefix, path, *index, array_len); + return 0; +} + +/* internal use for insert and delete */ +static int pyrna_struct_keyframe_parse(PointerRNA *ptr, PyObject *args, char *error_prefix, + char **path_full, int *index, float *cfra, char **group_name) /* return values */ +{ + char *path; + + if (!PyArg_ParseTuple(args, "s|ifs", &path, index, cfra, group_name)) { + PyErr_Format(PyExc_TypeError, "%.200s expected a string and optionally an int, float, and string arguments", error_prefix); return -1; } + if(pyrna_struct_anim_args_parse(ptr, error_prefix, path, path_full, index) < 0) + return -1; + if(*cfra==FLT_MAX) *cfra= CTX_data_scene(BPy_GetContext())->r.cfra; return 0; /* success */ } +static char pyrna_struct_keyframe_insert_doc[] = +".. method:: keyframe_insert(path, index=-1, frame=bpy.context.scene.frame_current)\n" +"\n" +" Insert a keyframe on the property given, adding fcurves and animation data when necessary.\n" +"\n" +" :arg path: path to the property to key, analogous to the fcurve's data path.\n" +" :type path: string\n" +" :arg index: array index of the property to key. Defaults to -1 which will key all indicies or a single channel if the property is not an array.\n" +" :type index: int\n" +" :arg frame: The frame on which the keyframe is inserted, defaulting to the current frame.\n" +" :type frame: float\n" +" :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n" +" :type group: str\n" +" :return: Success of keyframe insertion.\n" +" :rtype: boolean"; + static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args) { PyObject *result; @@ -1718,16 +1809,33 @@ static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *arg char *path_full= NULL; int index= -1; float cfra= FLT_MAX; + char *group_name= NULL; - if(pyrna_struct_keyframe_parse(&self->ptr, args, "bpy_struct.keyframe_insert():", &path_full, &index, &cfra) == -1) + if(pyrna_struct_keyframe_parse(&self->ptr, args, "bpy_struct.keyframe_insert():", &path_full, &index, &cfra, &group_name) == -1) return NULL; - result= PyBool_FromLong(insert_keyframe((ID *)self->ptr.id.data, NULL, NULL, path_full, index, cfra, 0)); + result= PyBool_FromLong(insert_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0)); MEM_freeN(path_full); return result; } +static char pyrna_struct_keyframe_delete_doc[] = +".. method:: keyframe_delete(path, index=-1, frame=bpy.context.scene.frame_current)\n" +"\n" +" Remove a keyframe from this properties fcurve.\n" +"\n" +" :arg path: path to the property to remove a key, analogous to the fcurve's data path.\n" +" :type path: string\n" +" :arg index: array index of the property to remove a key. Defaults to -1 removing all indicies or a single channel if the property is not an array.\n" +" :type index: int\n" +" :arg frame: The frame on which the keyframe is deleted, defaulting to the current frame.\n" +" :type frame: float\n" +" :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n" +" :type group: str\n" +" :return: Success of keyframe deleation.\n" +" :rtype: boolean"; + static PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args) { PyObject *result; @@ -1735,49 +1843,40 @@ static PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *arg char *path_full= NULL; int index= -1; float cfra= FLT_MAX; + char *group_name= NULL; - if(pyrna_struct_keyframe_parse(&self->ptr, args, "bpy_struct.keyframe_delete():", &path_full, &index, &cfra) == -1) + if(pyrna_struct_keyframe_parse(&self->ptr, args, "bpy_struct.keyframe_delete():", &path_full, &index, &cfra, &group_name) == -1) return NULL; - result= PyBool_FromLong(delete_keyframe((ID *)self->ptr.id.data, NULL, NULL, path_full, index, cfra, 0)); + result= PyBool_FromLong(delete_keyframe((ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0)); MEM_freeN(path_full); return result; } +static char pyrna_struct_driver_add_doc[] = +".. method:: driver_add(path, index=-1)\n" +"\n" +" Adds driver(s) to the given property\n" +"\n" +" :arg path: path to the property to drive, analogous to the fcurve's data path.\n" +" :type path: string\n" +" :arg index: array index of the property drive. Defaults to -1 for all indicies or a single channel if the property is not an array.\n" +" :type index: int\n" +" :return: The driver(s) added.\n" +" :rtype: :class:`FCurve` or list if index is -1 with an array property."; + static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args) { char *path, *path_full; - int index= -1; /* default to all */ - PropertyRNA *prop; + int index= -1; PyObject *ret; if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index)) return NULL; - if (self->ptr.data==NULL) { - PyErr_Format( PyExc_TypeError, "bpy_struct.driver_add(): this struct has no data, cant be animated", path); - return NULL; - } - - prop = RNA_struct_find_property(&self->ptr, path); - - if (prop==NULL) { - PyErr_Format( PyExc_TypeError, "bpy_struct.driver_add(): property \"%s\" not found", path); - return NULL; - } - - if (!RNA_property_animateable(&self->ptr, prop)) { - PyErr_Format( PyExc_TypeError, "bpy_struct.driver_add(): property \"%s\" not animatable", path); - return NULL; - } - - path_full= RNA_path_from_ID_to_property(&self->ptr, prop); - - if (path_full==NULL) { - PyErr_Format( PyExc_TypeError, "bpy_struct.driver_add(): could not make path to \"%s\"", path); + if(pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_add():", path, &path_full, &index) < 0) return NULL; - } if(ANIM_add_driver((ID *)self->ptr.id.data, path_full, index, 0, DRIVER_TYPE_PYTHON)) { ID *id= self->ptr.id.data; @@ -1804,8 +1903,8 @@ static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args) } } else { - ret= Py_None; - Py_INCREF(ret); + PyErr_SetString(PyExc_TypeError, "bpy_struct.driver_add(): failed because of an internal error"); + return NULL; } MEM_freeN(path_full); @@ -1813,6 +1912,47 @@ static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args) return ret; } + +static char pyrna_struct_driver_remove_doc[] = +".. method:: driver_remove(path, index=-1)\n" +"\n" +" Remove driver(s) from the given property\n" +"\n" +" :arg path: path to the property to drive, analogous to the fcurve's data path.\n" +" :type path: string\n" +" :arg index: array index of the property drive. Defaults to -1 for all indicies or a single channel if the property is not an array.\n" +" :type index: int\n" +" :return: Success of driver removal.\n" +" :rtype: boolean"; + +static PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args) +{ + char *path, *path_full; + int index= -1; + PyObject *ret; + + 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) < 0) + return NULL; + + ret= PyBool_FromLong(ANIM_remove_driver((ID *)self->ptr.id.data, path_full, index, 0)); + + MEM_freeN(path_full); + + return ret; +} + + +static char pyrna_struct_is_property_set_doc[] = +".. method:: is_property_set(property)\n" +"\n" +" Check if a property is set, use for testing operator properties.\n" +"\n" +" :return: True when the property has been set.\n" +" :rtype: boolean"; + static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args) { char *name; @@ -1823,6 +1963,14 @@ static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *arg return PyBool_FromLong(RNA_property_is_set(&self->ptr, name)); } +static char pyrna_struct_is_property_hidden_doc[] = +".. method:: is_property_hidden(property)\n" +"\n" +" Check if a property is hidden.\n" +"\n" +" :return: True when the property is hidden.\n" +" :rtype: boolean"; + static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args) { PropertyRNA *prop; @@ -1838,6 +1986,11 @@ static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject * return PyBool_FromLong(hidden); } +static char pyrna_struct_path_resolve_doc[] = +".. method:: path_resolve(path)\n" +"\n" +" Returns the property from the path given or None if the property is not found."; + static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value) { char *path= _PyUnicode_AsString(value); @@ -1845,7 +1998,7 @@ static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value) PropertyRNA *r_prop; if(path==NULL) { - PyErr_SetString( PyExc_TypeError, "bpy_struct.items(): is only valid for collection types" ); + PyErr_SetString(PyExc_TypeError, "bpy_struct.path_resolve(): accepts only a single string argument"); return NULL; } @@ -1855,20 +2008,30 @@ static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value) Py_RETURN_NONE; } -static PyObject *pyrna_struct_path_to_id(BPy_StructRNA *self, PyObject *args) +static char pyrna_struct_path_from_id_doc[] = +".. method:: path_from_id(property=\"\")\n" +"\n" +" Returns the data path from the ID to this object (string).\n" +"\n" +" :arg property: Optional property name which can be used if the path is to a property of this object.\n" +" :type property: string\n" +" :return: The path from :class:`bpy_struct.id_data` to this struct and property (when given).\n" +" :rtype: str"; + +static PyObject *pyrna_struct_path_from_id(BPy_StructRNA *self, PyObject *args) { char *name= NULL; char *path; PropertyRNA *prop; PyObject *ret; - if (!PyArg_ParseTuple(args, "|s:path_to_id", &name)) + if (!PyArg_ParseTuple(args, "|s:path_from_id", &name)) return NULL; if(name) { prop= RNA_struct_find_property(&self->ptr, name); if(prop==NULL) { - PyErr_Format(PyExc_TypeError, "%.200s.path_to_id(\"%.200s\") not found", RNA_struct_identifier(self->ptr.type), name); + PyErr_Format(PyExc_TypeError, "%.200s.path_from_id(\"%.200s\") not found", RNA_struct_identifier(self->ptr.type), name); return NULL; } @@ -1879,8 +2042,8 @@ static PyObject *pyrna_struct_path_to_id(BPy_StructRNA *self, PyObject *args) } if(path==NULL) { - if(name) PyErr_Format(PyExc_TypeError, "%.200s.path_to_id(\"%s\") found but does not support path creation", RNA_struct_identifier(self->ptr.type), name); - else PyErr_Format(PyExc_TypeError, "%.200s.path_to_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type)); + if(name) PyErr_Format(PyExc_TypeError, "%.200s.path_from_id(\"%s\") found but does not support path creation", RNA_struct_identifier(self->ptr.type), name); + else PyErr_Format(PyExc_TypeError, "%.200s.path_from_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type)); return NULL; } @@ -1890,14 +2053,15 @@ static PyObject *pyrna_struct_path_to_id(BPy_StructRNA *self, PyObject *args) return ret; } -static PyObject *pyrna_struct_recast_type(BPy_StructRNA *self, PyObject *args) -{ - PointerRNA r_ptr; - RNA_pointer_recast(&self->ptr, &r_ptr); - return pyrna_struct_CreatePyObject(&r_ptr); -} +static char pyrna_prop_path_from_id_doc[] = +".. method:: path_from_id()\n" +"\n" +" Returns the data path from the ID to this property (string).\n" +"\n" +" :return: The path from :class:`bpy_struct.id_data` to this property.\n" +" :rtype: str"; -static PyObject *pyrna_prop_path_to_id(BPy_PropertyRNA *self) +static PyObject *pyrna_prop_path_from_id(BPy_PropertyRNA *self) { char *path; PropertyRNA *prop = self->prop; @@ -1906,7 +2070,7 @@ static PyObject *pyrna_prop_path_to_id(BPy_PropertyRNA *self) path= RNA_path_from_ID_to_property(&self->ptr, self->prop); if(path==NULL) { - PyErr_Format(PyExc_TypeError, "%.200s.%.200s.path_to_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop)); + PyErr_Format(PyExc_TypeError, "%.200s.%.200s.path_from_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop)); return NULL; } @@ -1916,6 +2080,21 @@ static PyObject *pyrna_prop_path_to_id(BPy_PropertyRNA *self) return ret; } +static char pyrna_struct_recast_type_doc[] = +".. method:: recast_type()\n" +"\n" +" Return a new instance, this is needed because types such as textures can be changed at runtime.\n" +"\n" +" :return: a new instance of this object with the type initialized again.\n" +" :rtype: subclass of :class:`bpy_struct`"; + +static PyObject *pyrna_struct_recast_type(BPy_StructRNA *self, PyObject *args) +{ + PointerRNA r_ptr; + RNA_pointer_recast(&self->ptr, &r_ptr); + return pyrna_struct_CreatePyObject(&r_ptr); +} + static void pyrna_dir_members_py(PyObject *list, PyObject *self) { PyObject *dict; @@ -2284,7 +2463,7 @@ static PyGetSetDef pyrna_prop_getseters[] = { #endif static PyGetSetDef pyrna_struct_getseters[] = { - {"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, "The ID data this datablock is from, (not available for all data)", NULL}, + {"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, "The :class:`ID` object this datablock is from or None, (not available for all data types)", NULL}, {NULL,NULL,NULL,NULL,NULL} /* Sentinel */ }; @@ -2361,6 +2540,18 @@ static PyObject *pyrna_prop_values(BPy_PropertyRNA *self) return ret; } +static char pyrna_struct_get_doc[] = +".. method:: get(key, default=None)\n" +"\n" +" Returns the value of the custom property assigned to key or default when not found (matches pythons dictionary function of the same name).\n" +"\n" +" :arg key: The key assosiated with the custom property.\n" +" :type key: string\n" +" :arg default: Optional argument for the value to return if *key* is not found.\n" +// " :type default: Undefined\n" +"\n" +" .. note:: Only :class:`ID`, :class:`Bone` and :class:`PoseBone` classes support custom properties.\n"; + static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args) { IDProperty *group, *idprop; @@ -2389,6 +2580,16 @@ static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args) return def; } +static char pyrna_struct_as_pointer_doc[] = +".. method:: as_pointer()\n" +"\n" +" Returns capsule which holds a pointer to blenders internal data\n" +"\n" +" :return: capsule with a name set from the struct type.\n" +" :rtype: PyCapsule\n" +"\n" +" .. note:: This is intended only for advanced script writers who need to pass blender data to their own C/Python modules.\n"; + static PyObject *pyrna_struct_as_pointer(BPy_StructRNA *self) { if(self->ptr.data) @@ -2705,23 +2906,23 @@ PyObject *pyrna_prop_collection_iter(BPy_PropertyRNA *self) static struct PyMethodDef pyrna_struct_methods[] = { /* only for PointerRNA's with ID'props */ - {"keys", (PyCFunction)pyrna_struct_keys, METH_NOARGS, NULL}, - {"values", (PyCFunction)pyrna_struct_values, METH_NOARGS, NULL}, - {"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, NULL}, - - {"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, NULL}, - - {"as_pointer", (PyCFunction)pyrna_struct_as_pointer, METH_NOARGS, NULL}, - - /* maybe this become and ID function */ - {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL}, - {"keyframe_delete", (PyCFunction)pyrna_struct_keyframe_delete, METH_VARARGS, NULL}, - {"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, NULL}, - {"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, NULL}, - {"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, NULL}, - {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, NULL}, - {"path_to_id", (PyCFunction)pyrna_struct_path_to_id, METH_VARARGS, NULL}, - {"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, NULL}, + {"keys", (PyCFunction)pyrna_struct_keys, METH_NOARGS, pyrna_struct_keys_doc}, + {"values", (PyCFunction)pyrna_struct_values, METH_NOARGS, pyrna_struct_values_doc}, + {"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, pyrna_struct_items_doc}, + + {"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, pyrna_struct_get_doc}, + + {"as_pointer", (PyCFunction)pyrna_struct_as_pointer, METH_NOARGS, pyrna_struct_as_pointer_doc}, + + {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, pyrna_struct_keyframe_insert_doc}, + {"keyframe_delete", (PyCFunction)pyrna_struct_keyframe_delete, METH_VARARGS, pyrna_struct_keyframe_delete_doc}, + {"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, pyrna_struct_driver_add_doc}, + {"driver_remove", (PyCFunction)pyrna_struct_driver_remove, METH_VARARGS, pyrna_struct_driver_remove_doc}, + {"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, pyrna_struct_is_property_set_doc}, + {"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, pyrna_struct_is_property_hidden_doc}, + {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, pyrna_struct_path_resolve_doc}, + {"path_from_id", (PyCFunction)pyrna_struct_path_from_id, METH_VARARGS, pyrna_struct_path_from_id_doc}, + {"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, pyrna_struct_recast_type_doc}, {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL}, /* experemental */ @@ -2732,7 +2933,7 @@ static struct PyMethodDef pyrna_struct_methods[] = { }; static struct PyMethodDef pyrna_prop_methods[] = { - {"path_to_id", (PyCFunction)pyrna_prop_path_to_id, METH_NOARGS, NULL}, + {"path_from_id", (PyCFunction)pyrna_prop_path_from_id, METH_NOARGS, pyrna_prop_path_from_id_doc}, {"__dir__", (PyCFunction)pyrna_prop_dir, METH_NOARGS, NULL}, {NULL, NULL, 0, NULL} }; |