Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / mesh_convert.c
index 17071b6d7ea5db892dfd751e3c62c9061922cb65..a48a74f2d67e73b8894621b95042adfcba272f2d 100644 (file)
@@ -22,6 +22,7 @@
  *  \ingroup bke
  */
 
+
 #include "MEM_guardedalloc.h"
 
 #include "DNA_scene_types.h"
 #include "BKE_displist.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
-#include "BKE_modifier.h"
 #include "BKE_mball.h"
-#include "BKE_depsgraph.h"
+/* these 2 are only used by conversion functions */
 #include "BKE_curve.h"
 /* -- */
 #include "BKE_object.h"
 
 #include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
 
 /* Define for cases when you want extra validation of mesh
  * after certain modifications.
@@ -479,6 +480,58 @@ int BKE_mesh_nurbs_displist_to_mdata(
        return 0;
 }
 
+Mesh *BKE_mesh_new_nomain_from_curve_displist(Object *ob, ListBase *dispbase)
+{
+       Curve *cu = ob->data;
+       Mesh *mesh;
+       MVert *allvert;
+       MEdge *alledge;
+       MLoop *allloop;
+       MPoly *allpoly;
+       MLoopUV *alluv = NULL;
+       int totvert, totedge, totloop, totpoly;
+       bool use_orco_uv = (cu->flag & CU_UV_ORCO) != 0;
+
+       if (BKE_mesh_nurbs_displist_to_mdata(
+               ob, dispbase, &allvert, &totvert, &alledge,
+               &totedge, &allloop, &allpoly, (use_orco_uv) ? &alluv : NULL,
+               &totloop, &totpoly) != 0)
+       {
+               /* Error initializing mdata. This often happens when curve is empty */
+               return BKE_mesh_new_nomain(0, 0, 0, 0, 0);
+       }
+
+       mesh = BKE_mesh_new_nomain(totvert, totedge, 0, totloop, totpoly);
+       mesh->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
+
+       memcpy(mesh->mvert, allvert, totvert * sizeof(MVert));
+       memcpy(mesh->medge, alledge, totedge * sizeof(MEdge));
+       memcpy(mesh->mloop, allloop, totloop * sizeof(MLoop));
+       memcpy(mesh->mpoly, allpoly, totpoly * sizeof(MPoly));
+
+       if (alluv) {
+               const char *uvname = "Orco";
+               CustomData_add_layer_named(&mesh->ldata, CD_MLOOPUV, CD_ASSIGN, alluv, totloop, uvname);
+       }
+
+       MEM_freeN(allvert);
+       MEM_freeN(alledge);
+       MEM_freeN(allloop);
+       MEM_freeN(allpoly);
+
+       return mesh;
+}
+
+Mesh *BKE_mesh_new_nomain_from_curve(Object *ob)
+{
+       ListBase disp = {NULL, NULL};
+
+       if (ob->curve_cache) {
+               disp = ob->curve_cache->disp;
+       }
+
+       return BKE_mesh_new_nomain_from_curve_displist(ob, &disp);
+}
 
 /* this may fail replacing ob->data, be sure to check ob->type */
 void BKE_mesh_from_nurbs_displist(Object *ob, ListBase *dispbase, const bool use_orco_uv, const char *obdata_name)
@@ -521,7 +574,6 @@ void BKE_mesh_from_nurbs_displist(Object *ob, ListBase *dispbase, const bool use
 
                if (alluv) {
                        const char *uvname = "Orco";
-                       me->mtpoly = CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DEFAULT, NULL, me->totpoly, uvname);
                        me->mloopuv = CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_ASSIGN, alluv, me->totloop, uvname);
                }
 
@@ -741,10 +793,10 @@ void BKE_mesh_to_curve_nurblist(DerivedMesh *dm, ListBase *nurblist, const int e
        }
 }
 
-void BKE_mesh_to_curve(Scene *scene, Object *ob)
+void BKE_mesh_to_curve(Depsgraph *depsgraph, Scene *scene, Object *ob)
 {
        /* make new mesh data from the original copy */
-       DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_MESH);
+       DerivedMesh *dm = mesh_get_derived_final(depsgraph, scene, ob, CD_MASK_MESH);
        ListBase nurblist = {NULL, NULL};
        bool needsFree = false;
 
@@ -781,13 +833,13 @@ void BKE_mesh_to_curve(Scene *scene, Object *ob)
 
 /* settings: 1 - preview, 2 - render */
 Mesh *BKE_mesh_new_from_object(
-        Main *bmain, Scene *sce, Object *ob,
-        int apply_modifiers, int settings, int calc_tessface, int calc_undeformed)
+        Depsgraph *depsgraph, Main *bmain, Scene *sce, Object *ob,
+        const bool apply_modifiers, const bool calc_tessface, const bool calc_undeformed)
 {
        Mesh *tmpmesh;
        Curve *tmpcu = NULL, *copycu;
        int i;
-       const bool render = (settings == eModifierMode_Render);
+       const bool render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
        const bool cage = !apply_modifiers;
        bool do_mat_id_data_us = true;
 
@@ -840,7 +892,7 @@ Mesh *BKE_mesh_new_from_object(
                        copycu->editnurb = tmpcu->editnurb;
 
                        /* get updated display list, and convert to a mesh */
-                       BKE_displist_make_curveTypes_forRender(sce, tmpobj, &dispbase, &derivedFinal, false, render);
+                       BKE_displist_make_curveTypes_forRender(depsgraph, sce, tmpobj, &dispbase, &derivedFinal, false, render);
 
                        copycu->editfont = NULL;
                        copycu->editnurb = NULL;
@@ -856,7 +908,7 @@ Mesh *BKE_mesh_new_from_object(
                        BKE_displist_free(&dispbase);
 
                        /* BKE_mesh_from_nurbs changes the type to a mesh, check it worked.
-                        * if it didn't the curve did not have any segments or otherwise 
+                        * if it didn't the curve did not have any segments or otherwise
                         * would have generated an empty mesh */
                        if (tmpobj->type != OB_MESH) {
                                BKE_libblock_free_us(bmain, tmpobj);
@@ -889,13 +941,7 @@ Mesh *BKE_mesh_new_from_object(
 
                        if (render) {
                                ListBase disp = {NULL, NULL};
-                               /* TODO(sergey): This is gonna to work for until EvaluationContext
-                                *               only contains for_render flag. As soon as CoW is
-                                *               implemented, this is to be rethought.
-                                */
-                               EvaluationContext eval_ctx;
-                               DEG_evaluation_context_init(&eval_ctx, DAG_EVAL_RENDER);
-                               BKE_displist_make_mball_forRender(&eval_ctx, sce, ob, &disp);
+                               BKE_displist_make_mball_forRender(depsgraph, sce, ob, &disp);
                                BKE_mesh_from_metaball(&disp, tmpmesh);
                                BKE_displist_free(&disp);
                        }
@@ -934,9 +980,9 @@ Mesh *BKE_mesh_new_from_object(
 
                                /* Write the display mesh into the dummy mesh */
                                if (render)
-                                       dm = mesh_create_derived_render(sce, ob, mask);
+                                       dm = mesh_create_derived_render(depsgraph, sce, ob, mask);
                                else
-                                       dm = mesh_create_derived_view(sce, ob, mask);
+                                       dm = mesh_create_derived_view(depsgraph, sce, ob, mask);
 
                                tmpmesh = BKE_mesh_add(bmain, ((ID *)ob->data)->name + 2);
                                DM_to_mesh(dm, tmpmesh, ob, mask, true);