Skip to content

Instantly share code, notes, and snippets.

@tamask
Created October 12, 2011 02:17
Show Gist options
  • Save tamask/1280052 to your computer and use it in GitHub Desktop.
Save tamask/1280052 to your computer and use it in GitHub Desktop.
Add Random Object
import bpy
import random
bl_info = {
'name': 'Add Random Object',
'version': (0, 0, 0),
'blender': (2, 5, 9),
'category': 'Object',
'description': 'Pick a random object and add it at the cursor',
}
def find_settings():
screen = bpy.data.screens.get('Default', bpy.data.screens[0])
try:
settings = screen.random_object_tools_settings
except:
settings = None
return settings
class AddRandomObject(bpy.types.Operator):
bl_idname = 'object.add_random'
bl_label = 'Add Random Object'
bl_options = {'REGISTER', 'UNDO'}
bl_description = 'Add random object.'
def execute(self, context):
screen = bpy.data.screens['Default']
settings = screen.random_object_tools_settings
group = bpy.data.groups[settings.group]
objects = group.objects.values()
source = random.choice(objects)
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.add(type='MESH', location=context.scene.cursor_location)
dest = bpy.context.active_object
dest.name = source.name
dest.data = source.data
return {'FINISHED'}
class AddRandomObjectTools(bpy.types.Panel):
bl_label = 'Add Random Object Tools'
bl_idname = 'OBJECT_PT_add_random_object_tools'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
layout = self.layout
settings = find_settings()
row = layout.row()
row.prop(settings, 'group')
class AddRandomObjectToolsSettings(bpy.types.PropertyGroup):
group = bpy.props.StringProperty(name='Group name')
def menu_func():
self.layout.operator(AddRandomObject.bl_idname, text="Add Random Object")
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_MT_object_specials.append(menu_func)
bpy.types.Screen.random_object_tools_settings = \
bpy.props.PointerProperty(type=AddRandomObjectToolsSettings)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.VIEW3D_MT_object_specials.remove(menu_func)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment