-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.py
182 lines (146 loc) · 7.09 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
145
146
147
148
149
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
# Helper class to make chart marker generation easier
# e.x: a=NiceScale(min, max)
# a.maxPoint, a.maxTicks, a.minPoint, a.niceMin, a.niceMax, a.tickSpacing
import math
import bpy
class HelperClass:
def remap(self, x, oMin, oMax, nMin, nMax):
if oMin == oMax:
print("range is 0!")
return None
if nMin == nMax:
print("range is 0!")
return None
# check for reversed input
reverseInput = False
oldMin = min( oMin, oMax )
oldMax = max( oMin, oMax )
if not oldMin == oMin:
reverseInput = True
# check for reversed output
reverseOutput = False
newMin = min( nMin, nMax )
newMax = max( nMin, nMax )
if not newMin == nMin:
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
return result
# xSize,ySize,zSize,x_axis_size, y_axis_size, z_axis_size, obj_dimensions, cylWidth, axisPadding, axisMat, False)
def generateAxis(self,xMax,yMax,zMax,xSize,ySize,zSize,pointDimensions,axisWidth,padding,axisMaterial):
# Extra width needed for axis to wrap objects nicely
extra_width_x = pointDimensions[0]/2
extra_width_y = pointDimensions[1]/2
extra_width_z = pointDimensions[2]/2
# Generate X Axis Cylinder
bpy.ops.mesh.primitive_cylinder_add(location=(xMax/2, -extra_width_y, - extra_width_z), radius=0.5)
# world bpy.ops.mesh.primitive_cylinder_add(location=(0, min(yAxisFull) - extra_width_y, min(zAxisFull) - extra_width_z), radius=0.5)
newXCyl = bpy.context.object # define object
bpy.context.object.rotation_euler = (0, 1.5708,0) # in radians (90 deg.)
bpy.context.object.dimensions = [axisWidth, axisWidth, xSize * padding]
bpy.context.object.data.materials.append(axisMaterial) # Set mat selection
# normal: bpy.ops.mesh.primitive_cylinder_add(location=(-x_padding, ySize/2, -z_padding), radius=0.5), radius=0.5)
# wrapped
bpy.ops.mesh.primitive_cylinder_add(
location=(extra_width_x + xMax, yMax/2, -extra_width_z), radius=0.5)
#world bpy.ops.mesh.primitive_cylinder_add(location=(min(xAxisFull) - x_padding, 0, min(zAxisFull) - z_padding), radius=0.5)
newYCyl = bpy.context.object # define object
bpy.context.object.rotation_euler = (0,1.5708,1.5708) # in radians
bpy.context.object.dimensions = [axisWidth, axisWidth, ySize * padding]
bpy.context.object.data.materials.append(axisMaterial) # Set mat selection
bpy.ops.mesh.primitive_cylinder_add(location=(-extra_width_x, -extra_width_y, zMax/2 - axisWidth*0.245), radius=0.5)
#world bpy.ops.mesh.primitive_cylinder_add(location=(min(xAxisFull) - x_padding ,min(yAxisFull) - y_padding,-axisWidth*0.245), radius=0.5)
newZCyl = bpy.context.object # define object
# account for axis size to line up XYZ cylinder edges
zSize = zSize + axisWidth/2
# Set max Z value + abs(min) to Z size
bpy.context.object.dimensions = [axisWidth, axisWidth, zSize * padding]
bpy.context.object.data.materials.append(axisMaterial) # Set mat selection
final_axis = newXCyl, newYCyl, newZCyl
return final_axis
def generateAxisWorldSpace(self, xArray, yArray, zArray, xSize, ySize, zSize, pointDimensions, axisWidth, padding, axisMaterial):
# Extra width needed for axis to wrap objects nicely
extra_width_x = pointDimensions[0]/2
extra_width_y = pointDimensions[1]/2
extra_width_z = pointDimensions[2]/2
# Generate X Axis Cylinder
bpy.ops.mesh.primitive_cylinder_add(
location=(0, min(yArray) - extra_width_y, min(zArray) - extra_width_z), radius=0.5)
newXCyl = bpy.context.object # define object
bpy.context.object.rotation_euler = (
0, 1.5708, 0) # in radians (90 deg.)
bpy.context.object.dimensions = [axisWidth, axisWidth, xSize * padding]
bpy.context.object.data.materials.append(
axisMaterial) # Set mat selection
# Generate Y Axis Cylinder
bpy.ops.mesh.primitive_cylinder_add(
location=(min(xArray) - extra_width_x, 0, min(zArray) - extra_width_z), radius=0.5)
newYCyl = bpy.context.object # define object
bpy.context.object.rotation_euler = (0, 1.5708, 1.5708) # in radians
bpy.context.object.dimensions = [axisWidth, axisWidth, ySize * padding]
bpy.context.object.data.materials.append(axisMaterial)
# Generate Z Axis Cylinder
bpy.ops.mesh.primitive_cylinder_add(location=(min(
xArray) - extra_width_x, min(yArray) - extra_width_y, -axisWidth*0.245), radius=0.5)
newZCyl = bpy.context.object # define object
# account for axis size to line up XYZ cylinder edges
zSize = zSize + axisWidth/2
# Set max Z value + abs(min) to Z size
bpy.context.object.dimensions = [axisWidth, axisWidth, zSize * padding]
bpy.context.object.data.materials.append(
axisMaterial) # Set mat selection
final_axis = newXCyl, newYCyl, newZCyl
return final_axis
class NiceScale:
def __init__(self, minv,maxv):
self.maxTicks = 6
self.tickSpacing = 0
self.lst = 10
self.niceMin = 0
self.niceMax = 0
self.minPoint = minv
self.maxPoint = maxv
self.calculate()
def calculate(self):
self.lst = self.niceNum(self.maxPoint - self.minPoint, False)
self.tickSpacing = self.niceNum(self.lst / (self.maxTicks - 1), True)
self.niceMin = math.floor(self.minPoint / self.tickSpacing) * self.tickSpacing
self.niceMax = math.ceil(self.maxPoint / self.tickSpacing) * self.tickSpacing
def niceNum(self, lst, rround):
self.lst = lst
exponent = 0 # exponent of range
fraction = 0 # fractional part of range
niceFraction = 0 # nice, rounded fraction
exponent = math.floor(math.log10(self.lst));
fraction = self.lst / math.pow(10, exponent);
if (self.lst):
if (fraction < 1.5):
niceFraction = 1
elif (fraction < 3):
niceFraction = 2
elif (fraction < 7):
niceFraction = 5;
else:
niceFraction = 10;
else :
if (fraction <= 1):
niceFraction = 1
elif (fraction <= 2):
niceFraction = 2
elif (fraction <= 5):
niceFraction = 5
else:
niceFraction = 10
return niceFraction * math.pow(10, exponent)
def setMinMaxPoints(self, minPoint, maxPoint):
self.minPoint = minPoint
self.maxPoint = maxPoint
self.calculate()
def setMaxTicks(self, maxTicks):
self.maxTicks = maxTicks;
self.calculate()
#class generate text