I’ve successfully written a custom operator, and assigned it to a button. But for some reason – even though the operator works in the console-- the button itself has an error performing all functions except the “Add Camera” and “Add Lamp” functions. Can some one please explain why my operator works beautifully until i use the button? Here’s my code.
#This script will add a camera into your scene, set to a .1 radius depth of field. It will automatically be focused on the selected object when the script is run. All new objects will be renamed automatically to avoid duplicates#`
bl_info = {
"name": "DOF Setup",
"author": "Hanson Schmidt",
"version": (1, 0),
"blender": (2, 78, 0),
"location": "Properties > Object > DOF Setup Panel",
"description": "Adds a depth-of-field camera set to focus on the selected object. A tracked lamp is also added.",
"warning": "",
"wiki_url": "",
"category": "Object",
}
import bpy
def main(context):
#Rename the selected object#
bpy.context.object.name = "dof-focus"
#Rename the mesh data associated with the selected object#
bpy.context.object.data.name = "dof-focus"
#Add the Key Light to the scene#
bpy.ops.object.lamp_add(type='SPOT', radius=1, view_align=False, location=(5, 5, 10), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
#Assign the Key Light a shadow size of 2#
bpy.context.object.data.shadow_soft_size = 2
#Rename the spot lamp to Key Light#
bpy.context.object.name = "Key Light"
#Add a 'Track To' constraint to the Lamp#
bpy.ops.object.constraint_add(type='TRACK_TO')
#Make the constraint track to the 'dof-focus' object#
bpy.context.object.constraints["Track To"].target = bpy.data.objects["dof-focus"]
#Set the constraint to track on th -Z axis#
bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
#Set the up axis for the constraint to +Y#
bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'
#Make the target space local#
bpy.context.object.constraints["Track To"].target_space = 'LOCAL'
#Make the owner space local#
bpy.context.object.constraints["Track To"].owner_space = 'LOCAL'
#Add the camera to the scene#
bpy.ops.object.camera_add(view_align=True, enter_editmode=False, location=(0, -10, 2), rotation=(1.5, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
#Set the depth of field target to the 'dof-focus'object#
bpy.context.object.data.dof_object = bpy.data.objects["dof-focus"]
#Set the aperture radius to .1#
bpy.context.object.data.cycles.aperture_size = 0.1
#Name the new Camera DOFcam#
bpy.context.object.name = "DOFcam"
#Name camera data associated with the camera to DOFcam#
bpy.context.object.data.name = "DOFcam"
#Set the scene's default camera to DOFcam#
bpy.context.scene.camera = bpy.data.objects["DOFcam"]
#Set the world background color to black to compliment the new lighting#
bpy.context.scene.world.horizon_color = (0, 0, 0)
class DofSetup(bpy.types.Operator):
"""Adds a depth-of-field camera set to focus on the selected object. A tracked lamp is also added."""
bl_idname = "myops.add_dof"
bl_label = "Add DOF Setup"
def execute(self, context):
main(context)
return {'FINISHED'}
class DofSetupPanel(bpy.types.Panel):
"""Creates a Panel in the Object Data window"""
bl_label = "DOF Setup"
bl_idname = "OBJECT_PT_dof"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_category = "OBJECT"
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator("myops.add_dof")
def register():
bpy.utils.register_class(DofSetup)
bpy.utils.register_class(DofSetupPanel)
def unregister():
bpy.utils.unregister_class(DofSetup)
bpy.utils.unregister_class(DofSetupPanel)
if __name__ == "__main__":
register()
# test call
# bpy.ops.myops.add_dof()