Using a Mesh struct similar to blender with as_pointer() API?

Hello,
I’m currently experimenting on a livebridge between blender and our studio game engine.
As an experiment, i wanted to write an exporter using C++ that could use the power of the as_pointer() method. Where could i find good example on how this can be done?

I find creating a struct similar to blender object.data ( Mesh struct ) quite challenging. An example would be quite nice!

You can check how Cycles does it. Cycles is CPP and reads blender data using as_pointers via a Python wrapper.

1 Like

No, the Cycles approach is not something add-ons can do.

1 Like

Getting the Mesh as a C++ data structure is not really possible. You can get a pointer to it but the data structure layout is internal to Blender and frequently changes.

What you can do is get a pointer to the various position, normal, UV, … attribute arrays with .as_pointer() in Python and then process those in C++.

1 Like

Thank you so much for your answer

That would explain why i coudln’t find any

DNA_mesh_types.h 

In the source code

neither any

typedef struct Mesh

Was it changed recently?

What kind of struct does C.object.data.as_pointer() lead to?

There is a DNA_mesh_types.h in the source code, and you can cast object.data.as_pointer() to Mesh* for mesh objects. It’s just going to break all the time as it is an internal data structure that frequently changes, it’s not part of the public API.

1 Like

Thanks
Indeed found it

Are you suggesting that C API users should use each of these individually?
o.data.vertices.data.as_pointer() ; o.data.edges.data.as_pointer(); o.data.polygons.data.as_pointer() ; o.data.loops.data.as_pointer() ; o.data.uv_layers.data.as_pointer()

Yes, that’s what other add-ons are doing. Note it needs to be data[0].as_pointer() instead of data.as_pointer().

As C.object.data.vertices[0].as_pointer()
I was thinking that C.object.data.vertices.foreach_get() via numpy was too slow for our project; but getting the memory adress of each Vertex individually seem quite un-productive

I’m sure many game engine developers would appreciate an efficient method to catch a Mesh struct quickly from C :smile:

You don’t need to get the address of every vertex individually, a pointer to the first element is a pointer to the entire array.

2 Likes

Hmm, but Cycles is an Addon isn’t it?

You don’t need to get the address of every vertex individually, a pointer to the first element is a pointer to the entire array.

Cycles seems being able to read whole depsgraph data
i see depsgraph.as_pointer() in Cycles addon source. And other Render engines need to loop over each objects, and each Loops/Edges/Polys/Verts/Uv’s ect… that seem slow (and a bit unfair :kissing:)

Thank you, will take a look
Curious about this luxcore approach

In context with the quote, the questions implies the following:
How can cycles can achieve things that other plugins can’t, if cycles is indeed implemented as a plugin.

Cycles has access to Blender internals because it is part of the Blender binary.

@JskJ You could get access to all internal struct by building your plugin C++ library within blender source code, and building your binaries alongside blender itself.

This week I was able to do so, see Example4, where I’m accessing the mesh structs only by passing the C.object.as_pointer() as an argument to my DLL :slight_smile:

But on the downside, you will need to create a DLL for each blender version you want to support and for each OS’s as well

While re-using/linking our code for your own purposes is perfectly OK and highly encouraged, the GPL license attached to some of these files cannot be removed. You seem to have accidentally removed the GPL license header from buildinfo.c which is present in the original copy

1 Like

Thanks for the quick fix!

1 Like

My bad, forgot to indicate the license :slight_smile:
Anyway, i hope this project will be helpful for the few who wish to understand as_pointer()

Cheers

1 Like