Implement bvhtree.fromObject
authorDalai Felinto <dfelinto@gmail.com>
Tue, 4 Dec 2018 22:47:13 +0000 (20:47 -0200)
committerDalai Felinto <dfelinto@gmail.com>
Wed, 5 Dec 2018 09:58:58 +0000 (07:58 -0200)
Passing depsgraph instead of scene, since a scene does not fully define the
state of object you want to use for the BVH.

Also, mesh_create_eval_final_view and mesh_create_eval_final_render are pretty
much the same, so mesh_create_eval_no_deform and
mesh_create_eval_no_deform_render are as well.

Issue reported on: T58734

Reviewers: sergey
https://developer.blender.org/D4032

source/blender/blenkernel/BKE_DerivedMesh.h
source/blender/blenkernel/BKE_mesh_runtime.h
source/blender/blenkernel/intern/DerivedMesh.c
source/blender/python/mathutils/CMakeLists.txt
source/blender/python/mathutils/mathutils_bvhtree.c

index baec7e773289fc049a3793094d2c554a08345f4b..e209938cc801216f54c11bb89baaeddb5881f470 100644 (file)
@@ -504,14 +504,6 @@ DerivedMesh *mesh_create_derived_render(
 
 /* same as above but wont use render settings */
 DerivedMesh *mesh_create_derived(struct Mesh *me, float (*vertCos)[3]);
-DerivedMesh *mesh_create_derived_no_deform(
-        struct Depsgraph *depsgraph, struct Scene *scene,
-        struct Object *ob, float (*vertCos)[3],
-        CustomDataMask dataMask);
-DerivedMesh *mesh_create_derived_no_deform_render(
-        struct Depsgraph *depsgraph, struct Scene *scene,
-        struct Object *ob, float (*vertCos)[3],
-        CustomDataMask dataMask);
 
 struct Mesh *editbmesh_get_eval_cage(
         struct Depsgraph *depsgraph, struct Scene *scene, struct Object *,
index 5d35f798ad71e5c8d06abe2fc9d2c87cee30145f..8f9de85a3028299a53b82daf43df61948719a1b5 100644 (file)
@@ -109,6 +109,16 @@ struct Mesh *mesh_create_eval_final_view(
         struct Depsgraph *depsgraph, struct Scene *scene,
         struct Object *ob, CustomDataMask dataMask);
 
+struct Mesh *mesh_create_eval_no_deform(
+        struct Depsgraph *depsgraph, struct Scene *scene,
+        struct Object *ob, float (*vertCos)[3],
+        CustomDataMask dataMask);
+struct Mesh *mesh_create_eval_no_deform_render(
+        struct Depsgraph *depsgraph, struct Scene *scene,
+        struct Object *ob, float (*vertCos)[3],
+        CustomDataMask dataMask);
+
+
 void BKE_mesh_runtime_eval_to_meshkey(struct Mesh *me_deformed, struct Mesh *me, struct KeyBlock *kb);
 
 
index abad99a49094a2c71fe8060b49963346e73d23ab..aa1966b00537e6a1b7a2b0a38311e906d507b257 100644 (file)
@@ -2348,13 +2348,26 @@ Mesh *mesh_create_eval_final_view(
        return final;
 }
 
-DerivedMesh *mesh_create_derived_no_deform(
+Mesh *mesh_create_eval_no_deform(
         struct Depsgraph *depsgraph, Scene *scene, Object *ob,
         float (*vertCos)[3], CustomDataMask dataMask)
 {
-       DerivedMesh *final;
+       Mesh *final;
 
-       mesh_calc_modifiers_dm(
+       mesh_calc_modifiers(
+               depsgraph, scene, ob, vertCos, 0, false, dataMask, -1, false, false,
+               NULL, &final);
+
+       return final;
+}
+
+Mesh *mesh_create_eval_no_deform_render(
+        struct Depsgraph *depsgraph, Scene *scene, Object *ob,
+        float (*vertCos)[3], CustomDataMask dataMask)
+{
+       Mesh *final;
+
+       mesh_calc_modifiers(
                depsgraph, scene, ob, vertCos, 0, false, dataMask, -1, false, false,
                NULL, &final);
 
index adf7c85d7c9296c3c3b5f85864ab0d3f382cd503..811cc1acbabadae0d8bae6b939e5bcc4d40ec9c8 100644 (file)
@@ -23,6 +23,7 @@ set(INC
        ../../blenlib
        ../../blenkernel
        ../../bmesh
+       ../../depsgraph
        ../../makesdna
        ../../../../intern/guardedalloc
 )
index dba5d52846bc0a19ee915282575e9288b2088518..e671fe6a43b590b8927a1d47dff01d0a803b6dfd 100644 (file)
@@ -57,6 +57,8 @@
 #include "BKE_mesh.h"
 #include "BKE_mesh_runtime.h"
 
+#include "DEG_depsgraph_query.h"
+
 #include "bmesh.h"
 
 #include "../bmesh/bmesh_py_types.h"
@@ -1050,11 +1052,9 @@ static PyObject *C_BVHTree_FromBMesh(PyObject *UNUSED(cls), PyObject *args, PyOb
 
 /* return various derived meshes based on requested settings */
 static Mesh *bvh_get_mesh(
-        const char *funcname, struct Scene *scene, Object *ob,
+        const char *funcname, struct Depsgraph *depsgraph, struct Scene *scene, Object *ob,
         bool use_deform, bool use_render, bool use_cage)
 {
-       /* TODO: This doesn't work currently because of missing depsgraph. */
-#if 0
        /* we only need minimum mesh data for topology and vertex locations */
        CustomDataMask mask = CD_MASK_BAREMESH;
 
@@ -1067,15 +1067,15 @@ static Mesh *bvh_get_mesh(
                                return NULL;
                        }
                        else {
-                               return mesh_create_derived_render(scene, ob, mask);
+                               return mesh_create_eval_final_render(depsgraph, scene, ob, mask);
                        }
                }
                else {
                        if (use_cage) {
-                               return mesh_get_derived_deform(scene, ob, mask);  /* ob->derivedDeform */
+                               return mesh_get_eval_deform(depsgraph, scene, ob, mask);  /* ob->derivedDeform */
                        }
                        else {
-                               return mesh_get_derived_final(scene, ob, mask);  /* ob->derivedFinal */
+                               return mesh_get_eval_final(depsgraph, scene, ob, mask);  /* ob->derivedFinal */
                        }
                }
        }
@@ -1088,7 +1088,7 @@ static Mesh *bvh_get_mesh(
                                return NULL;
                        }
                        else {
-                               return mesh_create_derived_no_deform_render(scene, ob, NULL, mask);
+                               return mesh_create_eval_no_deform_render(depsgraph, scene, ob, NULL, mask);
                        }
                }
                else {
@@ -1098,26 +1098,21 @@ static Mesh *bvh_get_mesh(
                                return NULL;
                        }
                        else {
-                               return mesh_create_derived_no_deform(scene, ob, NULL, mask);
+                               return mesh_create_eval_no_deform(depsgraph, scene, ob, NULL, mask);
                        }
                }
        }
-#else
-       UNUSED_VARS(funcname, scene, ob, use_deform, use_render, use_cage);
-#endif
-
-       return NULL;
 }
 
 PyDoc_STRVAR(C_BVHTree_FromObject_doc,
-".. classmethod:: FromObject(object, scene, deform=True, render=False, cage=False, epsilon=0.0)\n"
+".. classmethod:: FromObject(object, depsgraph, deform=True, render=False, cage=False, epsilon=0.0)\n"
 "\n"
 "   BVH tree based on :class:`Object` data.\n"
 "\n"
 "   :arg object: Object data.\n"
 "   :type object: :class:`Object`\n"
-"   :arg scene: Scene data to use for evaluating the mesh.\n"
-"   :type scene: :class:`Scene`\n"
+"   :arg depsgraph: Depsgraph to use for evaluating the mesh.\n"
+"   :type depsgraph: :class:`Depsgraph`\n"
 "   :arg deform: Use mesh with deformations.\n"
 "   :type deform: bool\n"
 "   :arg render: Use render settings.\n"
@@ -1129,10 +1124,11 @@ PYBVH_FROM_GENERIC_EPSILON_DOC
 static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
 {
        /* note, options here match 'bpy_bmesh_from_object' */
-       const char *keywords[] = {"object", "scene",  "deform", "render", "cage", "epsilon", NULL};
+       const char *keywords[] = {"object", "depsgraph", "deform", "render", "cage", "epsilon", NULL};
 
-       PyObject *py_ob, *py_scene;
+       PyObject *py_ob, *py_depsgraph;
        Object *ob;
+       struct Depsgraph *depsgraph;
        struct Scene *scene;
        Mesh *mesh;
        bool use_deform = true;
@@ -1149,18 +1145,20 @@ static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyO
 
        if (!PyArg_ParseTupleAndKeywords(
                args, kwargs, (char *)"OO|$O&O&O&f:BVHTree.FromObject", (char **)keywords,
-               &py_ob, &py_scene,
+               &py_ob, &py_depsgraph,
                PyC_ParseBool, &use_deform,
                PyC_ParseBool, &use_render,
                PyC_ParseBool, &use_cage,
                &epsilon) ||
            ((ob = PyC_RNA_AsPointer(py_ob, "Object")) == NULL) ||
-           ((scene = PyC_RNA_AsPointer(py_scene, "Scene")) == NULL))
+           ((depsgraph = PyC_RNA_AsPointer(py_depsgraph, "Depsgraph")) == NULL))
        {
                return NULL;
        }
 
-       mesh = bvh_get_mesh("BVHTree", scene, ob, use_deform, use_render, use_cage);
+       scene = DEG_get_evaluated_scene(depsgraph);
+       mesh = bvh_get_mesh("BVHTree", depsgraph, scene, ob, use_deform, use_render, use_cage);
+
        if (mesh == NULL) {
                return NULL;
        }