| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | # ##### 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> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-28 07:47:58 +00:00
										 |  |  | __all__ = ( | 
					
						
							|  |  |  |     "add_object_align_init", | 
					
						
							|  |  |  |     "object_data_add", | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | import bpy | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | import mathutils | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 19:12:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 17:18:16 +00:00
										 |  |  | def add_object_align_init(context, operator): | 
					
						
							| 
									
										
										
										
											2011-05-28 09:34:45 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     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` | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |     space_data = context.space_data | 
					
						
							|  |  |  |     if space_data.type != 'VIEW_3D': | 
					
						
							|  |  |  |         space_data = None | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |     # location | 
					
						
							|  |  |  |     if operator and operator.properties.is_property_set("location"): | 
					
						
							| 
									
										
										
										
											2010-08-11 16:40:36 +00:00
										 |  |  |         location = mathutils.Matrix.Translation(mathutils.Vector(operator.properties.location)) | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |         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: | 
					
						
							| 
									
										
										
										
											2011-02-05 07:04:23 +00:00
										 |  |  |             operator.properties.location = location.to_translation() | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |     # 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-26 14:52:00 +00:00
										 |  |  |     if operator and operator.properties.is_property_set("rotation") and not view_align_force: | 
					
						
							| 
									
										
										
										
											2011-02-05 07:04:23 +00:00
										 |  |  |         rotation = mathutils.Euler(operator.properties.rotation).to_matrix().to_4x4() | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         if view_align and space_data: | 
					
						
							| 
									
										
										
										
											2011-02-05 07:04:23 +00:00
										 |  |  |             rotation = space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4() | 
					
						
							| 
									
										
										
										
											2010-05-30 17:18:16 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             rotation = mathutils.Matrix() | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 17:18:16 +00:00
										 |  |  |         # set the operator properties | 
					
						
							|  |  |  |         if operator: | 
					
						
							|  |  |  |             operator.properties.rotation = rotation.to_euler() | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 17:18:16 +00:00
										 |  |  |     return location * rotation | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 17:18:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-08 11:42:11 +00:00
										 |  |  | def object_data_add(context, obdata, operator=None): | 
					
						
							| 
									
										
										
										
											2011-05-28 09:34:45 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     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` | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  |     scene = context.scene | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # ugh, could be made nicer | 
					
						
							|  |  |  |     for ob in scene.objects: | 
					
						
							| 
									
										
										
										
											2010-07-15 16:56:04 +00:00
										 |  |  |         ob.select = False | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     obj_new = bpy.data.objects.new(obdata.name, obdata) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     base = scene.objects.link(obj_new) | 
					
						
							| 
									
										
										
										
											2010-07-15 16:56:04 +00:00
										 |  |  |     base.select = True | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if context.space_data and context.space_data.type == 'VIEW_3D': | 
					
						
							|  |  |  |         base.layers_from_view(context.space_data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-03 17:39:29 +00:00
										 |  |  |     obj_new.matrix_world = add_object_align_init(context, operator) | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     obj_act = scene.objects.active | 
					
						
							| 
									
										
										
										
											2011-02-25 16:06:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-22 02:47:59 +00:00
										 |  |  |     # 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 | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type: | 
					
						
							| 
									
										
										
										
											2011-01-12 15:45:00 +00:00
										 |  |  |         bpy.ops.mesh.select_all(action='DESELECT') | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  |         bpy.ops.object.mode_set(mode='OBJECT') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 16:56:04 +00:00
										 |  |  |         obj_act.select = True | 
					
						
							| 
									
										
										
										
											2010-09-07 15:17:42 +00:00
										 |  |  |         scene.update()  # apply location | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  |         #scene.objects.active = obj_new | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-07 15:17:42 +00:00
										 |  |  |         bpy.ops.object.join()  # join into the active. | 
					
						
							| 
									
										
										
										
											2011-02-22 02:47:59 +00:00
										 |  |  |         bpy.data.meshes.remove(obdata) | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         bpy.ops.object.mode_set(mode='EDIT') | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         scene.objects.active = obj_new | 
					
						
							| 
									
										
										
										
											2010-08-17 13:14:41 +00:00
										 |  |  |         if context.user_preferences.edit.use_enter_edit_mode: | 
					
						
							| 
									
										
										
										
											2010-05-23 12:14:07 +00:00
										 |  |  |             bpy.ops.object.mode_set(mode='EDIT') | 
					
						
							| 
									
										
										
										
											2010-05-23 12:38:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return base |