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`
42 space_data = context.space_data
43 if space_data.type != 'VIEW_3D':
47 if operator and operator.properties.is_property_set("location"):
48 location = mathutils.Matrix.Translation(mathutils.Vector(operator.properties.location))
50 if space_data: # local view cursor is detected below
51 location = mathutils.Matrix.Translation(space_data.cursor_location)
53 location = mathutils.Matrix.Translation(context.scene.cursor_location)
56 operator.properties.location = location.to_translation()
59 view_align = (context.user_preferences.edit.object_align == 'VIEW')
60 view_align_force = False
62 if operator.properties.is_property_set("view_align"):
63 view_align = view_align_force = operator.view_align
65 operator.properties.view_align = view_align
67 if operator and operator.properties.is_property_set("rotation") and not view_align_force:
68 rotation = mathutils.Euler(operator.properties.rotation).to_matrix().to_4x4()
70 if view_align and space_data:
71 rotation = space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4()
73 rotation = mathutils.Matrix()
75 # set the operator properties
77 operator.properties.rotation = rotation.to_euler()
79 return location * rotation
82 def object_data_add(context, obdata, operator=None):
84 Add an object using the view context and preference to to initialize the
85 location, rotation and layer.
87 :arg context: The context to use.
88 :type context: :class:`Context`
89 :arg obdata: the data used for the new object.
90 :type obdata: valid object data type or None.
91 :arg operator: The operator, checked for location and rotation properties.
92 :type operator: :class:`Operator`
93 :return: the newly created object in the scene.
94 :rtype: :class:`ObjectBase`
98 # ugh, could be made nicer
99 for ob in scene.objects:
102 obj_new = bpy.data.objects.new(obdata.name, obdata)
104 base = scene.objects.link(obj_new)
107 if context.space_data and context.space_data.type == 'VIEW_3D':
108 base.layers_from_view(context.space_data)
110 obj_new.matrix_world = add_object_align_init(context, operator)
112 obj_act = scene.objects.active
115 # caused because entering editmodedoes not add a empty undo slot!
116 if context.user_preferences.edit.use_enter_edit_mode:
117 if not (obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type):
118 _obdata = bpy.data.meshes.new(obdata.name)
119 obj_act = bpy.data.objects.new(_obdata.name, _obdata)
120 obj_act.matrix_world = obj_new.matrix_world
121 scene.objects.link(obj_act)
122 scene.objects.active = obj_act
123 bpy.ops.object.mode_set(mode='EDIT')
124 bpy.ops.ed.undo_push(message="Enter Editmode") # need empty undo step
127 if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
128 bpy.ops.mesh.select_all(action='DESELECT')
129 bpy.ops.object.mode_set(mode='OBJECT')
131 obj_act.select = True
132 scene.update() # apply location
133 #scene.objects.active = obj_new
135 bpy.ops.object.join() # join into the active.
136 bpy.data.meshes.remove(obdata)
138 bpy.ops.object.mode_set(mode='EDIT')
140 scene.objects.active = obj_new
141 if context.user_preferences.edit.use_enter_edit_mode:
142 bpy.ops.object.mode_set(mode='EDIT')