Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamically create operators that can add custom objects
# for http://blender.stackexchange.com/q/56741/935
bl_info = {
'version': (1, 0),
'blender': (2, 75, 0),
'author': 'sambler',
'name': 'Custom Object Factory',
'location': 'blender',
'description': 'Create multiple custom operators that create objects' ,
'warning': 'only for testing',
'category': 'test',
}
import bpy
import os
# this example uses the following file layout
# located in blender's addons folder
#object_factory/
# __init__.py
# custom_data/
# Female_body
# RacingCar
# iPhone
# Male_body
# Tank
# Toaster
# this is the dirname that contains the object data
# it is located in the addon folder next to this file
obj_dir_name = 'custom_data'
# these names match files located in the addons obj_dir_name
# we can't access the filesystem during registration so
# we need to maintain this list manually
obj_list = [
'Female_body',
'RacingCar',
'iPhone',
'Male_body',
'Tank',
'Toaster'
]
# this holds the custom operators so we can cleanup when turned off
custom_items = []
def addCustomItems():
for obj_name in obj_list:
op_name = 'object.add_custom_' + obj_name.lower()
data_file = os.path.join(os.path.dirname(__file__), obj_dir_name, obj_name)
nc = type( 'DynOp_' + obj_name,
(CustomObjectBase, ),
{'bl_idname': op_name,
'bl_label': 'Add a ' + obj_name,
'bl_description': 'This adds an ' + obj_name,
'obj_file': data_file
})
custom_items.append(nc)
bpy.utils.register_class(nc)
def clearCustomItems():
for c in custom_items:
bpy.utils.unregister_class(c)
class CustomObjectBase(bpy.types.Operator):
"""Base for adding a custom object"""
bl_idname = 'object.add_custom_item'
bl_label = 'Add Item'
bl_description ='Base operator for adding custom objects'
bl_options = {'REGISTER', 'UNDO'}
obj_file = bpy.props.StringProperty()
def execute(self, context):
with open(self.obj_file, 'r') as f:
# read data and create mesh
obj_name = os.path.basename(self.obj_file)
obj = bpy.data.objects.new(obj_name, None)
context.scene.objects.link(obj)
return {'FINISHED'}
class AddCustomObjects(bpy.types.Menu):
"""My custom objects menu"""
bl_label = 'Custom Objects'
bl_idname = 'AddCustomObjects'
def draw(self, context):
lo = self.layout
for o in custom_items:
lo.operator(o.bl_idname, icon='OUTLINER_OB_ARMATURE')
def menu_func(self, context):
self.layout.menu('AddCustomObjects', text='Custom Objects', icon='OUTLINER_OB_ARMATURE')
def register():
bpy.utils.register_class(CustomObjectBase)
addCustomItems()
bpy.utils.register_class(AddCustomObjects)
bpy.types.INFO_MT_add.append(menu_func)
def unregister():
bpy.types.INFO_MT_add.remove(menu_func)
bpy.utils.unregister_class(AddCustomObjects)
clearCustomItems()
bpy.utils.unregister_class(CustomObjectBase)
if __name__ == '__main__':
register()
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.