Cycles: Expose volume voxel data interpolation to the interface
[blender.git] / intern / cycles / blender / blender_shader.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #include "background.h"
18 #include "graph.h"
19 #include "light.h"
20 #include "nodes.h"
21 #include "osl.h"
22 #include "scene.h"
23 #include "shader.h"
24
25 #include "blender_sync.h"
26 #include "blender_util.h"
27
28 #include "util_debug.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 typedef map<void*, ShaderInput*> PtrInputMap;
33 typedef map<void*, ShaderOutput*> PtrOutputMap;
34 typedef map<std::string, ProxyNode*> ProxyMap;
35
36 /* Find */
37
38 void BlenderSync::find_shader(BL::ID id, vector<uint>& used_shaders, int default_shader)
39 {
40         Shader *shader = (id)? shader_map.find(id): scene->shaders[default_shader];
41
42         for(size_t i = 0; i < scene->shaders.size(); i++) {
43                 if(scene->shaders[i] == shader) {
44                         used_shaders.push_back(i);
45                         scene->shaders[i]->tag_used(scene);
46                         break;
47                 }
48         }
49 }
50
51 /* Graph */
52
53 static BL::NodeSocket get_node_output(BL::Node b_node, const string& name)
54 {
55         BL::Node::outputs_iterator b_out;
56
57         for(b_node.outputs.begin(b_out); b_out != b_node.outputs.end(); ++b_out)
58                 if(b_out->name() == name)
59                         return *b_out;
60
61         assert(0);
62
63         return *b_out;
64 }
65
66 static float3 get_node_output_rgba(BL::Node b_node, const string& name)
67 {
68         BL::NodeSocket b_sock = get_node_output(b_node, name);
69         float value[4];
70         RNA_float_get_array(&b_sock.ptr, "default_value", value);
71         return make_float3(value[0], value[1], value[2]);
72 }
73
74 static float get_node_output_value(BL::Node b_node, const string& name)
75 {
76         BL::NodeSocket b_sock = get_node_output(b_node, name);
77         return RNA_float_get(&b_sock.ptr, "default_value");
78 }
79
80 static float3 get_node_output_vector(BL::Node b_node, const string& name)
81 {
82         BL::NodeSocket b_sock = get_node_output(b_node, name);
83         float value[3];
84         RNA_float_get_array(&b_sock.ptr, "default_value", value);
85         return make_float3(value[0], value[1], value[2]);
86 }
87
88 static ShaderSocketType convert_socket_type(BL::NodeSocket b_socket)
89 {
90         switch (b_socket.type()) {
91                 case BL::NodeSocket::type_VALUE:
92                         return SHADER_SOCKET_FLOAT;
93                 case BL::NodeSocket::type_INT:
94                         return SHADER_SOCKET_INT;
95                 case BL::NodeSocket::type_VECTOR:
96                         return SHADER_SOCKET_VECTOR;
97                 case BL::NodeSocket::type_RGBA:
98                         return SHADER_SOCKET_COLOR;
99                 case BL::NodeSocket::type_STRING:
100                         return SHADER_SOCKET_STRING;
101                 case BL::NodeSocket::type_SHADER:
102                         return SHADER_SOCKET_CLOSURE;
103                 
104                 default:
105                         return SHADER_SOCKET_UNDEFINED;
106         }
107 }
108
109 static void set_default_value(ShaderInput *input, BL::Node b_node, BL::NodeSocket b_sock, BL::BlendData b_data, BL::ID b_id)
110 {
111         /* copy values for non linked inputs */
112         switch(input->type) {
113         case SHADER_SOCKET_FLOAT: {
114                 input->set(get_float(b_sock.ptr, "default_value"));
115                 break;
116         }
117         case SHADER_SOCKET_INT: {
118                 input->set((float)get_int(b_sock.ptr, "default_value"));
119                 break;
120         }
121         case SHADER_SOCKET_COLOR: {
122                 input->set(float4_to_float3(get_float4(b_sock.ptr, "default_value")));
123                 break;
124         }
125         case SHADER_SOCKET_NORMAL:
126         case SHADER_SOCKET_POINT:
127         case SHADER_SOCKET_VECTOR: {
128                 input->set(get_float3(b_sock.ptr, "default_value"));
129                 break;
130         }
131         case SHADER_SOCKET_STRING: {
132                 input->set((ustring)blender_absolute_path(b_data, b_id, get_string(b_sock.ptr, "default_value")));
133                 break;
134         }
135         
136         case SHADER_SOCKET_CLOSURE:
137         case SHADER_SOCKET_UNDEFINED:
138                 break;
139         }
140 }
141
142 static void get_tex_mapping(TextureMapping *mapping, BL::TexMapping b_mapping)
143 {
144         if(!b_mapping)
145                 return;
146
147         mapping->translation = get_float3(b_mapping.translation());
148         mapping->rotation = get_float3(b_mapping.rotation());
149         mapping->scale = get_float3(b_mapping.scale());
150         mapping->type = (TextureMapping::Type)b_mapping.vector_type();
151
152         mapping->x_mapping = (TextureMapping::Mapping)b_mapping.mapping_x();
153         mapping->y_mapping = (TextureMapping::Mapping)b_mapping.mapping_y();
154         mapping->z_mapping = (TextureMapping::Mapping)b_mapping.mapping_z();
155 }
156
157 static void get_tex_mapping(TextureMapping *mapping, BL::ShaderNodeMapping b_mapping)
158 {
159         if(!b_mapping)
160                 return;
161
162         mapping->translation = get_float3(b_mapping.translation());
163         mapping->rotation = get_float3(b_mapping.rotation());
164         mapping->scale = get_float3(b_mapping.scale());
165         mapping->type = (TextureMapping::Type)b_mapping.vector_type();
166
167         mapping->use_minmax = b_mapping.use_min() || b_mapping.use_max();
168
169         if(b_mapping.use_min())
170                 mapping->min = get_float3(b_mapping.min());
171         if(b_mapping.use_max())
172                 mapping->max = get_float3(b_mapping.max());
173 }
174
175 static bool is_output_node(BL::Node b_node)
176 {
177         return (b_node.is_a(&RNA_ShaderNodeOutputMaterial)
178                     || b_node.is_a(&RNA_ShaderNodeOutputWorld)
179                     || b_node.is_a(&RNA_ShaderNodeOutputLamp));
180 }
181
182 static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, BL::ShaderNode b_node)
183 {
184         ShaderNode *node = NULL;
185
186         /* existing blender nodes */
187         if (b_node.is_a(&RNA_ShaderNodeRGBCurve)) {
188                 BL::ShaderNodeRGBCurve b_curve_node(b_node);
189                 RGBCurvesNode *curves = new RGBCurvesNode();
190                 curvemapping_color_to_array(b_curve_node.mapping(), curves->curves, RAMP_TABLE_SIZE, true);
191                 node = curves;
192         }
193         if (b_node.is_a(&RNA_ShaderNodeVectorCurve)) {
194                 BL::ShaderNodeVectorCurve b_curve_node(b_node);
195                 VectorCurvesNode *curves = new VectorCurvesNode();
196                 curvemapping_color_to_array(b_curve_node.mapping(), curves->curves, RAMP_TABLE_SIZE, false);
197                 node = curves;
198         }
199         else if (b_node.is_a(&RNA_ShaderNodeValToRGB)) {
200                 RGBRampNode *ramp = new RGBRampNode();
201                 BL::ShaderNodeValToRGB b_ramp_node(b_node);
202                 colorramp_to_array(b_ramp_node.color_ramp(), ramp->ramp, RAMP_TABLE_SIZE);
203                 ramp->interpolate = b_ramp_node.color_ramp().interpolation() != BL::ColorRamp::interpolation_CONSTANT;
204                 node = ramp;
205         }
206         else if (b_node.is_a(&RNA_ShaderNodeRGB)) {
207                 ColorNode *color = new ColorNode();
208                 color->value = get_node_output_rgba(b_node, "Color");
209                 node = color;
210         }
211         else if (b_node.is_a(&RNA_ShaderNodeValue)) {
212                 ValueNode *value = new ValueNode();
213                 value->value = get_node_output_value(b_node, "Value");
214                 node = value;
215         }
216         else if (b_node.is_a(&RNA_ShaderNodeCameraData)) {
217                 node = new CameraNode();
218         }
219         else if (b_node.is_a(&RNA_ShaderNodeInvert)) {
220                 node = new InvertNode();
221         }
222         else if (b_node.is_a(&RNA_ShaderNodeGamma)) {
223                 node = new GammaNode();
224         }
225         else if (b_node.is_a(&RNA_ShaderNodeBrightContrast)) {
226                 node = new BrightContrastNode();
227         }
228         else if (b_node.is_a(&RNA_ShaderNodeMixRGB)) {
229                 BL::ShaderNodeMixRGB b_mix_node(b_node);
230                 MixNode *mix = new MixNode();
231                 mix->type = MixNode::type_enum[b_mix_node.blend_type()];
232                 /* Tag if it's Mix */
233                 if(b_mix_node.blend_type() == 0) 
234                         mix->special_type = SHADER_SPECIAL_TYPE_MIX_RGB;
235
236                 mix->use_clamp = b_mix_node.use_clamp();
237                 node = mix;
238         }
239         else if (b_node.is_a(&RNA_ShaderNodeSeparateRGB)) {
240                 node = new SeparateRGBNode();
241         }
242         else if (b_node.is_a(&RNA_ShaderNodeCombineRGB)) {
243                 node = new CombineRGBNode();
244         }
245         else if (b_node.is_a(&RNA_ShaderNodeSeparateHSV)) {
246                 node = new SeparateHSVNode();
247         }
248         else if (b_node.is_a(&RNA_ShaderNodeCombineHSV)) {
249                 node = new CombineHSVNode();
250         }
251         else if (b_node.is_a(&RNA_ShaderNodeSeparateXYZ)) {
252                 node = new SeparateXYZNode();
253         }
254         else if (b_node.is_a(&RNA_ShaderNodeCombineXYZ)) {
255                 node = new CombineXYZNode();
256         }
257         else if (b_node.is_a(&RNA_ShaderNodeHueSaturation)) {
258                 node = new HSVNode();
259         }
260         else if (b_node.is_a(&RNA_ShaderNodeRGBToBW)) {
261                 node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
262         }
263         else if (b_node.is_a(&RNA_ShaderNodeMath)) {
264                 BL::ShaderNodeMath b_math_node(b_node);
265                 MathNode *math = new MathNode();
266                 math->type = MathNode::type_enum[b_math_node.operation()];
267                 math->use_clamp = b_math_node.use_clamp();
268                 node = math;
269         }
270         else if (b_node.is_a(&RNA_ShaderNodeVectorMath)) {
271                 BL::ShaderNodeVectorMath b_vector_math_node(b_node);
272                 VectorMathNode *vmath = new VectorMathNode();
273                 vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
274                 node = vmath;
275         }
276         else if (b_node.is_a(&RNA_ShaderNodeVectorTransform)) {
277                 BL::ShaderNodeVectorTransform b_vector_transform_node(b_node);
278                 VectorTransformNode *vtransform = new VectorTransformNode();
279                 vtransform->type = VectorTransformNode::type_enum[b_vector_transform_node.type()];
280                 vtransform->convert_from = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_from()];
281                 vtransform->convert_to = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_to()];
282                 node = vtransform;
283         }
284         else if (b_node.is_a(&RNA_ShaderNodeNormal)) {
285                 BL::Node::outputs_iterator out_it;
286                 b_node.outputs.begin(out_it);
287
288                 NormalNode *norm = new NormalNode();
289                 norm->direction = get_node_output_vector(b_node, "Normal");
290                 node = norm;
291         }
292         else if (b_node.is_a(&RNA_ShaderNodeMapping)) {
293                 BL::ShaderNodeMapping b_mapping_node(b_node);
294                 MappingNode *mapping = new MappingNode();
295
296                 get_tex_mapping(&mapping->tex_mapping, b_mapping_node);
297
298                 node = mapping;
299         }
300         else if (b_node.is_a(&RNA_ShaderNodeFresnel)) {
301                 node = new FresnelNode();
302         }
303         else if (b_node.is_a(&RNA_ShaderNodeLayerWeight)) {
304                 node = new LayerWeightNode();
305         }
306         else if (b_node.is_a(&RNA_ShaderNodeAddShader)) {
307                 node = new AddClosureNode();
308         }
309         else if (b_node.is_a(&RNA_ShaderNodeMixShader)) {
310                 node = new MixClosureNode();
311         }
312         else if (b_node.is_a(&RNA_ShaderNodeAttribute)) {
313                 BL::ShaderNodeAttribute b_attr_node(b_node);
314                 AttributeNode *attr = new AttributeNode();
315                 attr->attribute = b_attr_node.attribute_name();
316                 node = attr;
317         }
318         else if (b_node.is_a(&RNA_ShaderNodeBackground)) {
319                 node = new BackgroundNode();
320         }
321         else if (b_node.is_a(&RNA_ShaderNodeHoldout)) {
322                 node = new HoldoutNode();
323         }
324         else if (b_node.is_a(&RNA_ShaderNodeBsdfAnisotropic)) {
325                 BL::ShaderNodeBsdfAnisotropic b_aniso_node(b_node);
326                 AnisotropicBsdfNode *aniso = new AnisotropicBsdfNode();
327
328                 switch (b_aniso_node.distribution())
329                 {
330                 case BL::ShaderNodeBsdfAnisotropic::distribution_BECKMANN:
331                         aniso->distribution = ustring("Beckmann");
332                         break;
333                 case BL::ShaderNodeBsdfAnisotropic::distribution_GGX:
334                         aniso->distribution = ustring("GGX");
335                         break;
336                 case BL::ShaderNodeBsdfAnisotropic::distribution_ASHIKHMIN_SHIRLEY:
337                         aniso->distribution = ustring("Ashikhmin-Shirley");
338                         break;
339                 }
340
341                 node = aniso;
342         }
343         else if (b_node.is_a(&RNA_ShaderNodeBsdfDiffuse)) {
344                 node = new DiffuseBsdfNode();
345         }
346         else if (b_node.is_a(&RNA_ShaderNodeSubsurfaceScattering)) {
347                 BL::ShaderNodeSubsurfaceScattering b_subsurface_node(b_node);
348
349                 SubsurfaceScatteringNode *subsurface = new SubsurfaceScatteringNode();
350
351                 switch(b_subsurface_node.falloff()) {
352                 case BL::ShaderNodeSubsurfaceScattering::falloff_CUBIC:
353                         subsurface->closure = CLOSURE_BSSRDF_CUBIC_ID;
354                         break;
355                 case BL::ShaderNodeSubsurfaceScattering::falloff_GAUSSIAN:
356                         subsurface->closure = CLOSURE_BSSRDF_GAUSSIAN_ID;
357                         break;
358                 }
359
360                 node = subsurface;
361         }
362         else if (b_node.is_a(&RNA_ShaderNodeBsdfGlossy)) {
363                 BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
364                 GlossyBsdfNode *glossy = new GlossyBsdfNode();
365                 
366                 switch(b_glossy_node.distribution()) {
367                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
368                         glossy->distribution = ustring("Sharp");
369                         break;
370                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
371                         glossy->distribution = ustring("Beckmann");
372                         break;
373                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
374                         glossy->distribution = ustring("GGX");
375                         break;
376                 case BL::ShaderNodeBsdfGlossy::distribution_ASHIKHMIN_SHIRLEY:
377                         glossy->distribution = ustring("Ashikhmin-Shirley");
378                         break;
379                 }
380                 node = glossy;
381         }
382         else if (b_node.is_a(&RNA_ShaderNodeBsdfGlass)) {
383                 BL::ShaderNodeBsdfGlass b_glass_node(b_node);
384                 GlassBsdfNode *glass = new GlassBsdfNode();
385                 switch(b_glass_node.distribution()) {
386                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
387                         glass->distribution = ustring("Sharp");
388                         break;
389                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
390                         glass->distribution = ustring("Beckmann");
391                         break;
392                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
393                         glass->distribution = ustring("GGX");
394                         break;
395                 }
396                 node = glass;
397         }
398         else if (b_node.is_a(&RNA_ShaderNodeBsdfRefraction)) {
399                 BL::ShaderNodeBsdfRefraction b_refraction_node(b_node);
400                 RefractionBsdfNode *refraction = new RefractionBsdfNode();
401                 switch(b_refraction_node.distribution()) {
402                         case BL::ShaderNodeBsdfRefraction::distribution_SHARP:
403                                 refraction->distribution = ustring("Sharp");
404                                 break;
405                         case BL::ShaderNodeBsdfRefraction::distribution_BECKMANN:
406                                 refraction->distribution = ustring("Beckmann");
407                                 break;
408                         case BL::ShaderNodeBsdfRefraction::distribution_GGX:
409                                 refraction->distribution = ustring("GGX");
410                                 break;
411                 }
412                 node = refraction;
413         }
414         else if (b_node.is_a(&RNA_ShaderNodeBsdfToon)) {
415                 BL::ShaderNodeBsdfToon b_toon_node(b_node);
416                 ToonBsdfNode *toon = new ToonBsdfNode();
417                 switch(b_toon_node.component()) {
418                         case BL::ShaderNodeBsdfToon::component_DIFFUSE:
419                                 toon->component = ustring("Diffuse");
420                                 break;
421                         case BL::ShaderNodeBsdfToon::component_GLOSSY:
422                                 toon->component = ustring("Glossy");
423                                 break;
424                 }
425                 node = toon;
426         }
427         else if (b_node.is_a(&RNA_ShaderNodeBsdfHair)) {
428                 BL::ShaderNodeBsdfHair b_hair_node(b_node);
429                 HairBsdfNode *hair = new HairBsdfNode();
430                 switch(b_hair_node.component()) {
431                         case BL::ShaderNodeBsdfHair::component_Reflection:
432                                 hair->component = ustring("Reflection");
433                                 break;
434                         case BL::ShaderNodeBsdfHair::component_Transmission:
435                                 hair->component = ustring("Transmission");
436                                 break;
437                 }
438                 node = hair;
439         }
440         else if (b_node.is_a(&RNA_ShaderNodeBsdfTranslucent)) {
441                 node = new TranslucentBsdfNode();
442         }
443         else if (b_node.is_a(&RNA_ShaderNodeBsdfTransparent)) {
444                 node = new TransparentBsdfNode();
445         }
446         else if (b_node.is_a(&RNA_ShaderNodeBsdfVelvet)) {
447                 node = new VelvetBsdfNode();
448         }
449         else if (b_node.is_a(&RNA_ShaderNodeEmission)) {
450                 node = new EmissionNode();
451         }
452         else if (b_node.is_a(&RNA_ShaderNodeAmbientOcclusion)) {
453                 node = new AmbientOcclusionNode();
454         }
455         else if (b_node.is_a(&RNA_ShaderNodeVolumeScatter)) {
456                 node = new ScatterVolumeNode();
457         }
458         else if (b_node.is_a(&RNA_ShaderNodeVolumeAbsorption)) {
459                 node = new AbsorptionVolumeNode();
460         }
461         else if (b_node.is_a(&RNA_ShaderNodeNewGeometry)) {
462                 node = new GeometryNode();
463         }
464         else if (b_node.is_a(&RNA_ShaderNodeWireframe)) {
465                 BL::ShaderNodeWireframe b_wireframe_node(b_node);
466                 WireframeNode *wire = new WireframeNode();
467                 wire->use_pixel_size = b_wireframe_node.use_pixel_size();
468                 node = wire;
469         }
470         else if (b_node.is_a(&RNA_ShaderNodeWavelength)) {
471                 node = new WavelengthNode();
472         }
473         else if (b_node.is_a(&RNA_ShaderNodeBlackbody)) {
474                 node = new BlackbodyNode();
475         }
476         else if (b_node.is_a(&RNA_ShaderNodeLightPath)) {
477                 node = new LightPathNode();
478         }
479         else if (b_node.is_a(&RNA_ShaderNodeLightFalloff)) {
480                 node = new LightFalloffNode();
481         }
482         else if (b_node.is_a(&RNA_ShaderNodeObjectInfo)) {
483                 node = new ObjectInfoNode();
484         }
485         else if (b_node.is_a(&RNA_ShaderNodeParticleInfo)) {
486                 node = new ParticleInfoNode();
487         }
488         else if (b_node.is_a(&RNA_ShaderNodeHairInfo)) {
489                 node = new HairInfoNode();
490         }
491         else if (b_node.is_a(&RNA_ShaderNodeBump)) {
492                 BL::ShaderNodeBump b_bump_node(b_node);
493                 BumpNode *bump = new BumpNode();
494                 bump->invert = b_bump_node.invert();
495                 node = bump;
496         }
497         else if (b_node.is_a(&RNA_ShaderNodeScript)) {
498 #ifdef WITH_OSL
499                 if(scene->shader_manager->use_osl()) {
500                         /* create script node */
501                         BL::ShaderNodeScript b_script_node(b_node);
502                         OSLScriptNode *script_node = new OSLScriptNode();
503
504                         /* Generate inputs/outputs from node sockets
505                          *
506                          * Note: the node sockets are generated from OSL parameters,
507                          * so the names match those of the corresponding parameters exactly.
508                          *
509                          * Note 2: ShaderInput/ShaderOutput store shallow string copies only!
510                          * Socket names must be stored in the extra lists instead. */
511                         BL::Node::inputs_iterator b_input;
512
513                         for (b_script_node.inputs.begin(b_input); b_input != b_script_node.inputs.end(); ++b_input) {
514                                 script_node->input_names.push_back(ustring(b_input->name()));
515                                 ShaderInput *input = script_node->add_input(script_node->input_names.back().c_str(),
516                                                                             convert_socket_type(*b_input));
517                                 set_default_value(input, b_node, *b_input, b_data, b_ntree);
518                         }
519
520                         BL::Node::outputs_iterator b_output;
521
522                         for (b_script_node.outputs.begin(b_output); b_output != b_script_node.outputs.end(); ++b_output) {
523                                 script_node->output_names.push_back(ustring(b_output->name()));
524                                 script_node->add_output(script_node->output_names.back().c_str(),
525                                                         convert_socket_type(*b_output));
526                         }
527
528                         /* load bytecode or filepath */
529                         OSLShaderManager *manager = (OSLShaderManager*)scene->shader_manager;
530                         string bytecode_hash = b_script_node.bytecode_hash();
531
532                         if(!bytecode_hash.empty()) {
533                                 /* loaded bytecode if not already done */
534                                 if(!manager->shader_test_loaded(bytecode_hash))
535                                         manager->shader_load_bytecode(bytecode_hash, b_script_node.bytecode());
536
537                                 script_node->bytecode_hash = bytecode_hash;
538                         }
539                         else {
540                                 /* set filepath */
541                                 script_node->filepath = blender_absolute_path(b_data, b_ntree, b_script_node.filepath());
542                         }
543
544                         node = script_node;
545                 }
546 #endif
547         }
548         else if (b_node.is_a(&RNA_ShaderNodeTexImage)) {
549                 BL::ShaderNodeTexImage b_image_node(b_node);
550                 BL::Image b_image(b_image_node.image());
551                 ImageTextureNode *image = new ImageTextureNode();
552                 if(b_image) {
553                         /* builtin images will use callback-based reading because
554                          * they could only be loaded correct from blender side
555                          */
556                         bool is_builtin = b_image.packed_file() ||
557                                           b_image.source() == BL::Image::source_GENERATED ||
558                                           b_image.source() == BL::Image::source_MOVIE;
559
560                         if(is_builtin) {
561                                 /* for builtin images we're using image datablock name to find an image to
562                                  * read pixels from later
563                                  *
564                                  * also store frame number as well, so there's no differences in handling
565                                  * builtin names for packed images and movies
566                                  */
567                                 int scene_frame = b_scene.frame_current();
568                                 int image_frame = image_user_frame_number(b_image_node.image_user(), scene_frame);
569                                 image->filename = b_image.name() + "@" + string_printf("%d", image_frame);
570                                 image->builtin_data = b_image.ptr.data;
571                         }
572                         else {
573                                 image->filename = image_user_file_path(b_image_node.image_user(), b_image, b_scene.frame_current());
574                                 image->builtin_data = NULL;
575                         }
576
577                         image->animated = b_image_node.image_user().use_auto_refresh();
578                         image->use_alpha = b_image.use_alpha();
579
580                         /* TODO(sergey): Does not work properly when we change builtin type. */
581                         if (b_image.is_updated()) {
582                                 scene->image_manager->tag_reload_image(image->filename,
583                                                                        image->builtin_data,
584                                                                        (InterpolationType)b_image_node.interpolation());
585                         }
586                 }
587                 image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
588                 image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()];
589                 image->interpolation = (InterpolationType)b_image_node.interpolation();
590                 image->projection_blend = b_image_node.projection_blend();
591                 get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
592                 node = image;
593         }
594         else if (b_node.is_a(&RNA_ShaderNodeTexEnvironment)) {
595                 BL::ShaderNodeTexEnvironment b_env_node(b_node);
596                 BL::Image b_image(b_env_node.image());
597                 EnvironmentTextureNode *env = new EnvironmentTextureNode();
598                 if(b_image) {
599                         bool is_builtin = b_image.packed_file() ||
600                                           b_image.source() == BL::Image::source_GENERATED ||
601                                           b_image.source() == BL::Image::source_MOVIE;
602
603                         if(is_builtin) {
604                                 int scene_frame = b_scene.frame_current();
605                                 int image_frame = image_user_frame_number(b_env_node.image_user(), scene_frame);
606                                 env->filename = b_image.name() + "@" + string_printf("%d", image_frame);
607                                 env->builtin_data = b_image.ptr.data;
608                         }
609                         else {
610                                 env->filename = image_user_file_path(b_env_node.image_user(), b_image, b_scene.frame_current());
611                                 env->animated = b_env_node.image_user().use_auto_refresh();
612                                 env->builtin_data = NULL;
613                         }
614
615                         env->use_alpha = b_image.use_alpha();
616
617                         /* TODO(sergey): Does not work properly when we change builtin type. */
618                         if (b_image.is_updated()) {
619                                 scene->image_manager->tag_reload_image(env->filename,
620                                                                        env->builtin_data,
621                                                                        INTERPOLATION_LINEAR);
622                         }
623                 }
624                 env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
625                 env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()];
626                 get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
627                 node = env;
628         }
629         else if (b_node.is_a(&RNA_ShaderNodeTexGradient)) {
630                 BL::ShaderNodeTexGradient b_gradient_node(b_node);
631                 GradientTextureNode *gradient = new GradientTextureNode();
632                 gradient->type = GradientTextureNode::type_enum[(int)b_gradient_node.gradient_type()];
633                 get_tex_mapping(&gradient->tex_mapping, b_gradient_node.texture_mapping());
634                 node = gradient;
635         }
636         else if (b_node.is_a(&RNA_ShaderNodeTexVoronoi)) {
637                 BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
638                 VoronoiTextureNode *voronoi = new VoronoiTextureNode();
639                 voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
640                 get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
641                 node = voronoi;
642         }
643         else if (b_node.is_a(&RNA_ShaderNodeTexMagic)) {
644                 BL::ShaderNodeTexMagic b_magic_node(b_node);
645                 MagicTextureNode *magic = new MagicTextureNode();
646                 magic->depth = b_magic_node.turbulence_depth();
647                 get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
648                 node = magic;
649         }
650         else if (b_node.is_a(&RNA_ShaderNodeTexWave)) {
651                 BL::ShaderNodeTexWave b_wave_node(b_node);
652                 WaveTextureNode *wave = new WaveTextureNode();
653                 wave->type = WaveTextureNode::type_enum[(int)b_wave_node.wave_type()];
654                 get_tex_mapping(&wave->tex_mapping, b_wave_node.texture_mapping());
655                 node = wave;
656         }
657         else if (b_node.is_a(&RNA_ShaderNodeTexChecker)) {
658                 BL::ShaderNodeTexChecker b_checker_node(b_node);
659                 CheckerTextureNode *checker = new CheckerTextureNode();
660                 get_tex_mapping(&checker->tex_mapping, b_checker_node.texture_mapping());
661                 node = checker;
662         }
663         else if (b_node.is_a(&RNA_ShaderNodeTexBrick)) {
664                 BL::ShaderNodeTexBrick b_brick_node(b_node);
665                 BrickTextureNode *brick = new BrickTextureNode();
666                 brick->offset = b_brick_node.offset();
667                 brick->offset_frequency = b_brick_node.offset_frequency();
668                 brick->squash = b_brick_node.squash();
669                 brick->squash_frequency = b_brick_node.squash_frequency();
670                 get_tex_mapping(&brick->tex_mapping, b_brick_node.texture_mapping());
671                 node = brick;
672         }
673         else if (b_node.is_a(&RNA_ShaderNodeTexNoise)) {
674                 BL::ShaderNodeTexNoise b_noise_node(b_node);
675                 NoiseTextureNode *noise = new NoiseTextureNode();
676                 get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
677                 node = noise;
678         }
679         else if (b_node.is_a(&RNA_ShaderNodeTexMusgrave)) {
680                 BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
681                 MusgraveTextureNode *musgrave = new MusgraveTextureNode();
682                 musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
683                 get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
684                 node = musgrave;
685         }
686         else if (b_node.is_a(&RNA_ShaderNodeTexCoord)) {
687                 BL::ShaderNodeTexCoord b_tex_coord_node(b_node);
688                 TextureCoordinateNode *tex_coord = new TextureCoordinateNode();
689                 tex_coord->from_dupli = b_tex_coord_node.from_dupli();
690                 node = tex_coord;
691         }
692         else if (b_node.is_a(&RNA_ShaderNodeTexSky)) {
693                 BL::ShaderNodeTexSky b_sky_node(b_node);
694                 SkyTextureNode *sky = new SkyTextureNode();
695                 sky->type = SkyTextureNode::type_enum[(int)b_sky_node.sky_type()];
696                 sky->sun_direction = normalize(get_float3(b_sky_node.sun_direction()));
697                 sky->turbidity = b_sky_node.turbidity();
698                 sky->ground_albedo = b_sky_node.ground_albedo();
699                 get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
700                 node = sky;
701         }
702         else if (b_node.is_a(&RNA_ShaderNodeNormalMap)) {
703                 BL::ShaderNodeNormalMap b_normal_map_node(b_node);
704                 NormalMapNode *nmap = new NormalMapNode();
705                 nmap->space = NormalMapNode::space_enum[(int)b_normal_map_node.space()];
706                 nmap->attribute = b_normal_map_node.uv_map();
707                 node = nmap;
708         }
709         else if (b_node.is_a(&RNA_ShaderNodeTangent)) {
710                 BL::ShaderNodeTangent b_tangent_node(b_node);
711                 TangentNode *tangent = new TangentNode();
712                 tangent->direction_type = TangentNode::direction_type_enum[(int)b_tangent_node.direction_type()];
713                 tangent->axis = TangentNode::axis_enum[(int)b_tangent_node.axis()];
714                 tangent->attribute = b_tangent_node.uv_map();
715                 node = tangent;
716         }
717         else if (b_node.is_a(&RNA_ShaderNodeUVMap)) {
718                 BL::ShaderNodeUVMap b_uvmap_node(b_node);
719                 UVMapNode *uvm = new UVMapNode();
720                 uvm->attribute = b_uvmap_node.uv_map();
721                 uvm->from_dupli = b_uvmap_node.from_dupli();
722                 node = uvm;
723         }
724
725         if(node)
726                 graph->add(node);
727
728         return node;
729 }
730
731 static bool node_use_modified_socket_name(ShaderNode *node)
732 {
733         if (node->special_type == SHADER_SPECIAL_TYPE_SCRIPT)
734                 return false;
735
736         return true;
737 }
738
739 static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
740 {
741         string name = b_socket.name();
742         
743         if (node_use_modified_socket_name(node)) {
744                 BL::Node::inputs_iterator b_input;
745                 bool found = false;
746                 int counter = 0, total = 0;
747
748                 for (b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
749                         if (b_input->name() == name) {
750                                 if (!found)
751                                         counter++;
752                                 total++;
753                         }
754
755                         if(b_input->ptr.data == b_socket.ptr.data)
756                                 found = true;
757                 }
758
759                 /* rename if needed */
760                 if (name == "Shader")
761                         name = "Closure";
762
763                 if (total > 1)
764                         name = string_printf("%s%d", name.c_str(), counter);
765         }
766
767         return node->input(name.c_str());
768 }
769
770 static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
771 {
772         string name = b_socket.name();
773
774         if (node_use_modified_socket_name(node)) {
775                 BL::Node::outputs_iterator b_output;
776                 bool found = false;
777                 int counter = 0, total = 0;
778
779                 for (b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
780                         if (b_output->name() == name) {
781                                 if (!found)
782                                         counter++;
783                                 total++;
784                         }
785
786                         if(b_output->ptr.data == b_socket.ptr.data)
787                                 found = true;
788                 }
789
790                 /* rename if needed */
791                 if (name == "Shader")
792                         name = "Closure";
793
794                 if (total > 1)
795                         name = string_printf("%s%d", name.c_str(), counter);
796         }
797
798         return node->output(name.c_str());
799 }
800
801 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree,
802                       const ProxyMap &proxy_input_map, const ProxyMap &proxy_output_map)
803 {
804         /* add nodes */
805         BL::ShaderNodeTree::nodes_iterator b_node;
806         PtrInputMap input_map;
807         PtrOutputMap output_map;
808
809         BL::Node::inputs_iterator b_input;
810         BL::Node::outputs_iterator b_output;
811
812         /* find the node to use for output if there are multiple */
813         bool found_active_output = false;
814         BL::ShaderNode output_node(PointerRNA_NULL);
815
816         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
817                 if (is_output_node(*b_node)) {
818                         BL::ShaderNodeOutputMaterial b_output_node(*b_node);
819
820                         if(b_output_node.is_active_output()) {
821                                 output_node = b_output_node;
822                                 found_active_output = true;
823                                 break;
824                         }
825                         else if(!output_node.ptr.data && !found_active_output) {
826                                 output_node = b_output_node;
827                         }
828                 }
829         }
830
831         /* add nodes */
832         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
833                 if (b_node->mute() || b_node->is_a(&RNA_NodeReroute)) {
834                         /* replace muted node with internal links */
835                         BL::Node::internal_links_iterator b_link;
836                         for (b_node->internal_links.begin(b_link); b_link != b_node->internal_links.end(); ++b_link) {
837                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(b_link->to_socket()));
838
839                                 input_map[b_link->from_socket().ptr.data] = proxy->inputs[0];
840                                 output_map[b_link->to_socket().ptr.data] = proxy->outputs[0];
841
842                                 graph->add(proxy);
843                         }
844                 }
845                 else if (b_node->is_a(&RNA_ShaderNodeGroup) || b_node->is_a(&RNA_NodeCustomGroup)) {
846                         
847                         BL::ShaderNodeTree b_group_ntree(PointerRNA_NULL);
848                         if (b_node->is_a(&RNA_ShaderNodeGroup))
849                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeGroup)(*b_node)).node_tree());
850                         else
851                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeCustomGroup)(*b_node)).node_tree());
852                         ProxyMap group_proxy_input_map, group_proxy_output_map;
853
854                         /* Add a proxy node for each socket
855                          * Do this even if the node group has no internal tree,
856                          * so that links have something to connect to and assert won't fail.
857                          */
858                         for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
859                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_input));
860                                 graph->add(proxy);
861
862                                 /* register the proxy node for internal binding */
863                                 group_proxy_input_map[b_input->identifier()] = proxy;
864
865                                 input_map[b_input->ptr.data] = proxy->inputs[0];
866
867                                 set_default_value(proxy->inputs[0], *b_node, *b_input, b_data, b_ntree);
868                         }
869                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
870                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_output));
871                                 graph->add(proxy);
872
873                                 /* register the proxy node for internal binding */
874                                 group_proxy_output_map[b_output->identifier()] = proxy;
875
876                                 output_map[b_output->ptr.data] = proxy->outputs[0];
877                         }
878                         
879                         if (b_group_ntree)
880                                 add_nodes(scene, b_data, b_scene, graph, b_group_ntree, group_proxy_input_map, group_proxy_output_map);
881                 }
882                 else if (b_node->is_a(&RNA_NodeGroupInput)) {
883                         /* map each socket to a proxy node */
884                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
885                                 ProxyMap::const_iterator proxy_it = proxy_input_map.find(b_output->identifier());
886                                 if (proxy_it != proxy_input_map.end()) {
887                                         ProxyNode *proxy = proxy_it->second;
888
889                                         output_map[b_output->ptr.data] = proxy->outputs[0];
890                                 }
891                         }
892                 }
893                 else if (b_node->is_a(&RNA_NodeGroupOutput)) {
894                         BL::NodeGroupOutput b_output_node(*b_node);
895                         /* only the active group output is used */
896                         if (b_output_node.is_active_output()) {
897                                 /* map each socket to a proxy node */
898                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
899                                         ProxyMap::const_iterator proxy_it = proxy_output_map.find(b_input->identifier());
900                                         if (proxy_it != proxy_output_map.end()) {
901                                                 ProxyNode *proxy = proxy_it->second;
902
903                                                 input_map[b_input->ptr.data] = proxy->inputs[0];
904
905                                                 set_default_value(proxy->inputs[0], *b_node, *b_input, b_data, b_ntree);
906                                         }
907                                 }
908                         }
909                 }
910                 else {
911                         ShaderNode *node = NULL;
912
913                         if (is_output_node(*b_node)) {
914                                 if (b_node->ptr.data == output_node.ptr.data) {
915                                         node = graph->output();
916                                 }
917                         }
918                         else {
919                                 node = add_node(scene, b_data, b_scene, graph, b_ntree, BL::ShaderNode(*b_node));
920                         }
921
922                         if(node) {
923                                 /* map node sockets for linking */
924                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
925                                         ShaderInput *input = node_find_input_by_name(node, *b_node, *b_input);
926                                         if (!input) {
927                                                 /* XXX should not happen, report error? */
928                                                 continue;
929                                         }
930                                         input_map[b_input->ptr.data] = input;
931
932                                         set_default_value(input, *b_node, *b_input, b_data, b_ntree);
933                                 }
934                                 for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
935                                         ShaderOutput *output = node_find_output_by_name(node, *b_node, *b_output);
936                                         if (!output) {
937                                                 /* XXX should not happen, report error? */
938                                                 continue;
939                                         }
940                                         output_map[b_output->ptr.data] = output;
941                                 }
942                         }
943                 }
944         }
945
946         /* connect nodes */
947         BL::NodeTree::links_iterator b_link;
948
949         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
950                 /* get blender link data */
951                 BL::NodeSocket b_from_sock = b_link->from_socket();
952                 BL::NodeSocket b_to_sock = b_link->to_socket();
953
954                 ShaderOutput *output = 0;
955                 ShaderInput *input = 0;
956
957                 PtrOutputMap::iterator output_it = output_map.find(b_from_sock.ptr.data);
958                 if (output_it != output_map.end())
959                         output = output_it->second;
960                 PtrInputMap::iterator input_it = input_map.find(b_to_sock.ptr.data);
961                 if (input_it != input_map.end())
962                         input = input_it->second;
963
964                 /* either node may be NULL when the node was not exported, typically
965                  * because the node type is not supported */
966                 if(output && input)
967                         graph->connect(output, input);
968         }
969 }
970
971 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree)
972 {
973         static const ProxyMap empty_proxy_map;
974         add_nodes(scene, b_data, b_scene, graph, b_ntree, empty_proxy_map, empty_proxy_map);
975 }
976
977 /* Sync Materials */
978
979 void BlenderSync::sync_materials(bool update_all)
980 {
981         shader_map.set_default(scene->shaders[scene->default_surface]);
982
983         /* material loop */
984         BL::BlendData::materials_iterator b_mat;
985
986         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
987                 Shader *shader;
988
989                 /* test if we need to sync */
990                 if(shader_map.sync(&shader, *b_mat) || update_all) {
991                         ShaderGraph *graph = new ShaderGraph();
992
993                         shader->name = b_mat->name().c_str();
994                         shader->pass_id = b_mat->pass_index();
995
996                         /* create nodes */
997                         if(b_mat->use_nodes() && b_mat->node_tree()) {
998                                 BL::ShaderNodeTree b_ntree(b_mat->node_tree());
999
1000                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
1001                         }
1002                         else {
1003                                 ShaderNode *closure, *out;
1004
1005                                 closure = graph->add(new DiffuseBsdfNode());
1006                                 closure->input("Color")->value = get_float3(b_mat->diffuse_color());
1007                                 out = graph->output();
1008
1009                                 graph->connect(closure->output("BSDF"), out->input("Surface"));
1010                         }
1011
1012                         /* settings */
1013                         PointerRNA cmat = RNA_pointer_get(&b_mat->ptr, "cycles");
1014                         shader->use_mis = get_boolean(cmat, "sample_as_light");
1015                         shader->use_transparent_shadow = get_boolean(cmat, "use_transparent_shadow");
1016                         shader->heterogeneous_volume = !get_boolean(cmat, "homogeneous_volume");
1017                         shader->volume_sampling_method = RNA_enum_get(&cmat, "volume_sampling");
1018                         shader->volume_interpolation_method = RNA_enum_get(&cmat, "volume_interpolation");
1019
1020                         shader->set_graph(graph);
1021                         shader->tag_update(scene);
1022                 }
1023         }
1024 }
1025
1026 /* Sync World */
1027
1028 void BlenderSync::sync_world(bool update_all)
1029 {
1030         Background *background = scene->background;
1031         Background prevbackground = *background;
1032
1033         BL::World b_world = b_scene.world();
1034
1035         if(world_recalc || update_all || b_world.ptr.data != world_map) {
1036                 Shader *shader = scene->shaders[scene->default_background];
1037                 ShaderGraph *graph = new ShaderGraph();
1038
1039                 /* create nodes */
1040                 if(b_world && b_world.use_nodes() && b_world.node_tree()) {
1041                         BL::ShaderNodeTree b_ntree(b_world.node_tree());
1042
1043                         add_nodes(scene, b_data, b_scene, graph, b_ntree);
1044
1045                         /* volume */
1046                         PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
1047                         shader->heterogeneous_volume = !get_boolean(cworld, "homogeneous_volume");
1048                         shader->volume_sampling_method = RNA_enum_get(&cworld, "volume_sampling");
1049                         shader->volume_interpolation_method = RNA_enum_get(&cworld, "volume_interpolation");
1050                 }
1051                 else if(b_world) {
1052                         ShaderNode *closure, *out;
1053
1054                         closure = graph->add(new BackgroundNode());
1055                         closure->input("Color")->value = get_float3(b_world.horizon_color());
1056                         out = graph->output();
1057
1058                         graph->connect(closure->output("Background"), out->input("Surface"));
1059                 }
1060
1061                 if(b_world) {
1062                         /* AO */
1063                         BL::WorldLighting b_light = b_world.light_settings();
1064
1065                         if(b_light.use_ambient_occlusion())
1066                                 background->ao_factor = b_light.ao_factor();
1067                         else
1068                                 background->ao_factor = 0.0f;
1069
1070                         background->ao_distance = b_light.distance();
1071
1072                         /* visibility */
1073                         PointerRNA cvisibility = RNA_pointer_get(&b_world.ptr, "cycles_visibility");
1074                         uint visibility = 0;
1075
1076                         visibility |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
1077                         visibility |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
1078                         visibility |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
1079                         visibility |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
1080                         visibility |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
1081
1082                         background->visibility = visibility;
1083                 }
1084
1085                 shader->set_graph(graph);
1086                 shader->tag_update(scene);
1087                 background->tag_update(scene);
1088         }
1089
1090         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
1091
1092         /* when doing preview render check for BI's transparency settings,
1093          * this is so because bledner's preview render routines are not able
1094          * to tweak all cycles's settings depending on different circumstances
1095          */
1096         if(b_engine.is_preview() == false)
1097                 background->transparent = get_boolean(cscene, "film_transparent");
1098         else
1099                 background->transparent = b_scene.render().alpha_mode() == BL::RenderSettings::alpha_mode_TRANSPARENT;
1100
1101         background->use = render_layer.use_background;
1102
1103         if(background->modified(prevbackground))
1104                 background->tag_update(scene);
1105 }
1106
1107 /* Sync Lamps */
1108
1109 void BlenderSync::sync_lamps(bool update_all)
1110 {
1111         shader_map.set_default(scene->shaders[scene->default_light]);
1112
1113         /* lamp loop */
1114         BL::BlendData::lamps_iterator b_lamp;
1115
1116         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp) {
1117                 Shader *shader;
1118
1119                 /* test if we need to sync */
1120                 if(shader_map.sync(&shader, *b_lamp) || update_all) {
1121                         ShaderGraph *graph = new ShaderGraph();
1122
1123                         /* create nodes */
1124                         if(b_lamp->use_nodes() && b_lamp->node_tree()) {
1125                                 shader->name = b_lamp->name().c_str();
1126
1127                                 BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
1128
1129                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
1130                         }
1131                         else {
1132                                 ShaderNode *closure, *out;
1133                                 float strength = 1.0f;
1134
1135                                 if(b_lamp->type() == BL::Lamp::type_POINT ||
1136                                    b_lamp->type() == BL::Lamp::type_SPOT ||
1137                                    b_lamp->type() == BL::Lamp::type_AREA)
1138                                 {
1139                                         strength = 100.0f;
1140                                 }
1141
1142                                 closure = graph->add(new EmissionNode());
1143                                 closure->input("Color")->value = get_float3(b_lamp->color());
1144                                 closure->input("Strength")->value.x = strength;
1145                                 out = graph->output();
1146
1147                                 graph->connect(closure->output("Emission"), out->input("Surface"));
1148                         }
1149
1150                         shader->set_graph(graph);
1151                         shader->tag_update(scene);
1152                 }
1153         }
1154 }
1155
1156 void BlenderSync::sync_shaders()
1157 {
1158         /* for auto refresh images */
1159         bool auto_refresh_update = false;
1160
1161         if(preview) {
1162                 ImageManager *image_manager = scene->image_manager;
1163                 int frame = b_scene.frame_current();
1164                 auto_refresh_update = image_manager->set_animation_frame_update(frame);
1165         }
1166
1167         shader_map.pre_sync();
1168
1169         sync_world(auto_refresh_update);
1170         sync_lamps(auto_refresh_update);
1171         sync_materials(auto_refresh_update);
1172
1173         /* false = don't delete unused shaders, not supported */
1174         shader_map.post_sync(false);
1175 }
1176
1177 CCL_NAMESPACE_END
1178