1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# SPDX-FileCopyrightText: 2025 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
import unittest
import bpy
import sys
import pathlib
from mathutils import Vector
class TestMeshJoin(unittest.TestCase):
def test_simple(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add()
mesh = bpy.data.objects['Cube'].data
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = bpy.data.objects['Cube']
bpy.ops.object.join()
self.assertEqual(len(mesh.vertices), 16)
def test_face_sets(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_monkey_add()
bpy.ops.mesh.primitive_uv_sphere_add()
cube_obj = bpy.data.objects['Cube']
cube = cube_obj.data
monkey = bpy.data.objects['Suzanne'].data
sphere = bpy.data.objects['Sphere'].data
attr = cube.attributes.new(name=".sculpt_face_set", type='INT', domain='FACE')
attr.data[0].value = 1
attr.data[1].value = 1
attr.data[2].value = 45
attr.data[3].value = 45
attr.data[4].value = 45
attr.data[5].value = 45
b = monkey.attributes.new(name=".sculpt_face_set", type='INT', domain='FACE')
b.data[0].value = 52
b.data[1].value = 52
b.data[2].value = 52
b.data[3].value = 52
b.data[4].value = 52
b.data[5].value = 52
sphere.attributes.new(name=".sculpt_face_set", type='INT', domain='FACE')
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_obj
bpy.ops.object.join()
joined_attr = cube.attributes[".sculpt_face_set"]
self.assertEqual(len(joined_attr.data), 1018)
self.assertEqual(joined_attr.data[0].value, 1)
self.assertEqual(joined_attr.data[1].value, 1)
self.assertEqual(joined_attr.data[2].value, 45)
self.assertEqual(joined_attr.data[9].value, 98)
self.assertEqual(joined_attr.data[20].value, 46)
def test_materials_simple(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
mat_1 = bpy.data.materials.new('mat_1')
bpy.ops.mesh.primitive_cube_add()
cube_1 = bpy.context.view_layer.objects.active
cube_1.data.materials.append(mat_1)
bpy.ops.mesh.primitive_cube_add()
cube_2 = bpy.context.view_layer.objects.active
cube_2.data.materials.append(mat_1)
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_1
bpy.ops.object.join()
result_mesh = cube_1.data
self.assertEqual(len(result_mesh.materials), 1)
self.assertFalse(result_mesh.attributes.get("material_index"))
def test_no_materials_with_indices(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
mat_1 = bpy.data.materials.new('mat_1')
bpy.ops.mesh.primitive_cube_add()
cube_1 = bpy.context.view_layer.objects.active
cube_1.data.materials.append(mat_1)
material_indices = cube_1.data.attributes.new(name="material_index", type='INT', domain='FACE')
bpy.ops.mesh.primitive_cube_add()
cube_2 = bpy.context.view_layer.objects.active
material_indices = cube_2.data.attributes.new(name="material_index", type='INT', domain='FACE')
material_indices.data.foreach_set('value', [0, 1, 1, 700, 1, 2])
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_1
bpy.ops.object.join()
result_mesh = cube_1.data
self.assertEqual(len(result_mesh.materials), 2)
material_indices = cube_1.data.attributes["material_index"]
self.assertTrue(material_indices)
material_index_data = [m.value for m in material_indices.data]
self.assertEqual(material_index_data, [0] * 6 + [0, 1, 1, 700, 1, 2])
def test_materials(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
mat_1 = bpy.data.materials.new('mat_1')
mat_2 = bpy.data.materials.new('mat_2')
mat_3 = bpy.data.materials.new('mat_3')
mat_4 = bpy.data.materials.new('mat_4')
bpy.ops.mesh.primitive_cube_add()
cube_no_mats = bpy.context.view_layer.objects.active
bpy.ops.mesh.primitive_cube_add()
cube_2 = bpy.context.view_layer.objects.active
cube_2.data.materials.append(mat_1)
cube_2.data.materials.append(mat_2)
cube_2.data.materials.append(mat_3)
material_indices = cube_2.data.attributes.new(name="material_index", type='INT', domain='FACE')
material_indices.data.foreach_set('value', [0, 1, 2, 0, 1, 2])
bpy.ops.mesh.primitive_cube_add()
cube_2_mats = bpy.context.view_layer.objects.active
cube_2_mats.data.materials.append(mat_3)
cube_2_mats.data.materials.append(mat_4)
material_indices = cube_2_mats.data.attributes.new(name="material_index", type='INT', domain='FACE')
material_indices.data.foreach_set('value', [0, 0, 0, 1, 1, 1])
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_no_mats
bpy.ops.object.join()
result_mesh = cube_no_mats.data
self.assertEqual(len(result_mesh.materials), 5)
material_indices = result_mesh.attributes["material_index"]
material_index_data = [m.value for m in material_indices.data]
self.assertEqual(material_index_data, [0] * 6 + [1, 2, 3, 1, 2, 3] + [3, 3, 3, 4, 4, 4])
def test_shared_object_data(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
cube_1 = bpy.context.view_layer.objects.active
bpy.ops.object.duplicate(linked=True)
cube_2 = bpy.context.view_layer.objects.active
self.assertEqual(cube_1.data.name, cube_2.data.name)
bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_1
bpy.ops.object.join()
self.assertEqual(len(cube_1.data.vertices), 24)
def test_shape_keys(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
cube_1 = bpy.context.view_layer.objects.active
cube_1.shape_key_add(name="Basis")
key = cube_1.shape_key_add(name="A")
key = cube_1.shape_key_add(name="B")
cube_1.data.shape_keys.key_blocks["B"].data[0].co = Vector((-1, -4, -1))
bpy.ops.mesh.primitive_cube_add()
cube_2 = bpy.context.view_layer.objects.active
cube_2.shape_key_add(name="Basis")
key = cube_2.shape_key_add(name="A")
key = cube_2.shape_key_add(name="B")
key = cube_2.shape_key_add(name="C")
key = cube_2.shape_key_add(name="D")
cube_2.data.shape_keys.key_blocks["B"].data[5].co = Vector((1, -3, 1))
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_1
bpy.ops.object.join()
self.assertEqual(len(cube_1.data.vertices), 16)
self.assertEqual(len(cube_1.data.shape_keys.key_blocks), 5)
self.assertEqual(cube_1.data.shape_keys.key_blocks["B"].data[0].co, Vector((-1.0, -4.0, -1.0)))
self.assertEqual(cube_1.data.shape_keys.key_blocks["B"].data[13].co, Vector((1.0, -3.0, 1.0)))
def test_shape_keys_not_active(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
cube_1 = bpy.context.object
cube_1.shape_key_add(name="Basis")
key = cube_1.shape_key_add(name="A")
key = cube_1.shape_key_add(name="B")
key = cube_1.shape_key_add(name="C")
bpy.ops.mesh.primitive_cube_add()
cube_2 = bpy.context.object
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = cube_2
bpy.ops.object.join()
self.assertEqual(len(cube_2.data.vertices), 16)
self.assertEqual(len(cube_2.data.shape_keys.key_blocks), 4)
def test_no_faces(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.delete(type='ONLY_FACE')
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.object.join()
joined = bpy.context.object
self.assertEqual(len(joined.data.vertices), 16)
self.assertEqual(len(joined.data.polygons), 0)
def test_params(self):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.select_all(action='SELECT')
bpy.context.object.data.use_remesh_preserve_volume = False
bpy.ops.object.join()
joined = bpy.context.object
self.assertEqual(joined.data.use_remesh_preserve_volume, False)
if __name__ == '__main__':
import sys
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
unittest.main()
|