Newer
Older
# GPL # Original Author Liero #
import bpy
from bpy.types import Operator
from bpy.props import (
StringProperty,
BoolProperty,
EnumProperty,
)
def centro(sel):
x = sum([obj.location[0] for obj in sel]) / len(sel)
y = sum([obj.location[1] for obj in sel]) / len(sel)
z = sum([obj.location[2] for obj in sel]) / len(sel)
return (x, y, z)
class P2E(Operator):
bl_idname = "object.parent_to_empty"
bl_label = "Parent to Empty"
bl_description = "Parent selected objects to a new Empty"
bl_options = {"REGISTER", "UNDO"}
name="",
default='OBJECTS',
description='Give the empty / group a name'
)
name="Create Group",
default=False,
description="Also add objects to a group"
)
name='',
items=[('CURSOR', 'Cursor', 'Cursor'), ('ACTIVE', 'Active', 'Active'),
('CENTER', 'Center', 'Selection Center')],
description='Empty location',
default='CENTER'
)
name="Add Prefix",
default=False,
description="Add prefix to objects name"
)
@classmethod
def poll(cls, context):
Brendon Murphy
committed
objs = context.selected_objects
return (len(objs) > 0)
def draw(self, context):
layout = self.layout
column.prop(self, "locat")
column.prop(self, "grupo")
column.prop(self, "renom")
Brendon Murphy
committed
objs = context.selected_objects
act = context.object
sce = context.scene
try:
bpy.ops.object.mode_set()
except:
pass
Brendon Murphy
committed
if self.locat == 'CURSOR':
loc = sce.cursor_location
elif self.locat == 'ACTIVE':
loc = act.location
loc = centro(objs)
bpy.ops.object.add(type='EMPTY', location=loc)
Brendon Murphy
committed
context.object.name = self.nombre
context.object.show_name = True
context.object.show_in_front = True
bpy.ops.collection.create(name=self.nombre)
bpy.ops.collection.objects_add_active()
Brendon Murphy
committed
bpy.ops.object.parent_set(type='OBJECT')
bpy.ops.collection.objects_add_active()
Brendon Murphy
committed
if self.renom:
o.name = self.nombre + '_' + o.name
Brendon Murphy
committed
return {'FINISHED'}
class PreFix(Operator):
bl_idname = "object.toggle_prefix"
bl_label = "Toggle Sufix"
bl_description = "Toggle parent name as sufix for c"
bl_options = {"REGISTER", "UNDO"}
Brendon Murphy
committed
@classmethod
def poll(cls, context):
Brendon Murphy
committed
return (act and act.type == 'EMPTY')
def execute(self, context):
Brendon Murphy
committed
objs = act.children
Brendon Murphy
committed
remove = False
for o in objs:
if o.name.startswith(prefix):
remove = True
break
Brendon Murphy
committed
for o in objs:
if o.name.startswith(prefix):
o.name = o.name.partition(prefix)[2]
else:
for o in objs:
Brendon Murphy
committed
return {'FINISHED'}