Welcome to mirror list, hosted at ThFree Co, Russian Federation.

include__bmesh.rst « rst « python_api « doc - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1356a5e5d9a9722d2e6d95811dcf5287d241fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
..
   This document is appended to the auto generated bmesh api doc to avoid clogging up the C files with details.
   to test this run:
   ./blender.bin -b -noaudio -P doc/python_api/sphinx_doc_gen.py -- \
                 --partial bmesh* ; cd doc/python_api ; sphinx-build sphinx-in sphinx-out ; cd ../../


Submodules:

.. toctree::
   :maxdepth: 1

   bmesh.ops.rst
   bmesh.types.rst
   bmesh.utils.rst
   bmesh.geometry.rst


Introduction
------------

This API gives access the blenders internal mesh editing api, featuring geometry connectivity data and
access to editing operations such as split, separate, collapse and dissolve.

The features exposed closely follow the C API,
giving python access to the functions used by blenders own mesh editing tools.

For an overview of BMesh data types and how they reference each other see:
`BMesh Design Document <https://wiki.blender.org/index.php/Dev:Source/Modeling/BMesh/Design>`_ .


.. note::

   **Disk** and **Radial** data is not exposed by the python api since this is for internal use only.


.. warning:: TODO items are...

   * add access to BMesh **walkers**
   * add custom-data manipulation functions add/remove/rename.


Example Script
--------------

.. literalinclude:: __/__/__/release/scripts/templates_py/bmesh_simple.py


Stand-Alone Module
^^^^^^^^^^^^^^^^^^

The bmesh module is written to be standalone except for :mod:`mathutils`
which is used for vertex locations and normals.

The only other exception to this are when converting mesh data to and from :class:`bpy.types.Mesh`.


Mesh Access
-----------

There are 2 ways to access BMesh data, you can create a new BMesh by converting a mesh from
:class:`bpy.types.BlendData.meshes` or by accessing the current edit mode mesh.
see: :class:`bmesh.types.BMesh.from_mesh` and :mod:`bmesh.from_edit_mesh` respectively.

When explicitly converting from mesh data python **owns** the data, that is to say -
that the mesh only exists while python holds a reference to it,
and the script is responsible for putting it back into a mesh data-block when the edits are done.

Note that unlike :mod:`bpy`, a BMesh does not necessarily correspond to data in the currently open blend file,
a BMesh can be created, edited and freed without the user ever seeing or having access to it.
Unlike edit mode, the bmesh module can use multiple BMesh instances at once.

Take care when dealing with multiple BMesh instances since the mesh data can use a lot of memory, while a mesh that
python owns will be freed in when the script holds no references to it,
its good practice to call :class:`bmesh.types.BMesh.free` which will remove all the mesh data immediately and disable
further access.


EditMode Tessellation
^^^^^^^^^^^^^^^^^^^^^

When writing scripts that operate on editmode data you will normally want to re-calculate the tessellation after
running the  script, this needs to be called explicitly.

The BMesh its self does not store the triangulated faces, they are stored in the :class:`bpy.types.Mesh`,
to refresh tessellation triangles call :class:`bpy.types.Mesh.calc_loop_triangles`.


CustomData Access
-----------------

BMesh has a unified way to access mesh attributes such as UV's vertex colors, shape keys, edge crease etc.

This works by having a **layers** property on bmesh data sequences to access the custom data layers which can then be
used to access the actual data on each vert/edge/face/loop.

Here are some examples ...

.. code-block:: python

   uv_lay = bm.loops.layers.uv.active

   for face in bm.faces:
       for loop in face.loops:
           uv = loop[uv_lay].uv
           print("Loop UV: %f, %f" % uv[:])
           vert = loop.vert
           print("Loop Vert: (%f,%f,%f)" % vert.co[:])


.. code-block:: python

   shape_lay = bm.verts.layers.shape["Key.001"]

   for vert in bm.verts:
       shape = vert[shape_lay]
       print("Vert Shape: %f, %f, %f" % (shape.x, shape.y, shape.z))


.. code-block:: python

   # in this example the active vertex group index is used,
   # this is stored in the object, not the BMesh
   group_index = obj.vertex_groups.active_index

   # only ever one deform weight layer
   dvert_lay = bm.verts.layers.deform.active

   for vert in bm.verts:
       dvert = vert[dvert_lay]

       if group_index in dvert:
           print("Weight %f" % dvert[group_index])
       else:
           print("Setting Weight")
           dvert[group_index] = 0.5


Keeping a Correct State
-----------------------

When modeling in blender there are certain assumptions made about the state of the mesh.

* hidden geometry isn't selected.
* when an edge is selected, its vertices are selected too.
* when a face is selected, its edges and vertices are selected.
* duplicate edges / faces don't exist.
* faces have at least 3 vertices.

To give developers flexibility these conventions are not enforced,
however tools must leave the mesh in a valid state else other tools may behave incorrectly.

Any errors that arise from not following these conventions is considered a bug in the script,
not a bug in blender.


Selection / Flushing
^^^^^^^^^^^^^^^^^^^^

As mentioned above, it is possible to create an invalid selection state
(by selecting a state and then de-selecting one of its vertices's for example), mostly the best way to solve this is to
flush the selection after performing a series of edits. this validates the selection state.


Module Functions
----------------