Skip to content

Commit

Permalink
v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
leBluem authored Aug 4, 2022
1 parent e1573eb commit d842229
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
## Blender Addon to Add empty(ies) as Parent

thanks @chebhou for the base Addon https://github.com/chebhou/Add-Empty-Parent

- download: https://github.com/leBluem/Add-Empty-Parent/releases/tag/Add-Empty-Parent
- download: https://github.com/leBluem/Add-Empty-Parent/releases/tag/Add-Empty-Parent

The script creates an empty (those things with axis and no mesh-data) and sets it as a parent for the selected objects. After installing you can trigger it with pressing <kbd>P</kbd>, you can change this shortcut in the user preferences later.

v1.3:
-works with blender >=2.8, access also through "Add" menu
-new objects will be selected

***How to install :***
- goto Blender -> Edit -> preferences -> addons
- on the top click "Install", then browse for the downloaded zip file and click install
- you should find it now in the Addon-list as
- you should find it now in the Addon-list as
"Object: Add an empty as parent"

***How to use :***
Expand All @@ -26,6 +27,8 @@ The script creates an empty (those things with axis and no mesh-data) and sets i
- you can set the name of the empty in ***name*** field
- to use names of selected objects as base set ***use childname***

thanks @chebhou for the base Addon https://github.com/chebhou/Add-Empty-Parent

***example***

note: all three planes were selected before, "P" on keyboard shows this
Expand Down
78 changes: 48 additions & 30 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
bl_info = {
"name": "Add an empty as parent",
"author": "Chebhou, leBluem",
"version": (1, 1),
"blender": (2, 80, 0),
"description": "Adds a parent for the selected objects",
"category": "Object",
"url": "https://github.com/leBluem/Add-Empty-Parent/"
"version": (1,3),
"blender": (2,80,0),
"description": "Adds a parent for the selected objects, def. shortcut: P",
"location": "View3D > Add",
"category": "Add",
"doc_url": "https://github.com/leBluem/Add-Empty-Parent"
}

import bpy
import sys
import bpy, sys
from bpy.props import BoolProperty, EnumProperty, StringProperty, FloatProperty
from bpy.types import AddonPreferences, Operator, Panel, Menu

def add_parent(self, context):
selected_obj = bpy.context.selected_objects.copy()
added_objs = []

if len(selected_obj)>0:

if self.originFirst==1:
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')

if self.position != 'None' :
exec("bpy.ops.view3d.snap_cursor_to_%s()"%self.position)
exec("bpy.ops.view3d.snap_cursor_to_%s()"%str(self.position))

if self.individuell==0:
bpy.ops.object.empty_add(type = self.type)
Expand All @@ -29,27 +32,39 @@ def add_parent(self, context):
bpy.context.object.name = selected_obj[0].name + self.name
else:
bpy.context.object.name = self.name
# bpy.ops.transform.resize(value=(self.scaling, self.scaling, self.scaling))
# bpy.ops.transform.resize(value=(self.scaling, self.scaling, self.scaling))

added_objs.append(bpy.context.object.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

for s in added_objs:
bpy.data.objects[ s ].select_set(True)

else:
cnt=0
for obj in selected_obj :
bpy.context.view_layer.objects.active = bpy.data.objects[obj.name]
bpy.ops.object.empty_add(type = self.type, location=obj.location)
print ( str(self.type) )
bpy.ops.object.empty_add(type = str(self.type), location = obj.location)
bpy.ops.transform.resize(value=(self.scaling, self.scaling, self.scaling))
if self.usechildname==1:
bpy.context.object.name = selected_obj[0].name + self.name
else:
bpy.context.object.name = self.name + str(cnt)
added_objs.append(bpy.context.object.name)

inv_mat = bpy.context.object.matrix_world.inverted()
obj.parent = bpy.context.object
if self.inverse :
obj.matrix_parent_inverse = inv_mat

for s in added_objs:
bpy.data.objects[ s ].select_set(True)

cnt=cnt+1
else:
print('Nothing selected!')
Expand All @@ -60,61 +75,60 @@ class AddEmptyAsParent(bpy.types.Operator):
bl_label = "Add empty as parent"
bl_options = {'REGISTER', 'UNDO'}

type = EnumProperty(
type : EnumProperty(
name="empty type",
description="choose the empty type",
items=(('PLAIN_AXES', "Axis", "Axis"),
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")),
('CONE', "Cone", "Cone")],
default='PLAIN_AXES'
)
)

position = EnumProperty(
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"),),
items=[('center', "World center", "World center"),
('None', "Cursor position", "Cursor position"),
('selected', "Median point", "Median position"),
('active', "Active object position", "Active position")],
default='active'
)

individuell = BoolProperty(
individuell : BoolProperty(
name = "one parent per object",
default = 1,
description = "uncheck to set one parent for all selected"
)

originFirst = BoolProperty(
originFirst : BoolProperty(
name = "set origin to object first",
default = 1,
description = "uncheck to keep object origins"
)

inverse = BoolProperty(
inverse : BoolProperty(
name = "parent inverse",
default = 1,
description = "uncheck to set to center"
)

scaling = FloatProperty(
scaling : FloatProperty(
name="Scaling",
description="Scaling of Empty",
min=0.0,
max=100.0,
# unit='SIZE',
default=0.01
)

name = StringProperty(
name : StringProperty(
name ="name",
default ="AC_POBJECT_")

usechildname = BoolProperty(
usechildname : BoolProperty(
name = "use childname",
default = 0,
description = "check to use objectname + string above as empty name"
Expand All @@ -124,21 +138,24 @@ def execute(self, context):
add_parent(self, context)
return {'FINISHED'}

def VIEW3D_addempty_Menu(self, context):
self.layout.operator(AddEmptyAsParent.bl_idname)

addon_keymaps = []

# Register
classes = [
classes = (
AddEmptyAsParent
]
)

def register():
bpy.utils.register_class(AddEmptyAsParent)
wm = bpy.context.window_manager
bpy.types.VIEW3D_MT_add.append(VIEW3D_addempty_Menu)
kc = wm.keyconfigs.addon
# you can change the shortcut in preferences
if kc:
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
# you can chnge the shortcut later
kmi = km.keymap_items.new(AddEmptyAsParent.bl_idname, 'P', 'PRESS')
addon_keymaps.append((km, kmi))

Expand All @@ -147,6 +164,7 @@ def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.types.VIEW3D_MT_add.remove(VIEW3D_addempty_Menu)

if __name__ == "__main__":
register()

0 comments on commit d842229

Please sign in to comment.