-
Notifications
You must be signed in to change notification settings - Fork 0
/
movetext_sort.py
144 lines (97 loc) · 4.05 KB
/
movetext_sort.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
import bpy
from random import randrange
import math
rowCount =10
columnCount = 18
endFrame = 40
startFrame = 1
itemWidth = .1
itemHeight = .1
emptyLocation = bpy.data.objects["Empty"].location
print(emptyLocation)
'''
recurLayerCollection
Recursively transverse layer_collection for a particular name
Parameters --
layerColl: the layer collection to use as the start point
collName: collection name you are searching for
'''
def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found
'''
selectCollection
select a Collection via selecting its wrapping
LayerCollection
Parameters:
collectionName: name of the collection
'''
def selectCollection(collectionName):
# get the current collection's wrapping layer collection
layer_collection = bpy.context.view_layer.layer_collection
# layer collections wrap collections you need to find the layer collection
# that wraps your item to set it active
piecesLayer = recurLayerCollection(layer_collection,collectionName)
# make active
bpy.context.view_layer.active_layer_collection = piecesLayer
return piecesLayer.collection
def setUp(stuffCollection,stuffCount):
# context = bpy.context
# for ob in context.selected_objects:
# ob.animation_data_clear()
bpy.ops.screen.frame_jump(end=True)
for k in range(0,stuffCount):
ob = stuffCollection.all_objects[k]
ob.animation_data_clear()
# https://blender.stackexchange.com/questions/260149/set-keyframe-interpolation-constant-while-setting-a-keyframe-in-blender-python
def setConstantAction(t,idx):
my_action = bpy.data.actions.get(t.animation_data.action.name)
my_fcu = my_action.fcurves.find("location", index=idx)
for pt in my_fcu.keyframe_points:
pt.interpolation = 'BACK'
def main():
# in console bpy.data.collections['stuff'].objects['Cube']
col = selectCollection('parts')
stuffCollection = []
for k in col.all_objects:
stuffCollection.append(k)
def sortFunc(o):
ee = (emptyLocation[0] - o.location[0]) * (emptyLocation[0] - o.location[0])
ee = ee + (emptyLocation[1] - o.location[1]) * (emptyLocation[1] - o.location[1])
ee = math.sqrt(ee)
return ee
stuffCollection.sort(key=sortFunc)
stuffCount = len(col.all_objects)
setUp(col,stuffCount)
debugit = "frame is {0:d} add is [{1:.2f},{2:.2f},{3:.2f}]"
dirValue = 1
#lock values at last frame
for k in range(0,stuffCount):
thg = stuffCollection[k]
thg.keyframe_insert(data_path="location", frame=endFrame)
thg.keyframe_insert(data_path="rotation_euler",frame=endFrame)
for k in range(0,stuffCount):
thg = stuffCollection[k]
print(thg.name)
bpy.data.objects[thg.name].select_set(True)
bpy.context.view_layer.objects.active = thg
if (k % 2) == 0:
dirValue = 1
else:
dirValue = -1
dirValue = 1
#S thg.location[1] = thg.location[1] +(dirValue *(rowCount) * itemHeight) *.4 #comment out
thg.location[0] = thg.location[0] + dirValue * ( 3 * (k /stuffCount)) +.5
thg.rotation_euler[1] = (3.1415*2) / ((stuffCount -k)/stuffCount)
# thg.rotation_euler[2] = thg.rotation_euler[2] + (randrange(2)) # comment out
# thg.rotation_euler[2] = thg.rotation_euler[2] * dirValue # comment out
thg.keyframe_insert(data_path="location", frame=1)
thg.keyframe_insert(data_path="rotation_euler",frame=1)
setConstantAction(thg,0)
bpy.data.objects[thg.name].select_set(False)
main()