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 #####
25 def add_object_align_init(context, operator):
26 space_data = context.space_data
27 if space_data.type != 'VIEW_3D':
31 if operator and operator.properties.is_property_set("location"):
32 location = mathutils.Matrix.Translation(mathutils.Vector(operator.properties.location))
34 if space_data: # local view cursor is detected below
35 location = mathutils.Matrix.Translation(space_data.cursor_location)
37 location = mathutils.Matrix.Translation(context.scene.cursor_location)
40 operator.properties.location = location.to_translation()
43 view_align = (context.user_preferences.edit.object_align == 'VIEW')
44 view_align_force = False
46 if operator.properties.is_property_set("view_align"):
47 view_align = view_align_force = operator.view_align
49 operator.properties.view_align = view_align
51 if operator and operator.properties.is_property_set("rotation") and not view_align_force:
52 rotation = mathutils.Euler(operator.properties.rotation).to_matrix().to_4x4()
54 if view_align and space_data:
55 rotation = space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4()
57 rotation = mathutils.Matrix()
59 # set the operator properties
61 operator.properties.rotation = rotation.to_euler()
63 return location * rotation
66 def object_data_add(context, obdata, operator=None):
70 # ugh, could be made nicer
71 for ob in scene.objects:
74 obj_new = bpy.data.objects.new(obdata.name, obdata)
76 base = scene.objects.link(obj_new)
79 if context.space_data and context.space_data.type == 'VIEW_3D':
80 base.layers_from_view(context.space_data)
82 obj_new.matrix_world = add_object_align_init(context, operator)
84 obj_act = scene.objects.active
87 # caused because entering editmodedoes not add a empty undo slot!
88 if context.user_preferences.edit.use_enter_edit_mode:
89 if not (obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type):
90 _obdata = bpy.data.meshes.new(obdata.name)
91 obj_act = bpy.data.objects.new(_obdata.name, _obdata)
92 obj_act.matrix_world = obj_new.matrix_world
93 scene.objects.link(obj_act)
94 scene.objects.active = obj_act
95 bpy.ops.object.mode_set(mode='EDIT')
96 bpy.ops.ed.undo_push(message="Enter Editmode") # need empty undo step
99 if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
100 bpy.ops.mesh.select_all(action='DESELECT')
101 bpy.ops.object.mode_set(mode='OBJECT')
103 obj_act.select = True
104 scene.update() # apply location
105 #scene.objects.active = obj_new
107 bpy.ops.object.join() # join into the active.
108 bpy.data.meshes.remove(obdata)
110 bpy.ops.object.mode_set(mode='EDIT')
112 scene.objects.active = obj_new
113 if context.user_preferences.edit.use_enter_edit_mode:
114 bpy.ops.object.mode_set(mode='EDIT')