-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_xz.py
292 lines (232 loc) · 9.67 KB
/
extract_xz.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import c4d
from c4d import gui
PRINTING = True
def main():
if PRINTING:
explanation = 'Extract XZ'
explanation += '\n\nThis script transfers the X and Z animation from the Hips to a new null, to allow for better blending between motion clips.'
explanation += '\nIt also centers the animation to the origin, at the currently active frame.\nYou have to bake it afterwards.'
if not gui.QuestionDialog(explanation + '\n\nDo you want to run this?'):
return
else:
gui.MessageDialog("Running part 1...")
root_orig = doc.GetActiveObject()
if root_orig == None:
gui.MessageDialog("Select the skeleton's root that you want to extract from. (This script makes a copy")
return
# Deselect
doc.AddUndo(c4d.UNDOTYPE_BITS, root_orig)
root_orig.DelBit(c4d.BIT_ACTIVE)
# Run
if extract_xz( CopyAndShow(root_orig) ):
if PRINTING:
gui.MessageDialog("Successfully ran part 1.\nNow run Timeline > Functions > Bake Objects..., then run the highest version of extract_xz_pt2 to clean up the scene, and you're done.")
return
def extract_xz(root):
## Starting up
# Check root
if root == None or root.GetDown() == None:
gui.MessageDialog('Select the root null')
return False
doc.StartUndo()
# Clean up previous runs
if root.GetDown().GetName() == "Pos_Orig":
# Move Hips to top
doc.AddUndo(c4d.UNDOTYPE_CHANGE, root.GetDown().GetDown())
doc.InsertObject(root.GetDown().GetDown(), root)
# Delete Pos_Orig
doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, root.GetDown().GetNext())
root.GetDown().GetNext().Remove()
# Delete Hips_orig
if root.GetDown().GetNext().GetName() == "Hips_orig":
doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, root.GetDown().GetNext())
root.GetDown().GetNext().Remove()
# Find Hips
if root.GetDown().GetName() != "Hips":
gui.MessageDialog('Hips not found. Should be directly under root null')
return False
Hips = root.GetDown()
## Create Pos_Orig null
# Deselect root
doc.AddUndo(c4d.UNDOTYPE_BITS, Hips)
root.DelBit(c4d.BIT_ACTIVE)
# Select Hips
doc.AddUndo(c4d.UNDOTYPE_BITS, Hips)
Hips.SetBit(c4d.BIT_ACTIVE)
# Convert Hips to new null (same PSR)
c4d.CallCommand(1019941) # Convert to Null
# Find the newly created Pos_Orig
Pos_Orig = Hips.GetNext()
if Pos_Orig.GetName() != "Hips":
gui.MessageDialog('Dont put anything next to Hips')
doc.EndUndo()
return False
doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, Pos_Orig)
# Deselect Hips
doc.AddUndo(c4d.UNDOTYPE_BITS, Hips)
Hips.DelBit(c4d.BIT_ACTIVE)
# Rename Pos_Orig
doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, Pos_Orig)
Pos_Orig.SetName("Pos_Orig")
# Move Pos_Orig to outside of Hips
doc.AddUndo(c4d.UNDOTYPE_CHANGE, Pos_Orig)
doc.InsertObject(Pos_Orig,parent=root)
# Zero Pos_Orig's rotation and y position
doc.AddUndo(c4d.UNDOTYPE_CHANGE, Pos_Orig)
Pos_Orig.SetRelRot(c4d.Vector(0))
Pos_Orig.SetRelPos(c4d.Vector( Pos_Orig.GetRelPos()[0], 0, Pos_Orig.GetRelPos()[2] ))
## Create Hips_orig
# Copy Hips to Hips_orig
Hips_orig = Hips.GetClone()
if Hips_orig == None:
print('Failed to copy Hips')
doc.EndUndo()
return False
doc.InsertObject(Hips_orig, parent=root)
doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, Hips_orig)
# Rename Hips_orig
doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, Hips_orig)
Hips_orig.SetName("Hips_orig")
# Delete Hips_orig children
if Hips_orig.GetChildren() != None:
for child in Hips_orig.GetChildren():
doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, child)
child.Remove()
# Move Hips under Pos_Orig
doc.InsertObject(Hips, parent=Pos_Orig)
doc.AddUndo(c4d.UNDOTYPE_CHANGE, Hips)
# Move Pos_Orig to before Hips_orig
doc.InsertObject(Pos_Orig, parent=root)
doc.AddUndo(c4d.UNDOTYPE_CHANGE, Pos_Orig)
######## XPRESSO START
## Create Xpresso Network
# Set up the Xpresso tag
xtagname = "Extract XZ Drive Tag"
if Pos_Orig.GetFirstTag() != None:
if Pos_Orig.GetFirstTag().GetName() == xtagname:
doc.AddUndo(c4d.UNDOTYPE_DELETEOBJ, Pos_Orig.GetFirstTag())
Pos_Orig.GetFirstTag().Remove()
xtag = c4d.BaseTag(c4d.Texpresso) # lol I'm assuming this is supposed to be Xpresso
xtag.SetName(xtagname)
Pos_Orig.InsertTag(xtag)
doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, xtag)
nodemaster = xtag.GetNodeMaster()
xgroupname = "Extract XZ Xgroup"
xgroup = nodemaster.GetRoot()
xgroup.SetName(xgroupname)
spacing = 200
## Driving Pos_Orig
# Create Pos_Orig node
n_Pos_Orig = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_OBJECT, x=spacing*6, y=spacing*1)
n_Pos_Orig[c4d.GV_OBJECT_OBJECT_ID] = Pos_Orig
p_Pos_Orig_gpos = n_Pos_Orig.AddPort(c4d.GV_PORT_INPUT, c4d.ID_BASEOBJECT_GLOBAL_POSITION) # Can't find a way to get X,Y,Z separately. Resorting to Adapter nodes
# Subtract offset from Pos_Orig position
n_Offset = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_MATH, x=spacing*5, y=spacing*1)
n_Offset[c4d.GV_DYNAMIC_DATATYPE] = c4d.DTYPE_VECTOR ####### IMPORTANT, NOT IN DOCUMENTATION: Setting datatype of nodes
n_Offset[c4d.GV_MATH_FUNCTION_ID] = c4d.GV_SUB_NODE_FUNCTION
n_Offset.SetName("Offset")
# Get starting position of Pos_Orig
n_StartingPos = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_CONST, x=spacing*4, y=spacing*1+100)
n_StartingPos[c4d.GV_DYNAMIC_DATATYPE] = c4d.DTYPE_VECTOR ####### IMPORTANT, NOT IN DOCUMENTATION: Setting datatype of nodes
doc.SetTime(c4d.BaseTime(0)) # Set to frame 0 to get offset from frame 0
c4d.EventAdd()
n_StartingPos[c4d.GV_CONST_VALUE] = GetGlobalPos(Pos_Orig)
n_StartingPos.SetName("StartingPos")
# Create Reals2Vector node 1
n_r2v_1 = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_REAL2VECT, x=spacing*3, y=spacing*1)
n_r2v_1.SetName("r2v_1")
# Create Vector2Reals node 1
n_v2r_1 = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_VECT2REAL, x=spacing*2, y=spacing*1)
n_v2r_1.SetName("v2r_1")
# Create Hips_orig node
n_Hips_orig = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_OBJECT, x=spacing*1, y=spacing*1)
n_Hips_orig[c4d.GV_OBJECT_OBJECT_ID] = Hips_orig
p_Hips_orig_gpos = n_Hips_orig.AddPort(c4d.GV_PORT_OUTPUT, c4d.ID_BASEOBJECT_GLOBAL_POSITION)
## Driving Hips
# Create Hips node
n_Hips = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_OBJECT, x=spacing*4, y=spacing*2)
n_Hips[c4d.GV_OBJECT_OBJECT_ID] = Hips
p_Hips_pos = n_Hips.AddPort(c4d.GV_PORT_INPUT, c4d.ID_BASEOBJECT_POSITION)
# Create Vector2Reals node 2
n_v2r_2 = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_VECT2REAL, x=spacing*2, y=spacing*2)
n_v2r_2.SetName("v2r_2")
# Create Reals2Vector node 2
n_r2v_2 = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_REAL2VECT, x=spacing*3, y=spacing*2)
n_r2v_2.SetName("r2v_2")
# Create Constant Zero node
n_Zero = nodemaster.CreateNode(xgroup, c4d.ID_OPERATOR_CONST, x=spacing*1, y=spacing*2)
n_Zero[c4d.GV_CONST_VALUE] = 0
n_Zero.SetName("Zero")
## Connect
# Group 1
p_Pos_Orig_gpos.Connect(n_Offset.GetOutPort(0))
n_Offset.GetInPort(0).Connect(n_r2v_1.GetOutPort(0))
n_Offset.GetInPort(1).Connect(n_StartingPos.GetOutPort(0))
n_r2v_1.GetInPort(0).Connect(n_v2r_1.GetOutPort(0))
n_r2v_1.GetInPort(1).Connect(n_Zero.GetOutPort(0))
n_r2v_1.GetInPort(2).Connect(n_v2r_1.GetOutPort(2))
p_Hips_orig_gpos.Connect(n_v2r_1.GetInPort(0))
# Group 2
p_Hips_pos.Connect(n_r2v_2.GetOutPort(0))
n_Zero.GetOutPort(0).Connect(n_r2v_2.GetInPort(0))
p_Hips_orig_gpos.Connect(n_v2r_2.GetInPort(0))
n_v2r_2.GetOutPort(1).Connect(n_r2v_2.GetInPort(1))
n_Zero.GetOutPort(0).Connect(n_r2v_2.GetInPort(2))
###### XPRESSO END
## Bake Objects
# Deselect root
doc.AddUndo(c4d.UNDOTYPE_BITS, root)
root.DelBit(c4d.BIT_ACTIVE)
# Select Pos_Orig
doc.AddUndo(c4d.UNDOTYPE_BITS, Pos_Orig)
Pos_Orig.SetBit(c4d.BIT_ACTIVE)
c4d.EventAdd()
# Bake
# Doesn't work the same for some reason. Resorting to user clicking it
#c4d.CallCommand(465001219) # Bake Objects...
# Rename root
doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, root)
root.SetName(root.GetName()[:-1-len(RemoveAllPrefixes(root.GetName(),"_ "))] + "_extractxz")
## Finishing up
doc.EndUndo()
c4d.EventAdd()
return True
#return extract_xz_pt2()
def UnfoldRig(root):
"""Returns an object and its children as a python list."""
objlist = []
objlist.append(root)
UnfoldRig_recurse(root.GetDown(), objlist)
return objlist
def UnfoldRig_recurse(root, objlist):
objlist.append(root)
if root.GetDown() != None:
objlist = UnfoldRig_recurse(root.GetDown(), objlist)
if root.GetNext() != None:
objlist = UnfoldRig_recurse(root.GetNext(), objlist)
return objlist
def RemoveAllPrefixes(name, delims="_"):
"""Returns the last 'word' in a [delims]-separated string.
Ex. RemoveAllPrefixes('DEF_New.RightArm', '._') returns 'RightArm'"""
bareName = ""
for char in reversed(name):
if char in delims:
break
bareName = char + bareName
return bareName
def CopyAndShow(obj):
doc.StartUndo()
# Copy
doc.InsertObject(obj.GetClone())
newobj = doc.GetFirstObject()
doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, newobj)
# Show
doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, newobj)
newobj[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = 2
doc.EndUndo()
return newobj
def GetGlobalPos(obj):
return obj.GetMg().off
# Execute main()
if __name__=='__main__':
main()