Welcome to mirror list, hosted at ThFree Co, Russian Federation.

add_light_template.py « add_advanced_objects - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e2c139fa8451ba53b35cf8496f801221c4ddbdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# gpl: author Rebellion

import bpy
from bpy.types import Operator
from bpy.props import BoolProperty


def add_lamps(self, context):

    if self.bKeyLight:
        keyLight = bpy.data.lamps.new(name="Key_Light", type="SPOT")
        ob = bpy.data.objects.new("Key_Light", keyLight)
        constraint = ob.constraints.new(type='COPY_LOCATION')
        constraint.use_offset = True
        constraint.owner_space = 'LOCAL'
        constraint.target = self.camera
        constraint = ob.constraints.new(type='TRACK_TO')
        constraint.target = self.target
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_X'
        constraint.owner_space = 'LOCAL'
        bpy.context.scene.objects.link(ob)
        ob.rotation_euler[2] = -0.785398

    if self.bFillLight:
        fillLight = bpy.data.lamps.new(name="Fill_Light", type="SPOT")
        ob = bpy.data.objects.new("Fill_Light", fillLight)
        constraint = ob.constraints.new(type='COPY_LOCATION')
        constraint.use_offset = True
        constraint.owner_space = 'LOCAL'
        constraint.target = self.camera
        constraint = ob.constraints.new(type='TRACK_TO')
        constraint.target = self.target
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_X'
        constraint.owner_space = 'LOCAL'
        bpy.context.scene.objects.link(ob)
        ob.rotation_euler[2] = 0.785398
        ob.data.energy = 0.3

    if self.bBackLight:
        backLight = bpy.data.lamps.new(name="Back_Light", type="SPOT")
        ob = bpy.data.objects.new("Back_Light", backLight)
        constraint = ob.constraints.new(type='COPY_LOCATION')
        constraint.use_offset = True
        constraint.owner_space = 'LOCAL'
        constraint.target = self.camera
        constraint = ob.constraints.new(type='TRACK_TO')
        constraint.target = self.target
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_X'
        constraint.owner_space = 'LOCAL'
        bpy.context.scene.objects.link(ob)
        ob.rotation_euler[2] = 3.14159
        ob.data.energy = 0.2

    if self.camera_constraint and self.camera is not None and \
       self.camera.type == "CAMERA":

        constraint = self.camera.constraints.new(type='TRACK_TO')
        constraint.target = self.target
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_Y'


class OBJECT_OT_add_light_template(Operator):
    bl_idname = "object.add_light_template"
    bl_label = "Add Light Template"
    bl_description = ("Add Key, Fill and Back Lights to the Scene\n"
                      "Needs an existing Active Object")
    bl_options = {'REGISTER', 'UNDO'}

    camera = None
    target = None

    bKeyLight = BoolProperty(
            name="Key Light",
            description="Enable Key Light in the Scene",
            default=True
            )
    bFillLight = BoolProperty(
            name="Fill Light",
            description="Enable Fill Light in the Scene",
            default=True
            )
    bBackLight = BoolProperty(
            name="Back Light",
            description="Enable Back Light in the Scene",
            default=True
            )
    camera_constraint = BoolProperty(
            name="Camera Constraint",
            description="Add a Constraint to the Camera Object",
            default=False
            )

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        try:
            objects = context.selected_objects

            if len(objects) == 2:
                for ob in objects:
                    if ob.type == 'CAMERA':
                        self.camera = ob
                    else:
                        self.target = ob
            elif len(objects) == 1:
                if objects[0].type == 'CAMERA':
                    self.camera = objects[0]
                    bpy.ops.object.empty_add()
                    self.target = context.active_object
                else:
                    self.camera = context.scene.camera
                    self.target = context.active_object
            elif len(objects) == 0:
                bpy.ops.object.empty_add()
                self.target = context.active_object
                self.camera = context.scene.camera

            add_lamps(self, context)

        except Exception as e:
            self.report({'WARNING'},
                        "Some operations could not be performed (See Console for more info)")

            print("\n[Add Advanced  Objects]\nOperator: "
                  "object.add_light_template\nError: {}".format(e))

        return {'FINISHED'}


def register():
    bpy.utils.register_class(OBJECT_OT_add_light_template)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_light_template)


if __name__ == "__main__":
    register()