Fix #100973: Node Wrangler: Previewing node if hierarchy not active
[blender-addons.git] / add_mesh_extra_objects / add_empty_as_parent.py
blobd7f30f188f8064bf4080ede4f7d05bc7f3e34845
1 # SPDX-FileCopyrightText: 2015-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Original Author Liero
7 import bpy
8 from bpy.types import Operator
9 from bpy.props import (
10 StringProperty,
11 BoolProperty,
12 EnumProperty,
16 def centro(sel):
17 x = sum([obj.location[0] for obj in sel]) / len(sel)
18 y = sum([obj.location[1] for obj in sel]) / len(sel)
19 z = sum([obj.location[2] for obj in sel]) / len(sel)
20 return (x, y, z)
23 class P2E(Operator):
24 bl_idname = "object.parent_to_empty"
25 bl_label = "Parent to Empty"
26 bl_description = "Parent selected objects to a new Empty"
27 bl_options = {"REGISTER", "UNDO"}
29 nombre: StringProperty(
30 name="",
31 default='OBJECTS',
32 description='Give the empty / group a name'
34 grupo: BoolProperty(
35 name="Create Group",
36 default=False,
37 description="Also add objects to a group"
39 locat: EnumProperty(
40 name='',
41 items=[('CURSOR', 'Cursor', 'Cursor'), ('ACTIVE', 'Active', 'Active'),
42 ('CENTER', 'Center', 'Selection Center')],
43 description='Empty location',
44 default='CENTER'
46 renom: BoolProperty(
47 name="Add Prefix",
48 default=False,
49 description="Add prefix to objects name"
52 @classmethod
53 def poll(cls, context):
54 objs = context.selected_objects
55 return (len(objs) > 0)
57 def draw(self, context):
58 layout = self.layout
59 layout.prop(self, "nombre")
60 column = layout.column(align=True)
61 column.prop(self, "locat")
62 column.prop(self, "grupo")
63 column.prop(self, "renom")
65 def execute(self, context):
66 objs = context.selected_objects
67 act = context.object
68 sce = context.scene
70 try:
71 bpy.ops.object.mode_set()
72 except:
73 pass
75 if self.locat == 'CURSOR':
76 loc = sce.cursor.location
77 elif self.locat == 'ACTIVE':
78 loc = act.location
79 else:
80 loc = centro(objs)
82 bpy.ops.object.add(type='EMPTY', location=loc)
83 context.object.name = self.nombre
84 context.object.show_name = True
85 context.object.show_in_front = True
87 if self.grupo:
88 bpy.ops.collection.create(name=self.nombre)
89 bpy.ops.collection.objects_add_active()
91 for o in objs:
92 o.select_set(True)
93 if not o.parent:
94 bpy.ops.object.parent_set(type='OBJECT')
95 if self.grupo:
96 bpy.ops.collection.objects_add_active()
97 o.select_set(False)
98 for o in objs:
99 if self.renom:
100 o.name = self.nombre + '_' + o.name
101 return {'FINISHED'}
104 class PreFix(Operator):
105 bl_idname = "object.toggle_prefix"
106 bl_label = "Toggle Sufix"
107 bl_description = "Toggle parent name as sufix for c"
108 bl_options = {"REGISTER", "UNDO"}
110 @classmethod
111 def poll(cls, context):
112 act = context.object
113 return (act and act.type == 'EMPTY')
115 def execute(self, context):
116 act = context.object
117 objs = act.children
118 prefix = act.name + '_'
119 remove = False
120 for o in objs:
121 if o.name.startswith(prefix):
122 remove = True
123 break
125 if remove is True:
126 for o in objs:
127 if o.name.startswith(prefix):
128 o.name = o.name.partition(prefix)[2]
129 else:
130 for o in objs:
131 o.name = prefix + o.name
133 return {'FINISHED'}