0

For a university project I must be able to load an object thanks to OpenGL. The object is a COLLADA (.DAE) and is created by using a marching cube algorithm (mcubes.marching_cubes). The object hasn’t got tangent space, but it is required to be imported into OpenGL.

The choice I made to resolve this problem was to add UV texture to the object and then calculate the tangents. As shown in the program I added the texture thanks to bpy module and I used pycollada module for the calculation of the tangents.

bpy.ops.wm.collada_import(filepath=filepath_in)
material = bpy.data.materials.new(matName)
material.diffuse_color = (r,g,b)

bpy.context.object.data.materials.append(material)
texUV = bpy.data.textures.new(textName, type="IMAGE")   
image = bpy.data.images.load(imagePath)
texUV.image = image

bpy.data.materials[matName].texture_slots.add()
bpy.data.materials[matName].active_texture = texUV 
bpy.data.materials[matName].texture_slots[0].texture_coords = "UV"
bpy.data.materials[matName].texture_slots[0].mapping = "FLAT"

#export réussi en collada 
bpy.ops.wm.collada_export(filepath=filepath_out, check_existing=True, filter_blender=True, filter_image=True, filter_movie=False, filter_python=False, filter_font=False, filter_sound=False, filter_text=False, filter_btx=False, filter_collada=True, filter_folder=True, filemode=8, apply_modifiers=True, export_mesh_type=0, export_mesh_type_selection='view', selected=True, include_children=False, include_armatures=False, deform_bones_only=False, active_uv_only=False, include_shapekeys=False, use_texture_copies=True, use_object_instantiation=True, sort_by_name=False)


def generateTexTg(inputDAE, outputDAE):
        my_mesh = Collada(inputDAE) 
        my_geom = my_mesh.geometries[0]
        my_triset = my_geom.primitives[0]
        print (my_triset.vertex[my_triset.vertex_index][0])
        print ("normal")
        print (my_triset.normal[my_triset.normal_index][0])
        print ("tg")
        #here comes the error
        print (my_triset.texcoordset[0][my_triset.texcoord_indexset[0]][0])
        my_triset.generateTexTangentsAndBinormals()
        my_mesh.write(outputDAE)
        print("tangentes ok")

When I visualize the object in Blender I can see the texture was indeed added.The problem I get when I try to calculate the tangents is that the object hasn’t got texture coordinates.

So, the first question would be is there an issue in the program I gave?

The second one is if you would have to calculate the tangent space of an object how would you do it?

Thank you for your help

franadam
  • 1
  • 3
  • Something like [this answer](https://blender.stackexchange.com/a/26121/935)? If the image is aligned with the mesh then uv data was made to align the two and should be in the .dae. If the image is a generic repeating texture, you can use generated tex coords instead of uv. – sambler Oct 15 '18 at 02:47
  • @sambler thank you, yes like that. The image is a generic repeating texture. I tried your proposition but I still have the same issue, the error I got is : Error: Tangent space computation needs an UVMap, "(null)" not found, aborting – franadam Oct 15 '18 at 12:51
  • `bpy.ops.mesh.uv_texture_add()` will create a default uvmap. Does that work? – sambler Oct 17 '18 at 10:00
  • @sambler yes that does ! Thanks – franadam Oct 25 '18 at 14:03

0 Answers0