def main():
    print(
        "Transfer a morph from one model to another, assuming the geometry is in the same position"
    )
    print("Needs source PMX, source morph, and dest PMX")

    # prompt PMX name
    print("Please enter name of DESTINATION PMX model file:")
    dest_name_pmx = core.prompt_user_filename(".pmx")
    dest_pmx = pmxlib.read_pmx(dest_name_pmx, moreinfo=True)

    # prompt PMX name
    print("Please enter name of SOURCE PMX model file:")
    source_name_pmx = core.prompt_user_filename(".pmx")
    source_pmx = pmxlib.read_pmx(source_name_pmx, moreinfo=True)

    while True:
        print("Please enter/paste JP name of morph to transfer:")
        s = input("name: >")
        # exit condition is empty input
        if s == "": break

        # find the morph with the matching name
        source_morph_idx = core.my_list_search(source_pmx.morphs,
                                               lambda x: x.name_jp == s)
        if source_morph_idx is None:
            print("err: could not find that name, try again")
            continue

        # verify vertex morph
        source_morph = source_pmx.morphs[source_morph_idx]
        if source_morph.morphtype != 1:
            print("err: for now, only support vertex morphs")
            continue

        newmorph = pmxstruct.PmxMorph(name_jp=source_morph.name_jp,
                                      name_en=source_morph.name_en,
                                      panel=source_morph.panel,
                                      morphtype=source_morph.morphtype,
                                      items=[])
        # have source, have dest, have morph
        # begin iterating!
        # for each vert ref in vertex morph, go to vert in source PMX to get position
        #
        already_used_verts = set()

        for asdf, morphitem in enumerate(source_morph.items):
            core.print_progress_oneline(asdf / len(source_morph.items))
            vertid = morphitem.vert_idx
            vertpos = source_pmx.verts[vertid].pos  # get vertex xyz
            # find the vert or verts in dest_pmx that correspond to this vert in source_pmx
            # problem: multiple vertices at the same location
            # in most cases, all verts at a location will move the same amount... but not always? how to handle?
            # TODO check thru source pmx morph for "diverging" vertices like this? same location in source but not same offset?
            # if some get left behind that's OK, that's usually material borders, easy to use morph editor, only see materials I don't want to morph, and remove those verts from the morph
            # solution: all verts within some radius? not perfect solution
            # radius is hardcoded... if no dest vert found within radius, then what? warn & report nearest?
            # maybe find nearest vertex, and then find all vertices within 110% of that radius?

            # calculate dist from here to each vert in dest_pmx
            # find all verts within this dist threshold
            matching_verts = []
            dist_list = []
            for d, v2 in enumerate(dest_pmx.verts):
                dist = core.my_euclidian_distance(
                    [vertpos[i] - v2.pos[i] for i in range(3)])
                dist_list.append(dist)
                if dist < THRESHOLD:
                    matching_verts.append(d)
            if not matching_verts:
                print(
                    "warning: unable to find any verts within the threshold for source vert ID %d"
                    % vertid)
                print("nearest vert is dist=%f" % min(dist_list))
            for v in matching_verts:
                if v not in already_used_verts:
                    already_used_verts.add(v)
                    newitem = pmxstruct.PmxMorphItemVertex(vert_idx=v,
                                                           move=morphitem.move)
                    newmorph.items.append(newitem)

            pass  # end of for-each-morphitem loop

        # done building the new morph, hopefully
        # make the vertices sorted cuz i can
        newmorph.items.sort(key=lambda x: x.vert_idx)

        if len(newmorph.items) != len(source_morph.items):
            print(
                "warning: length mismatch! source has %d and new has %d, this requires closer attention"
                % (len(source_morph.items), len(newmorph.items)))

        # add it to the dest pmx
        dest_pmx.morphs.append(newmorph)

        pass  # end of while-loop

    print("DONE")
    pmxlib.write_pmx("TRANSFER.pmx", dest_pmx)

    return None
コード例 #2
0
def main():
    # print info to explain the purpose of this file
    print(
        "This tool will rotate the given geometry such that the two 'prime vertices' specified have equal values along the desired axis"
    )
    print("Choose from 6 modes to pick axis of alignment and axis of rotation")
    print("Also choose whether to force their position to 0 or their average")
    # print info to explain what inputs it needs
    print(
        "Inputs: vertex CSV 'whatever.csv' containing all the geometry that will be rotated"
    )
    # print info to explain what outputs it creates
    print(
        "Outputs: vertex CSV 'whatever_aligned.csv' where all the input geometry has been rotated"
    )
    print("")

    ######################## mode selection #######################
    print(
        "Choose an axis to align the points on, and an axis to rotate around:")
    print(
        " 1 = X-align, Y-rotate: create left-right symmetry by rotating left/right"
    )  # DONE
    print(
        " 2 = X-align, Z-rotate: create left-right symmetry by tilting left/right"
    )  # DONE
    print(
        " 3 = Y-align, X-rotate: create front-back level by tilting front/back"
    )  # DONE
    print(
        " 4 = Y-align, Z-rotate: create left-right level by tilting left/right"
    )  # DONE
    print(" 5 = Z-align, X-rotate: create vertical by tilting front/back"
          )  # DONE
    print(
        " 6 = Z-align, Y-rotate: create left-right symmetry by rotating left/right"
    )  # DONE

    mode = core.prompt_user_choice((1, 2, 3, 4, 5, 6))
    if mode == 1:
        # DEFAULT CASE
        axis = "X"
        Xidx = 2
        Zidx = 4
    elif mode == 2:
        axis = "X"
        Xidx = 2
        Zidx = 3
    elif mode == 3:
        axis = "Y"
        Xidx = 3
        Zidx = 4
    elif mode == 4:
        axis = "Y"
        Xidx = 3
        Zidx = 2
    elif mode == 5:
        axis = "Z"
        Xidx = 4
        Zidx = 3
    elif mode == 6:
        axis = "Z"
        Xidx = 4
        Zidx = 2
    else:
        axis = "wtf"
        Xidx = 9999
        Zidx = 9999
        print("congrats you somehow broke it")

    # choose whether to align to axis=0 or align to axis=avg
    print("After rotate, shift prime points to " + axis + "=0 or leave at " +
          axis + "=avg?")
    print(" 1 = " + axis + "=0")
    print(" 2 = " + axis + "=avg")
    useavg = core.prompt_user_choice((1, 2))
    useavg = bool(useavg - 1)

    # promt vertex CSV name
    # input: vertex CSV file with all the vertexes that I want to modify
    print("Please enter name of vertex CSV input file:")
    input_filename_vertex = core.prompt_user_filename(".csv")
    rawlist_vertex = core.read_file_to_csvlist(input_filename_vertex,
                                               use_jis_encoding=True)

    # verify that these are the correct kind of CSVs
    if rawlist_vertex[0] != core.pmxe_vertex_csv_header:
        core.pause_and_quit("Err: '{}' is not a valid vertex CSV".format(
            input_filename_vertex))

    # i plan to re-write this out as a csv file, so let's maintain as much of the input formatting as possible
    header = rawlist_vertex.pop(0)

    ##########################################################
    # prompt for the two prime points:
    # text input 2 point IDs, to indicate the two points to align to x=0
    prime_vertex_one = input("Prime vertex ID #1: ")
    prime_points = []
    v1 = core.my_list_search(rawlist_vertex,
                             lambda x: x[1] == int(prime_vertex_one),
                             getitem=True)
    if v1 is None:
        core.pause_and_quit("Err: unable to find '" + prime_vertex_one +
                            "' in vertex CSV, unable to operate")
    prime_points.append(copy.copy(v1))

    prime_vertex_two = input("Prime vertex ID #2: ")
    v2 = core.my_list_search(rawlist_vertex,
                             lambda x: x[1] == int(prime_vertex_two),
                             getitem=True)
    if v2 is None:
        core.pause_and_quit("Err: unable to find '" + prime_vertex_two +
                            "' in vertex CSV, unable to operate")
    prime_points.append(copy.copy(v2))

    # CALCULATE THE AVERAGE
    # position of the two prime points... rotate around this point
    avgx = (prime_points[0][2] + prime_points[1][2]) / 2.0
    avgy = (prime_points[0][3] + prime_points[1][3]) / 2.0
    avgz = (prime_points[0][4] + prime_points[1][4]) / 2.0
    avg = [avgx, avgy, avgz]

    # note: rotating around Y-axis, so Y never ever comes into it. Z is my effective Y.
    # CALCULATE THE ANGLE:
    # first shift both prime points so that prime0 is on 0/0/0
    prime_points[1][2] -= prime_points[0][2]
    prime_points[1][3] -= prime_points[0][3]
    prime_points[1][4] -= prime_points[0][4]
    prime_points[0][2:5] = [0.0, 0.0, 0.0]
    # then calculate the angle between the two prime points
    # ensure deltaZ is positive: calculate the angle to whichever point has the greater z
    if prime_points[0][Zidx] < prime_points[1][Zidx]:
        vpx = prime_points[1][Xidx]
        vpz = prime_points[1][Zidx]
    else:
        vpx = -prime_points[1][Xidx]
        vpz = -prime_points[1][Zidx]
    # then use atan to calculate angle in radians
    angle = math.atan2(vpx, vpz)
    print("Angle = " + str(math.degrees(angle)))

    # APPLY THE ROTATION
    # horizontally rotate all points around the average point
    origin = (avg[Xidx - 2], avg[Zidx - 2])
    origin_nrm = (0.0, 0.0)
    for i, v in enumerate(rawlist_vertex):
        point = (v[Xidx], v[Zidx])
        newpoint = core.rotate2d(origin, angle, point)
        v[Xidx], v[Zidx] = newpoint
        # also rotate each normal!
        point_nrm = (v[Xidx + 3], v[Zidx + 3])
        newnrm = core.rotate2d(origin_nrm, angle, point_nrm)
        v[Xidx + 3], v[Zidx + 3] = newnrm
        # progress printout
        core.print_progress_oneline(i / len(rawlist_vertex))
    print("done rotating              ")
    # FORCE TO ZERO
    if not useavg:
        # first shift all geometry so that one of the prime points is on the x=0
        # choose to shift by prime0
        shift = avg[Xidx - 2]
        for v in rawlist_vertex:
            v[Xidx] -= shift
            # # anything extremely close to 0 becomes set to exactly 0
            if -0.000000001 < v[Xidx] < 0.000000001:
                v[Xidx] = 0.0
        print("done shifting to zero              ")

    # write out
    # re-add the header line
    rawlist_vertex = [header] + rawlist_vertex
    # build the output file name and ensure it is free
    output_filename = input_filename_vertex[:-4] + "_aligned.csv"
    output_filename = core.get_unused_file_name(output_filename)
    print("Writing aligned result to '" + output_filename + "'...")
    # export modified CSV
    core.write_csvlist_to_file(output_filename,
                               rawlist_vertex,
                               use_jis_encoding=True)

    core.pause_and_quit("Done with everything! Goodbye!")

    return None