forked from n1ckfg/C4DToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc4dtoolbox.py
244 lines (197 loc) · 5.58 KB
/
c4dtoolbox.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
import c4d, math, sys
import socket, time, threading
from random import uniform as rnd
#~~~~~~~~~~~~~ core ~~~~~~~~~~~~~~~~~
def getDoc():
return c4d.documents.GetActiveDocument()
def getFps():
doc = getDoc()
return doc.GetFps()
def getTarget():
doc = getDoc()
return doc.GetActiveObjects(0)
def getMode():
returns = ""
if c4d.IsCommandChecked(12101): # Object Mode
returns = "object"
elif c4d.IsCommandChecked(12298): # Model Mode
returns = "model"
return returns
def setMode(s = "object"):
returns = ""
if s=="model":
returns = "model"
c4d.CallCommand(12298) # Model Mode
elif s=="object":
returns = "object"
c4d.CallCommand(12101) # Object Mode
return returns
def refresh():
c4d.EventAdd()
#c4d.DrawViews(c4d.DRAWFLAGS_FORCEFULLREDRAW) #Update screen
def select(target=None):
#if not target:
# target = getTarget()
deselect()
target.SetBit(c4d.BIT_ACTIVE)
refresh()
#return target
def deselect(target=None):
if not target:
target = getTarget()
for i in range(0,len(target)):
target[i].DelBit(c4d.BIT_ACTIVE)
refresh()
#return target
'''
def iterate(target=None):
deselect()
select(target)
#refresh()
'''
def getFrame():
doc = getDoc()
fps = getFps()
curTime = doc.GetTime()
curFrame = curTime.GetFrame(fps)
return curFrame
def setFrame(_frame):
doc = getDoc()
fps = getFps()
doc.SetTime(c4d.BaseTime(_frame, fps))
#~~~~~~~~~~~~~ utilities ~~~~~~~~~~~~~~~~~
def setPos(x,y,z):
target = getTarget()
p = c4d.Vector(x,y,z)
for i in range(0,len(target)):
target[i].SetAbsPos(p)
refresh()
def setRot(x,y,z):
target = getTarget()
r = c4d.Vector(x,y,z)
for i in range(0,len(target)):
target[i].SetAbsRot(r)
refresh()
def setScale(x,y,z):
target = getTarget()
s = c4d.Vector(x,y,z)
for i in range(0,len(target)):
target[i].SetAbsScale(s)
refresh()
def getPos(target=None):
if not target:
target = getTarget()
p = target[0].GetAbsPos()
return p
def getRot(target=None):
if not target:
target = getTarget()
r = target[0].GetAbsRot()
return r
def getScale(target=None):
if not target:
target = getTarget()
s = target[0].GetAbsScale()
return s
#random 3d vector
def rnd3d(spread=5):
return c4d.Vector(rnd(-spread,spread),rnd(-spread,spread),rnd(-spread,spread))
#move to random location
def rndMove(spread=5):
val = rnd3d(spread)
move(val[0],val[1],val[2])
def ident(target=None):
if not target:
target = getTarget()
if len(target) > 0: # we have one or more target objects in the scene
for ob in target:
print "Object: " + ob.GetName() + ", type: <" + ob.GetTypeName() + ">, ID: " + str(ob.GetType())
def polyCube():
doc = getDoc()
target = c4d.BaseObject(c4d.Ocube) # Create new cube
target.SetRelPos(c4d.Vector(20)) # Set position of cube
doc.InsertObject(target)
#target.SetBit(c4d.BIT_ACTIVE)
select(target)
refresh()
#~~~~~~~~~~~~~ keyframes ~~~~~~~~~~~~~~~~~
# Based on https://github.com/jusu/Cinema4D-Helpers
#
# Misc
#
def frame(doc):
"""Get current frame number."""
return doc.GetTime().GetFrame(doc.GetFps())
#
# Keying
#
def getVecIds(id=c4d.ID_BASEOBJECT_REL_POSITION):
"""Get IDs for X, Y, Z components of a vector id. For example, XYZ of c4d.ID_BASEOBJECT_GLOBAL_POSITION"""
x = c4d.DescID(c4d.DescLevel(id, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0))
y = c4d.DescID(c4d.DescLevel(id, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0))
z = c4d.DescID(c4d.DescLevel(id, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_Z, c4d.DTYPE_REAL, 0))
return (x, y, z)
def createKey(op, id, value, forFrame = None):
doc = op.GetDocument()
if not doc: raise Exception, "object must be in a document"
if forFrame == None:
forFrame = frame(doc)
# First check if the track type already exists, otherwise create it...
track=op.FindCTrack(id)
if not track:
track=c4d.CTrack(op,id)
op.InsertTrackSorted(track)
curve=track.GetCurve()
key=curve.AddKey(c4d.BaseTime(forFrame, doc.GetFps()))
if type(value)==int or type(value)==float:
key["key"].SetValue(curve,value)
else:
key["key"].SetGeData(curve,value)
def addFloatKey(op, id):
createKey(op, id, op[id])
def addVectorKey(op, id):
ids = getVecIds(id)
v = op[id]
createKey(op, ids[0], v.x)
createKey(op, ids[1], v.y)
createKey(op, ids[2], v.z)
def addKey(op, id):
t = type(op[id])
if t == int or t == float:
addFloatKey(op, id)
else:
addVectorKey(op, id)
def keyframe(target=None, id=None):
if not target:
target=getTarget()
if not id:
#origMode = getMode()
setMode("object")
#for i in range(0,len(target)):
id=c4d.ID_BASEOBJECT_REL_POSITION
addKey(target[0],id)
id=c4d.ID_BASEOBJECT_REL_ROTATION
addKey(target[0],id)
id=c4d.ID_BASEOBJECT_REL_SCALE
addKey(target[0],id)
#setMode(origMode)
else:
#for i in range(0,len(target)):
addKey(target[0],id)
refresh()
#~~~~~~~~~~~~~ MayaToolbox shortcuts ~~~~~~~~~~~~~~~~~
move = setPos
rotate = setRot
scale = setScale
s = getTarget
rndVec = rnd3d
'''
def m(p=[0,0,0]):
move(p[0],p[1],[2])
'''
def t(_t=None):
try:
setFrame(_t)
except:
print "time: " + str(getFrame())
return getFrame()