Skip to content

Instantly share code, notes, and snippets.

@jackcrane
Last active November 20, 2024 19:41
Show Gist options
  • Save jackcrane/c3d91d6a6de907ec67d6efa9404e2a75 to your computer and use it in GitHub Desktop.
Save jackcrane/c3d91d6a6de907ec67d6efa9404e2a75 to your computer and use it in GitHub Desktop.

Revisions

  1. jackcrane revised this gist Nov 20, 2024. 1 changed file with 15 additions and 2 deletions.
    17 changes: 15 additions & 2 deletions combine.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,9 @@
    import os
    import numpy as np

    # Define texture path
    texture_path = "palette.png"

    # Parse the XML file
    xml_path = "scene.xml"
    tree = etree.parse(xml_path)
    @@ -14,6 +17,12 @@
    length = len(root.findall("mesh"))
    print(f"Found {length} meshes in the XML file")
    iterator = 0

    # Load the texture
    if not os.path.exists(texture_path):
    raise FileNotFoundError(f"Texture file not found: {texture_path}")

    # Iterate over each mesh in the XML
    for mesh_data in root.findall("mesh"):
    iterator += 1
    print(f"Processing mesh {iterator} of {length}")
    @@ -44,14 +53,18 @@
    transform_matrix = np.dot(translation_matrix, rotation_matrix)
    mesh.apply_transform(transform_matrix)

    # Apply texture to the mesh
    if hasattr(mesh, "visual"):
    mesh.visual = trimesh.visual.TextureVisuals(image=texture_path)

    # Add the mesh to the scene
    scene.add_geometry(mesh)

    if iterator > 100:
    break

    # Export the merged scene
    output_path = "path/to/output.obj"
    scene.export(output_path)
    output_path = "output.obj"
    scene.export(output_path, include_texture=True)

    print(f"Scene exported to {output_path}")
  2. jackcrane created this gist Nov 20, 2024.
    57 changes: 57 additions & 0 deletions combine.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    import trimesh
    from lxml import etree
    import os
    import numpy as np

    # Parse the XML file
    xml_path = "scene.xml"
    tree = etree.parse(xml_path)
    root = tree.getroot()

    # Create an empty scene
    scene = trimesh.Scene()

    length = len(root.findall("mesh"))
    print(f"Found {length} meshes in the XML file")
    iterator = 0
    for mesh_data in root.findall("mesh"):
    iterator += 1
    print(f"Processing mesh {iterator} of {length}")

    obj_file = mesh_data.get("file")
    position = [float(x) for x in mesh_data.get("pos").split()]
    rotation = [float(x) for x in mesh_data.get("rot").split()]

    # Load the OBJ file
    if not os.path.exists(obj_file):
    print(f"File not found: {obj_file}")
    continue

    mesh = trimesh.load_mesh(obj_file)

    # Apply translation
    translation_matrix = trimesh.transformations.translation_matrix(position)

    # Apply rotation (convert degrees to radians for trimesh)
    rotation_matrix = trimesh.transformations.euler_matrix(
    np.radians(rotation[0]), # Rotation around X-axis
    np.radians(rotation[1]), # Rotation around Y-axis
    np.radians(rotation[2]), # Rotation around Z-axis
    axes="sxyz"
    )

    # Combine transformations
    transform_matrix = np.dot(translation_matrix, rotation_matrix)
    mesh.apply_transform(transform_matrix)

    # Add the mesh to the scene
    scene.add_geometry(mesh)

    if iterator > 100:
    break

    # Export the merged scene
    output_path = "path/to/output.obj"
    scene.export(output_path)

    print(f"Scene exported to {output_path}")