-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyaf_light.py
182 lines (145 loc) · 5.4 KB
/
yaf_light.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!BPY
import yafrayinterface
import math
from math import *
import Blender
from Blender import *
from Blender.Mathutils import *
import time
class yafLight:
def __init__(self, interface):
self.yi = interface
def makeSphere(self, nu, nv, x, y, z, rad, mat):
yi = self.yi
# get next free id from interface
ID = yi.getNextFreeID()
yi.startGeometry();
if not yi.startTriMesh(ID, 2+(nu-1)*nv, 2*(nu-1)*nv, False, False):
yi.printError("Exporter: Error on starting trimesh!")
yi.addVertex(x, y, z+rad);
yi.addVertex(x, y, z-rad);
for v in range(0, nv):
t = v/float(nv)
sin_v = sin(2.0*pi*t)
cos_v = cos(2.0*pi*t)
for u in range(1, nu):
s = u/float(nu);
sin_u = sin(pi*s)
cos_u = cos(pi*s)
yi.addVertex(x + cos_v*sin_u*rad, y + sin_v*sin_u*rad, z + cos_u*rad)
for v in range(0, nv):
yi.addTriangle( 0, 2+v*(nu-1), 2+((v+1)%nv)*(nu-1), mat );
yi.addTriangle( 1, ((v+1)%nv)*(nu-1)+nu, v*(nu-1)+nu, mat );
for u in range(0, nu-2):
yi.addTriangle( 2+v*(nu-1)+u, 2+v*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u, mat );
yi.addTriangle( 2+v*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u+1, 2+((v+1)%nv)*(nu-1)+u, mat );
yi.endTriMesh();
yi.endGeometry();
return ID
def createLight(self, yi, obj, matrix = None, lamp_mat = None, dupliNum = None):
name = obj.name
if dupliNum != None:
name += str(dupliNum)
if matrix == None:
matrix = obj.getMatrix()
pos = matrix[3]
dir = matrix[2]
up = matrix[1]
to = [pos[0] - dir[0], pos[1] - dir[1], pos[2] - dir[2]]
yi.paramsClearAll()
props = obj.properties["YafRay"]
lampType = props["type"]
power = props["power"]
color = props["color"]
yi.printInfo("Exporter: Creating Lamp: \"" + name + "\" type: " + lampType)
if lampType == "Point":
yi.paramsSetString("type", "pointlight")
power = 0.5 * power * power
elif lampType == "Sphere":
radius = props["radius"]
power = 0.5*power*power/(radius * radius)
if props["createGeometry"] == True:
ID = self.makeSphere(24, 48, pos[0], pos[1], pos[2], radius, lamp_mat)
yi.paramsSetInt("object", ID)
yi.paramsSetString("type", "spherelight")
yi.paramsSetInt("samples", props["samples"])
yi.paramsSetFloat("radius", radius)
elif lampType == "Spot":
light = obj.getData()
yi.paramsSetString("type", "spotlight")
#print "spot ", light.getSpotSize()
yi.paramsSetFloat("cone_angle", light.getSpotSize() / 2)
yi.paramsSetFloat("blend", light.getSpotBlend())
yi.paramsSetPoint("to", to[0], to[1], to[2])
yi.paramsSetBool("soft_shadows", props["SpotSoftShadows"])
yi.paramsSetFloat("shadowFuzzyness", props["SpotShadowFuzzyness"])
yi.paramsSetBool("photon_only", props["SpotPhotonOnly"])
yi.paramsSetInt("samples", props["SpotSamples"])
power = 0.5*power*power
elif lampType == "IES Light":
light = obj.getData()
yi.paramsSetString("type", "ieslight")
yi.paramsSetPoint("to", to[0], to[1], to[2])
import os;
if not os.path.exists(props["iesfile"]):
return False
yi.paramsSetString("file", props["iesfile"])
yi.paramsSetInt("samples", props["iesSamples"])
yi.paramsSetBool("soft_shadows", props["iesSoftShadows"])
yi.paramsSetFloat("cone_angle", light.getSpotSize() / 2)
power = 0.5*power*power
elif lampType == "Sun":
yi.paramsSetString("type", "sunlight")
yi.paramsSetInt("samples", props["samples"])
yi.paramsSetFloat("angle", props["angle"])
yi.paramsSetPoint("direction", dir[0], dir[1], dir[2])
elif lampType == "Directional":
yi.paramsSetString("type", "directional")
#if props["infinite"] == True:
yi.paramsSetBool("infinite", props["infinite"])
yi.paramsSetFloat("radius", props["radius"])
yi.paramsSetPoint("direction", dir[0], dir[1], dir[2])
elif lampType == "Area":
yi.paramsSetString("type", "arealight")
areaLight = obj.getData()
sizeX = areaLight.getAreaSizeX()
#sizeY = areaLight.getAreaSizeY()
sizeY = sizeX
matrix = matrix.__copy__()
matrix.transpose()
# generate an untransformed rectangle in the XY plane with
# the light's position as the centerpoint and transform it
# using its transformation matrix
point = Vector(-sizeX/2, -sizeY/2, 0, 1)
corner1 = Vector(-sizeX/2, sizeY/2, 0, 1)
corner2 = Vector(sizeX/2, sizeY/2, 0, 1)
corner3 = Vector(sizeX/2, -sizeY/2, 0, 1)
point = matrix * point
corner1 = matrix * corner1
corner2 = matrix * corner2
corner3 = matrix * corner3
#print "point: ", point, corner1, corner2, corner3
if props["createGeometry"] == True:
ID = yi.getNextFreeID()
yi.startGeometry();
yi.startTriMesh(ID, 4, 2, False, False);
yi.addVertex(point[0], point[1], point[2]);
yi.addVertex(corner1[0], corner1[1], corner1[2]);
yi.addVertex(corner2[0], corner2[1], corner2[2]);
yi.addVertex(corner3[0], corner3[1], corner3[2]);
yi.addTriangle(0, 1, 2, lamp_mat);
yi.addTriangle(0, 2, 3, lamp_mat);
yi.endTriMesh();
yi.endGeometry();
yi.paramsSetInt("object", ID);
yi.paramsClearAll();
yi.paramsSetString("type", "arealight");
yi.paramsSetInt("samples", props["samples"])
yi.paramsSetPoint("corner", point[0], point[1], point[2]);
yi.paramsSetPoint("point1", corner1[0], corner1[1], corner1[2]);
yi.paramsSetPoint("point2", corner3[0], corner3[1], corner3[2]);
yi.paramsSetPoint("from", pos[0], pos[1], pos[2])
yi.paramsSetColor("color", color[0], color[1], color[2])
yi.paramsSetFloat("power", power)
yi.createLight(name)
return True