Cleanup: quiet float argument to in type warning
[blender-addons.git] / add_mesh_extra_objects / add_empty_as_parent.py
blob62b7751c15eb86a391f4123e8d5b1e18e76140e4
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Original Author Liero
5 import bpy
6 from bpy.types import Operator
7 from bpy.props import (
8 StringProperty,
9 BoolProperty,
10 EnumProperty,
14 def centro(sel):
15 x = sum([obj.location[0] for obj in sel]) / len(sel)
16 y = sum([obj.location[1] for obj in sel]) / len(sel)
17 z = sum([obj.location[2] for obj in sel]) / len(sel)
18 return (x, y, z)
21 class P2E(Operator):
22 bl_idname = "object.parent_to_empty"
23 bl_label = "Parent to Empty"
24 bl_description = "Parent selected objects to a new Empty"
25 bl_options = {"REGISTER", "UNDO"}
27 nombre: StringProperty(
28 name="",
29 default='OBJECTS',
30 description='Give the empty / group a name'
32 grupo: BoolProperty(
33 name="Create Group",
34 default=False,
35 description="Also add objects to a group"
37 locat: EnumProperty(
38 name='',
39 items=[('CURSOR', 'Cursor', 'Cursor'), ('ACTIVE', 'Active', 'Active'),
40 ('CENTER', 'Center', 'Selection Center')],
41 description='Empty location',
42 default='CENTER'
44 renom: BoolProperty(
45 name="Add Prefix",
46 default=False,
47 description="Add prefix to objects name"
50 @classmethod
51 def poll(cls, context):
52 objs = context.selected_objects
53 return (len(objs) > 0)
55 def draw(self, context):
56 layout = self.layout
57 layout.prop(self, "nombre")
58 column = layout.column(align=True)
59 column.prop(self, "locat")
60 column.prop(self, "grupo")
61 column.prop(self, "renom")
63 def execute(self, context):
64 objs = context.selected_objects
65 act = context.object
66 sce = context.scene
68 try:
69 bpy.ops.object.mode_set()
70 except:
71 pass
73 if self.locat == 'CURSOR':
74 loc = sce.cursor.location
75 elif self.locat == 'ACTIVE':
76 loc = act.location
77 else:
78 loc = centro(objs)
80 bpy.ops.object.add(type='EMPTY', location=loc)
81 context.object.name = self.nombre
82 context.object.show_name = True
83 context.object.show_in_front = True
85 if self.grupo:
86 bpy.ops.collection.create(name=self.nombre)
87 bpy.ops.collection.objects_add_active()
89 for o in objs:
90 o.select_set(True)
91 if not o.parent:
92 bpy.ops.object.parent_set(type='OBJECT')
93 if self.grupo:
94 bpy.ops.collection.objects_add_active()
95 o.select_set(False)
96 for o in objs:
97 if self.renom:
98 o.name = self.nombre + '_' + o.name
99 return {'FINISHED'}
102 class PreFix(Operator):
103 bl_idname = "object.toggle_prefix"
104 bl_label = "Toggle Sufix"
105 bl_description = "Toggle parent name as sufix for c"
106 bl_options = {"REGISTER", "UNDO"}
108 @classmethod
109 def poll(cls, context):
110 act = context.object
111 return (act and act.type == 'EMPTY')
113 def execute(self, context):
114 act = context.object
115 objs = act.children
116 prefix = act.name + '_'
117 remove = False
118 for o in objs:
119 if o.name.startswith(prefix):
120 remove = True
121 break
123 if remove is True:
124 for o in objs:
125 if o.name.startswith(prefix):
126 o.name = o.name.partition(prefix)[2]
127 else:
128 for o in objs:
129 o.name = prefix + o.name
131 return {'FINISHED'}