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

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'release/scripts/modules/bpy_extras')
-rw-r--r--release/scripts/modules/bpy_extras/__init__.py31
-rw-r--r--release/scripts/modules/bpy_extras/image_utils.py110
-rw-r--r--release/scripts/modules/bpy_extras/io_utils.py385
-rw-r--r--release/scripts/modules/bpy_extras/mesh_utils.py510
-rw-r--r--release/scripts/modules/bpy_extras/object_utils.py144
-rw-r--r--release/scripts/modules/bpy_extras/view3d_utils.py128
6 files changed, 1308 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy_extras/__init__.py b/release/scripts/modules/bpy_extras/__init__.py
new file mode 100644
index 00000000000..06d41fa670e
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/__init__.py
@@ -0,0 +1,31 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+"""
+Utility modules assosiated with the bpy module.
+"""
+
+__all__ = (
+ "object_utils",
+ "io_utils",
+ "image_utils",
+ "mesh_utils",
+ "view3d_utils",
+)
diff --git a/release/scripts/modules/bpy_extras/image_utils.py b/release/scripts/modules/bpy_extras/image_utils.py
new file mode 100644
index 00000000000..e56c1c651c4
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/image_utils.py
@@ -0,0 +1,110 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+__all__ = (
+ "load_image",
+)
+
+
+# limited replacement for BPyImage.comprehensiveImageLoad
+def load_image(imagepath,
+ dirname="",
+ place_holder=False,
+ recursive=False,
+ ncase_cmp=True,
+ convert_callback=None,
+ verbose=False,
+ ):
+ """
+ Return an image from the file path with options to search multiple paths and
+ return a placeholder if its not found.
+
+ :arg filepath: The image filename
+ If a path precedes it, this will be searched as well.
+ :type filepath: string
+ :arg dirname: is the directory where the image may be located - any file at
+ the end will be ignored.
+ :type dirname: string
+ :arg place_holder: if True a new place holder image will be created.
+ this is usefull so later you can relink the image to its original data.
+ :type place_holder: bool
+ :arg recursive: If True, directories will be recursivly searched.
+ Be carefull with this if you have files in your root directory because
+ it may take a long time.
+ :type recursive: bool
+ :arg ncase_cmp: on non windows systems, find the correct case for the file.
+ :type ncase_cmp: bool
+ :arg convert_callback: a function that takes an existing path and returns a new one.
+ Use this when loading image formats blender may not support, the CONVERT_CALLBACK
+ can take the path for a GIF (for example), convert it to a PNG and return the PNG's path.
+ For formats blender can read, simply return the path that is given.
+ :type convert_callback: function
+ :return: an image or None
+ :rtype: :class:`Image`
+ """
+ import os
+ import bpy
+
+ # TODO: recursive
+
+ def _image_load(path):
+ import bpy
+
+ if convert_callback:
+ path = convert_callback(path)
+
+ image = bpy.data.images.load(path)
+
+ if verbose:
+ print(" image loaded '%s'" % path)
+
+ return image
+
+ if verbose:
+ print("load_image('%s', '%s', ...)" % (imagepath, dirname))
+
+ if os.path.exists(imagepath):
+ return _image_load(imagepath)
+
+ variants = [imagepath]
+
+ if dirname:
+ variants += [os.path.join(dirname, imagepath),
+ os.path.join(dirname, bpy.path.basename(imagepath)),
+ ]
+
+ for filepath_test in variants:
+ if ncase_cmp:
+ ncase_variants = filepath_test, bpy.path.resolve_ncase(filepath_test)
+ else:
+ ncase_variants = (filepath_test, )
+
+ for nfilepath in ncase_variants:
+ if os.path.exists(nfilepath):
+ return _image_load(nfilepath)
+
+ if place_holder:
+ image = bpy.data.images.new(bpy.path.basename(imagepath), 128, 128)
+ # allow the path to be resolved later
+ image.filepath = imagepath
+ return image
+
+ # TODO comprehensiveImageLoad also searched in bpy.config.textureDir
+ return None
diff --git a/release/scripts/modules/bpy_extras/io_utils.py b/release/scripts/modules/bpy_extras/io_utils.py
new file mode 100644
index 00000000000..0a3f1392653
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/io_utils.py
@@ -0,0 +1,385 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+__all__ = (
+ "ExportHelper",
+ "ImportHelper",
+ "axis_conversion",
+ "axis_conversion_ensure",
+ "create_derived_objects",
+ "free_derived_objects",
+ "unpack_list",
+ "unpack_face_list",
+ "path_reference",
+ "path_reference_copy",
+ "path_reference_mode",
+ "unique_name"
+)
+
+import bpy
+from bpy.props import StringProperty, BoolProperty, EnumProperty
+
+
+class ExportHelper:
+ filepath = StringProperty(name="File Path", description="Filepath used for exporting the file", maxlen=1024, default="", subtype='FILE_PATH')
+ check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
+
+ # subclasses can override with decorator
+ # True == use ext, False == no ext, None == do nothing.
+ check_extension = True
+
+ def invoke(self, context, event):
+ import os
+ if not self.filepath:
+ blend_filepath = context.blend_data.filepath
+ if not blend_filepath:
+ blend_filepath = "untitled"
+ else:
+ blend_filepath = os.path.splitext(blend_filepath)[0]
+
+ self.filepath = blend_filepath + self.filename_ext
+
+ context.window_manager.fileselect_add(self)
+ return {'RUNNING_MODAL'}
+
+ def check(self, context):
+ check_extension = self.check_extension
+
+ if check_extension is None:
+ return False
+
+ filepath = bpy.path.ensure_ext(self.filepath, self.filename_ext if check_extension else "")
+
+ if filepath != self.filepath:
+ self.filepath = filepath
+ return True
+
+ return False
+
+
+class ImportHelper:
+ filepath = StringProperty(name="File Path", description="Filepath used for importing the file", maxlen=1024, default="", subtype='FILE_PATH')
+
+ def invoke(self, context, event):
+ context.window_manager.fileselect_add(self)
+ return {'RUNNING_MODAL'}
+
+
+# Axis conversion function, not pretty LUT
+# use lookup tabes to convert between any axis
+_axis_convert_matrix = (
+ ((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)),
+ ((-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, -1.0, 0.0)),
+ ((-1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
+ ((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)),
+ ((0.0, -1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
+ ((0.0, -1.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0)),
+ ((0.0, -1.0, 0.0), (0.0, 0.0, 1.0), (-1.0, 0.0, 0.0)),
+ ((0.0, -1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
+ ((0.0, 0.0, -1.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
+ ((0.0, 0.0, -1.0), (0.0, -1.0, 0.0), (-1.0, 0.0, 0.0)),
+ ((0.0, 0.0, -1.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
+ ((0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
+ ((0.0, 0.0, 1.0), (-1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
+ ((0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
+ ((0.0, 0.0, 1.0), (0.0, 1.0, 0.0), (-1.0, 0.0, 0.0)),
+ ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
+ ((0.0, 1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
+ ((0.0, 1.0, 0.0), (0.0, 0.0, -1.0), (-1.0, 0.0, 0.0)),
+ ((0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
+ ((0.0, 1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
+ ((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)),
+ ((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)),
+ ((1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
+ )
+
+# store args as a single int
+# (X Y Z -X -Y -Z) --> (0, 1, 2, 3, 4, 5)
+# each value is ((src_forward, src_up), (dst_forward, dst_up))
+# where all 4 values are or'd into a single value...
+# (i1<<0 | i1<<3 | i1<<6 | i1<<9)
+_axis_convert_lut = (
+ {0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A, 0x09A, 0x2A2, 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C, 0x745, 0x94D, 0x15D, 0x365},
+ {0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A, 0x11A, 0x522, 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC, 0x645, 0xA4D, 0x05D, 0x465},
+ {0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A, 0x05A, 0xA62, 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C, 0x705, 0x50D, 0x11D, 0xB25},
+ {0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A, 0x15A, 0x962, 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C, 0x685, 0x28D, 0x09D, 0x8A5},
+ {0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A, 0x35A, 0x162, 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C, 0x885, 0x68D, 0x29D, 0x0A5},
+ {0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A, 0x21A, 0x422, 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC, 0x8C5, 0xACD, 0x2DD, 0x4E5},
+ {0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA, 0x2DA, 0xAE2, 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C, 0x805, 0x40D, 0x21D, 0xA25},
+ {0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A, 0x29A, 0x6A2, 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC, 0x945, 0x14D, 0x35D, 0x765},
+ {0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A, 0x45A, 0x062, 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C, 0xB05, 0x70D, 0x51D, 0x125},
+ {0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA, 0x4DA, 0x2E2, 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C, 0xA05, 0x80D, 0x41D, 0x225},
+ {0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A, 0x41A, 0x822, 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C, 0xAC5, 0x2CD, 0x4DD, 0x8E5},
+ {0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A, 0x51A, 0x722, 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC, 0xA45, 0x04D, 0x45D, 0x665},
+ {0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A, 0xB1A, 0x122, 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C, 0x445, 0x64D, 0xA5D, 0x065},
+ {0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A, 0xA1A, 0x222, 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C, 0x4C5, 0x8CD, 0xADD, 0x2E5},
+ {0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA, 0xADA, 0x8E2, 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C, 0x405, 0x20D, 0xA1D, 0x825},
+ {0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A, 0xA5A, 0x662, 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC, 0x505, 0x10D, 0xB1D, 0x725},
+ {0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A, 0x89A, 0x0A2, 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C, 0x345, 0x74D, 0x95D, 0x165},
+ {0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA, 0x8DA, 0x4E2, 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC, 0x205, 0xA0D, 0x81D, 0x425},
+ {0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A, 0x81A, 0xA22, 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C, 0x2C5, 0x4CD, 0x8DD, 0xAE5},
+ {0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A, 0x95A, 0x762, 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC, 0x285, 0x08D, 0x89D, 0x6A5},
+ {0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A, 0x75A, 0x362, 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C, 0x085, 0x88D, 0x69D, 0x2A5},
+ {0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A, 0x65A, 0x462, 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC, 0x105, 0xB0D, 0x71D, 0x525},
+ {0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A, 0x71A, 0xB22, 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C, 0x045, 0x44D, 0x65D, 0xA65},
+ )
+
+_axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5}
+
+
+def axis_conversion(from_forward='Y', from_up='Z', to_forward='Y', to_up='Z'):
+ """
+ Each argument us an axis in ['X', 'Y', 'Z', '-X', '-Y', '-Z']
+ where the first 2 are a source and the second 2 are the target.
+ """
+ from mathutils import Matrix
+ from functools import reduce
+
+ if from_forward == to_forward and from_up == to_up:
+ return Matrix().to_3x3()
+
+ if from_forward[-1] == from_up[-1] or to_forward[-1] == to_up[-1]:
+ raise Exception("invalid axis arguments passed, "
+ "can't use up/forward on the same axis.")
+
+ value = reduce(int.__or__, (_axis_convert_num[a] << (i * 3)
+ for i, a in enumerate((from_forward,
+ from_up,
+ to_forward,
+ to_up,
+ ))))
+
+ for i, axis_lut in enumerate(_axis_convert_lut):
+ if value in axis_lut:
+ return Matrix(_axis_convert_matrix[i])
+ assert(0)
+
+
+def axis_conversion_ensure(operator, forward_attr, up_attr):
+ """
+ Function to ensure an operator has valid axis conversion settings, intended
+ to be used from :class:`Operator.check`.
+
+ :arg operator: the operator to access axis attributes from.
+ :type operator: :class:`Operator`
+ :arg forward_attr: attribute storing the forward axis
+ :type forward_attr: string
+ :arg up_attr: attribute storing the up axis
+ :type up_attr: string
+ :return: True if the value was modified.
+ :rtype: boolean
+ """
+ def validate(axis_forward, axis_up):
+ if axis_forward[-1] == axis_up[-1]:
+ axis_up = axis_up[0:-1] + 'XYZ'[('XYZ'.index(axis_up[-1]) + 1) % 3]
+
+ return axis_forward, axis_up
+
+ change = False
+
+ axis = getattr(operator, forward_attr), getattr(operator, up_attr)
+ axis_new = validate(*axis)
+
+ if axis != axis_new:
+ setattr(operator, forward_attr, axis_new[0])
+ setattr(operator, up_attr, axis_new[1])
+
+ return True
+ else:
+ return False
+
+
+# return a tuple (free, object list), free is True if memory should be freed later with free_derived_objects()
+def create_derived_objects(scene, ob):
+ if ob.parent and ob.parent.dupli_type in {'VERTS', 'FACES'}:
+ return False, None
+
+ if ob.dupli_type != 'NONE':
+ ob.dupli_list_create(scene)
+ return True, [(dob.object, dob.matrix) for dob in ob.dupli_list]
+ else:
+ return False, [(ob, ob.matrix_world)]
+
+
+def free_derived_objects(ob):
+ ob.dupli_list_clear()
+
+
+def unpack_list(list_of_tuples):
+ flat_list = []
+ flat_list_extend = flat_list.extend # a tich faster
+ for t in list_of_tuples:
+ flat_list_extend(t)
+ return flat_list
+
+
+# same as above except that it adds 0 for triangle faces
+def unpack_face_list(list_of_tuples):
+ #allocate the entire list
+ flat_ls = [0] * (len(list_of_tuples) * 4)
+ i = 0
+
+ for t in list_of_tuples:
+ if len(t) == 3:
+ if t[2] == 0:
+ t = t[1], t[2], t[0]
+ else: # assuem quad
+ if t[3] == 0 or t[2] == 0:
+ t = t[2], t[3], t[0], t[1]
+
+ flat_ls[i:i + len(t)] = t
+ i += 4
+ return flat_ls
+
+
+path_reference_mode = EnumProperty(
+ name="Path Mode",
+ description="Method used to reference paths",
+ items=(('AUTO', "Auto", "Use Relative paths with subdirectories only"),
+ ('ABSOLUTE', "Absolute", "Always write absolute paths"),
+ ('RELATIVE', "Relative", "Always write relative patsh (where possible)"),
+ ('MATCH', "Match", "Match Absolute/Relative setting with input path"),
+ ('STRIP', "Strip Path", "Filename only"),
+ ('COPY', "Copy", "copy the file to the destination path (or subdirectory)"),
+ ),
+ default='AUTO'
+ )
+
+
+def path_reference(filepath, base_src, base_dst, mode='AUTO', copy_subdir="", copy_set=None):
+ """
+ Return a filepath relative to a destination directory, for use with
+ exporters.
+
+ :arg filepath: the file path to return, supporting blenders relative '//' prefix.
+ :type filepath: string
+ :arg base_src: the directory the *filepath* is relative too (normally the blend file).
+ :type base_src: string
+ :arg base_dst: the directory the *filepath* will be referenced from (normally the export path).
+ :type base_dst: string
+ :arg mode: the method used get the path in ['AUTO', 'ABSOLUTE', 'RELATIVE', 'MATCH', 'STRIP', 'COPY']
+ :type mode: string
+ :arg copy_subdir: the subdirectory of *base_dst* to use when mode='COPY'.
+ :type copy_subdir: string
+ :arg copy_set: collect from/to pairs when mode='COPY', pass to *path_reference_copy* when exportign is done.
+ :type copy_set: set
+ :return: the new filepath.
+ :rtype: string
+ """
+ import os
+ is_relative = filepath.startswith("//")
+ filepath_abs = os.path.normpath(bpy.path.abspath(filepath, base_src))
+
+ if mode in ('ABSOLUTE', 'RELATIVE', 'STRIP'):
+ pass
+ elif mode == 'MATCH':
+ mode = 'RELATIVE' if is_relative else 'ABSOLUTE'
+ elif mode == 'AUTO':
+ mode = 'RELATIVE' if bpy.path.is_subdir(filepath, base_dst) else 'ABSOLUTE'
+ elif mode == 'COPY':
+ if copy_subdir:
+ subdir_abs = os.path.join(os.path.normpath(base_dst), copy_subdir)
+ else:
+ subdir_abs = os.path.normpath(base_dst)
+
+ filepath_cpy = os.path.join(subdir_abs, os.path.basename(filepath))
+
+ copy_set.add((filepath_abs, filepath_cpy))
+
+ filepath_abs = filepath_cpy
+ mode = 'RELATIVE'
+ else:
+ raise Exception("invalid mode given %r" % mode)
+
+ if mode == 'ABSOLUTE':
+ return filepath_abs
+ elif mode == 'RELATIVE':
+ return os.path.relpath(filepath_abs, base_dst)
+ elif mode == 'STRIP':
+ return os.path.basename(filepath_abs)
+
+
+def path_reference_copy(copy_set, report=print):
+ """
+ Execute copying files of path_reference
+
+ :arg copy_set: set of (from, to) pairs to copy.
+ :type copy_set: set
+ :arg report: function used for reporting warnings, takes a string argument.
+ :type report: function
+ """
+ if not copy_set:
+ return
+
+ import os
+ import shutil
+
+ for file_src, file_dst in copy_set:
+ if not os.path.exists(file_src):
+ report("missing %r, not copying" % file_src)
+ elif os.path.exists(file_dst) and os.path.samefile(file_src, file_dst):
+ pass
+ else:
+ dir_to = os.path.dirname(file_dst)
+
+ if not os.path.isdir(dir_to):
+ os.makedirs(dir_to)
+
+ shutil.copy(file_src, file_dst)
+
+
+def unique_name(key, name, name_dict, name_max=-1, clean_func=None):
+ """
+ Helper function for storing unique names which may have special characters
+ stripped and restricted to a maximum length.
+
+ :arg key: unique item this name belongs to, name_dict[key] will be reused
+ when available.
+ This can be the object, mesh, material, etc instance its self.
+ :type key: any hashable object assosiated with the *name*.
+ :arg name: The name used to create a unique value in *name_dict*.
+ :type name: string
+ :arg name_dict: This is used to cache namespace to ensure no collisions
+ occur, this should be an empty dict initially and only modified by this
+ function.
+ :type name_dict: dict
+ :arg clean_func: Function to call on *name* before creating a unique value.
+ :type clean_func: function
+ """
+ name_new = name_dict.get(key)
+ if name_new is None:
+ count = 1
+ name_dict_values = name_dict.values()
+ name_new = name_new_orig = name if clean_func is None else clean_func(name)
+
+ if name_max == -1:
+ while name_new in name_dict_values:
+ name_new = "%s.%03d" % (name_new_orig, count)
+ count += 1
+ else:
+ name_new = name_new[:name_max]
+ while name_new in name_dict_values:
+ count_str = "%03d" % count
+ name_new = "%.*s.%s" % (name_max - (len(count_str) + 1), name_new_orig, count_str)
+ count += 1
+
+ name_dict[key] = name_new
+
+ return name_new
diff --git a/release/scripts/modules/bpy_extras/mesh_utils.py b/release/scripts/modules/bpy_extras/mesh_utils.py
new file mode 100644
index 00000000000..ecd620ff2c9
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/mesh_utils.py
@@ -0,0 +1,510 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8-80 compliant>
+
+__all__ = (
+ "mesh_linked_faces",
+ "edge_face_count_dict",
+ "edge_face_count",
+ "edge_loops_from_faces",
+ "edge_loops_from_edges",
+ "ngon_tesselate",
+ "face_random_points",
+)
+
+
+def mesh_linked_faces(mesh):
+ """
+ Splits the mesh into connected faces, use this for seperating cubes from
+ other mesh elements within 1 mesh datablock.
+
+ :arg mesh: the mesh used to group with.
+ :type mesh: :class:`Mesh`
+ :return: lists of lists containing faces.
+ :rtype: list
+ """
+
+ # Build vert face connectivity
+ vert_faces = [[] for i in range(len(mesh.vertices))]
+ for f in mesh.faces:
+ for v in f.vertices:
+ vert_faces[v].append(f)
+
+ # sort faces into connectivity groups
+ face_groups = [[f] for f in mesh.faces]
+ face_mapping = list(range(len(mesh.faces))) # map old, new face location
+
+ # Now clump faces iterativly
+ ok = True
+ while ok:
+ ok = False
+
+ for i, f in enumerate(mesh.faces):
+ mapped_index = face_mapping[f.index]
+ mapped_group = face_groups[mapped_index]
+
+ for v in f.vertices:
+ for nxt_f in vert_faces[v]:
+ if nxt_f != f:
+ nxt_mapped_index = face_mapping[nxt_f.index]
+
+ # We are not a part of the same group
+ if mapped_index != nxt_mapped_index:
+ ok = True
+
+ # Assign mapping to this group so they
+ # all map to this group
+ for grp_f in face_groups[nxt_mapped_index]:
+ face_mapping[grp_f.index] = mapped_index
+
+ # Move faces into this group
+ mapped_group.extend(face_groups[nxt_mapped_index])
+
+ # remove reference to the list
+ face_groups[nxt_mapped_index] = None
+
+ # return all face groups that are not null
+ # this is all the faces that are connected in their own lists.
+ return [fg for fg in face_groups if fg]
+
+
+def edge_face_count_dict(mesh):
+ """
+ :return: dict of edge keys with their value set to the number of
+ faces using each edge.
+ :rtype: dict
+ """
+ face_edge_keys = [face.edge_keys for face in mesh.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
+
+
+def edge_face_count(mesh):
+ """
+ :return: list face users for each item in mesh.edges.
+ :rtype: list
+ """
+ edge_face_count = edge_face_count_dict(mesh)
+ get = dict.get
+ return [get(edge_face_count, ed.key, 0) for ed in mesh.edges]
+
+
+def edge_loops_from_faces(mesh, faces=None, seams=()):
+ """
+ Edge loops defined by faces
+
+ Takes me.faces or a list of faces and returns the edge loops
+ These edge loops are the edges that sit between quads, so they dont touch
+ 1 quad, note: not connected will make 2 edge loops,
+ both only containing 2 edges.
+
+ return a list of edge key lists
+ [[(0, 1), (4, 8), (3, 8)], ...]
+
+ :arg mesh: the mesh used to get edge loops from.
+ :type mesh: :class:`Mesh`
+ :arg faces: optional face list to only use some of the meshes faces.
+ :type faces: :class:`MeshFaces`, sequence or or NoneType
+ :return: return a list of edge vertex index lists.
+ :rtype: list
+ """
+
+ OTHER_INDEX = 2, 3, 0, 1 # opposite face index
+
+ if faces is None:
+ faces = mesh.faces
+
+ edges = {}
+
+ for f in faces:
+# if len(f) == 4:
+ if f.vertices_raw[3] != 0:
+ edge_keys = f.edge_keys
+ for i, edkey in enumerate(f.edge_keys):
+ edges.setdefault(edkey, []).append(edge_keys[OTHER_INDEX[i]])
+
+ for edkey in seams:
+ edges[edkey] = []
+
+ # Collect edge loops here
+ edge_loops = []
+
+ for edkey, ed_adj in edges.items():
+ if 0 < len(ed_adj) < 3: # 1 or 2
+ # Seek the first edge
+ context_loop = [edkey, ed_adj[0]]
+ edge_loops.append(context_loop)
+ if len(ed_adj) == 2:
+ other_dir = ed_adj[1]
+ else:
+ other_dir = None
+
+ ed_adj[:] = []
+
+ flipped = False
+
+ while 1:
+ # from knowing the last 2, look for th next.
+ ed_adj = edges[context_loop[-1]]
+ if len(ed_adj) != 2:
+ # the original edge had 2 other edges
+ if other_dir and flipped == False:
+ flipped = True # only flip the list once
+ context_loop.reverse()
+ ed_adj[:] = []
+ context_loop.append(other_dir) # save 1 lookiup
+
+ ed_adj = edges[context_loop[-1]]
+ if len(ed_adj) != 2:
+ ed_adj[:] = []
+ break
+ else:
+ ed_adj[:] = []
+ break
+
+ i = ed_adj.index(context_loop[-2])
+ context_loop.append(ed_adj[not i])
+
+ # Dont look at this again
+ ed_adj[:] = []
+
+ return edge_loops
+
+
+def edge_loops_from_edges(mesh, edges=None):
+ """
+ Edge loops defined by edges
+
+ Takes me.edges or a list of edges and returns the edge loops
+
+ return a list of vertex indices.
+ [ [1, 6, 7, 2], ...]
+
+ closed loops have matching start and end values.
+ """
+ line_polys = []
+
+ # Get edges not used by a face
+ if edges is None:
+ edges = mesh.edges
+
+ if not hasattr(edges, "pop"):
+ edges = edges[:]
+
+ while edges:
+ current_edge = edges.pop()
+ vert_end, vert_start = current_edge.vertices[:]
+ line_poly = [vert_start, vert_end]
+
+ ok = True
+ while ok:
+ ok = False
+ #for i, ed in enumerate(edges):
+ i = len(edges)
+ while i:
+ i -= 1
+ ed = edges[i]
+ v1, v2 = ed.vertices
+ if v1 == vert_end:
+ line_poly.append(v2)
+ vert_end = line_poly[-1]
+ ok = 1
+ del edges[i]
+ # break
+ elif v2 == vert_end:
+ line_poly.append(v1)
+ vert_end = line_poly[-1]
+ ok = 1
+ del edges[i]
+ #break
+ elif v1 == vert_start:
+ line_poly.insert(0, v2)
+ vert_start = line_poly[0]
+ ok = 1
+ del edges[i]
+ # break
+ elif v2 == vert_start:
+ line_poly.insert(0, v1)
+ vert_start = line_poly[0]
+ ok = 1
+ del edges[i]
+ #break
+ line_polys.append(line_poly)
+
+ return line_polys
+
+
+def ngon_tesselate(from_data, indices, fix_loops=True):
+ '''
+ Takes a polyline of indices (fgon) and returns a list of face
+ indicie lists. Designed to be used for importers that need indices for an
+ fgon to create from existing verts.
+
+ from_data: either a mesh, or a list/tuple of vectors.
+ indices: a list of indices to use this list is the ordered closed polyline
+ to fill, and can be a subset of the data given.
+ fix_loops: If this is enabled polylines that use loops to make multiple
+ polylines are delt with correctly.
+ '''
+
+ from mathutils.geometry import tesselate_polygon
+ from mathutils import Vector
+ vector_to_tuple = Vector.to_tuple
+
+ if not indices:
+ return []
+
+ def mlen(co):
+ # manhatten length of a vector, faster then length
+ return abs(co[0]) + abs(co[1]) + abs(co[2])
+
+ def vert_treplet(v, i):
+ return v, vector_to_tuple(v, 6), i, mlen(v)
+
+ def ed_key_mlen(v1, v2):
+ if v1[3] > v2[3]:
+ return v2[1], v1[1]
+ else:
+ return v1[1], v2[1]
+
+ if not fix_loops:
+ '''
+ Normal single concave loop filling
+ '''
+ if type(from_data) in (tuple, list):
+ verts = [Vector(from_data[i]) for ii, i in enumerate(indices)]
+ else:
+ verts = [from_data.vertices[i].co for ii, i in enumerate(indices)]
+
+ # same as reversed(range(1, len(verts))):
+ for i in range(len(verts) - 1, 0, -1):
+ if verts[i][1] == verts[i - 1][0]:
+ verts.pop(i - 1)
+
+ fill = tesselate_polygon([verts])
+
+ else:
+ '''
+ Seperate this loop into multiple loops be finding edges that are
+ used twice. This is used by lightwave LWO files a lot
+ '''
+
+ if type(from_data) in (tuple, list):
+ verts = [vert_treplet(Vector(from_data[i]), ii)
+ for ii, i in enumerate(indices)]
+ else:
+ verts = [vert_treplet(from_data.vertices[i].co, ii)
+ for ii, i in enumerate(indices)]
+
+ edges = [(i, i - 1) for i in range(len(verts))]
+ if edges:
+ edges[0] = (0, len(verts) - 1)
+
+ if not verts:
+ return []
+
+ edges_used = set()
+ edges_doubles = set()
+ # We need to check if any edges are used twice location based.
+ for ed in edges:
+ edkey = ed_key_mlen(verts[ed[0]], verts[ed[1]])
+ if edkey in edges_used:
+ edges_doubles.add(edkey)
+ else:
+ edges_used.add(edkey)
+
+ # Store a list of unconnected loop segments split by double edges.
+ # will join later
+ loop_segments = []
+
+ v_prev = verts[0]
+ context_loop = [v_prev]
+ loop_segments = [context_loop]
+
+ for v in verts:
+ if v != v_prev:
+ # Are we crossing an edge we removed?
+ if ed_key_mlen(v, v_prev) in edges_doubles:
+ context_loop = [v]
+ loop_segments.append(context_loop)
+ else:
+ if context_loop and context_loop[-1][1] == v[1]:
+ #raise "as"
+ pass
+ else:
+ context_loop.append(v)
+
+ v_prev = v
+ # Now join loop segments
+
+ def join_seg(s1, s2):
+ if s2[-1][1] == s1[0][1]:
+ s1, s2 = s2, s1
+ elif s1[-1][1] == s2[0][1]:
+ pass
+ else:
+ return False
+
+ # If were stuill here s1 and s2 are 2 segments in the same polyline
+ s1.pop() # remove the last vert from s1
+ s1.extend(s2) # add segment 2 to segment 1
+
+ if s1[0][1] == s1[-1][1]: # remove endpoints double
+ s1.pop()
+
+ s2[:] = [] # Empty this segment s2 so we dont use it again.
+ return True
+
+ joining_segments = True
+ while joining_segments:
+ joining_segments = False
+ segcount = len(loop_segments)
+
+ for j in range(segcount - 1, -1, -1): # reversed(range(segcount)):
+ seg_j = loop_segments[j]
+ if seg_j:
+ for k in range(j - 1, -1, -1): # reversed(range(j)):
+ if not seg_j:
+ break
+ seg_k = loop_segments[k]
+
+ if seg_k and join_seg(seg_j, seg_k):
+ joining_segments = True
+
+ loop_list = loop_segments
+
+ for verts in loop_list:
+ while verts and verts[0][1] == verts[-1][1]:
+ verts.pop()
+
+ loop_list = [verts for verts in loop_list if len(verts) > 2]
+ # DONE DEALING WITH LOOP FIXING
+
+ # vert mapping
+ vert_map = [None] * len(indices)
+ ii = 0
+ for verts in loop_list:
+ if len(verts) > 2:
+ for i, vert in enumerate(verts):
+ vert_map[i + ii] = vert[2]
+ ii += len(verts)
+
+ fill = tesselate_polygon([[v[0] for v in loop] for loop in loop_list])
+ #draw_loops(loop_list)
+ #raise 'done loop'
+ # map to original indices
+ fill = [[vert_map[i] for i in reversed(f)] for f in fill]
+
+ if not fill:
+ print('Warning Cannot scanfill, fallback on a triangle fan.')
+ fill = [[0, i - 1, i] for i in range(2, len(indices))]
+ else:
+ # Use real scanfill.
+ # See if its flipped the wrong way.
+ flip = None
+ for fi in fill:
+ if flip != None:
+ break
+ for i, vi in enumerate(fi):
+ if vi == 0 and fi[i - 1] == 1:
+ flip = False
+ break
+ elif vi == 1 and fi[i - 1] == 0:
+ flip = True
+ break
+
+ if not flip:
+ for i, fi in enumerate(fill):
+ fill[i] = tuple([ii for ii in reversed(fi)])
+
+ return fill
+
+
+def face_random_points(num_points, faces):
+ """
+ Generates a list of random points over mesh faces.
+
+ :arg num_points: the number of random points to generate on each face.
+ :type int:
+ :arg faces: list of the faces to generate points on.
+ :type faces: :class:`MeshFaces`, sequence
+ :return: list of random points over all faces.
+ :rtype: list
+ """
+
+ from random import random
+ from mathutils.geometry import area_tri
+
+ # Split all quads into 2 tris, tris remain unchanged
+ tri_faces = []
+ for f in faces:
+ tris = []
+ verts = f.id_data.vertices
+ fv = f.vertices[:]
+ tris.append((verts[fv[0]].co,
+ verts[fv[1]].co,
+ verts[fv[2]].co,
+ ))
+ if len(fv) == 4:
+ tris.append((verts[fv[0]].co,
+ verts[fv[3]].co,
+ verts[fv[2]].co,
+ ))
+ tri_faces.append(tris)
+
+ # For each face, generate the required number of random points
+ sampled_points = [None] * (num_points * len(faces))
+ for i, tf in enumerate(tri_faces):
+ for k in range(num_points):
+ # If this is a quad, we need to weight its 2 tris by their area
+ if len(tf) != 1:
+ area1 = area_tri(*tf[0])
+ area2 = area_tri(*tf[1])
+ area_tot = area1 + area2
+
+ area1 = area1 / area_tot
+ area2 = area2 / area_tot
+
+ vecs = tf[0 if (random() < area1) else 1]
+ else:
+ vecs = tf[0]
+
+ u1 = random()
+ u2 = random()
+ u_tot = u1 + u2
+
+ if u_tot > 1:
+ u1 = 1.0 - u1
+ u2 = 1.0 - u2
+
+ side1 = vecs[1] - vecs[0]
+ side2 = vecs[2] - vecs[0]
+
+ p = vecs[0] + u1 * side1 + u2 * side2
+
+ sampled_points[num_points * i + k] = p
+
+ return sampled_points
diff --git a/release/scripts/modules/bpy_extras/object_utils.py b/release/scripts/modules/bpy_extras/object_utils.py
new file mode 100644
index 00000000000..51a8d4b5e23
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/object_utils.py
@@ -0,0 +1,144 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+__all__ = (
+ "add_object_align_init",
+ "object_data_add",
+)
+
+
+import bpy
+import mathutils
+
+
+def add_object_align_init(context, operator):
+ """
+ Return a matrix using the operator settings and view context.
+
+ :arg context: The context to use.
+ :type context: :class:`Context`
+ :arg operator: The operator, checked for location and rotation properties.
+ :type operator: :class:`Operator`
+ :return: the matrix from the context and settings.
+ :rtype: :class:`Matrix`
+ """
+ space_data = context.space_data
+ if space_data.type != 'VIEW_3D':
+ space_data = None
+
+ # location
+ if operator and operator.properties.is_property_set("location"):
+ location = mathutils.Matrix.Translation(mathutils.Vector(operator.properties.location))
+ else:
+ if space_data: # local view cursor is detected below
+ location = mathutils.Matrix.Translation(space_data.cursor_location)
+ else:
+ location = mathutils.Matrix.Translation(context.scene.cursor_location)
+
+ if operator:
+ operator.properties.location = location.to_translation()
+
+ # rotation
+ view_align = (context.user_preferences.edit.object_align == 'VIEW')
+ view_align_force = False
+ if operator:
+ if operator.properties.is_property_set("view_align"):
+ view_align = view_align_force = operator.view_align
+ else:
+ operator.properties.view_align = view_align
+
+ if operator and operator.properties.is_property_set("rotation") and not view_align_force:
+ rotation = mathutils.Euler(operator.properties.rotation).to_matrix().to_4x4()
+ else:
+ if view_align and space_data:
+ rotation = space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4()
+ else:
+ rotation = mathutils.Matrix()
+
+ # set the operator properties
+ if operator:
+ operator.properties.rotation = rotation.to_euler()
+
+ return location * rotation
+
+
+def object_data_add(context, obdata, operator=None):
+ """
+ Add an object using the view context and preference to to initialize the
+ location, rotation and layer.
+
+ :arg context: The context to use.
+ :type context: :class:`Context`
+ :arg obdata: the data used for the new object.
+ :type obdata: valid object data type or None.
+ :arg operator: The operator, checked for location and rotation properties.
+ :type operator: :class:`Operator`
+ :return: the newly created object in the scene.
+ :rtype: :class:`ObjectBase`
+ """
+ scene = context.scene
+
+ # ugh, could be made nicer
+ for ob in scene.objects:
+ ob.select = False
+
+ obj_new = bpy.data.objects.new(obdata.name, obdata)
+
+ base = scene.objects.link(obj_new)
+ base.select = True
+
+ if context.space_data and context.space_data.type == 'VIEW_3D':
+ base.layers_from_view(context.space_data)
+
+ obj_new.matrix_world = add_object_align_init(context, operator)
+
+ obj_act = scene.objects.active
+
+ # XXX
+ # caused because entering editmodedoes not add a empty undo slot!
+ if context.user_preferences.edit.use_enter_edit_mode:
+ if not (obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type):
+ _obdata = bpy.data.meshes.new(obdata.name)
+ obj_act = bpy.data.objects.new(_obdata.name, _obdata)
+ obj_act.matrix_world = obj_new.matrix_world
+ scene.objects.link(obj_act)
+ scene.objects.active = obj_act
+ bpy.ops.object.mode_set(mode='EDIT')
+ bpy.ops.ed.undo_push(message="Enter Editmode") # need empty undo step
+ # XXX
+
+ if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
+ bpy.ops.mesh.select_all(action='DESELECT')
+ bpy.ops.object.mode_set(mode='OBJECT')
+
+ obj_act.select = True
+ scene.update() # apply location
+ #scene.objects.active = obj_new
+
+ bpy.ops.object.join() # join into the active.
+ bpy.data.meshes.remove(obdata)
+
+ bpy.ops.object.mode_set(mode='EDIT')
+ else:
+ scene.objects.active = obj_new
+ if context.user_preferences.edit.use_enter_edit_mode:
+ bpy.ops.object.mode_set(mode='EDIT')
+
+ return base
diff --git a/release/scripts/modules/bpy_extras/view3d_utils.py b/release/scripts/modules/bpy_extras/view3d_utils.py
new file mode 100644
index 00000000000..5796abce72c
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/view3d_utils.py
@@ -0,0 +1,128 @@
+# ##### 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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+__all__ = (
+ "region_2d_to_vector_3d",
+ "region_2d_to_location_3d",
+ "location_3d_to_region_2d",
+)
+
+
+def region_2d_to_vector_3d(region, rv3d, coord):
+ """
+ Return a direction vector from the viewport at the spesific 2d region
+ coordinate.
+
+ :arg region: region of the 3D viewport, typically bpy.context.region.
+ :type region: :class:`Region`
+ :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
+ :type rv3d: :class:`RegionView3D`
+ :arg coord: 2d coordinates relative to the region:
+ (event.mouse_region_x, event.mouse_region_y) for example.
+ :type coord: 2d vector
+ :return: normalized 3d vector.
+ :rtype: :class:`Vector`
+ """
+ from mathutils import Vector
+
+ if rv3d.is_perspective:
+ persinv = rv3d.perspective_matrix.inverted()
+
+ out = Vector(((2.0 * coord[0] / region.width) - 1.0,
+ (2.0 * coord[1] / region.height) - 1.0,
+ -0.5
+ ))
+
+ w = ((out[0] * persinv[0][3]) +
+ (out[1] * persinv[1][3]) +
+ (out[2] * persinv[2][3]) + persinv[3][3])
+
+ return ((persinv * out) / w) - rv3d.view_matrix.inverted()[3].xyz
+ else:
+ return rv3d.view_matrix.inverted()[2].xyz.normalized()
+
+
+def region_2d_to_location_3d(region, rv3d, coord, depth_location):
+ """
+ Return a 3d location from the region relative 2d coords, aligned with
+ *depth_location*.
+
+ :arg region: region of the 3D viewport, typically bpy.context.region.
+ :type region: :class:`Region`
+ :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
+ :type rv3d: :class:`RegionView3D`
+ :arg coord: 2d coordinates relative to the region;
+ (event.mouse_region_x, event.mouse_region_y) for example.
+ :type coord: 2d vector
+ :arg depth_location: the returned vectors depth is aligned with this since
+ there is no defined depth with a 2d region input.
+ :type depth_location: 3d vector
+ :return: normalized 3d vector.
+ :rtype: :class:`Vector`
+ """
+ from mathutils import Vector
+ from mathutils.geometry import intersect_point_line
+
+ persmat = rv3d.perspective_matrix.copy()
+ coord_vec = region_2d_to_vector_3d(region, rv3d, coord)
+ depth_location = Vector(depth_location)
+
+ if rv3d.is_perspective:
+ from mathutils.geometry import intersect_line_plane
+
+ origin_start = rv3d.view_matrix.inverted()[3].to_3d()
+ origin_end = origin_start + coord_vec
+ view_vec = rv3d.view_matrix.inverted()[2]
+ return intersect_line_plane(origin_start, origin_end, depth_location, view_vec, 1)
+ else:
+ dx = (2.0 * coord[0] / region.width) - 1.0
+ dy = (2.0 * coord[1] / region.height) - 1.0
+ persinv = persmat.inverted()
+ viewinv = rv3d.view_matrix.inverted()
+ origin_start = (persinv[0].xyz * dx) + (persinv[1].xyz * dy) + viewinv[3].xyz
+ origin_end = origin_start + coord_vec
+ return intersect_point_line(depth_location, origin_start, origin_end)[0]
+
+
+def location_3d_to_region_2d(region, rv3d, coord):
+ """
+ Return the *region* relative 2d location of a 3d position.
+
+ :arg region: region of the 3D viewport, typically bpy.context.region.
+ :type region: :class:`Region`
+ :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
+ :type rv3d: :class:`RegionView3D`
+ :arg coord: 3d worldspace location.
+ :type coord: 3d vector
+ :return: 2d location
+ :rtype: :class:`Vector`
+ """
+ from mathutils import Vector
+
+ prj = rv3d.perspective_matrix * Vector((coord[0], coord[1], coord[2], 1.0))
+ if prj.w > 0.0:
+ width_half = region.width / 2.0
+ height_half = region.height / 2.0
+
+ return Vector((width_half + width_half * (prj.x / prj.w),
+ height_half + height_half * (prj.y / prj.w),
+ ))
+ else:
+ return None