Merge branch 'blender-v2.81-release'
[blender-addons.git] / add_mesh_extra_objects / add_empty_as_parent.py
blobda4295ffde6cb10e71f2651f4c7aa98dc535b332
1 # GPL # Original Author Liero #
3 import bpy
4 from bpy.types import Operator
5 from bpy.props import (
6 StringProperty,
7 BoolProperty,
8 EnumProperty,
12 def centro(sel):
13 x = sum([obj.location[0] for obj in sel]) / len(sel)
14 y = sum([obj.location[1] for obj in sel]) / len(sel)
15 z = sum([obj.location[2] for obj in sel]) / len(sel)
16 return (x, y, z)
19 class P2E(Operator):
20 bl_idname = "object.parent_to_empty"
21 bl_label = "Parent to Empty"
22 bl_description = "Parent selected objects to a new Empty"
23 bl_options = {"REGISTER", "UNDO"}
25 nombre: StringProperty(
26 name="",
27 default='OBJECTS',
28 description='Give the empty / group a name'
30 grupo: BoolProperty(
31 name="Create Group",
32 default=False,
33 description="Also add objects to a group"
35 locat: EnumProperty(
36 name='',
37 items=[('CURSOR', 'Cursor', 'Cursor'), ('ACTIVE', 'Active', 'Active'),
38 ('CENTER', 'Center', 'Selection Center')],
39 description='Empty location',
40 default='CENTER'
42 renom: BoolProperty(
43 name="Add Prefix",
44 default=False,
45 description="Add prefix to objects name"
48 @classmethod
49 def poll(cls, context):
50 objs = context.selected_objects
51 return (len(objs) > 0)
53 def draw(self, context):
54 layout = self.layout
55 layout.prop(self, "nombre")
56 column = layout.column(align=True)
57 column.prop(self, "locat")
58 column.prop(self, "grupo")
59 column.prop(self, "renom")
61 def execute(self, context):
62 objs = context.selected_objects
63 act = context.object
64 sce = context.scene
66 try:
67 bpy.ops.object.mode_set()
68 except:
69 pass
71 if self.locat == 'CURSOR':
72 loc = sce.cursor.location
73 elif self.locat == 'ACTIVE':
74 loc = act.location
75 else:
76 loc = centro(objs)
78 bpy.ops.object.add(type='EMPTY', location=loc)
79 context.object.name = self.nombre
80 context.object.show_name = True
81 context.object.show_in_front = True
83 if self.grupo:
84 bpy.ops.collection.create(name=self.nombre)
85 bpy.ops.collection.objects_add_active()
87 for o in objs:
88 o.select_set(True)
89 if not o.parent:
90 bpy.ops.object.parent_set(type='OBJECT')
91 if self.grupo:
92 bpy.ops.collection.objects_add_active()
93 o.select_set(False)
94 for o in objs:
95 if self.renom:
96 o.name = self.nombre + '_' + o.name
97 return {'FINISHED'}
100 class PreFix(Operator):
101 bl_idname = "object.toggle_prefix"
102 bl_label = "Toggle Sufix"
103 bl_description = "Toggle parent name as sufix for c"
104 bl_options = {"REGISTER", "UNDO"}
106 @classmethod
107 def poll(cls, context):
108 act = context.object
109 return (act and act.type == 'EMPTY')
111 def execute(self, context):
112 act = context.object
113 objs = act.children
114 prefix = act.name + '_'
115 remove = False
116 for o in objs:
117 if o.name.startswith(prefix):
118 remove = True
119 break
121 if remove is True:
122 for o in objs:
123 if o.name.startswith(prefix):
124 o.name = o.name.partition(prefix)[2]
125 else:
126 for o in objs:
127 o.name = prefix + o.name
129 return {'FINISHED'}