Cycles: Code cleanup, spaces around keywords
[blender-staging.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::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_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 #else
547                 (void)b_data;
548                 (void)b_ntree;
549 #endif
550         }
551         else if(b_node.is_a(&RNA_ShaderNodeTexImage)) {
552                 BL::ShaderNodeTexImage b_image_node(b_node);
553                 BL::Image b_image(b_image_node.image());
554                 ImageTextureNode *image = new ImageTextureNode();
555                 if(b_image) {
556                         /* builtin images will use callback-based reading because
557                          * they could only be loaded correct from blender side
558                          */
559                         bool is_builtin = b_image.packed_file() ||
560                                           b_image.source() == BL::Image::source_GENERATED ||
561                                           b_image.source() == BL::Image::source_MOVIE;
562
563                         if(is_builtin) {
564                                 /* for builtin images we're using image datablock name to find an image to
565                                  * read pixels from later
566                                  *
567                                  * also store frame number as well, so there's no differences in handling
568                                  * builtin names for packed images and movies
569                                  */
570                                 int scene_frame = b_scene.frame_current();
571                                 int image_frame = image_user_frame_number(b_image_node.image_user(), scene_frame);
572                                 image->filename = b_image.name() + "@" + string_printf("%d", image_frame);
573                                 image->builtin_data = b_image.ptr.data;
574                         }
575                         else {
576                                 image->filename = image_user_file_path(b_image_node.image_user(), b_image, b_scene.frame_current());
577                                 image->builtin_data = NULL;
578                         }
579
580                         image->animated = b_image_node.image_user().use_auto_refresh();
581                         image->use_alpha = b_image.use_alpha();
582
583                         /* TODO(sergey): Does not work properly when we change builtin type. */
584                         if(b_image.is_updated()) {
585                                 scene->image_manager->tag_reload_image(image->filename,
586                                                                        image->builtin_data,
587                                                                        (InterpolationType)b_image_node.interpolation());
588                         }
589                 }
590                 image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
591                 image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()];
592                 image->interpolation = (InterpolationType)b_image_node.interpolation();
593                 image->projection_blend = b_image_node.projection_blend();
594                 get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
595                 node = image;
596         }
597         else if(b_node.is_a(&RNA_ShaderNodeTexEnvironment)) {
598                 BL::ShaderNodeTexEnvironment b_env_node(b_node);
599                 BL::Image b_image(b_env_node.image());
600                 EnvironmentTextureNode *env = new EnvironmentTextureNode();
601                 if(b_image) {
602                         bool is_builtin = b_image.packed_file() ||
603                                           b_image.source() == BL::Image::source_GENERATED ||
604                                           b_image.source() == BL::Image::source_MOVIE;
605
606                         if(is_builtin) {
607                                 int scene_frame = b_scene.frame_current();
608                                 int image_frame = image_user_frame_number(b_env_node.image_user(), scene_frame);
609                                 env->filename = b_image.name() + "@" + string_printf("%d", image_frame);
610                                 env->builtin_data = b_image.ptr.data;
611                         }
612                         else {
613                                 env->filename = image_user_file_path(b_env_node.image_user(), b_image, b_scene.frame_current());
614                                 env->animated = b_env_node.image_user().use_auto_refresh();
615                                 env->builtin_data = NULL;
616                         }
617
618                         env->use_alpha = b_image.use_alpha();
619
620                         /* TODO(sergey): Does not work properly when we change builtin type. */
621                         if(b_image.is_updated()) {
622                                 scene->image_manager->tag_reload_image(env->filename,
623                                                                        env->builtin_data,
624                                                                        INTERPOLATION_LINEAR);
625                         }
626                 }
627                 env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
628                 env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()];
629                 get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
630                 node = env;
631         }
632         else if(b_node.is_a(&RNA_ShaderNodeTexGradient)) {
633                 BL::ShaderNodeTexGradient b_gradient_node(b_node);
634                 GradientTextureNode *gradient = new GradientTextureNode();
635                 gradient->type = GradientTextureNode::type_enum[(int)b_gradient_node.gradient_type()];
636                 get_tex_mapping(&gradient->tex_mapping, b_gradient_node.texture_mapping());
637                 node = gradient;
638         }
639         else if(b_node.is_a(&RNA_ShaderNodeTexVoronoi)) {
640                 BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
641                 VoronoiTextureNode *voronoi = new VoronoiTextureNode();
642                 voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
643                 get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
644                 node = voronoi;
645         }
646         else if(b_node.is_a(&RNA_ShaderNodeTexMagic)) {
647                 BL::ShaderNodeTexMagic b_magic_node(b_node);
648                 MagicTextureNode *magic = new MagicTextureNode();
649                 magic->depth = b_magic_node.turbulence_depth();
650                 get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
651                 node = magic;
652         }
653         else if(b_node.is_a(&RNA_ShaderNodeTexWave)) {
654                 BL::ShaderNodeTexWave b_wave_node(b_node);
655                 WaveTextureNode *wave = new WaveTextureNode();
656                 wave->type = WaveTextureNode::type_enum[(int)b_wave_node.wave_type()];
657                 get_tex_mapping(&wave->tex_mapping, b_wave_node.texture_mapping());
658                 node = wave;
659         }
660         else if(b_node.is_a(&RNA_ShaderNodeTexChecker)) {
661                 BL::ShaderNodeTexChecker b_checker_node(b_node);
662                 CheckerTextureNode *checker = new CheckerTextureNode();
663                 get_tex_mapping(&checker->tex_mapping, b_checker_node.texture_mapping());
664                 node = checker;
665         }
666         else if(b_node.is_a(&RNA_ShaderNodeTexBrick)) {
667                 BL::ShaderNodeTexBrick b_brick_node(b_node);
668                 BrickTextureNode *brick = new BrickTextureNode();
669                 brick->offset = b_brick_node.offset();
670                 brick->offset_frequency = b_brick_node.offset_frequency();
671                 brick->squash = b_brick_node.squash();
672                 brick->squash_frequency = b_brick_node.squash_frequency();
673                 get_tex_mapping(&brick->tex_mapping, b_brick_node.texture_mapping());
674                 node = brick;
675         }
676         else if(b_node.is_a(&RNA_ShaderNodeTexNoise)) {
677                 BL::ShaderNodeTexNoise b_noise_node(b_node);
678                 NoiseTextureNode *noise = new NoiseTextureNode();
679                 get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
680                 node = noise;
681         }
682         else if(b_node.is_a(&RNA_ShaderNodeTexMusgrave)) {
683                 BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
684                 MusgraveTextureNode *musgrave = new MusgraveTextureNode();
685                 musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
686                 get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
687                 node = musgrave;
688         }
689         else if(b_node.is_a(&RNA_ShaderNodeTexCoord)) {
690                 BL::ShaderNodeTexCoord b_tex_coord_node(b_node);
691                 TextureCoordinateNode *tex_coord = new TextureCoordinateNode();
692                 tex_coord->from_dupli = b_tex_coord_node.from_dupli();
693                 if(b_tex_coord_node.object()) {
694                         tex_coord->use_transform = true;
695                         tex_coord->ob_tfm = get_transform(b_tex_coord_node.object().matrix_world());
696                 }
697                 node = tex_coord;
698         }
699         else if(b_node.is_a(&RNA_ShaderNodeTexSky)) {
700                 BL::ShaderNodeTexSky b_sky_node(b_node);
701                 SkyTextureNode *sky = new SkyTextureNode();
702                 sky->type = SkyTextureNode::type_enum[(int)b_sky_node.sky_type()];
703                 sky->sun_direction = normalize(get_float3(b_sky_node.sun_direction()));
704                 sky->turbidity = b_sky_node.turbidity();
705                 sky->ground_albedo = b_sky_node.ground_albedo();
706                 get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
707                 node = sky;
708         }
709         else if(b_node.is_a(&RNA_ShaderNodeNormalMap)) {
710                 BL::ShaderNodeNormalMap b_normal_map_node(b_node);
711                 NormalMapNode *nmap = new NormalMapNode();
712                 nmap->space = NormalMapNode::space_enum[(int)b_normal_map_node.space()];
713                 nmap->attribute = b_normal_map_node.uv_map();
714                 node = nmap;
715         }
716         else if(b_node.is_a(&RNA_ShaderNodeTangent)) {
717                 BL::ShaderNodeTangent b_tangent_node(b_node);
718                 TangentNode *tangent = new TangentNode();
719                 tangent->direction_type = TangentNode::direction_type_enum[(int)b_tangent_node.direction_type()];
720                 tangent->axis = TangentNode::axis_enum[(int)b_tangent_node.axis()];
721                 tangent->attribute = b_tangent_node.uv_map();
722                 node = tangent;
723         }
724         else if(b_node.is_a(&RNA_ShaderNodeUVMap)) {
725                 BL::ShaderNodeUVMap b_uvmap_node(b_node);
726                 UVMapNode *uvm = new UVMapNode();
727                 uvm->attribute = b_uvmap_node.uv_map();
728                 uvm->from_dupli = b_uvmap_node.from_dupli();
729                 node = uvm;
730         }
731
732         if(node)
733                 graph->add(node);
734
735         return node;
736 }
737
738 static bool node_use_modified_socket_name(ShaderNode *node)
739 {
740         if(node->special_type == SHADER_SPECIAL_TYPE_SCRIPT)
741                 return false;
742
743         return true;
744 }
745
746 static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
747 {
748         string name = b_socket.name();
749         
750         if(node_use_modified_socket_name(node)) {
751                 BL::Node::inputs_iterator b_input;
752                 bool found = false;
753                 int counter = 0, total = 0;
754
755                 for(b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
756                         if(b_input->name() == name) {
757                                 if(!found)
758                                         counter++;
759                                 total++;
760                         }
761
762                         if(b_input->ptr.data == b_socket.ptr.data)
763                                 found = true;
764                 }
765
766                 /* rename if needed */
767                 if(name == "Shader")
768                         name = "Closure";
769
770                 if(total > 1)
771                         name = string_printf("%s%d", name.c_str(), counter);
772         }
773
774         return node->input(name.c_str());
775 }
776
777 static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
778 {
779         string name = b_socket.name();
780
781         if(node_use_modified_socket_name(node)) {
782                 BL::Node::outputs_iterator b_output;
783                 bool found = false;
784                 int counter = 0, total = 0;
785
786                 for(b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
787                         if(b_output->name() == name) {
788                                 if(!found)
789                                         counter++;
790                                 total++;
791                         }
792
793                         if(b_output->ptr.data == b_socket.ptr.data)
794                                 found = true;
795                 }
796
797                 /* rename if needed */
798                 if(name == "Shader")
799                         name = "Closure";
800
801                 if(total > 1)
802                         name = string_printf("%s%d", name.c_str(), counter);
803         }
804
805         return node->output(name.c_str());
806 }
807
808 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree,
809                       const ProxyMap &proxy_input_map, const ProxyMap &proxy_output_map)
810 {
811         /* add nodes */
812         BL::ShaderNodeTree::nodes_iterator b_node;
813         PtrInputMap input_map;
814         PtrOutputMap output_map;
815
816         BL::Node::inputs_iterator b_input;
817         BL::Node::outputs_iterator b_output;
818
819         /* find the node to use for output if there are multiple */
820         bool found_active_output = false;
821         BL::ShaderNode output_node(PointerRNA_NULL);
822
823         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
824                 if(is_output_node(*b_node)) {
825                         BL::ShaderNodeOutputMaterial b_output_node(*b_node);
826
827                         if(b_output_node.is_active_output()) {
828                                 output_node = b_output_node;
829                                 found_active_output = true;
830                                 break;
831                         }
832                         else if(!output_node.ptr.data && !found_active_output) {
833                                 output_node = b_output_node;
834                         }
835                 }
836         }
837
838         /* add nodes */
839         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
840                 if(b_node->mute() || b_node->is_a(&RNA_NodeReroute)) {
841                         /* replace muted node with internal links */
842                         BL::Node::internal_links_iterator b_link;
843                         for(b_node->internal_links.begin(b_link); b_link != b_node->internal_links.end(); ++b_link) {
844                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(b_link->to_socket()));
845
846                                 input_map[b_link->from_socket().ptr.data] = proxy->inputs[0];
847                                 output_map[b_link->to_socket().ptr.data] = proxy->outputs[0];
848
849                                 graph->add(proxy);
850                         }
851                 }
852                 else if(b_node->is_a(&RNA_ShaderNodeGroup) || b_node->is_a(&RNA_NodeCustomGroup)) {
853                         
854                         BL::ShaderNodeTree b_group_ntree(PointerRNA_NULL);
855                         if(b_node->is_a(&RNA_ShaderNodeGroup))
856                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeGroup)(*b_node)).node_tree());
857                         else
858                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeCustomGroup)(*b_node)).node_tree());
859                         ProxyMap group_proxy_input_map, group_proxy_output_map;
860
861                         /* Add a proxy node for each socket
862                          * Do this even if the node group has no internal tree,
863                          * so that links have something to connect to and assert won't fail.
864                          */
865                         for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
866                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_input));
867                                 graph->add(proxy);
868
869                                 /* register the proxy node for internal binding */
870                                 group_proxy_input_map[b_input->identifier()] = proxy;
871
872                                 input_map[b_input->ptr.data] = proxy->inputs[0];
873
874                                 set_default_value(proxy->inputs[0], *b_input, b_data, b_ntree);
875                         }
876                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
877                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_output));
878                                 graph->add(proxy);
879
880                                 /* register the proxy node for internal binding */
881                                 group_proxy_output_map[b_output->identifier()] = proxy;
882
883                                 output_map[b_output->ptr.data] = proxy->outputs[0];
884                         }
885                         
886                         if(b_group_ntree)
887                                 add_nodes(scene, b_data, b_scene, graph, b_group_ntree, group_proxy_input_map, group_proxy_output_map);
888                 }
889                 else if(b_node->is_a(&RNA_NodeGroupInput)) {
890                         /* map each socket to a proxy node */
891                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
892                                 ProxyMap::const_iterator proxy_it = proxy_input_map.find(b_output->identifier());
893                                 if(proxy_it != proxy_input_map.end()) {
894                                         ProxyNode *proxy = proxy_it->second;
895
896                                         output_map[b_output->ptr.data] = proxy->outputs[0];
897                                 }
898                         }
899                 }
900                 else if(b_node->is_a(&RNA_NodeGroupOutput)) {
901                         BL::NodeGroupOutput b_output_node(*b_node);
902                         /* only the active group output is used */
903                         if(b_output_node.is_active_output()) {
904                                 /* map each socket to a proxy node */
905                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
906                                         ProxyMap::const_iterator proxy_it = proxy_output_map.find(b_input->identifier());
907                                         if(proxy_it != proxy_output_map.end()) {
908                                                 ProxyNode *proxy = proxy_it->second;
909
910                                                 input_map[b_input->ptr.data] = proxy->inputs[0];
911
912                                                 set_default_value(proxy->inputs[0], *b_input, b_data, b_ntree);
913                                         }
914                                 }
915                         }
916                 }
917                 else {
918                         ShaderNode *node = NULL;
919
920                         if(is_output_node(*b_node)) {
921                                 if(b_node->ptr.data == output_node.ptr.data) {
922                                         node = graph->output();
923                                 }
924                         }
925                         else {
926                                 node = add_node(scene, b_data, b_scene, graph, b_ntree, BL::ShaderNode(*b_node));
927                         }
928
929                         if(node) {
930                                 /* map node sockets for linking */
931                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
932                                         ShaderInput *input = node_find_input_by_name(node, *b_node, *b_input);
933                                         if(!input) {
934                                                 /* XXX should not happen, report error? */
935                                                 continue;
936                                         }
937                                         input_map[b_input->ptr.data] = input;
938
939                                         set_default_value(input, *b_input, b_data, b_ntree);
940                                 }
941                                 for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
942                                         ShaderOutput *output = node_find_output_by_name(node, *b_node, *b_output);
943                                         if(!output) {
944                                                 /* XXX should not happen, report error? */
945                                                 continue;
946                                         }
947                                         output_map[b_output->ptr.data] = output;
948                                 }
949                         }
950                 }
951         }
952
953         /* connect nodes */
954         BL::NodeTree::links_iterator b_link;
955
956         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
957                 /* Ignore invalid links to avoid unwanted cycles created in graph. */
958                 if(!b_link->is_valid()) {
959                         continue;
960                 }
961                 /* get blender link data */
962                 BL::NodeSocket b_from_sock = b_link->from_socket();
963                 BL::NodeSocket b_to_sock = b_link->to_socket();
964
965                 ShaderOutput *output = 0;
966                 ShaderInput *input = 0;
967
968                 PtrOutputMap::iterator output_it = output_map.find(b_from_sock.ptr.data);
969                 if(output_it != output_map.end())
970                         output = output_it->second;
971                 PtrInputMap::iterator input_it = input_map.find(b_to_sock.ptr.data);
972                 if(input_it != input_map.end())
973                         input = input_it->second;
974
975                 /* either node may be NULL when the node was not exported, typically
976                  * because the node type is not supported */
977                 if(output && input)
978                         graph->connect(output, input);
979         }
980 }
981
982 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree)
983 {
984         static const ProxyMap empty_proxy_map;
985         add_nodes(scene, b_data, b_scene, graph, b_ntree, empty_proxy_map, empty_proxy_map);
986 }
987
988 /* Sync Materials */
989
990 void BlenderSync::sync_materials(bool update_all)
991 {
992         shader_map.set_default(scene->shaders[scene->default_surface]);
993
994         /* material loop */
995         BL::BlendData::materials_iterator b_mat;
996
997         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
998                 Shader *shader;
999
1000                 /* test if we need to sync */
1001                 if(shader_map.sync(&shader, *b_mat) || update_all) {
1002                         ShaderGraph *graph = new ShaderGraph();
1003
1004                         shader->name = b_mat->name().c_str();
1005                         shader->pass_id = b_mat->pass_index();
1006
1007                         /* create nodes */
1008                         if(b_mat->use_nodes() && b_mat->node_tree()) {
1009                                 BL::ShaderNodeTree b_ntree(b_mat->node_tree());
1010
1011                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
1012                         }
1013                         else {
1014                                 ShaderNode *closure, *out;
1015
1016                                 closure = graph->add(new DiffuseBsdfNode());
1017                                 closure->input("Color")->value = get_float3(b_mat->diffuse_color());
1018                                 out = graph->output();
1019
1020                                 graph->connect(closure->output("BSDF"), out->input("Surface"));
1021                         }
1022
1023                         /* settings */
1024                         PointerRNA cmat = RNA_pointer_get(&b_mat->ptr, "cycles");
1025                         shader->use_mis = get_boolean(cmat, "sample_as_light");
1026                         shader->use_transparent_shadow = get_boolean(cmat, "use_transparent_shadow");
1027                         shader->heterogeneous_volume = !get_boolean(cmat, "homogeneous_volume");
1028                         shader->volume_sampling_method = (VolumeSampling)RNA_enum_get(&cmat, "volume_sampling");
1029                         shader->volume_interpolation_method = (VolumeInterpolation)RNA_enum_get(&cmat, "volume_interpolation");
1030
1031                         shader->set_graph(graph);
1032                         shader->tag_update(scene);
1033                 }
1034         }
1035 }
1036
1037 /* Sync World */
1038
1039 void BlenderSync::sync_world(bool update_all)
1040 {
1041         Background *background = scene->background;
1042         Background prevbackground = *background;
1043
1044         BL::World b_world = b_scene.world();
1045
1046         if(world_recalc || update_all || b_world.ptr.data != world_map) {
1047                 Shader *shader = scene->shaders[scene->default_background];
1048                 ShaderGraph *graph = new ShaderGraph();
1049
1050                 /* create nodes */
1051                 if(b_world && b_world.use_nodes() && b_world.node_tree()) {
1052                         BL::ShaderNodeTree b_ntree(b_world.node_tree());
1053
1054                         add_nodes(scene, b_data, b_scene, graph, b_ntree);
1055
1056                         /* volume */
1057                         PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
1058                         shader->heterogeneous_volume = !get_boolean(cworld, "homogeneous_volume");
1059                         shader->volume_sampling_method = (VolumeSampling)RNA_enum_get(&cworld, "volume_sampling");
1060                         shader->volume_interpolation_method = (VolumeInterpolation)RNA_enum_get(&cworld, "volume_interpolation");
1061                 }
1062                 else if(b_world) {
1063                         ShaderNode *closure, *out;
1064
1065                         closure = graph->add(new BackgroundNode());
1066                         closure->input("Color")->value = get_float3(b_world.horizon_color());
1067                         out = graph->output();
1068
1069                         graph->connect(closure->output("Background"), out->input("Surface"));
1070                 }
1071
1072                 if(b_world) {
1073                         /* AO */
1074                         BL::WorldLighting b_light = b_world.light_settings();
1075
1076                         if(b_light.use_ambient_occlusion())
1077                                 background->ao_factor = b_light.ao_factor();
1078                         else
1079                                 background->ao_factor = 0.0f;
1080
1081                         background->ao_distance = b_light.distance();
1082
1083                         /* visibility */
1084                         PointerRNA cvisibility = RNA_pointer_get(&b_world.ptr, "cycles_visibility");
1085                         uint visibility = 0;
1086
1087                         visibility |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
1088                         visibility |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
1089                         visibility |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
1090                         visibility |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
1091                         visibility |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
1092
1093                         background->visibility = visibility;
1094                 }
1095
1096                 shader->set_graph(graph);
1097                 shader->tag_update(scene);
1098                 background->tag_update(scene);
1099         }
1100
1101         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
1102
1103         /* when doing preview render check for BI's transparency settings,
1104          * this is so because bledner's preview render routines are not able
1105          * to tweak all cycles's settings depending on different circumstances
1106          */
1107         if(b_engine.is_preview() == false)
1108                 background->transparent = get_boolean(cscene, "film_transparent");
1109         else
1110                 background->transparent = b_scene.render().alpha_mode() == BL::RenderSettings::alpha_mode_TRANSPARENT;
1111
1112         background->use = render_layer.use_background;
1113
1114         if(background->modified(prevbackground))
1115                 background->tag_update(scene);
1116 }
1117
1118 /* Sync Lamps */
1119
1120 void BlenderSync::sync_lamps(bool update_all)
1121 {
1122         shader_map.set_default(scene->shaders[scene->default_light]);
1123
1124         /* lamp loop */
1125         BL::BlendData::lamps_iterator b_lamp;
1126
1127         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp) {
1128                 Shader *shader;
1129
1130                 /* test if we need to sync */
1131                 if(shader_map.sync(&shader, *b_lamp) || update_all) {
1132                         ShaderGraph *graph = new ShaderGraph();
1133
1134                         /* create nodes */
1135                         if(b_lamp->use_nodes() && b_lamp->node_tree()) {
1136                                 shader->name = b_lamp->name().c_str();
1137
1138                                 BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
1139
1140                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
1141                         }
1142                         else {
1143                                 ShaderNode *closure, *out;
1144                                 float strength = 1.0f;
1145
1146                                 if(b_lamp->type() == BL::Lamp::type_POINT ||
1147                                    b_lamp->type() == BL::Lamp::type_SPOT ||
1148                                    b_lamp->type() == BL::Lamp::type_AREA)
1149                                 {
1150                                         strength = 100.0f;
1151                                 }
1152
1153                                 closure = graph->add(new EmissionNode());
1154                                 closure->input("Color")->value = get_float3(b_lamp->color());
1155                                 closure->input("Strength")->value.x = strength;
1156                                 out = graph->output();
1157
1158                                 graph->connect(closure->output("Emission"), out->input("Surface"));
1159                         }
1160
1161                         shader->set_graph(graph);
1162                         shader->tag_update(scene);
1163                 }
1164         }
1165 }
1166
1167 void BlenderSync::sync_shaders()
1168 {
1169         /* for auto refresh images */
1170         bool auto_refresh_update = false;
1171
1172         if(preview) {
1173                 ImageManager *image_manager = scene->image_manager;
1174                 int frame = b_scene.frame_current();
1175                 auto_refresh_update = image_manager->set_animation_frame_update(frame);
1176         }
1177
1178         shader_map.pre_sync();
1179
1180         sync_world(auto_refresh_update);
1181         sync_lamps(auto_refresh_update);
1182         sync_materials(auto_refresh_update);
1183
1184         /* false = don't delete unused shaders, not supported */
1185         shader_map.post_sync(false);
1186 }
1187
1188 CCL_NAMESPACE_END
1189