Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOW TO: Run XAtlas in Blender. #19

Open
mifth opened this issue Nov 7, 2024 · 0 comments
Open

HOW TO: Run XAtlas in Blender. #19

mifth opened this issue Nov 7, 2024 · 0 comments

Comments

@mifth
Copy link

mifth commented Nov 7, 2024

Hi, I was able to run the xatlas in blender and I just want to share my experience.

Blender 4.1+.
1 Open Blender
2 Open Blender's Python Console: Shift+F4
3 Run: import pip
4 Run: pip.main(['install', 'xatlas'])
5 Open Text Editor: Shift+F11
6 Paste the code above
7 Select an object(it should have a UV map) and press Play button in the Text Editor.

Code for the Text Editor:

import bpy

import xatlas
import numpy as np


context = bpy.context
active_obj: bpy.types.Object = context.active_object

# Get Verts
mesh_verts = np.zeros(len(active_obj.data.vertices) * 3, dtype=np.float64)
active_obj.data.vertices.foreach_get('co', mesh_verts)
mesh_verts.shape = (len(active_obj.data.vertices), 3)

poly_indeces = np.zeros(len(active_obj.data.loop_triangles) * 3, dtype=np.int32)
active_obj.data.loop_triangles.foreach_get('vertices', poly_indeces)
poly_indeces.shape = (len(active_obj.data.loop_triangles), 3)

poly_loops = np.zeros(len(active_obj.data.loop_triangles) * 3, dtype=np.int32)
active_obj.data.loop_triangles.foreach_get('loops', poly_loops)
poly_loops.shape = (len(active_obj.data.loop_triangles), 3)

mesh_normals = np.zeros(len(active_obj.data.vertices) * 3, dtype=np.float64)
active_obj.data.vertices.foreach_get('normal', mesh_normals)
mesh_normals.shape = (len(active_obj.data.vertices), 3)

atlas = xatlas.Atlas()
atlas.add_mesh(mesh_verts, poly_indeces, mesh_normals)

chart_options = xatlas.ChartOptions()
#chart_options.normal_deviation_weight = 0.5
#chart_options.roundness_weight = 0.5
#chart_options.straightness_weight = 0.5
#chart_options.normal_seam_weight = 0.5
#chart_options.texture_seam_weight = 0.5
#chart_options.max_cost = 0.5
#chart_options.fix_winding = True
#chart_options.use_unput_mesh_uvs = True

pack_options = xatlas.PackOptions()
#pack_options.padding = 5
#pack_options.create_image = False

atlas.generate(chart_options, pack_options, True)

xatlas_vmapping, xatlas_indices, xatlas_uvs = atlas[0]

active_uv = active_obj.data.uv_layers.active

if active_uv:
    for i, poly_indices in enumerate(xatlas_indices):
        poly_loop_ids = poly_loops[i]

        for j in range(len(poly_loop_ids)):
            active_uv.data[poly_loop_ids[j]].uv = xatlas_uvs[poly_indices[j]]

del atlas
del chart_options
del pack_options

Pictures:
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant