Skip to content

Instantly share code, notes, and snippets.

@adhihargo
Last active December 21, 2015 15:19
Show Gist options
  • Save adhihargo/6325992 to your computer and use it in GitHub Desktop.
Save adhihargo/6325992 to your computer and use it in GitHub Desktop.
For a selected object, create an array of instances of the object's group.
import bpy
from itertools import product
from mathutils import Vector
def get_enum_items(self, context):
obj = context.active_object
groups = obj.users_group
group_names = [g.name for g in groups]
return [(gn, gn, 'Instance group "%s"'%gn) for gn in group_names]
class OBJECT_OT_add_group_instance_array(bpy.types.Operator):
"""For a selected object, create an array of instances of the object's group."""
bl_idname = 'object.add_group_instance_array'
bl_label = 'Add Group Instance Array'
bl_options = {'REGISTER', 'UNDO'}
count = bpy.props.IntVectorProperty(
name = "Count", subtype = "XYZ", default = (1, 0, 0), min = 0,
description = "Object count along each axis.")
offset = bpy.props.FloatVectorProperty(
name = "Offset", subtype = "XYZ", default = (1.0, 1.0, 1.0),
description = "Object coordinate offset along each axis.")
group = bpy.props.EnumProperty(
name = "Group", items = get_enum_items,
description = "Group to instance.")
@classmethod
def poll(self, context):
return context.active_object != None \
and len(context.active_object.users_group) > 0
def execute(self, context):
obj = context.active_object
group = bpy.data.groups[str(self.group)]
center = group.dupli_offset
count_fun = lambda x: range(x+1)
count_product = list(product(*map(count_fun, self.count)))[1:]
for count in count_product:
loc = center + Vector([count[0] * self.offset[0],
count[1] * self.offset[1],
count[2] * self.offset[2]])
dup_obj = bpy.data.objects.new(group.name + '_array.000', None)
dup_obj.location = loc
dup_obj.dupli_type = 'GROUP'
dup_obj.dupli_group = group
context.scene.objects.link(dup_obj)
context.scene.objects.active = obj
return {'FINISHED'}
def register():
bpy.utils.register_module(__name__)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment