2
$\begingroup$

Is there a shortcut to make several selected objects children of a new empty object?

That would be so handy, instead of always doing a new empty and moving it somewhere and then parenting all objects to it.

$\endgroup$
1
$\begingroup$

Here is a script that create an empty and make it a parent for all selected objects, select the objects and run the script

import bpy

selected_obj = bpy.context.selected_objects.copy()
bpy.ops.object.empty_add()
for obj in selected_obj :
    obj.parent = bpy.context.object

the script at the end is in an addon for reuse , install it and you can trigger it with pressing P, you can change this in the user preferences :

  • select the objects
  • press P
  • select the Empty type ( axis, arrow, cube ...)
  • select the empty position ( world center, median point, at cursor, active object )
  • check parent inverse if you want to set the inverse
  • you can set the name of the empty in name field

enter image description here

bl_info = {
"name": "Add a parent",
"author": "Chebhou",
"version": (1, 0),
"blender": (2, 65, 0),
"description": "Adds a parent for the selected objects",
"category": "Object"}


import bpy
import sys
from bpy.props import BoolProperty, EnumProperty, StringProperty

def add_parent(self, context):
    selected_obj = bpy.context.selected_objects.copy()
    if self.position != 'None' :
        exec("bpy.ops.view3d.snap_cursor_to_%s()"%self.position)
    bpy.ops.object.empty_add(type = self.type)
    bpy.context.object.name = self.name
    inv_mat = bpy.context.object.matrix_world.inverted()
    for obj in selected_obj :
        obj.parent = bpy.context.object
        if self.inverse :
            obj.matrix_parent_inverse = inv_mat

class AddParent(bpy.types.Operator):
    """Create a new parent"""
    bl_idname = "object.add_parent"
    bl_label = "Add a parent"
    bl_options = {'REGISTER', 'UNDO'}

    type  = EnumProperty(
        name="empty type",
        description="choose the empty type",
        items=(('PLAIN_AXES', "Axis", "Axis"),
                ('ARROWS', "Arrows", "Arrows"),
                ('SINGLE_ARROW', "Single arrow", "Single arrow"),
                ('CIRCLE', "Circle", "Circle"),
                ('CUBE', "Cube", "Cube"),
               ('SPHERE', "Sphere", "Sphere"),
               ('CONE', "Cone", "Cone")),
        default='PLAIN_AXES'
        )

    inverse = BoolProperty(                        
                name = "parent inverse",                          
                default = 0,                
                description = "check to set the inverse"
                )


    position  = EnumProperty(
                name="parent position",
                description="where to create the parent",
                items=(('center', "World center", "World center"),
                       ('None', "Cursor position", "Cursor position"),
                       ('selected', "Median point", "Median position"),
                       ('active', "Active object position", "Active position"),),
                default='center'
                )

    name      = StringProperty(
                name ="name",
                default ="Parent")

    def execute(self, context):
        add_parent(self, context)
        return {'FINISHED'}


addon_keymaps = []

def register():
    bpy.utils.register_class(AddParent)

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
        kmi = km.keymap_items.new(AddParent.bl_idname, 'P', 'PRESS')# you can chnge the shortcut later
        addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(AddParent)

    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()

if __name__ == "__main__":
    register()

link to Addon file

| improve this answer | |
$\endgroup$
0
$\begingroup$

without the ops:

import bpy

context = bpy.context
scene = context.scene
objects = bpy.data.objects

selected_objects = context.selected_objects.copy()
mt = objects.new('parent_empty', None)
scene.objects.link(mt)

for o in selected_objects:
    o.parent = mt

You'd need to wrap this in operator /addon code so you could trigger it from a shortcut or menu, permanently..

Templates for Operators can be found in: TextEditor -> Templates -> Python -> Operator Simple

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.