Cycles: svn merge -r41531:41613 ^/trunk/blender
[blender.git] / source / blender / editors / uvedit / uvedit_ops.c
index 62b875c..173ab80 100644 (file)
@@ -37,7 +37,9 @@
 #include "MEM_guardedalloc.h"
 
 #include "DNA_object_types.h"
+#include "DNA_material_types.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_node_types.h"
 #include "DNA_scene_types.h"
 
 #include "BLI_math.h"
 #include "BKE_depsgraph.h"
 #include "BKE_image.h"
 #include "BKE_library.h"
+#include "BKE_main.h"
+#include "BKE_material.h"
 #include "BKE_mesh.h"
+#include "BKE_node.h"
 #include "BKE_report.h"
+#include "BKE_scene.h"
 
 #include "ED_image.h"
 #include "ED_mesh.h"
+#include "ED_node.h"
 #include "ED_uvedit.h"
 #include "ED_object.h"
 #include "ED_screen.h"
@@ -87,9 +94,46 @@ int ED_uvedit_test(Object *obedit)
        return ret;
 }
 
+/**************************** object active image *****************************/
+
+static int is_image_texture_node(bNode *node)
+{
+       return ELEM(node->type, SH_NODE_TEX_IMAGE, SH_NODE_TEX_ENVIRONMENT);
+}
+
+int ED_object_get_active_image(Object *ob, int mat_nr, Image **ima, ImageUser **iuser, bNode **node_r)
+{
+       Material *ma= give_current_material(ob, mat_nr);
+       bNode *node= (ma && ma->use_nodes)? nodeGetActiveTexture(ma->nodetree): NULL;
+
+       if(node && is_image_texture_node(node)) {
+               if(ima) *ima= (Image*)node->id;
+               if(iuser) *iuser= NULL;
+               if(node_r) *node_r= node;
+               return TRUE;
+       }
+       
+       if(ima) *ima= NULL;
+       if(iuser) *iuser= NULL;
+       if(node_r) *node_r= node;
+
+       return FALSE;
+}
+
+void ED_object_assign_active_image(Main *bmain, Object *ob, int mat_nr, Image *ima)
+{
+       Material *ma= give_current_material(ob, mat_nr);
+       bNode *node= (ma && ma->use_nodes)? nodeGetActiveTexture(ma->nodetree): NULL;
+
+       if(node && is_image_texture_node(node)) {
+               node->id= &ima->id;
+               ED_node_generic_update(bmain, ma->nodetree, node);
+       }
+}
+
 /************************* assign image ************************/
 
-void ED_uvedit_assign_image(Scene *scene, Object *obedit, Image *ima, Image *previma)
+void ED_uvedit_assign_image(Main *bmain, Scene *scene, Object *obedit, Image *ima, Image *previma)
 {
        EditMesh *em;
        EditFace *efa;
@@ -109,35 +153,46 @@ void ED_uvedit_assign_image(Scene *scene, Object *obedit, Image *ima, Image *pre
                BKE_mesh_end_editmesh(obedit->data, em);
                return;
        }
-       
-       /* ensure we have a uv layer */
-       if(!CustomData_has_layer(&em->fdata, CD_MTFACE)) {
-               EM_add_data_layer(em, &em->fdata, CD_MTFACE, NULL);
-               update= 1;
+
+       if(scene_use_new_shading_nodes(scene)) {
+               /* new shading system, assign image in material */
+               int sloppy= 1;
+               EditFace *efa= EM_get_actFace(em, sloppy);
+
+               if(efa)
+                       ED_object_assign_active_image(bmain, obedit, efa->mat_nr, ima);
        }
+       else {
+               /* old shading system, assign image to selected faces */
+               
+               /* ensure we have a uv layer */
+               if(!CustomData_has_layer(&em->fdata, CD_MTFACE)) {
+                       EM_add_data_layer(em, &em->fdata, CD_MTFACE, NULL);
+                       update= 1;
+               }
 
-       /* now assign to all visible faces */
-       for(efa= em->faces.first; efa; efa= efa->next) {
-               tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
+               /* now assign to all visible faces */
+               for(efa= em->faces.first; efa; efa= efa->next) {
+                       tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 
-               if(uvedit_face_visible(scene, previma, efa, tf)) {
-                       if(ima) {
-                               tf->tpage= ima;
-                               
-                               if(ima->id.us==0) id_us_plus(&ima->id);
-                               else id_lib_extern(&ima->id);
-                       }
-                       else {
-                               tf->tpage= NULL;
-                       }
+                       if(uvedit_face_visible(scene, previma, efa, tf)) {
+                               if(ima) {
+                                       tf->tpage= ima;
+                                       
+                                       if(ima->id.us==0) id_us_plus(&ima->id);
+                                       else id_lib_extern(&ima->id);
+                               }
+                               else
+                                       tf->tpage= NULL;
 
-                       update = 1;
+                               update = 1;
+                       }
                }
-       }
 
-       /* and update depdency graph */
-       if(update)
-               DAG_id_tag_update(obedit->data, 0);
+               /* and update depdency graph */
+               if(update)
+                       DAG_id_tag_update(obedit->data, 0);
+       }
 
        BKE_mesh_end_editmesh(obedit->data, em);
 }