-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndPointCreator.py
167 lines (118 loc) · 5.79 KB
/
EndPointCreator.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
import bpy
import math
import sys
moduleParentName = '.'.join(__name__.split('.')[:-1])
ImproperCruveException = sys.modules[moduleParentName + '.Exceptions'].ImproperCruveException
ParamNames = sys.modules[moduleParentName + '.Constants'].ParamNames
Keywords = sys.modules[moduleParentName + '.Constants'].Keywords
def createEndPoint(curveOB, bevelShape): ##End point rounding
endPointBevel = bevelShape.copy()
endPointBevel.name = curveOB.name + "_end_point"
endPointBevel.modifiers.new('Screw', 'SCREW')
endPointBevel.modifiers.get("Screw").angle = math.pi
endPointBevel.modifiers.get("Screw").axis = 'Y'
endPointBevel.rotation_euler[0] = math.pi/2
endPointBevel.location = (0,0,0)
endPointBevel.constraints.new("FOLLOW_PATH")
endPointBevel.constraints.get("Follow Path").target = curveOB
endPointBevel.constraints.get("Follow Path").use_fixed_location = True
endPointBevel.constraints.get("Follow Path").use_curve_radius = True
endPointBevel.constraints.get("Follow Path").use_curve_follow = True
driver = endPointBevel.constraints.get("Follow Path").driver_add("offset_factor").driver
var = driver.variables.new()
var.name = "var"
var.targets[0].id = curveOB
var.targets[0].data_path = "data.bevel_factor_end"
driver.expression = var.name
return endPointBevel
def createStartPoint(curveOB, bevelShape): ##Start point rounding
startPointBevel = bevelShape.copy()
startPointBevel.name = curveOB.name + "_start_point"
startPointBevel.modifiers.new('Screw', 'SCREW')
startPointBevel.modifiers.get("Screw").angle = math.pi
startPointBevel.modifiers.get("Screw").axis = 'Y'
startPointBevel.rotation_euler[0] = math.pi/2
startPointBevel.location = (0,0,0)
startPointBevel.constraints.new("FOLLOW_PATH")
startPointBevel.constraints.get("Follow Path").target = curveOB
startPointBevel.constraints.get("Follow Path").use_fixed_location = True
startPointBevel.constraints.get("Follow Path").use_curve_radius = True
startPointBevel.constraints.get("Follow Path").use_curve_follow = True
driver = startPointBevel.constraints.get("Follow Path").driver_add("offset_factor").driver
var = driver.variables.new()
var.name = "var"
var.targets[0].id = curveOB
var.targets[0].data_path = "data.bevel_factor_start"
driver.expression = var.name
return startPointBevel
def createEndPoints(curveOB, params, coin, seamDistanceProp = "Stitcher_Props.SeamDistance"):
headProb = params[ParamNames.seamAbberations][ParamNames.probability]
abbAmount = params[ParamNames.seamAbberations][ParamNames.amount]
bevelShape = curveOB.data.bevel_object
if (bevelShape == None):
raise ImproperCruveException(curveOB)
endPointBevel = createEndPoint(curveOB, bevelShape)
startPointBevel = createStartPoint(curveOB, bevelShape)
mat = bpy.data.materials.get(Keywords.materialName)
if (len(endPointBevel.data.materials) == 0):
endPointBevel.data.materials.append(mat)
if (len(startPointBevel.data.materials) == 0):
startPointBevel.data.materials.append(mat)
try:
curveOB.data["lengthOfCurve"]
endPointBevel["layerNumber"] = startPointBevel["layerNumber"] = curveOB["layerNumber"]
endPointBevel["type"] = startPointBevel["type"] = "End_Point"
endPointBevel["parent"] = startPointBevel["parent"] = curveOB
except KeyError:
raise ImproperCruveException(curveOB)
curveOB.data["startOffset"] = 0.00
curveOB.data["endOffset"] = 0.00
##Make start and end offset absolute according to length, so that the seams can be managed from a central value using offset
driver = curveOB.data.driver_add("bevel_factor_start").driver
var0 = driver.variables.new()
var0.name = "var1"
var0.targets[0].id = curveOB
var0.targets[0].data_path = 'data["startOffset"]'
var1 = driver.variables.new()
var1.name = "var2"
var1.targets[0].id_type = curveOB.type
var1.targets[0].id = curveOB.data
var1.targets[0].data_path = '["lengthOfCurve"]'
if (coin.toss(headProb)):
driver.expression = "(1 - ({1} - ({0} + {2}))/{1}) ".format(var0.name, var1.name, coin.uniform(0,abbAmount))
else:
driver.expression = "(1 - ({1} - {0})/{1})".format(var0.name, var1.name)
driver = curveOB.data.driver_add("bevel_factor_end").driver
var0 = driver.variables.new()
var0.name = "var1"
var0.targets[0].id = curveOB
var0.targets[0].data_path = 'data["endOffset"]'
var1 = driver.variables.new()
var1.name = "var2"
var1.targets[0].id_type = curveOB.type
var1.targets[0].id = curveOB.data
var1.targets[0].data_path = '["lengthOfCurve"]'
if (coin.toss(headProb)):
driver.expression = "(({1} - ({0} + {2}))/{1}) ".format(var0.name, var1.name, coin.uniform(0,abbAmount))
else:
driver.expression = "({1} - {0})/{1}".format(var0.name, var1.name)
##Linking everything to the seam distance slider on the addon
driver = curveOB.data.driver_add('["endOffset"]').driver
var = driver.variables.new()
var.name = "var"
var.targets[0].id_type = 'SCENE'
var.targets[0].id = bpy.context.scene
var.targets[0].data_path = seamDistanceProp
driver.expression = var.name
driver = curveOB.data.driver_add('["startOffset"]').driver
var = driver.variables.new()
var.name = "var"
var.targets[0].id_type = 'SCENE'
var.targets[0].id = bpy.context.scene
var.targets[0].data_path = seamDistanceProp
driver.expression = var.name
return (endPointBevel, startPointBevel)
if __name__ == "__main__":
endPoint, startPoint = createEndPoints(bpy.data.objects.get("myCurve"))
bpy.data.collections.get("Layer 1").objects.link(endPoint)
bpy.data.collections.get("Layer 1").objects.link(startPoint)