Cycles: add location/rotate/scale and XYZ mapping options for all texture nodes,
[blender.git] / intern / cycles / blender / blender_shader.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "background.h"
20 #include "graph.h"
21 #include "light.h"
22 #include "nodes.h"
23 #include "scene.h"
24 #include "shader.h"
25
26 #include "blender_sync.h"
27 #include "blender_util.h"
28
29 #include "util_debug.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 typedef map<void*, ShaderNode*> PtrNodeMap;
34 typedef pair<ShaderNode*, std::string> SocketPair;
35 typedef map<void*, SocketPair> PtrSockMap;
36
37 /* Find */
38
39 void BlenderSync::find_shader(BL::ID id, vector<uint>& used_shaders)
40 {
41         Shader *shader = shader_map.find(id);
42
43         for(size_t i = 0; i < scene->shaders.size(); i++) {
44                 if(scene->shaders[i] == shader) {
45                         used_shaders.push_back(i);
46                         break;
47                 }
48         }
49 }
50
51 /* Graph */
52
53 static BL::NodeSocket get_node_input(BL::Node *b_group_node, BL::NodeSocket b_in)
54 {
55         if(b_group_node) {
56
57                 BL::NodeTree b_ntree = BL::NodeGroup(*b_group_node).node_tree();
58                 BL::NodeTree::links_iterator b_link;
59
60                 for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
61                         if(b_link->to_socket().ptr.data == b_in.ptr.data) {
62                                 BL::Node::inputs_iterator b_gin;
63
64                                 for(b_group_node->inputs.begin(b_gin); b_gin != b_group_node->inputs.end(); ++b_gin)
65                                         if(b_gin->group_socket().ptr.data == b_link->from_socket().ptr.data)
66                                                 return *b_gin;
67
68                         }
69                 }
70         }
71
72         return b_in;
73 }
74
75 static BL::NodeSocket get_node_output(BL::Node b_node, const string& name)
76 {
77         BL::Node::outputs_iterator b_out;
78
79         for(b_node.outputs.begin(b_out); b_out != b_node.outputs.end(); ++b_out)
80                 if(b_out->name() == name)
81                         return *b_out;
82
83         assert(0);
84
85         return *b_out;
86 }
87
88 static float3 get_node_output_rgba(BL::Node b_node, const string& name)
89 {
90         BL::NodeSocketRGBA sock(get_node_output(b_node, name));
91         return get_float3(sock.default_value());
92 }
93
94 static float get_node_output_value(BL::Node b_node, const string& name)
95 {
96         BL::NodeSocketFloatNone sock(get_node_output(b_node, name));
97         return sock.default_value();
98 }
99
100 static void get_tex_mapping(TextureMapping *mapping, BL::TexMapping b_mapping)
101 {
102         mapping->translation = get_float3(b_mapping.location());
103         mapping->rotation = get_float3(b_mapping.rotation())*(M_PI/180.0f); /* in degrees! */
104         mapping->scale = get_float3(b_mapping.scale());
105
106         mapping->x_mapping = (TextureMapping::Mapping)b_mapping.mapping_x();
107         mapping->y_mapping = (TextureMapping::Mapping)b_mapping.mapping_y();
108         mapping->z_mapping = (TextureMapping::Mapping)b_mapping.mapping_z();
109 }
110
111 static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *b_group_node, BL::ShaderNode b_node)
112 {
113         ShaderNode *node = NULL;
114
115         switch(b_node.type()) {
116                 /* not supported */
117                 case BL::ShaderNode::type_CAMERA: break;
118                 case BL::ShaderNode::type_COMBRGB: break;
119                 case BL::ShaderNode::type_CURVE_RGB: break;
120                 case BL::ShaderNode::type_CURVE_VEC: break;
121                 case BL::ShaderNode::type_GEOM: break;
122                 case BL::ShaderNode::type_HUE_SAT: break;
123                 case BL::ShaderNode::type_INVERT: break;
124                 case BL::ShaderNode::type_MATERIAL: break;
125                 case BL::ShaderNode::type_MATERIAL_EXT: break;
126                 case BL::ShaderNode::type_NORMAL: break;
127                 case BL::ShaderNode::type_OUTPUT: break;
128                 case BL::ShaderNode::type_SCRIPT: break;
129                 case BL::ShaderNode::type_SEPRGB: break;
130                 case BL::ShaderNode::type_SQUEEZE: break;
131                 case BL::ShaderNode::type_TEXTURE: break;
132                 case BL::ShaderNode::type_VALTORGB: break;
133                 /* handled outside this function */
134                 case BL::ShaderNode::type_GROUP: break;
135                 /* existing blender nodes */
136                 case BL::ShaderNode::type_RGB: {
137                         ColorNode *color = new ColorNode();
138                         color->value = get_node_output_rgba(b_node, "Color");
139                         node = color;
140                         break;
141                 }
142                 case BL::ShaderNode::type_VALUE: {
143                         ValueNode *value = new ValueNode();
144                         value->value = get_node_output_value(b_node, "Value");
145                         node = value;
146                         break;
147                 }
148                 case BL::ShaderNode::type_MIX_RGB: {
149                         BL::ShaderNodeMixRGB b_mix_node(b_node);
150                         MixNode *mix = new MixNode();
151                         mix->type = MixNode::type_enum[b_mix_node.blend_type()];
152                         node = mix;
153                         break;
154                 }
155                 case BL::ShaderNode::type_RGBTOBW: {
156                         node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
157                         break;
158                 }
159                 case BL::ShaderNode::type_MATH: {
160                         BL::ShaderNodeMath b_math_node(b_node);
161                         MathNode *math = new MathNode();
162                         math->type = MathNode::type_enum[b_math_node.operation()];
163                         node = math;
164                         break;
165                 }
166                 case BL::ShaderNode::type_VECT_MATH: {
167                         BL::ShaderNodeVectorMath b_vector_math_node(b_node);
168                         VectorMathNode *vmath = new VectorMathNode();
169                         vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
170                         node = vmath;
171                         break;
172                 }
173                 case BL::ShaderNode::type_MAPPING: {
174                         BL::ShaderNodeMapping b_mapping_node(b_node);
175                         MappingNode *mapping = new MappingNode();
176
177                         get_tex_mapping(&mapping->tex_mapping, b_mapping_node.mapping());
178
179                         node = mapping;
180                         break;
181                 }
182
183                 /* new nodes */
184                 case BL::ShaderNode::type_OUTPUT_MATERIAL:
185                 case BL::ShaderNode::type_OUTPUT_WORLD:
186                 case BL::ShaderNode::type_OUTPUT_LAMP: {
187                         node = graph->output();
188                         break;
189                 }
190                 case BL::ShaderNode::type_FRESNEL: {
191                         node = new FresnelNode();
192                         break;
193                 }
194                 case BL::ShaderNode::type_BLEND_WEIGHT: {
195                         node = new BlendWeightNode();
196                         break;
197                 }
198                 case BL::ShaderNode::type_ADD_SHADER: {
199                         node = new AddClosureNode();
200                         break;
201                 }
202                 case BL::ShaderNode::type_MIX_SHADER: {
203                         node = new MixClosureNode();
204                         break;
205                 }
206                 case BL::ShaderNode::type_ATTRIBUTE: {
207                         BL::ShaderNodeAttribute b_attr_node(b_node);
208                         AttributeNode *attr = new AttributeNode();
209                         attr->attribute = b_attr_node.attribute_name();
210                         node = attr;
211                         break;
212                 }
213                 case BL::ShaderNode::type_BACKGROUND: {
214                         node = new BackgroundNode();
215                         break;
216                 }
217                 case BL::ShaderNode::type_HOLDOUT: {
218                         node = new HoldoutNode();
219                         break;
220                 }
221                 case BL::ShaderNode::type_BSDF_ANISOTROPIC: {
222                         node = new WardBsdfNode();
223                         break;
224                 }
225                 case BL::ShaderNode::type_BSDF_DIFFUSE: {
226                         node = new DiffuseBsdfNode();
227                         break;
228                 }
229                 case BL::ShaderNode::type_BSDF_GLOSSY: {
230                         BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
231                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
232
233                         switch(b_glossy_node.distribution()) {
234                                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
235                                         glossy->distribution = ustring("Sharp");
236                                         break;
237                                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
238                                         glossy->distribution = ustring("Beckmann");
239                                         break;
240                                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
241                                         glossy->distribution = ustring("GGX");
242                                         break;
243                         }
244                         node = glossy;
245                         break;
246                 }
247                 case BL::ShaderNode::type_BSDF_GLASS: {
248                         BL::ShaderNodeBsdfGlass b_glass_node(b_node);
249                         GlassBsdfNode *glass = new GlassBsdfNode();
250                         switch(b_glass_node.distribution()) {
251                                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
252                                         glass->distribution = ustring("Sharp");
253                                         break;
254                                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
255                                         glass->distribution = ustring("Beckmann");
256                                         break;
257                                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
258                                         glass->distribution = ustring("GGX");
259                                         break;
260                         }
261                         node = glass;
262                         break;
263                 }
264                 case BL::ShaderNode::type_BSDF_TRANSLUCENT: {
265                         node = new TranslucentBsdfNode();
266                         break;
267                 }
268                 case BL::ShaderNode::type_BSDF_TRANSPARENT: {
269                         node = new TransparentBsdfNode();
270                         break;
271                 }
272                 case BL::ShaderNode::type_BSDF_VELVET: {
273                         node = new VelvetBsdfNode();
274                         break;
275                 }
276                 case BL::ShaderNode::type_EMISSION: {
277                         node = new EmissionNode();
278                         break;
279                 }
280                 case BL::ShaderNode::type_VOLUME_ISOTROPIC: {
281                         node = new IsotropicVolumeNode();
282                         break;
283                 }
284                 case BL::ShaderNode::type_VOLUME_TRANSPARENT: {
285                         node = new TransparentVolumeNode();
286                         break;
287                 }
288                 case BL::ShaderNode::type_GEOMETRY: {
289                         node = new GeometryNode();
290                         break;
291                 }
292                 case BL::ShaderNode::type_LIGHT_PATH: {
293                         node = new LightPathNode();
294                         break;
295                 }
296                 case BL::ShaderNode::type_TEX_IMAGE: {
297                         BL::ShaderNodeTexImage b_image_node(b_node);
298                         BL::Image b_image(b_image_node.image());
299                         ImageTextureNode *image = new ImageTextureNode();
300                         /* todo: handle generated/builtin images */
301                         if(b_image)
302                                 image->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
303                         image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
304                         get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
305                         node = image;
306                         break;
307                 }
308                 case BL::ShaderNode::type_TEX_ENVIRONMENT: {
309                         BL::ShaderNodeTexEnvironment b_env_node(b_node);
310                         BL::Image b_image(b_env_node.image());
311                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
312                         if(b_image)
313                                 env->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
314                         env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
315                         get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
316                         node = env;
317                         break;
318                 }
319                 case BL::ShaderNode::type_TEX_NOISE: {
320                         BL::ShaderNodeTexNoise b_noise_node(b_node);
321                         NoiseTextureNode *noise = new NoiseTextureNode();
322                         get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
323                         node = noise;
324                         break;
325                 }
326                 case BL::ShaderNode::type_TEX_BLEND: {
327                         BL::ShaderNodeTexBlend b_blend_node(b_node);
328                         BlendTextureNode *blend = new BlendTextureNode();
329                         blend->progression = BlendTextureNode::progression_enum[(int)b_blend_node.progression()];
330                         blend->axis = BlendTextureNode::axis_enum[(int)b_blend_node.axis()];
331                         get_tex_mapping(&blend->tex_mapping, b_blend_node.texture_mapping());
332                         node = blend;
333                         break;
334                 }
335                 case BL::ShaderNode::type_TEX_VORONOI: {
336                         BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
337                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
338                         voronoi->distance_metric = VoronoiTextureNode::distance_metric_enum[(int)b_voronoi_node.distance_metric()];
339                         voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
340                         get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
341                         node = voronoi;
342                         break;
343                 }
344                 case BL::ShaderNode::type_TEX_MAGIC: {
345                         BL::ShaderNodeTexMagic b_magic_node(b_node);
346                         MagicTextureNode *magic = new MagicTextureNode();
347                         magic->depth = b_magic_node.turbulence_depth();
348                         get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
349                         node = magic;
350                         break;
351                 }
352                 case BL::ShaderNode::type_TEX_MARBLE: {
353                         BL::ShaderNodeTexMarble b_marble_node(b_node);
354                         MarbleTextureNode *marble = new MarbleTextureNode();
355                         marble->depth = b_marble_node.turbulence_depth();
356                         marble->basis = MarbleTextureNode::basis_enum[(int)b_marble_node.noise_basis()];
357                         marble->type = MarbleTextureNode::type_enum[(int)b_marble_node.marble_type()];
358                         marble->wave = MarbleTextureNode::wave_enum[(int)b_marble_node.wave_type()];
359                         marble->hard = b_marble_node.noise_type() == BL::ShaderNodeTexMarble::noise_type_HARD;
360                         get_tex_mapping(&marble->tex_mapping, b_marble_node.texture_mapping());
361                         node = marble;
362                         break;
363                 }
364                 case BL::ShaderNode::type_TEX_CLOUDS: {
365                         BL::ShaderNodeTexClouds b_clouds_node(b_node);
366                         CloudsTextureNode *clouds = new CloudsTextureNode();
367                         clouds->depth = b_clouds_node.turbulence_depth();
368                         clouds->basis = CloudsTextureNode::basis_enum[(int)b_clouds_node.noise_basis()];
369                         clouds->hard = b_clouds_node.noise_type() == BL::ShaderNodeTexClouds::noise_type_HARD;
370                         get_tex_mapping(&clouds->tex_mapping, b_clouds_node.texture_mapping());
371                         node = clouds;
372                         break;
373                 }
374                 case BL::ShaderNode::type_TEX_WOOD: {
375                         BL::ShaderNodeTexWood b_wood_node(b_node);
376                         WoodTextureNode *wood = new WoodTextureNode();
377                         wood->type = WoodTextureNode::type_enum[(int)b_wood_node.wood_type()];
378                         wood->basis = WoodTextureNode::basis_enum[(int)b_wood_node.noise_basis()];
379                         wood->hard = b_wood_node.noise_type() == BL::ShaderNodeTexWood::noise_type_HARD;
380                         wood->wave = WoodTextureNode::wave_enum[(int)b_wood_node.wave_type()];
381                         get_tex_mapping(&wood->tex_mapping, b_wood_node.texture_mapping());
382                         node = wood;
383                         break;
384                 }
385                 case BL::ShaderNode::type_TEX_MUSGRAVE: {
386                         BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
387                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
388                         musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
389                         musgrave->basis = MusgraveTextureNode::basis_enum[(int)b_musgrave_node.noise_basis()];
390                         get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
391                         node = musgrave;
392                         break;
393                 }
394                 case BL::ShaderNode::type_TEX_STUCCI: {
395                         BL::ShaderNodeTexStucci b_stucci_node(b_node);
396                         StucciTextureNode *stucci = new StucciTextureNode();
397                         stucci->type = StucciTextureNode::type_enum[(int)b_stucci_node.stucci_type()];
398                         stucci->basis = StucciTextureNode::basis_enum[(int)b_stucci_node.noise_basis()];
399                         stucci->hard = b_stucci_node.noise_type() == BL::ShaderNodeTexStucci::noise_type_HARD;
400                         get_tex_mapping(&stucci->tex_mapping, b_stucci_node.texture_mapping());
401                         node = stucci;
402                         break;
403                 }
404                 case BL::ShaderNode::type_TEX_DISTORTED_NOISE: {
405                         BL::ShaderNodeTexDistortedNoise b_distnoise_node(b_node);
406                         DistortedNoiseTextureNode *distnoise = new DistortedNoiseTextureNode();
407                         distnoise->basis = DistortedNoiseTextureNode::basis_enum[(int)b_distnoise_node.noise_basis()];
408                         distnoise->distortion_basis = DistortedNoiseTextureNode::basis_enum[(int)b_distnoise_node.noise_distortion()];
409                         get_tex_mapping(&distnoise->tex_mapping, b_distnoise_node.texture_mapping());
410                         node = distnoise;
411                         break;
412                 }
413                 case BL::ShaderNode::type_TEX_COORD: {
414                         node = new TextureCoordinateNode();;
415                         break;
416                 }
417                 case BL::ShaderNode::type_TEX_SKY: {
418                         BL::ShaderNodeTexSky b_sky_node(b_node);
419                         SkyTextureNode *sky = new SkyTextureNode();
420                         sky->sun_direction = get_float3(b_sky_node.sun_direction());
421                         sky->turbidity = b_sky_node.turbidity();
422                         get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
423                         node = sky;
424                         break;
425                 }
426         }
427
428         if(node && node != graph->output())
429                 graph->add(node);
430
431         return node;
432 }
433
434 static SocketPair node_socket_map_pair(PtrNodeMap& node_map, BL::Node b_node, BL::NodeSocket b_socket)
435 {
436         BL::Node::inputs_iterator b_input;
437         BL::Node::outputs_iterator b_output;
438         string name = b_socket.name();
439         bool found = false;
440         int counter = 0, total = 0;
441
442         /* find in inputs */
443         for(b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
444                 if(b_input->name() == name) {
445                         if(!found)
446                                 counter++;
447                         total++;
448                 }
449
450                 if(b_input->ptr.data == b_socket.ptr.data)
451                         found = true;
452         }
453
454         if(!found) {
455                 /* find in outputs */
456                 found = false;
457                 counter = 0;
458                 total = 0;
459
460                 for(b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
461                         if(b_output->name() == name) {
462                                 if(!found)
463                                         counter++;
464                                 total++;
465                         }
466
467                         if(b_output->ptr.data == b_socket.ptr.data)
468                                 found = true;
469                 }
470         }
471
472         /* rename if needed */
473         if(name == "Shader")
474                 name = "Closure";
475
476         if(total > 1)
477                 name = string_printf("%s%d", name.c_str(), counter);
478
479         return SocketPair(node_map[b_node.ptr.data], name);
480 }
481
482 static void add_nodes(BL::BlendData b_data, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, BL::Node *b_group_node, PtrSockMap& sockets_map)
483 {
484         /* add nodes */
485         BL::ShaderNodeTree::nodes_iterator b_node;
486         PtrNodeMap node_map;
487         map<void*, PtrSockMap> node_groups;
488
489         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
490                 if(b_node->is_a(&RNA_NodeGroup)) {
491                         BL::NodeGroup b_gnode(*b_node);
492                         BL::ShaderNodeTree b_group_ntree(b_gnode.node_tree());
493
494                         node_groups[b_node->ptr.data] = PtrSockMap();
495                         add_nodes(b_data, graph, b_group_ntree, &b_gnode, node_groups[b_node->ptr.data]);
496                 }
497                 else {
498                         ShaderNode *node = add_node(b_data, graph, b_group_node, BL::ShaderNode(*b_node));
499
500                         if(node) {
501                                 BL::Node::inputs_iterator b_input;
502                                 BL::Node::outputs_iterator b_output;
503
504                                 node_map[b_node->ptr.data] = node;
505
506                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
507                                         SocketPair pair = node_socket_map_pair(node_map, *b_node, *b_input);
508                                         ShaderInput *input = pair.first->input(pair.second.c_str());
509                                         BL::NodeSocket sock(get_node_input(b_group_node, *b_input));
510
511                                         assert(input);
512
513                                         /* copy values for non linked inputs */
514                                         switch(input->type) {
515                                                 case SHADER_SOCKET_FLOAT: {
516                                                         BL::NodeSocketFloatNone value_sock(sock);
517                                                         input->set(value_sock.default_value());
518                                                         break;
519                                                 }
520                                                 case SHADER_SOCKET_COLOR: {
521                                                         BL::NodeSocketRGBA rgba_sock(sock);
522                                                         input->set(get_float3(rgba_sock.default_value()));
523                                                         break;
524                                                 }
525                                                 case SHADER_SOCKET_NORMAL:
526                                                 case SHADER_SOCKET_POINT:
527                                                 case SHADER_SOCKET_VECTOR: {
528                                                         BL::NodeSocketVectorNone vec_sock(sock);
529                                                         input->set(get_float3(vec_sock.default_value()));
530                                                         break;
531                                                 }
532                                                 case SHADER_SOCKET_CLOSURE:
533                                                         break;
534                                         }
535                                 }
536                         }
537                 }
538         }
539
540         /* connect nodes */
541         BL::NodeTree::links_iterator b_link;
542
543         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
544                 /* get blender link data */
545                 BL::Node b_from_node = b_link->from_node();
546                 BL::Node b_to_node = b_link->to_node();
547
548                 BL::NodeSocket b_from_sock = b_link->from_socket();
549                 BL::NodeSocket b_to_sock = b_link->to_socket();
550
551                 /* if link with group socket, add to map so we can connect it later */
552                 if(b_group_node) {
553                         if(!b_from_node) {
554                                 sockets_map[b_from_sock.ptr.data] =
555                                         node_socket_map_pair(node_map, b_to_node, b_to_sock);
556
557                                 continue;
558                         }
559                         else if(!b_to_node) {
560                                 sockets_map[b_to_sock.ptr.data] =
561                                         node_socket_map_pair(node_map, b_from_node, b_from_sock);
562
563                                 continue;
564                         }
565                 }
566
567                 SocketPair from_pair, to_pair;
568
569                 /* from sock */
570                 if(b_from_node.is_a(&RNA_NodeGroup)) {
571                         /* group node */
572                         BL::NodeSocket group_sock = b_from_sock.group_socket();
573                         from_pair = node_groups[b_from_node.ptr.data][group_sock.ptr.data];
574                 }
575                 else {
576                         /* regular node */
577                         from_pair = node_socket_map_pair(node_map, b_from_node, b_from_sock);
578                 }
579
580                 /* to sock */
581                 if(b_to_node.is_a(&RNA_NodeGroup)) {
582                         /* group node */
583                         BL::NodeSocket group_sock = b_to_sock.group_socket();
584                         to_pair = node_groups[b_to_node.ptr.data][group_sock.ptr.data];
585                 }
586                 else {
587                         /* regular node */
588                         to_pair = node_socket_map_pair(node_map, b_to_node, b_to_sock);
589                 }
590
591                 /* in case of groups there may not actually be a node inside the group
592                    that the group socket connects to, so from_node or to_node may be NULL */
593                 if(from_pair.first && to_pair.first) {
594                         ShaderOutput *output = from_pair.first->output(from_pair.second.c_str());
595                         ShaderInput *input = to_pair.first->input(to_pair.second.c_str());
596
597                         graph->connect(output, input);
598                 }
599         }
600 }
601
602 /* Sync Materials */
603
604 void BlenderSync::sync_materials()
605 {
606         shader_map.set_default(scene->shaders[scene->default_surface]);
607
608         /* material loop */
609         BL::BlendData::materials_iterator b_mat;
610
611         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
612                 Shader *shader;
613                 
614                 /* test if we need to sync */
615                 if(shader_map.sync(&shader, *b_mat)) {
616                         ShaderGraph *graph = new ShaderGraph();
617
618                         shader->name = b_mat->name().c_str();
619
620                         /* create nodes */
621                         if(b_mat->use_nodes() && b_mat->node_tree()) {
622                                 PtrSockMap sock_to_node;
623                                 BL::ShaderNodeTree b_ntree(b_mat->node_tree());
624
625                                 add_nodes(b_data, graph, b_ntree, NULL, sock_to_node);
626                         }
627                         else {
628                                 ShaderNode *closure, *out;
629
630                                 closure = graph->add(new DiffuseBsdfNode());
631                                 closure->input("Color")->value = get_float3(b_mat->diffuse_color());
632                                 out = graph->output();
633
634                                 graph->connect(closure->output("BSDF"), out->input("Surface"));
635                         }
636
637                         /* settings */
638                         PointerRNA cmat = RNA_pointer_get(&b_mat->ptr, "cycles");
639                         shader->sample_as_light = get_boolean(cmat, "sample_as_light");
640                         shader->homogeneous_volume = get_boolean(cmat, "homogeneous_volume");
641
642                         shader->set_graph(graph);
643                         shader->tag_update(scene);
644                 }
645         }
646 }
647
648 /* Sync World */
649
650 void BlenderSync::sync_world()
651 {
652         Background *background = scene->background;
653         Background prevbackground = *background;
654
655         BL::World b_world = b_scene.world();
656
657         if(world_recalc || b_world.ptr.data != world_map) {
658                 Shader *shader = scene->shaders[scene->default_background];
659                 ShaderGraph *graph = new ShaderGraph();
660
661                 /* create nodes */
662                 if(b_world && b_world.use_nodes() && b_world.node_tree()) {
663                         PtrSockMap sock_to_node;
664                         BL::ShaderNodeTree b_ntree(b_world.node_tree());
665
666                         add_nodes(b_data, graph, b_ntree, NULL, sock_to_node);
667                 }
668                 else if(b_world) {
669                         ShaderNode *closure, *out;
670
671                         closure = graph->add(new BackgroundNode());
672                         closure->input("Color")->value = get_float3(b_world.horizon_color());
673                         out = graph->output();
674
675                         graph->connect(closure->output("Background"), out->input("Surface"));
676                 }
677
678                 shader->set_graph(graph);
679                 shader->tag_update(scene);
680         }
681
682         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
683         background->transparent = get_boolean(cscene, "film_transparent");
684
685         if(background->modified(prevbackground))
686                 background->tag_update(scene);
687
688         world_map = b_world.ptr.data;
689         world_recalc = false;
690 }
691
692 /* Sync Lamps */
693
694 void BlenderSync::sync_lamps()
695 {
696         shader_map.set_default(scene->shaders[scene->default_light]);
697
698         /* lamp loop */
699         BL::BlendData::lamps_iterator b_lamp;
700
701         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp) {
702                 Shader *shader;
703                 
704                 /* test if we need to sync */
705                 if(shader_map.sync(&shader, *b_lamp)) {
706                         ShaderGraph *graph = new ShaderGraph();
707
708                         /* create nodes */
709                         if(b_lamp->use_nodes() && b_lamp->node_tree()) {
710                                 shader->name = b_lamp->name().c_str();
711
712                                 PtrSockMap sock_to_node;
713                                 BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
714
715                                 add_nodes(b_data, graph, b_ntree, NULL, sock_to_node);
716                         }
717                         else {
718                                 ShaderNode *closure, *out;
719
720                                 closure = graph->add(new EmissionNode());
721                                 closure->input("Color")->value = get_float3(b_lamp->color());
722                                 closure->input("Strength")->value.x = b_lamp->energy()*10.0f;
723                                 out = graph->output();
724
725                                 graph->connect(closure->output("Emission"), out->input("Surface"));
726                         }
727
728                         shader->set_graph(graph);
729                         shader->tag_update(scene);
730                 }
731         }
732 }
733
734 void BlenderSync::sync_shaders()
735 {
736         shader_map.pre_sync();
737
738         sync_world();
739         sync_lamps();
740         sync_materials();
741
742         /* false = don't delete unused shaders, not supported */
743         shader_map.post_sync(false);
744 }
745
746 CCL_NAMESPACE_END
747