0
$\begingroup$

I'm making a plugin to extract meshes from a game.
Actually, it extracts well the vertices and indicies/faces of each mesh.

Blender version: 3.2.0
Addon target: 2.80.0-3.2.0+

A "Tie" (terrain component) is a group of mesh, and as you know a mesh is a group of vertices and indices.

I want to know how I can join these meshes into ONE object, and to that for each TIE of a map.

        for tie in assetmanager.ties:
            for idx, vertex in enumerate(tie.vertices):
                meshdataname = "TieData_{0}_{1}".format(str(tie.tie.tie.tuid)[:5], idx)
                verts = list[tuple[float, float, float]]()
                faces = tie.indices[idx]
                uvs = []
                edges = []  # Ignore
                for mesh_vertex in vertex:
                    verts.append(mesh_vertex.__loctuple__())
                    uvs.append(mesh_vertex.__uvstuple__())
                meshdata = bpy.data.meshes.new(meshdataname)
                meshdata.from_pydata(verts, edges, faces)
                print(meshdata)
                obj = bpy.data.objects.new(name="Tie_{0}_{1}".format(str(tie.tie.tie.tuid)[:5], idx),
                                           object_data=bpy.data.meshes[meshdataname])
                print(obj)
                if not operator.parent_meshes_to_objects:
                    ties_collection.objects.link(obj)
                else:
                    tie_blenderobjects.append(obj)
            if operator.parent_meshes_to_objects:
                bpy.context.selected_objects = tie_blenderobjects
                bpy.ops.object.join()

Legend:

  • mesh_vertex.__loctuple__() -> Gives a single tuple[float, float, float], it's the postion for ONE vertex.
  • tie.indices -> List of lists, themselves containing a tuple[int, int, int] (it's triangles)
  • tie.vertices -> List of lists, themselves containing a custom class with location tuple of a vertex.

bpy.context.selected_objects looks like it is readonly, so I don't know how to select these objects...

The tie.indices and tie.vertices are in the exact same order, it means that index of mesh_vertices matches tie.indices[index_of_mesh_vertices]

And I need to know how I can make all the meshes look right into one object.

I want to proceed that way because I want to make these objects easy to use for users and I don't want to merge vertices and indices because of technical restrictions/limitations/complications.

$\endgroup$
4
  • $\begingroup$ Hello ! You can join two mesh data containers easily, you'll have to extend the first vertices location list with the second one, and extend the first face indices list with the second, but add an integer offset to the second face indices list equal to the length of the first vertices location list(=number of vertices of the first mesh). But the final result will depend on the actual input meshes, if you have overlapping mesh you may need to add a cleaning pass to remove duplicate geometry for example. If the two meshes can be connected you'll also have to merge vertices with the same location $\endgroup$
    – Gorgious
    Commented Nov 29, 2022 at 7:29
  • $\begingroup$ Thank you for your answer ! I already tried, but the second mesh indices are starting from 0-1 again, how can I fix that ? I think I have a little idea, but I'm not sure The second thing is that I want to know how to fusion several meshes data/meshes objects because in my case it's an option at the import, but I also thought to another possibility: Make groups of objects ? iirc we can move these like if they were single objects ? $\endgroup$
    – V E L Δ
    Commented Nov 29, 2022 at 12:40
  • $\begingroup$ You can use a separate counter defined earlier in your code like idx_offset and use it in your faces index lists with faces = [i + idx_offset for i in tie.indices[idx]] then increment it every time you finish a tie idx_offset += len(tie.vertices) Another solution though is to join your meshes after you imported them with bpy.ops.object.join(). Grouping in Blender can be done with parent objects. Parent everything to an empty for example and you'll be able to move everything when you move the empty. $\endgroup$
    – Gorgious
    Commented Nov 29, 2022 at 14:27
  • $\begingroup$ Yeah, grouping is defo better, thanks !! However, I would love to know how I can select several meshes and merge/join them into one $\endgroup$
    – V E L Δ
    Commented Nov 29, 2022 at 15:17

0

You must log in to answer this question.

Browse other questions tagged .