Changeset View
Changeset View
Standalone View
Standalone View
release/scripts/modules/bpy_extras/object_utils.py
| Show First 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | else: | ||||
| # set the operator properties | # set the operator properties | ||||
| if operator: | if operator: | ||||
| properties.rotation = rotation.to_euler() | properties.rotation = rotation.to_euler() | ||||
| return location * rotation | return location * rotation | ||||
| def object_data_add(context, obdata, operator=None, use_active_layer=True, name=None): | def object_data_add(context, obdata, operator=None, name=None): | ||||
| """ | """ | ||||
| Add an object using the view context and preference to to initialize the | Add an object using the view context and preference to initialize the | ||||
| location, rotation and layer. | location, rotation and layer. | ||||
| :arg context: The context to use. | :arg context: The context to use. | ||||
| :type context: :class:`bpy.types.Context` | :type context: :class:`bpy.types.Context` | ||||
| :arg obdata: the data used for the new object. | :arg obdata: the data used for the new object. | ||||
| :type obdata: valid object data type or None. | :type obdata: valid object data type or None. | ||||
| :arg operator: The operator, checked for location and rotation properties. | :arg operator: The operator, checked for location and rotation properties. | ||||
| :type operator: :class:`bpy.types.Operator` | :type operator: :class:`bpy.types.Operator` | ||||
| :arg name: Optional name | :arg name: Optional name | ||||
| :type name: string | :type name: string | ||||
| :return: the newly created object in the scene. | :return: the newly created object in the scene. | ||||
| :rtype: :class:`bpy.types.ObjectBase` | :rtype: :class:`bpy.types.Object` | ||||
| """ | """ | ||||
| scene = context.scene | scene = context.scene | ||||
| layer = context.render_layer | |||||
| scene_collection = context.scene_collection | |||||
| # ugh, could be made nicer | bpy.ops.object.select_all(action='DESELECT') | ||||
| for ob in scene.objects: | |||||
| ob.select = False | |||||
| if name is None: | if name is None: | ||||
| name = "Object" if obdata is None else obdata.name | name = "Object" if obdata is None else obdata.name | ||||
| obj_new = bpy.data.objects.new(name, obdata) | obj_new = bpy.data.objects.new(name, obdata) | ||||
| scene_collection.objects.link(obj_new) | |||||
| base = scene.objects.link(obj_new) | obj_new.select = True | ||||
| base.select = True | |||||
| v3d = None | |||||
| if context.space_data and context.space_data.type == 'VIEW_3D': | |||||
| v3d = context.space_data | |||||
| if v3d and v3d.local_view: | |||||
| base.layers_from_view(context.space_data) | |||||
| if operator is not None and any(operator.layers): | |||||
| base.layers = operator.layers | |||||
| else: | |||||
| if use_active_layer: | |||||
| if v3d and v3d.local_view: | |||||
| base.layers[scene.active_layer] = True | |||||
| else: | |||||
| if v3d and not v3d.lock_camera_and_layers: | |||||
| base.layers = [True if i == v3d.active_layer | |||||
| else False for i in range(len(v3d.layers))] | |||||
| else: | |||||
| base.layers = [True if i == scene.active_layer | |||||
| else False for i in range(len(scene.layers))] | |||||
| else: | |||||
| if v3d: | |||||
| base.layers_from_view(context.space_data) | |||||
| if operator is not None: | |||||
| operator.layers = base.layers | |||||
| obj_new.matrix_world = add_object_align_init(context, operator) | obj_new.matrix_world = add_object_align_init(context, operator) | ||||
| obj_act = scene.objects.active | obj_act = layer.objects.active | ||||
| # XXX | # XXX | ||||
| # caused because entering edit-mode does not add a empty undo slot! | # caused because entering edit-mode does not add a empty undo slot! | ||||
| if context.user_preferences.edit.use_enter_edit_mode: | if context.user_preferences.edit.use_enter_edit_mode: | ||||
| if not (obj_act and | if not (obj_act and | ||||
| obj_act.mode == 'EDIT' and | obj_act.mode == 'EDIT' and | ||||
| obj_act.type == obj_new.type): | obj_act.type == obj_new.type): | ||||
| _obdata = bpy.data.meshes.new(name) | _obdata = bpy.data.meshes.new(name) | ||||
| obj_act = bpy.data.objects.new(_obdata.name, _obdata) | obj_act = bpy.data.objects.new(_obdata.name, _obdata) | ||||
| obj_act.matrix_world = obj_new.matrix_world | obj_act.matrix_world = obj_new.matrix_world | ||||
| scene.objects.link(obj_act) | scene_collection.objects.link(obj_act) | ||||
| scene.objects.active = obj_act | layer.objects.active = obj_act | ||||
| bpy.ops.object.mode_set(mode='EDIT') | bpy.ops.object.mode_set(mode='EDIT') | ||||
| # need empty undo step | # need empty undo step | ||||
| bpy.ops.ed.undo_push(message="Enter Editmode") | bpy.ops.ed.undo_push(message="Enter Editmode") | ||||
| # XXX | # XXX | ||||
| if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type: | if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type: | ||||
| bpy.ops.mesh.select_all(action='DESELECT') | bpy.ops.mesh.select_all(action='DESELECT') | ||||
| bpy.ops.object.mode_set(mode='OBJECT') | bpy.ops.object.mode_set(mode='OBJECT') | ||||
| obj_act.select = True | obj_act.select = True | ||||
| scene.update() # apply location | scene.update() # apply location | ||||
| # scene.objects.active = obj_new | # scene.objects.active = obj_new | ||||
| # Match up UV layers, this is needed so adding an object with UV's | # Match up UV layers, this is needed so adding an object with UV's | ||||
| # doesn't create new layers when there happens to be a naming mis-match. | # doesn't create new layers when there happens to be a naming mis-match. | ||||
| uv_new = obdata.uv_layers.active | uv_new = obdata.uv_layers.active | ||||
| if uv_new is not None: | if uv_new is not None: | ||||
| uv_act = obj_act.data.uv_layers.active | uv_act = obj_act.data.uv_layers.active | ||||
| if uv_act is not None: | if uv_act is not None: | ||||
| uv_new.name = uv_act.name | uv_new.name = uv_act.name | ||||
| bpy.ops.object.join() # join into the active. | bpy.ops.object.join() # join into the active. | ||||
| if obdata: | if obdata: | ||||
| bpy.data.meshes.remove(obdata) | bpy.data.meshes.remove(obdata) | ||||
| # base is freed, set to active object | |||||
| base = scene.object_bases.active | |||||
| bpy.ops.object.mode_set(mode='EDIT') | bpy.ops.object.mode_set(mode='EDIT') | ||||
| else: | else: | ||||
| scene.objects.active = obj_new | layer.objects.active = obj_new | ||||
| if context.user_preferences.edit.use_enter_edit_mode: | if context.user_preferences.edit.use_enter_edit_mode: | ||||
| bpy.ops.object.mode_set(mode='EDIT') | bpy.ops.object.mode_set(mode='EDIT') | ||||
| return base | return obj_new | ||||
| class AddObjectHelper: | class AddObjectHelper: | ||||
| def view_align_update_callback(self, context): | def view_align_update_callback(self, context): | ||||
| if not self.view_align: | if not self.view_align: | ||||
| self.rotation.zero() | self.rotation.zero() | ||||
| view_align = BoolProperty( | view_align = BoolProperty( | ||||
| name="Align to View", | name="Align to View", | ||||
| default=False, | default=False, | ||||
| update=view_align_update_callback, | update=view_align_update_callback, | ||||
| ) | ) | ||||
| location = FloatVectorProperty( | location = FloatVectorProperty( | ||||
| name="Location", | name="Location", | ||||
| subtype='TRANSLATION', | subtype='TRANSLATION', | ||||
| ) | ) | ||||
| rotation = FloatVectorProperty( | rotation = FloatVectorProperty( | ||||
| name="Rotation", | name="Rotation", | ||||
| subtype='EULER', | subtype='EULER', | ||||
| ) | ) | ||||
| layers = BoolVectorProperty( | |||||
| name="Layers", | |||||
| size=20, | |||||
| subtype='LAYER', | |||||
| options={'HIDDEN', 'SKIP_SAVE'}, | |||||
| ) | |||||
| @classmethod | @classmethod | ||||
| def poll(self, context): | def poll(self, context): | ||||
| return context.scene.library is None | return context.scene.library is None | ||||
| def object_add_grid_scale(context): | def object_add_grid_scale(context): | ||||
| """ | """ | ||||
| ▲ Show 20 Lines • Show All 111 Lines • Show Last 20 Lines | |||||