1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 "add_object_align_init",
31 def add_object_align_init(context, operator):
33 Return a matrix using the operator settings and view context.
35 :arg context: The context to use.
36 :type context: :class:`Context`
37 :arg operator: The operator, checked for location and rotation properties.
38 :type operator: :class:`Operator`
39 :return: the matrix from the context and settings.
40 :rtype: :class:`Matrix`
43 from mathutils import Matrix, Vector, Euler
44 properties = operator.properties if operator is not None else None
46 space_data = context.space_data
47 if space_data.type != 'VIEW_3D':
51 if operator and properties.is_property_set("location"):
52 location = Matrix.Translation(Vector(properties.location))
54 if space_data: # local view cursor is detected below
55 location = Matrix.Translation(space_data.cursor_location)
57 location = Matrix.Translation(context.scene.cursor_location)
60 properties.location = location.to_translation()
63 view_align = (context.user_preferences.edit.object_align == 'VIEW')
64 view_align_force = False
66 if properties.is_property_set("view_align"):
67 view_align = view_align_force = operator.view_align
69 properties.view_align = view_align
71 if operator and (properties.is_property_set("rotation") and
72 not view_align_force):
74 rotation = Euler(properties.rotation).to_matrix().to_4x4()
76 if view_align and space_data:
77 rotation = space_data.region_3d.view_matrix.to_3x3().inverted()
80 rotation = mathutils.Matrix()
82 # set the operator properties
84 properties.rotation = rotation.to_euler()
86 return location * rotation
89 def object_data_add(context, obdata, operator=None):
91 Add an object using the view context and preference to to initialize the
92 location, rotation and layer.
94 :arg context: The context to use.
95 :type context: :class:`Context`
96 :arg obdata: the data used for the new object.
97 :type obdata: valid object data type or None.
98 :arg operator: The operator, checked for location and rotation properties.
99 :type operator: :class:`Operator`
100 :return: the newly created object in the scene.
101 :rtype: :class:`ObjectBase`
103 scene = context.scene
105 # ugh, could be made nicer
106 for ob in scene.objects:
109 obj_new = bpy.data.objects.new(obdata.name, obdata)
111 base = scene.objects.link(obj_new)
114 if context.space_data and context.space_data.type == 'VIEW_3D':
115 base.layers_from_view(context.space_data)
117 obj_new.matrix_world = add_object_align_init(context, operator)
119 obj_act = scene.objects.active
122 # caused because entering editmodedoes not add a empty undo slot!
123 if context.user_preferences.edit.use_enter_edit_mode:
125 obj_act.mode == 'EDIT' and
126 obj_act.type == obj_new.type):
128 _obdata = bpy.data.meshes.new(obdata.name)
129 obj_act = bpy.data.objects.new(_obdata.name, _obdata)
130 obj_act.matrix_world = obj_new.matrix_world
131 scene.objects.link(obj_act)
132 scene.objects.active = obj_act
133 bpy.ops.object.mode_set(mode='EDIT')
134 # need empty undo step
135 bpy.ops.ed.undo_push(message="Enter Editmode")
138 if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
139 bpy.ops.mesh.select_all(action='DESELECT')
140 bpy.ops.object.mode_set(mode='OBJECT')
142 obj_act.select = True
143 scene.update() # apply location
144 #scene.objects.active = obj_new
146 bpy.ops.object.join() # join into the active.
147 bpy.data.meshes.remove(obdata)
149 bpy.ops.object.mode_set(mode='EDIT')
151 scene.objects.active = obj_new
152 if context.user_preferences.edit.use_enter_edit_mode:
153 bpy.ops.object.mode_set(mode='EDIT')