I'm need to programmatically generate box UVW maps in 3D-models, similar to how UVW Map -> Box
works in 3ds Max.
Example with box UV, what I'm after
I've tried javascript-based solutions such as this or this, but it seems to give various results depending on if the mesh/geometry is merged or if it already has any built in UV. Or it has different directions.
Applying box UV-map via Blender or 3ds Max to the same 3D-model always gives perfect results though.
Best case scenario would be a command line tool, similar to how gltf-pipeline works:
generate-box-map -i model.gltf -type box -size 50
I've found tools/projects such as PyMeshLab, Meshmatic, or running a Blender instance and attempting to do it via python, but I couldn't find a solution. Perhaps there's a native/easier way of doing this?
I ended up solving this in Blender through Docker via this image https://github.com/linuxserver/docker-blender.
The project structure is
app.js
Receives a request via express
, body contains https://some-storage.com/model.gltf
and starts a Blender process via child_process.exec(`blender -b --addons magic_uv -P /app/src/main.py --log-level -1 -- ${pathToDownloadedGLTFFile}`)
main.py
...
argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"
param_input_file = argv[0]
bpy.ops.import_scene.gltf(filepath=param_input_file)
for obj in bpy.data.objects:
if obj.type == 'MESH':
obj.select_set(True) # select the object
bpy.context.view_layer.objects.active = obj # needed to enable Edit mode below
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.uv.muv_uvw_box_map(size=instructions['size'], rotation=(instructions['rotationX'], instructions['rotationY'], instructions['rotationZ']))
...
# export back to GLTF and write to /some/dir/model.gltf
Regarding the problem with different directions of UV, setting rotation.x
to 270 seems to fix it.