Running From A Script
In order to run a packing operation from a script in Blender, use the following basic example (you can adapt it to your own needs).
Note the script requires a working UVPackmaster engine installation to run properly.
# MIT License
#
# Copyright (c) 2022 Lukasz Czyz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import bpy
import bmesh
import bl_ext.user_default.uvpackmaster3 as uvpm3
# Make the object active
obj_to_pack = bpy.context.view_layer.objects.active
bpy.ops.object.editmode_toggle()
# Select all UVs
bm = bmesh.from_edit_mesh(obj_to_pack.data)
uv_layer = bm.loops.layers.uv.verify()
for face in bm.faces:
for loop in face.loops:
loop_uv = loop[uv_layer]
loop_uv.select = True
# === PACKING OPERATION BEGIN ===
# Reset all UVPM parameters to defaults
bpy.ops.uvpackmaster3.reset_to_defaults()
# Set values for all required UVPM parameters directly in the script.
uvpm3_prefs = uvpm3.utils.get_prefs()
uvpm3_props = uvpm3.utils.get_main_props(bpy.context)
# To get the python path for the given parameter, open Blender normally (with GUI),
# right mouse click over the parameter in the packer UI, then select 'Copy Data Path'
# from the menu. The path will be copied to clipboard.
uvpm3_props.precision = 500
# Select packing mode id - uncomment only one line below
mode_id = "pack.single_tile"
# mode_id = "pack.tiles"
# mode_id = "pack.groups_to_tiles"
# mode_id = "pack.groups_together"
# If you want to use the packing mode currently selected in the blend file (e.g. after loading a UVPM preset), uncomment the following line
# mode_id = uvpm3_props.active_main_mode_id
pack_op_type = uvpm3.enums.PackOpType.PACK.code
# pack_op_type = uvpm3.enums.PackOpType.PACK_TO_OTHERS.code
# pack_op_type = uvpm3.enums.PackOpType.REPACK_WITH_OTHERS.code
try:
bpy.ops.uvpackmaster3.pack(mode_id=mode_id, pack_op_type=pack_op_type)
except Exception as ex:
print('Pack operation failed: ' + str(ex))
if uvpm3_prefs.engine_retcode != 0:
raise RuntimeError('UVPM 3 operation not succeeded (a warning or error occurred)! Return code: {}'.format(int(uvpm3_prefs.engine_retcode)))
# === PACKING OPERATION END ===
bpy.ops.object.editmode_toggle()