0
$\begingroup$

How can I access the actual structure of a Mesh datablock in memory using the value returned from bpy.types.Mesh.as_pointer() In 2.79 Blender there used to be a possibility to do that by just passing the pointer value to your C++ module or Python's ctypes and referencing that pointer with an appropriate type. Example for 2.7 can be seen here: https://blenderartists.org/t/exporting-triangles-api-challenges/694524/2

However, when I access it the same way in 2.8, I get a struct which is 0-ed. It does contain a valid ID sub-struct in it, but the rest of the data is nulled. Could that be the issue brought by the new depsgraph in 2.8?

C++ side:

#include "mesh_utils.hpp"

extern "C"
{
    #include "bl_src/makesdna/DNA_mesh_types.h"
    #include <blenkernel/BKE_editmesh.h>
    #include <bmesh.h>
}

#include <iostream>

namespace bpy_boost
{

BlenderMesh::BlenderMesh(::std::uintptr_t mesh_pointer)
{
    const Mesh* mesh = reinterpret_cast<const Mesh*>(mesh_pointer);

    ::std::cout << (uint32_t)mesh->totloop << ::std::endl;
}



}

Python side:

def execute(self, context):

        from ..bpy_boost.src.bpy_boost import Mesh

        ds = bpy.context.evaluated_depsgraph_get()
        obj = bpy.context.view_layer.objects.active

        object_eval = obj.evaluated_get(ds)
        if object_eval:
            mesh = object_eval.to_mesh()

            if mesh: 
                mesh.calc_loop_triangles()
                if not mesh.loop_triangles:
                    object_eval.to_mesh_clear()
                    mesh = None

            if mesh:
                if mesh.use_auto_smooth:
                    if not mesh.has_custom_normals:
                        mesh.calc_normals()
                    mesh.split_faces()

                mesh.calc_loop_triangles()

                if mesh.has_custom_normals:
                    mesh.calc_normals_split()

            ptr = mesh.as_pointer()

            Mesh(ptr)

        return {'FINISHED'}

The validity of the pointer value between Python and C++ has been checked. Python-C++ interface is done with Cython and is not provided here since it is not important.

$\endgroup$
1
$\begingroup$

I managed to figure this issue out. It made a mistake using a Mesh structure from 2.9 Blender sources instead of 2.82 which is the Blender build I am running this code on. The mesh.as_pointer() does give a valid pointer to the DNA Mesh struct right away.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.