diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-11-08 04:13:19 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-11-08 04:13:19 +0300 |
commit | fac2ca1c7ca26b9e7019d5c3367b1cb758ba8ca4 (patch) | |
tree | 9f4b7e554ceee580d01fb22702d8b6ff01064bbf /release | |
parent | 30c4c4599dfb72b430a409648bb1407a0f197332 (diff) |
bpy/rna api class feature
- python defined classes will be used when available (otherwise automaically generated metaclasses are made as before)
- use properties rather then functions for python defined rna class's
- call the classes getattr AFTER doing an RNA lookup, avoids setting and clearing exceptions for most attribute lookups, tested UI scripts are ~25% faster.
- extending rna py classes this way is a nicer alternative to modifying the generated metaclasses in place.
Example class
--- snip
class Object(bpy.types.ID):
def _get_children(self):
return [child for child in bpy.data.objects if child.parent == self]
children = property(_get_children)
--- snip
The C initialization function looks in bpy_types.py for classes matching RNA structure names, using them when available.
This means all objects in python will be instances of these classes.
Python properties/funcs defined in ID py class will also be available for subclasses for eg. (Group Mesh etc)
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/modules/bpy_ext/Mesh.py | 72 | ||||
-rw-r--r-- | release/scripts/modules/bpy_ext/Object.py | 22 | ||||
-rw-r--r-- | release/scripts/modules/bpy_ext/__init__.py | 20 | ||||
-rw-r--r-- | release/scripts/modules/bpy_types.py | 82 | ||||
-rw-r--r-- | release/scripts/op/mesh.py | 4 | ||||
-rw-r--r-- | release/scripts/op/mesh_skin.py | 6 |
6 files changed, 87 insertions, 119 deletions
diff --git a/release/scripts/modules/bpy_ext/Mesh.py b/release/scripts/modules/bpy_ext/Mesh.py index c8abb15ae69..e69de29bb2d 100644 --- a/release/scripts/modules/bpy_ext/Mesh.py +++ b/release/scripts/modules/bpy_ext/Mesh.py @@ -1,72 +0,0 @@ -# ##### 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -def ord_ind(i1,i2): - if i1<i2: return i1,i2 - return i2,i1 - -def edge_key(ed): - v1, v2 = tuple(ed.verts) - return ord_ind(v1, v2) - -def face_edge_keys(face): - verts = tuple(face.verts) - if len(verts)==3: - return ord_ind(verts[0], verts[1]), ord_ind(verts[1], verts[2]), ord_ind(verts[2], verts[0]) - - return ord_ind(verts[0], verts[1]), ord_ind(verts[1], verts[2]), ord_ind(verts[2], verts[3]), ord_ind(verts[3], verts[0]) - -def mesh_edge_keys(mesh): - return [edge_key for face in mesh.faces for edge_key in face.edge_keys()] - -def mesh_edge_face_count_dict(mesh, face_edge_keys=None): - - # Optional speedup - if face_edge_keys==None: - face_edge_keys = [face.edge_keys() for face in face_list] - - face_edge_count = {} - for face_keys in face_edge_keys: - for key in face_keys: - try: - face_edge_count[key] += 1 - except: - face_edge_count[key] = 1 - - - return face_edge_count - -def mesh_edge_face_count(mesh, face_edge_keys=None): - edge_face_count_dict = mesh.edge_face_count_dict(face_edge_keys) - return [edge_face_count_dict.get(ed.key(), 0) for ed in mesh.edges] - -import bpy - -# * Edge * -class_obj = bpy.types.MeshEdge -class_obj.key = edge_key - -# * Face * -class_obj = bpy.types.MeshFace -class_obj.edge_keys = face_edge_keys - -# * Mesh * -class_obj = bpy.types.Mesh -class_obj.edge_keys = mesh_edge_keys -class_obj.edge_face_count = mesh_edge_face_count -class_obj.edge_face_count_dict = mesh_edge_face_count_dict diff --git a/release/scripts/modules/bpy_ext/Object.py b/release/scripts/modules/bpy_ext/Object.py index 1febb50cee8..e69de29bb2d 100644 --- a/release/scripts/modules/bpy_ext/Object.py +++ b/release/scripts/modules/bpy_ext/Object.py @@ -1,22 +0,0 @@ -# ##### 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -import bpy -class_obj = bpy.types.Object - -class_obj.getChildren = lambda self: [child for child in bpy.data.objects if child.parent == self] diff --git a/release/scripts/modules/bpy_ext/__init__.py b/release/scripts/modules/bpy_ext/__init__.py index 949a282c545..e69de29bb2d 100644 --- a/release/scripts/modules/bpy_ext/__init__.py +++ b/release/scripts/modules/bpy_ext/__init__.py @@ -1,20 +0,0 @@ -# ##### 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -import bpy_ext.Object -import bpy_ext.Mesh diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py new file mode 100644 index 00000000000..d2bed9f7faa --- /dev/null +++ b/release/scripts/modules/bpy_types.py @@ -0,0 +1,82 @@ +# ##### 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +import bpy + +StructRNA = bpy.types.Struct.__bases__[0] +# StructRNA = bpy.types.Struct + + +class Object(bpy.types.ID): + + def _get_children(self): + return [child for child in bpy.data.objects if child.parent == self] + + children = property(_get_children) + + +def ord_ind(i1,i2): + if i1<i2: return i1,i2 + return i2,i1 + +class Mesh(bpy.types.ID): + + def _get_edge_keys(self): + return [edge_key for face in self.faces for edge_key in face.edge_keys] + + edge_keys = property(_get_edge_keys) + + def _get_edge_face_count_dict(self): + face_edge_keys = [face.edge_keys for face in self.faces] + face_edge_count = {} + for face_keys in face_edge_keys: + for key in face_keys: + try: + face_edge_count[key] += 1 + except: + face_edge_count[key] = 1 + + return face_edge_count + + edge_face_count_dict = property(_get_edge_face_count_dict) + + def _get_edge_face_count(self): + edge_face_count_dict = self.edge_face_count_dict + return [edge_face_count_dict.get(ed.key, 0) for ed in mesh.edges] + + edge_face_count = property(_get_edge_face_count) + + +class MeshEdge(StructRNA): + + def _get_key(self): + return ord_ind(*tuple(self.verts)) + + key = property(_get_key) + + +class MeshFace(StructRNA): + + def _get_edge_keys(self): + verts = tuple(self.verts) + if len(verts)==3: + return ord_ind(verts[0], verts[1]), ord_ind(verts[1], verts[2]), ord_ind(verts[2], verts[0]) + + return ord_ind(verts[0], verts[1]), ord_ind(verts[1], verts[2]), ord_ind(verts[2], verts[3]), ord_ind(verts[3], verts[0]) + + edge_keys = property(_get_edge_keys) diff --git a/release/scripts/op/mesh.py b/release/scripts/op/mesh.py index 8ea9ea03062..f78e33730f0 100644 --- a/release/scripts/op/mesh.py +++ b/release/scripts/op/mesh.py @@ -28,9 +28,9 @@ def main(context): mesh = ob.data face_list = [face for face in mesh.faces] - face_edge_keys = [face.edge_keys() for face in face_list] + face_edge_keys = [face.edge_keys for face in face_list] - edge_face_count = mesh.edge_face_count_dict(face_edge_keys) + edge_face_count = mesh.edge_face_count_dict def test_interior(index): for key in face_edge_keys[index]: diff --git a/release/scripts/op/mesh_skin.py b/release/scripts/op/mesh_skin.py index 85df6463eb0..f9a92c8cd82 100644 --- a/release/scripts/op/mesh_skin.py +++ b/release/scripts/op/mesh_skin.py @@ -256,11 +256,11 @@ def getSelectedEdges(context, me, ob): if MESH_MODE == 'FACE': context.scene.tool_settings.mesh_selection_mode = 'EDGE' # value is [edge, face_sel_user_in] - edge_dict= dict((ed.key(), [ed, 0]) for ed in me.edges) + edge_dict= dict((ed.key, [ed, 0]) for ed in me.edges) for f in me.faces: if f.selected: - for edkey in f.edge_keys(): + for edkey in f.edge_keys: edge_dict[edkey][1] += 1 context.scene.tool_settings.mesh_selection_mode = MESH_MODE @@ -280,7 +280,7 @@ def getVertLoops(selEdges, me): vert_used = [False] * tot for ed in selEdges: - i1, i2 = ed.key() + i1, i2 = ed.key vert_siblings[i1].append(i2) vert_siblings[i2].append(i1) |