Newer
Older
# ##### BEGIN GPL LICENSE BLOCK #####
Campbell Barton
committed
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
Campbell Barton
committed
import os #remove this
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
import bpy
try:
import mathutils
MATHUTILS = mathutils
except:
import Mathutils
MATHUTILS = Mathutils
from math import *
from bpy.props import IntProperty, FloatProperty ,EnumProperty
from itertools import *
NARROW_UI = 180
MAX_INPUT_NUMBER = 50
#Global_Scale = 0.001 #1 blender unit = X mm
GLOBAL_SCALE = 0.1 #1 blender unit = X mm
#Global_Scale = 1.0 #1 blender unit = X mm
# next two utility functions are stolen from import_obj.py
def unpack_list(list_of_tuples):
l = []
for t in list_of_tuples:
l.extend(t)
return l
def unpack_face_list(list_of_tuples):
l = []
for t in list_of_tuples:
face = [i for i in t]
if len(face) != 3 and len(face) != 4:
raise RuntimeError("{0} vertices in face.".format(len(face)))
# rotate indices if the 4th is 0
if len(face) == 4 and face[3] == 0:
face = [face[3], face[0], face[1], face[2]]
if len(face) == 3:
face.append(0)
l.extend(face)
return l
'''
Remove Doubles takes a list on Verts and a list of Faces and
removes the doubles, much like Blender does in edit mode.
It doesn’t have the range function but it will round the corrdinates
and remove verts that are very close togther. The function
is useful because you can perform a “Remove Doubles” with out
having to enter Edit Mode. Having to enter edit mode has the
disadvantage of not being able to interactively change the properties.
'''
def RemoveDoubles(verts,faces,Decimal_Places = 4):
new_verts = []
new_faces = []
dict_verts = {}
Rounded_Verts = []
for v in verts:
Rounded_Verts.append([round(v[0],Decimal_Places),round(v[1],Decimal_Places),round(v[2],Decimal_Places)])
for face in faces:
new_face = []
for vert_index in face:
Real_co = tuple(verts[vert_index])
Rounded_co = tuple(Rounded_Verts[vert_index])
if Rounded_co not in dict_verts:
dict_verts[Rounded_co] = len(dict_verts)
new_verts.append(Real_co)
if dict_verts[Rounded_co] not in new_face:
new_face.append(dict_verts[Rounded_co])
if len(new_face) == 3 or len(new_face) == 4:
new_faces.append(new_face)
return new_verts,new_faces
def Scale_Mesh_Verts(verts,scale_factor):
Ret_verts = []
for v in verts:
Ret_verts.append([v[0]*scale_factor,v[1]*scale_factor,v[2]*scale_factor])
return Ret_verts
#Create a matrix representing a rotation.
#
#Parameters:
#
# * angle (float) - The angle of rotation desired.
# * matSize (int) - The size of the rotation matrix to construct. Can be 2d, 3d, or 4d.
# * axisFlag (string (optional)) - Possible values:
# o "x - x-axis rotation"
# o "y - y-axis rotation"
# o "z - z-axis rotation"
# o "r - arbitrary rotation around vector"
# * axis (Vector object. (optional)) - The arbitrary axis of rotation used with "R"
#
#Returns: Matrix object.
# A new rotation matrix.
def Simple_RotationMatrix(angle, matSize, axisFlag):
if matSize != 4 :
print ("Simple_RotationMatrix can only do 4x4")
q = radians(angle) #make the rotation go clockwise
if axisFlag == 'x':
matrix = MATHUTILS.Matrix(((1,0,0,0),(0,cos(q),sin(q),0),(0,-sin(q),cos(q),0),(0,0,0,1)))
matrix = MATHUTILS.Matrix(((cos(q),0,-sin(q),0),(0,1,0,0),(sin(q),0,cos(q),0),(0,0,0,1)))
matrix = MATHUTILS.Matrix(((cos(q),sin(q),0,0),(-sin(q),cos(q),0,0),(0,0,1,0),(0,0,0,1)))
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
else:
print ("Simple_RotationMatrix can only do x y z axis")
return matrix
##########################################################################################
##########################################################################################
## Converter Functions For Bolt Factory
##########################################################################################
##########################################################################################
def Flat_To_Radius(FLAT):
h = (float(FLAT)/2)/cos(radians(30))
return h
def Get_Phillips_Bit_Height(Bit_Dia):
Flat_Width_half = (Bit_Dia*(0.5/1.82))/2.0
Bit_Rad = Bit_Dia / 2.0
x = Bit_Rad - Flat_Width_half
y = tan(radians(60))*x
return float(y)
##########################################################################################
##########################################################################################
## Miscellaneous Utilities
##########################################################################################
##########################################################################################
# Returns a list of verts rotated by the given matrix. Used by SpinDup
def Rot_Mesh(verts,matrix):
ret = []
#print ("rot mat",matrix)
for v in verts:
vec = MATHUTILS.Vector(v) * matrix
ret.append([vec.x,vec.y,vec.z])
return ret
# Returns a list of faces that has there index incremented by offset
def Copy_Faces(faces,offset):
ret = []
for f in faces:
fsub = []
for i in range(len(f)):
fsub.append(f[i]+ offset)
ret.append(fsub)
return ret
# Much like Blenders built in SpinDup.
Loading
Loading full blame...