Created
April 16, 2020 15:21
-
-
Save openroomxyz/0d1b269df253a1a9e8ed087d983dc559 to your computer and use it in GitHub Desktop.
Blender Python : How to create a Wave with Boolean :) ?
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 | |
| import math | |
| import random | |
| 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, 30): | |
| for j in range(0, 30): | |
| nam = "A_"+str(i)+"_"+str(uuid.uuid4()) | |
| cube_uuid.append(nam) #str(uuid.uuid4()) | |
| center_x = 15 | |
| center_y = 15 | |
| d = math.sqrt(math.pow((i - center_x),2.0)+math.pow((j - center_y), 2.0)) | |
| create_a_cube(nam, [i,j, math.sin(d)], [random.random(), random.random(), random.random()], [1, 1, 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