3
$\begingroup$

I have a "good practice" question. I am importing multiple meshes as Mesh objects with python. Before assigning them to an object and linking them to a scene, I would like to join certain meshes.

The only way I find to do this is to create objects, select them and then join them.

Is there a way to join the meshes without making them into objects?

Thanks a lot.

(Edit: Sadly I can't use bmesh for this operation, because i'm using custom split normals)

$\endgroup$
  • $\begingroup$ Can you alter the importer code? $\endgroup$ – batFINGER Jun 14 '16 at 13:22
  • $\begingroup$ yes because I am writing the importer. $\endgroup$ – Madlaina Kalunder Jun 14 '16 at 13:48
  • $\begingroup$ in which case, can you append each mesh to previous while importing? . can you elaborate somewhat on what data you are importing from, are you importing each seperately, do they share the same local coords? $\endgroup$ – batFINGER Jun 14 '16 at 14:15
  • $\begingroup$ Yes, that is excactly the question. How can I append the mesh to another mesh without creating actual object datablocks or using bmesh (loss of custom vertex normals)? I am importing a custom data format (so i import all the meshes together) and I want to merge specific meshes to a single mesh datablock and only then create an object and link it to the scene. they share the coords // I am looking for something like the bmesh option answer below. just with mesh. i don't want to append the vertex and polygon sets) $\endgroup$ – Madlaina Kalunder Jun 14 '16 at 15:42
  • $\begingroup$ Even using TLousky's bmesh solution, you have your custom normals from the import data, reassign them after join. Your q implies to me you know what meshes you want to join on import, ie you are importing multiple meshes at once from one or more files. Your comment implies you are using from_pydata in your importer, in which case why not append (after offsetting index) the vert and polygon lists (and custom normals etc) at this stage if you know those two meshes are to be joined. $\endgroup$ – batFINGER Jun 14 '16 at 16:33
1
$\begingroup$

You can use the bmesh module to do this. You start with creating a new, empty bmesh object with bm = bmesh.new(), then every time you want to add a mesh to this bmesh, you can use bm.from_mesh( mesh ):

import bpy, bmesh

# Reference to existing meshes, replace these with your code generated meshes
m1 = bpy.data.meshes[0] 
m2 = bpy.data.meshes[1]

bm = bmesh.new()

bm.from_mesh( m1 ) # Add mesh 1 to bmesh
bm.from_mesh( m2 ) # Add mesh 2 to bmesh

m3 = bpy.data.meshes.new( "newMesh" )
bm.to_mesh( m3 )

o = bpy.data.objects.new( "new", m3 )
bpy.context.scene.objects.link( o )
$\endgroup$
  • $\begingroup$ That would be really great, but sadly I can't use bmesh since I am using custom split vertex normals. Thanks a lot for your answer though! i hope it will help someone else too $\endgroup$ – Madlaina Kalunder Jun 14 '16 at 11:59

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.