Module Geometry

Source Code for Module Geometry

 1  # Blender.Geometry module and its subtypes 
 2   
 3  """ 
 4  The Blender.Geometry submodule. 
 5   
 6  Geometry 
 7  ======== 
 8   
 9  This new module provides access to a geometry function. 
10  """ 
11   
12 -def PolyFill(polylines):
13 """ 14 Takes a list of polylines and calculates triangles that would fill in the polylines. 15 Multiple lines can be used to make holes inside a polyline, or fill in 2 seperate lines at once. 16 @type polylines: List of lists containing vectors, each representing a closed polyline. 17 @rtype: list 18 @return: a list if tuples each a tuple of 3 ints representing a triangle indexing the points given. 19 @note: 2D Vectors will have an assumed Z axis of zero, 4D Vectors W axis is ignored. 20 @note: The order of points in a polyline effect the direction returned triangles face, reverse the order of a polyline to flip the normal of returned faces. 21 22 I{B{Example:}} 23 24 The example below creates 2 polylines and fills them in with faces, then makes a mesh in the current scene:: 25 import Blender 26 Vector= Blender.Mathutils.Vector 27 28 # Outline of 5 points 29 polyline1= [Vector(-2.0, 1.0, 1.0), Vector(-1.0, 2.0, 1.0), Vector(1.0, 2.0, 1.0), Vector(1.0, -1.0, 1.0), Vector(-1.0, -1.0, 1.0)] 30 polyline2= [Vector(-1, 1, 1.0), Vector(0, 1, 1.0), Vector(0, 0, 1.0), Vector(-1.0, 0.0, 1.0)] 31 fill= Blender.Geometry.PolyFill([polyline1, polyline2]) 32 33 # Make a new mesh and add the truangles into it 34 me= Blender.Mesh.New() 35 me.verts.extend(polyline1) 36 me.verts.extend(polyline2) 37 me.faces.extend(fill) # Add the faces, they reference the verts in polyline 1 and 2 38 39 scn = Blender.Scene.GetCurrent() 40 ob = scn.objects.new(me) 41 Blender.Redraw() 42 """
43
44 -def LineIntersect2D(vec1, vec2, vec3, vec4):
45 """ 46 Takes 2 lines vec1, vec2 for the 2 points of the first line and vec2, vec3 for the 2 points of the second line. 47 @rtype: Vector 48 @return: a 2D Vector for the intersection or None where there is no intersection. 49 """
50
51 -def ClosestPointOnLine(pt, vec1, vec2):
52 """ 53 Takes 2 lines vec1, vec2 for the 2 points of the first line and vec2, vec3 for the 2 points of the second line. 54 @rtype: tuple 55 @return: a tuple containing a vector and a float, the vector is the closest point on the line, the float is the position on the line, between 0 and 1 the point is on the line. 56 """
57
58 -def PointInTriangle2D(pt, tri_pt1, tri_pt2, tri_pt3):
59 """ 60 Takes 4 vectors (one for the test point and 3 for the triangle) 61 This is a 2d function so only X and Y are used, Z and W will be ignored. 62 @rtype: bool 63 @return: True or False depending on the points intersection. 64 """
65
66 -def BoxPack2D(boxlist):
67 """ 68 Takes a list of 2D boxes and packs them into a square. 69 Each box in boxlist must be a list of at least 4 items - [x,y,w,h], after running this script, 70 the X and Y values in each box will be moved to packed, non overlapping locations. 71 72 Example:: 73 74 # Make 500 random boxes, pack them and make a mesh from it 75 from Blender import Geometry, Scene, Mesh 76 import random 77 boxes = [] 78 for i in xrange(500): 79 boxes.append( [0,0, random.random()+0.1, random.random()+0.1] ) 80 boxsize = Geometry.BoxPack2D(boxes) 81 print 'BoxSize', boxsize 82 me = Mesh.New() 83 for x in boxes: 84 me.verts.extend([(x[0],x[1], 0), (x[0],x[1]+x[3], 0), (x[0]+x[2],x[1]+x[3], 0), (x[0]+x[2],x[1], 0) ]) 85 v1= me.verts[-1] 86 v2= me.verts[-2] 87 v3= me.verts[-3] 88 v4= me.verts[-4] 89 me.faces.extend([(v1,v2,v3,v4)]) 90 scn = Scene.GetCurrent() 91 scn.objects.new(me) 92 93 @note: Each boxlist item can be longer then 4, the extra items are ignored and stay untouched. 94 @rtype: tuple 95 @return: a tuple pair - (width, height) of all the packed boxes. 96 """
97