Created
April 16, 2020 15:05
-
-
Save openroomxyz/8621f7c59fa58b0aa5d4d886327cc4a5 to your computer and use it in GitHub Desktop.
Blender Python : How do i generate an geometric Union of Multiple Cubes / or others Objects (Union Modifier, efficient way)?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bpy | |
| import uuid | |
| def create_a_cube(name, position, set_rotation, scale): | |
| # Create a simple cube. | |
| bpy.ops.mesh.primitive_cube_add() | |
| # Get the cube object and rename it. | |
| cube = bpy.context.object | |
| cube.name = name | |
| cube.rotation_euler = set_rotation | |
| cube.scale = scale | |
| cube.location = position | |
| return cube | |
| def Union(name_a, name_b, new_one_name): | |
| # Create a boolean modifier named 'my_bool_mod' for the cube. | |
| mod_bool = bpy.data.objects[name_a].modifiers.new('my_bool_mod', 'BOOLEAN') | |
| # Set the mode of the modifier to DIFFERENCE. | |
| mod_bool.operation = 'UNION' #'DIFFERENCE' | |
| # Set the object to be used by the modifier. | |
| mod_bool.object = bpy.data.objects[name_b] | |
| bpy.context.view_layer.objects.active = bpy.data.objects[name_a] | |
| # Apply the modifier. | |
| res = bpy.ops.object.modifier_apply(apply_as='DATA', modifier = 'my_bool_mod') | |
| bpy.data.objects[name_a].name = new_one_name | |
| objs = bpy.data.objects | |
| objs.remove(objs[name_b], do_unlink=True) | |
| return res | |
| def UnionTheListOfObjectsWithName(cube_id_list): | |
| lek = 0 | |
| first = [] | |
| second = [] | |
| new_list = [] | |
| for i in cube_id_list: | |
| if len(first) == 0: | |
| first.append(i) | |
| else: | |
| second.append(i) | |
| if (first[0] in bpy.data.objects) and (second[0] in bpy.data.objects): | |
| name = "L_"+str(lek)+"_"+str(uuid.uuid4()) | |
| Union(first[0], second[0], name ) | |
| new_list.append(name) | |
| lek += 1 | |
| first.clear() | |
| second.clear() | |
| if len(first) == 1: | |
| new_list.append(first[0]) | |
| if(len(new_list) > 1): | |
| return UnionTheListOfObjectsWithName(new_list) #recursive call | |
| def GenerateObjects(): | |
| cube_uuid = [] | |
| for i in range(0, 101): | |
| nam = "A_"+str(i)+"_"+str(uuid.uuid4()) | |
| cube_uuid.append(nam) #str(uuid.uuid4()) | |
| create_a_cube(nam, [i, 0, 0], [3.1415, 3.1415 * 0.5 * i * 0.1, 3.1415 * 1.2312], [2, 5, 1]) | |
| return cube_uuid | |
| list_of_object_names = GenerateObjects() | |
| UnionTheListOfObjectsWithName(list_of_object_names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment