import bpy import math half_pi = math.pi/2 quat_pi = math.pi/4 name = "Fiber " #create random holes from mathutils import Vector from random import random from random import randint from random import uniform def check_circle_bounbox( circC, circR, rectC, rectR ): ''' Make sure this circle does not protrude outside the rectangle's bounding box ''' maxX = rectC.x + rectR minX = rectC.x - rectR maxY = rectC.y + rectR minY = rectC.y - rectR withinX = ( circC.x + circR <= maxX ) and ( circC.x - circR >= minX ) withinY = ( circC.y + circR <= maxY ) and ( circC.y - circR >= minY ) return withinX and withinY def check_overlap( circles, circC, circR ): ''' Make sure the distance between the current circle's center and all other circle centers is greater than or equal to the circle's perimeter (2r) ''' return len( [ True for c in circles if ( c - circC ).length >= circR * 2 ] ) == len( circles ) circleRadius = .008#.000001 circleCount = 50#1679656006 rectCenter = Vector((0, 0, 0)) rectRadius = 0.1 circles = [] maxIterations = 1000 # Set max number of loop iterations to prevent infinite loop nameCounter = 1 #z = 0 # All cylinders lie on Z = 0 i = 0 # Current loop iteration while len( circles ) < circleCount and i < maxIterations: x = rectCenter.x + 2 * rectRadius * random() - rectRadius #- math.sqrt(2*(rectRadius^2)) #* 2 for limiting the recctangular area y = rectCenter.y + 2 * rectRadius * random() - rectRadius #y = rectCenter.y + 2 * rectRadius * random() - rectRadius - 0.02 #- math.sqrt(2*(rectRadius^2)) #* 2 for limiting the recctangular area z = rectCenter.z + 2 * rectRadius * random() - rectRadius #- math.sqrt(2*(rectRadius^2)) #* 2 for limiting the recctangular area #z = abs(rectCenter.z + 2 * rectRadius * random() - rectRadius) #z-position always positive circC = Vector((x, y, z )) if check_circle_bounbox( circC, circleRadius, rectCenter, rectRadius ) \ and check_overlap( circles, circC, circleRadius ): circles.append( circC ) i += 1 for c in circles: rot_x = uniform(0, 2*math.pi) rot_y = uniform(0, 2*math.pi) rot_z = uniform(0, 2*math.pi) #bpy.ops.mesh.primitive_circle_add( radius = circleRadius, location = c, fill_type = 'NGON' ) #bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius = circleRadius, depth= 0.03, location = c, end_fill_type='NGON', rotation=(rot_x, rot_y, rot_z)) bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius = circleRadius, depth= 0.03, location = c, rotation=(rot_x, rot_y, rot_z)) currentPiece = bpy.context.active_object currentPiece.name = name+str(nameCounter) nameCounter += 1 #bpy.ops.object.editmode_toggle() #bpy.ops.mesh.select_all(action='TOGGLE') #bpy.ops.mesh.edge_face_add() #bpy.ops.mesh.f2() #bpy.ops.mesh.normals_make_consistent(inside=False) #bpy.ops.object.editmode_toggle() #bpy.ops.object.editmode_toggle() #bpy.ops.mesh.f2() #bpy.ops.mesh.normals_make_consistent(inside=False) #bpy.ops.object.editmode_toggle() #bpy.ops.object.make_single_user(object=True, obdata=True) #bpy.ops.object.convert(target='MESH') #bpy.ops.object.modifier_apply(modifier="Boolean") #bpy.ops.object.booltron_destructive_union(triangulate=False, cleanup=False, keep_objects=False, pos_correct=False, pos_offset=0.005, solver='CARVE', double_threshold=1e-006) bpy.context.space_data.viewport_shade = 'SOLID' bpy.ops.object.shade_smooth()