DarkStarSword / 3d-fixes

Stereoscopic 3D fixes using Helix mod & 3DMigoto
105 stars 126 forks source link

Blender 4.2 LTS is on the horizon and bpy api changes loom over us- #33

Closed leotorrez closed 1 month ago

leotorrez commented 2 months ago

I have been trying to crack down on this issue and likely will get a working solution soon but I am not too skillful on the bpy side of things. However I would like to provide the information I've gathered so far.

When it comes to importing, the following changes broke the current implementation:

https://developer.blender.org/docs/release_notes/4.1/python_api/#mesh

here a short video to illustrate the changes: https://www.youtube.com/watch?v=WT29Hyv2XX8

namely the removal of normal related functions in favor of the new smooth modifier workflow.

To solve the issue we require a vector implementation of custom_attribute_float which the plugin currently lacks, I might have to crack down on that issue first before attempting to continue... Because using the built in attribute functions might break in past versions of blender.

The question that comes to mind is, how much backwards compatibility should we account for in the upcoming 4.2 version of the plugin?

When it comes to exporting meshes out of blender... issues will come again but I will research on that after solving the import

The reference updated plugin I was using as guide to fix our issue was the following: https://projects.blender.org/extensions/io_mesh_stl/commit/079f7d1f218a44757bcd2cdc23fdb180a2059647

leotorrez commented 2 months ago

UPDATE:

I've come to find a fix for the issue. It was not as complicated as I expected in the end.

They have simplified the API in fact making it less verbose.

The changes were made in import_normals_step1 and step2:

def import_normals_step1(mesh, data, vertex_layers, operator, translate_normal):
    # Ensure normals are 3-dimensional:
    # XXX: Assertion triggers in DOA6
    if len(data[0]) == 4:
        if [x[3] for x in data] != [0.0]*len(data):
            #raise Fatal('Normals are 4D')
            operator.report({'WARNING'}, 'Normals are 4D, storing W coordinate in NORMAL.w vertex layer. Beware that some types of edits on this mesh may be problematic.')
            vertex_layers['NORMAL.w'] = [[x[3]] for x in data]

    normals = [tuple(map(translate_normal, (x[0], x[1], x[2]))) for x in data]
    return normals

def import_vertices(mesh, obj, vb, operator, semantic_translations={}, flip_normal=False):
    mesh.vertices.add(len(vb.vertices))

    blend_indices = {}
    blend_weights = {}
    texcoords = {}
    vertex_layers = {}
    normals = ()
    use_normals = False

...

      elif translated_elem_name == 'NORMAL':
              use_normals = True
              translate_normal = normal_import_translation(elem, flip_normal)
              normals = import_normals_step1(mesh, data, vertex_layers, operator, translate_normal)

...

    return (blend_indices, blend_weights, texcoords, vertex_layers, use_normals, normals)

def import_3dmigoto_vb_ib(operator, context, paths, flip_texcoord_v=True, flip_winding=False, flip_normal=False, axis_forward='-Z', axis_up='Y', pose_cb_off=[0,0], pose_cb_step=1):

...

    (blend_indices, blend_weights, texcoords, vertex_layers, use_normals, normals) = import_vertices(mesh, obj, vb, operator, semantic_translations, flip_normal)

...

    # Validate closes the loops so they don't disappear after edit mode and probably other important things:
    mesh.validate(verbose=False, clean_customdata=False)  # *Very* important to not remove lnors here!
    # Not actually sure update is necessary. It seems to update the vertex normals, not sure what else:
    mesh.update()
    if use_normals:
        mesh.normals_split_custom_set_from_vertices(normals) # 4.1 replaces step2 entirely

A proper implementation would have to do extra checks and clean ups according to how much backwards compatibility the authors want to keep. Personally I believe 4.2 being the new LTS we should support back to 3.6 and no more- But I will leave that to the author to decide. I can add said corrections and make a pull request once I know your intentions on this regard.

DarkStarSword commented 2 months ago

Backwards compatibility to 3.6 sounds fine to me (anyone who needs to use an older version of Blender always has the option of using an older version of the script so long as they don't need new features). Please go ahead and send a pull request for this when you are ready.