3
""" Registration info for Blender menus: <- these words are ignored
7
Tip: 'See Trajectory of selected object'
10
__author__ = '3R - R3gis'
12
__url__ = ["Script's site , http://blenderfrance.free.fr/python/Trajectory_en.htm","Author's site , http://cybercreator.free.fr", "French Blender support forum, http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender"]
13
__email__=["3R, r3gis@free.fr"]
20
* Launch with alt+P (or put it in .script folder)
22
Allow to see in real time trajectory of selected object.
24
On first run, it ask you
25
- If you want that actually selected object have they trajectory always shown
26
- If you want to use Space Handler or a Scriptlink in Redraw mode
27
- Future and Past : it is the frame in past and future
28
of the beggining and the end of the path
29
- Width of line that represent the trajectory
31
Then the object's trajectory will be shown in all 3D areas.
32
When trajectory is red, you can modifiy it by moving object.
33
When trajectory is blue and you want to be able to modify it, inser a Key (I-Key)
35
Points appears on trajectory :
36
- Left Clic to modify position
37
- Right Clic to go to the frame it represents
40
In scriptlink mode, it create one script link so make sure that 'Enable Script Link' toogle is on
41
In SpaceHandler mode, you have to go in View>>SpaceHandlerScript menu to activate Trajectory
47
# --------------------------------------------------------------------------
48
# ***** BEGIN GPL LICENSE BLOCK *****
50
# Copyright (C) 2004-2006: Regis Montoya
52
# This program is free software; you can redistribute it and/or
53
# modify it under the terms of the GNU General Public License
54
# as published by the Free Software Foundation; either version 2
55
# of the License, or (at your option) any later version.
57
# This program is distributed in the hope that it will be useful,
58
# but WITHOUT ANY WARRANTY; without even the implied warranty of
59
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60
# GNU General Public License for more details.
62
# You should have received a copy of the GNU General Public License
63
# along with this program; if not, write to the Free Software Foundation,
64
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
66
# ***** END GPL LICENCE BLOCK *****
67
# --------------------------------------------------------------------------
68
#################################
72
# ou sur le newsgroup:
73
# http://zoo-logique.org/3D.Blender/
74
#################################
75
#Many thanks to cambo for his fixes
76
#################################
83
scene= Blender.Scene.GetCurrent()
87
def write_script(name, script):
89
#List texts and their name
90
#write : type of writing : 1->New, 2->Overwrite
92
for text in Blender.Text.Get():
93
if text.name==name and text.asLines()[1] != "#"+str(__version__):
94
scripting= Blender.Text.Get(name)
95
scripting.write(script)
99
scripting= Blender.Text.New(name)
100
scripting.write(script)
104
def link_script(name, type):
107
scene.getScriptLinks(type).index(name) # Does nothing. just fails of the script isnt there.
109
scene.addScriptLink(name, type)
113
def text_remove(name):
115
#try to delete text if already linked
117
text= Blender.Text.Get(name)
119
scene.clearScriptLinks([name])
120
Blender.Text.unlink(text)
122
print('---Initialisation of Trajectory_'+str(__version__)+'.py---')
124
#Whether is already running, also check if it's the last version of the script : second line contain the version fo the script
125
ask_modif= 0 # Default
126
for text in Blender.Text.Get():
127
if text.name == 'Trajectory' and text.asLines()[1] == "#"+str(__version__):
128
#We ask if script modify his seetings, keep it or stop script
129
ask_modif= Blender.Draw.PupMenu("Script already launch %t|Modify settings%x0|Keep settings%x1|Stop script%x2|")
130
if ask_modif==-1: # user canceled.
141
handle_mode= Blender.Draw.Create(0)
142
selection_mode= Blender.Draw.Create(0)
143
future= Blender.Draw.Create(35)
144
past= Blender.Draw.Create(20)
145
width= Blender.Draw.Create(2)
148
block.append(("Use Space Handlers", handle_mode, "You have to activate for each area by View>>SpaceHandler")) #You can delete this option...
149
block.append(("Always draw for sel.", selection_mode, "Selected object will have their trajectory always shown"))
150
block.append(("Past :", past, 1, 900))
151
block.append(("Futur:", future, 1, 900))
152
block.append(("Width:", width, 1,5))
3
""" Registration info for Blender menus: <- these words are ignored
7
Tip: 'See Trajectory of selected object'
10
__author__ = '3R - R3gis'
12
__url__ = ["Script's site , http://blenderfrance.free.fr/python/Trajectory_en.htm","Author's site , http://cybercreator.free.fr", "French Blender support forum, http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender"]
13
__email__=["3R, r3gis@free.fr"]
20
* Launch with alt+P (or put it in .script folder)
22
Allow to see in real time trajectory of selected object.
24
On first run, it ask you
25
- If you want that actually selected object have they trajectory always shown
26
- If you want to use Space Handler or a Scriptlink in Redraw mode
27
- Future and Past : it is the frame in past and future
28
of the beggining and the end of the path
29
- Width of line that represent the trajectory
31
Then the object's trajectory will be shown in all 3D areas.
32
When trajectory is red, you can modifiy it by moving object.
33
When trajectory is blue and you want to be able to modify it, inser a Key (I-Key)
35
Points appears on trajectory :
36
- Left Clic to modify position
37
- Right Clic to go to the frame it represents
40
In scriptlink mode, it create one script link so make sure that 'Enable Script Link' toogle is on
41
In SpaceHandler mode, you have to go in View>>SpaceHandlerScript menu to activate Trajectory
47
# --------------------------------------------------------------------------
48
# ***** BEGIN GPL LICENSE BLOCK *****
50
# Copyright (C) 2004-2006: Regis Montoya
52
# This program is free software; you can redistribute it and/or
53
# modify it under the terms of the GNU General Public License
54
# as published by the Free Software Foundation; either version 2
55
# of the License, or (at your option) any later version.
57
# This program is distributed in the hope that it will be useful,
58
# but WITHOUT ANY WARRANTY; without even the implied warranty of
59
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60
# GNU General Public License for more details.
62
# You should have received a copy of the GNU General Public License
63
# along with this program; if not, write to the Free Software Foundation,
64
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
66
# ***** END GPL LICENCE BLOCK *****
67
# --------------------------------------------------------------------------
68
#################################
72
# ou sur le newsgroup:
73
# http://zoo-logique.org/3D.Blender/
74
#################################
75
#Many thanks to cambo for his fixes
76
#################################
83
scene= Blender.Scene.GetCurrent()
87
def write_script(name, script):
89
#List texts and their name
90
#write : type of writing : 1->New, 2->Overwrite
92
for text in Blender.Text.Get():
93
if text.name==name and text.asLines()[1] != "#"+str(__version__):
96
scripting.write(script)
100
scripting= Blender.Text.New(name)
101
scripting.write(script)
103
def link_script(name, type):
105
scriptlinks = scene.getScriptLinks(type) # none or list
106
if not scriptlinks or name not in scriptlinks:
107
scene.addScriptLink(name, type)
111
def text_remove(name):
113
#try to delete text if already linked
115
text= Blender.Text.Get(name)
117
scene.clearScriptLinks([name])
118
Blender.Text.unlink(text)
120
print('---Initialisation of Trajectory_'+str(__version__)+'.py---')
122
#Whether is already running, also check if it's the last version of the script : second line contain the version fo the script
123
ask_modif= 0 # Default
124
for text in Blender.Text.Get():
125
if text.name == 'Trajectory' and text.asLines()[1] == "#"+str(__version__):
126
#We ask if script modify his seetings, keep it or stop script
127
ask_modif= Blender.Draw.PupMenu("Script already launch %t|Modify settings%x0|Keep settings%x1|Stop script%x2|")
128
if ask_modif==-1: # user canceled.
139
handle_mode= Blender.Draw.Create(0)
140
selection_mode= Blender.Draw.Create(0)
141
future= Blender.Draw.Create(35)
142
past= Blender.Draw.Create(20)
143
width= Blender.Draw.Create(2)
146
block.append(("Space Handlers", handle_mode, "You have to activate for each area by View>>SpaceHandler")) #You can delete this option...
147
block.append(("Always Draw", selection_mode, "Selected object will have their trajectory always shown"))
148
block.append(("Past :", past, 1, 900))
149
block.append(("Futur:", future, 1, 900))
150
block.append(("Width:", width, 1,5))
154
152
if not Blender.Draw.PupBlock("Trajectory seetings", block):
157
handle_mode= handle_mode.val
158
selection_mode= selection_mode.val
164
#put names of selected objects in objects_select if option choosen by user
165
if selection_mode==1:
166
objects_select= [ob.name for ob in Blender.Object.GetSelected()]
173
DrawPart="#SPACEHANDLER.VIEW3D.DRAW\n"
176
except:DrawPart="#BadlyMade"
179
#Here is the script to write in Blender and to link, options are also written now
180
DrawPart=DrawPart+"#"+str(__version__)+"""
181
#This script is a part of Trajectory.py and have to be linked to the scene in Redraw if not in HANDLER mode.
182
#Author : 3R - Regis Montoya
183
#It's better to use the Trajectory_"version_number".py
184
#You can modify the two following value to change the path settings
185
future="""+str(future)+"""
186
past="""+str(past)+"""
187
object_init_names="""+str(objects_select)+"""
191
from Blender import BGL, Draw, Ipo
192
from Blender.BGL import *
193
from Blender.Draw import *
196
from Blender.Mathutils import Vector
199
frameC=Blender.Get('curframe')
200
render_context=Blender.Scene.getCurrent().getRenderingContext()
201
#ajust number of frames with NewMap and OldMapvalue values
202
k=1.00*render_context.oldMapValue()/render_context.newMapValue()
204
tr=-1*int(log(k*0.1, 10))
206
tr=-1*int(log(k, 10))
207
#The real and integer frame to compare to ipos keys frames
208
frameCtr=round(frameC*k, tr)
210
frameC=int(round(frameC*k, 0))
213
#List objects that we have to show trajectory in $objects
214
# In this case, using a dict for unique objects is the fastest way.
215
object_dict= dict([(ob.name, ob) for ob in Blender.Object.GetSelected()])
216
for obname in object_init_names:
217
try: # checking if its alredy there.
219
except: # Object is not there.
220
try: # Object may be removed.
221
object_dict[obname]= Blender.Object.Get(obname)
223
pass # object was removed.
225
#This fonction give the resulting matrix of all parents at a given frame
226
#parent_list is the list of all parents [object, matrix, locX_ipo, locY, Z, rotX, Y, Z, sizeX, Y, Z] of current object
227
def matrixForTraj(frame, parent_list):
228
DecMatC=Blender.Mathutils.Matrix([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1])
230
for parent_data in parent_list:
231
parent_ob= parent_data[0]
233
try: X= parent_data[5].evaluate(frame)*pi/18
234
except: X= parent_ob.RotX
235
try: Y= parent_data[6].evaluate(frame)*pi/18
236
except: Y= parent_ob.RotY
237
try: Z= parent_data[7].evaluate(frame)*pi/18
238
except: Z= parent_ob.RotZ
239
try: LX= parent_data[2].evaluate(frame)
240
except: LX= parent_ob.LocX
241
try: LY= parent_data[3].evaluate(frame)
242
except: LY= parent_ob.LocY
243
try: LZ= parent_data[4].evaluate(frame)
244
except: LZ= parent_ob.LocZ
245
try: SX= parent_data[8].evaluate(frame)
246
except: SX= parent_ob.SizeX
247
try: SY= parent_data[9].evaluate(frame)
248
except: SY= parent_ob.SizeY
249
try: SZ= parent_data[10].evaluate(frame)
250
except: SZ= parent_ob.SizeZ
252
NMat=Blender.Mathutils.Matrix([cos(Y)*cos(Z)*SX,SX*cos(Y)*sin(Z),-SX*sin(Y),0],
253
[(-cos(X)*sin(Z)+sin(Y)*sin(X)*cos(Z))*SY,(sin(X)*sin(Y)*sin(Z)+cos(X)*cos(Z))*SY,sin(X)*cos(Y)*SY,0],
254
[(cos(X)*sin(Y)*cos(Z)+sin(X)*sin(Z))*SZ,(cos(X)*sin(Y)*sin(Z)-sin(X)*cos(Z))*SZ,SZ*cos(X)*cos(Y),0],
256
DecMatC=DecMatC*parent_data[1]*NMat
261
matview=Blender.Window.GetPerspMatrix()
263
#Fonction to draw trajectories
267
global TestLIST, matview
268
#we draw trajectories for all objects in list
273
#List with trajectories' vertexs
278
ipoLocX=contextIpo[Ipo.OB_LOCX]
279
ipoLocY=contextIpo[Ipo.OB_LOCY]
280
ipoLocZ=contextIpo[Ipo.OB_LOCZ]
281
ipoTime=contextIpo[Ipo.OB_TIME]
282
else: # only do if there is no IPO (if no ipo curves : return None object and don't go in this except)
283
ipoLocX= ipoLocY= ipoLocZ= ipoTime= None
288
#Get all parents of ob
294
#Get parents's infos :
295
#list of [name, initial matrix at make parent, ipo in X,Y,Z,rotX,rotY,rotZ,sizeX,Y,Z]
297
Init_Mat=Blender.Mathutils.Matrix(child.getMatrix('worldspace')) #must be done like it (it isn't a matrix otherwise)
299
Init_Mat=Init_Mat*child.getMatrix('localspace')
300
Init_Mat=parent.getMatrix()*Init_Mat
303
contextIpo= parent.ipo # None or IPO
305
ipo_Parent_LocX=contextIpo[Ipo.OB_LOCX]
306
ipo_Parent_LocY=contextIpo[Ipo.OB_LOCY]
307
ipo_Parent_LocZ=contextIpo[Ipo.OB_LOCZ]
308
ipo_Parent_RotX=contextIpo[Ipo.OB_ROTX]
309
ipo_Parent_RotY=contextIpo[Ipo.OB_ROTY]
310
ipo_Parent_RotZ=contextIpo[Ipo.OB_ROTZ]
311
ipo_Parent_SizeX=contextIpo[Ipo.OB_SIZEX]
312
ipo_Parent_SizeY=contextIpo[Ipo.OB_SIZEY]
313
ipo_Parent_SizeZ=contextIpo[Ipo.OB_SIZEZ]
315
ipo_Parent_LocX=ipo_Parent_LocY=ipo_Parent_LocZ=\
316
ipo_Parent_RotX=ipo_Parent_RotY=ipo_Parent_RotZ=\
317
ipo_Parent_SizeX=ipo_Parent_SizeY=ipo_Parent_SizeZ= None
319
parent_list.append([parent, Init_Mat, ipo_Parent_LocX, ipo_Parent_LocY, ipo_Parent_LocZ, ipo_Parent_RotX, ipo_Parent_RotY, ipo_Parent_RotZ, ipo_Parent_SizeX, ipo_Parent_SizeY, ipo_Parent_SizeZ])
324
#security : if one of parents object are a path>>follow : trajectory don't work properly so it have to draw nothing
325
for parent in parent_list:
326
# getData() is slow especialy with NMesh.
328
if parent[0].getType() == 'Curve':
329
if parent[0].data.flag & 1<<4: # Follow path, 4th bit
332
#ob >> re-assign obj and not parent
336
if ipoLocX: LXC= ipoLocX.evaluate(frameC)
338
if ipoLocY: LYC= ipoLocY.evaluate(frameC)
340
if ipoLocZ: LZC= ipoLocZ.evaluate(frameC)
343
vect= Vector([ob.LocX, ob.LocY, ob.LocZ, 1])
346
#If trajectory is being modified and we are at a frame where a ipo key already exist
347
if round(ob.LocX, 5)!=round(LXC, 5):
348
for bez in ipoLocX.bezierPoints:
349
if round(bez.getPoints()[0], tr)==frameCtr:
350
bez.setPoints((frameCr, vect[0]))
352
if round(ob.LocY, 5)!=round(LYC, 5):
353
for bez in ipoLocY.bezierPoints:
354
if round(bez.getPoints()[0], tr)==frameCtr:
355
bez.setPoints((frameCr, vect[1]))
357
if round(ob.LocZ, 5)!=round(LZC, 5):
358
for bez in ipoLocZ.bezierPoints:
359
if round(bez.getPoints()[0], tr)==frameCtr:
360
bez.setPoints((frameCr, vect[2]))
363
#change trajectory color if at an ipoKey
366
if ipoLocX: # FIXED like others it was just in case ipoLocX==None
367
for bez in ipoLocX.bezierPoints:
368
bezier_Coord=round(bez.getPoints()[0], tr)
369
if bezier_Coord not in VertexFrame:
370
VertexFrame.append(bezier_Coord)
371
if bezier_Coord==frameCtr:
372
color=[1, color[1]-0.3]
374
for bez in ipoLocY.bezierPoints:
375
bezier_Coord=round(bez.getPoints()[0], tr)
376
if bezier_Coord not in VertexFrame:
377
VertexFrame.append(bezier_Coord)
378
if round(bez.getPoints()[0], tr)==frameCtr:
379
color=[1, color[1]-0.3]
381
for bez in ipoLocZ.bezierPoints:
382
bezier_Coord=round(bez.getPoints()[0], tr)
383
if bezier_Coord not in VertexFrame:
384
VertexFrame.append(bezier_Coord)
385
if round(bez.getPoints()[0], tr)==frameCtr:
386
color=[1, color[1]-0.3]
389
#put in LocX, LocY and LocZ all points of trajectory
390
for frame in xrange(frameC-past, frameC+future):
391
DecMat=matrixForTraj(frame, parent_list)
393
if ipoLocX: LX= ipoLocX.evaluate(frame)
395
if ipoLocY: LY= ipoLocY.evaluate(frame)
397
if ipoLocZ: LZ= ipoLocZ.evaluate(frame)
400
vect= Vector([LX, LY, LZ, 1])
407
#draw part : get current view
408
MatPreBuff= [matview[i][j] for i in xrange(4) for j in xrange(4)]
410
MatBuff=BGL.Buffer(GL_FLOAT, 16, MatPreBuff)
413
glMatrixMode(GL_PROJECTION)
415
glLoadMatrixf(MatBuff)
417
#draw trajectory line
418
glLineWidth("""+str(width)+""")
420
glBegin(GL_LINE_STRIP)
421
for i in xrange(len(LocX)):
422
glColor3f((i+1)*1.00/len(LocX)*color[0], 0, (i+1)*1.00/len(LocX)*color[1])
423
glVertex3f(LocX[i], LocY[i], LocZ[i])
427
#draw trajectory's "vertexs"
428
if not Blender.Window.EditMode():
434
for frame in VertexFrame:
435
ix=int(frame)-frameC+past
436
if ix>=0 and ix<len(LocX):
437
glColor3f(1, 0.7, 0.2)
438
glVertex3f(LocX[ix], LocY[ix], LocZ[ix])
439
TestPOINTS.append(Vector([LocX[ix], LocY[ix], LocZ[ix], 1]))
440
TestFRAME.append(int(frame))
443
#this list contains info about where to check if we click over a "vertex" in 3D view
444
TestLIST.append((ob, TestPOINTS, TestFRAME))
450
for ob in object_dict.itervalues():
454
#Fonction to handle trajectories
458
#use TestLIST and matview defined by Trace_Traj
459
global TestLIST, matview
460
for screen in Blender.Window.GetScreenInfo(Blender.Window.Types.VIEW3D):
461
if screen['id']==Blender.Window.GetAreaID():
462
x0, y0, x1, y1= screen['vertices']
465
#Projection of GL matrix in 3D view
467
glMatrixMode(GL_PROJECTION)
470
#Global coordinates' matrix
471
glOrtho(x0, x1, y0, y1, -1, 0)
472
glMatrixMode(GL_MODELVIEW)
474
#Test mouse clics and other events
477
if Blender.Window.QTest():
478
evt, val= Blender.Window.QRead()
479
if (evt==LEFTMOUSE or evt==RIGHTMOUSE) and not Blender.Window.EditMode():
480
mouse_co=Blender.Window.GetMouseCoords()
481
#if click on trajectory "vertexs"...
482
for ob, TestPOINTS, TestFRAME in TestLIST: # ob is now used, line 552 to know what object it had to select
483
for k, Vect in enumerate(TestPOINTS):
486
pt=[(proj[0]/proj[3])*(x1-x0)/2+(x1+x0)/2, (proj[1]/proj[3])*(y1-y0)/2+(y1+y0)/2]
488
if mouse_co[0]<pt[0]+4 and mouse_co[0]>pt[0]-4 and mouse_co[1]>pt[1]-4 and mouse_co[1]<pt[1]+4:
490
#remember current selected object
491
object_names=[obj.name for obj in Blender.Object.GetSelected()]
492
#this script allow to simulate a GKey, but I have to write a script
493
#another way would made a infinit redraw or don't allow to move object
494
#it auto unlink and delete itself
497
from Blender import Draw, Window
498
from Blender.Window import *
499
from Blender.Draw import *
501
from Blender.Mathutils import Vector
503
# The following code is a bit of a hack, it allows clicking on the points and dragging directly
504
#It simulate user press GKey
505
#It also set the cursor position at center (because user have previously clic on area and moved the cursor):
506
#And I can't get previous cursor position : redraw appear after it has been moved
507
#If there is no better way you can remove this comments
510
#SetKeyQualifiers(1) #FIXED : the bug in older versions seems to have been fixed
512
QAdd(f, Blender.Draw.GKEY, 1, 0)
517
while Blender.Window.QTest():
518
ev=Blender.Window.QRead()[0]
519
if ev not in (4, 5, 18, 112, 213): #all event needed to move object
520
#SetKeyQualifiers(1) #FIXED too, same reason that above
522
SetKeyQualifiers(Blender.Window.GetKeyQualifiers())
526
if ev in (RIGHTMOUSE, LEFTMOUSE, ESCKEY):
528
Blender.Set('curframe',\"\"\"+str(Blender.Get('curframe'))+\"\"\")
529
Blender.Object.GetSelected()[0].sel= False
530
for obname in \"\"\"+str(object_names)+\"\"\":
531
ob=Blender.Object.Get(obname)
534
scripting=Blender.Text.Get('Edit_Trajectory')
536
Blender.Text.unlink(scripting)
539
#FIXED Edit_Trajectory was longer : all SetKeyQualifiers removed
540
scene=Blender.Scene.GetCurrent()
542
scripting=Blender.Text.Get('Edit_Trajectory')
545
scripting=Blender.Text.New('Edit_Trajectory')
547
scripting.write(script)
548
#script= scripting #FIXED seems not needed anymore
550
#Go to frame that correspond to selected "vertex"
551
Blender.Set('curframe', TestFRAME[k])
553
#un select all objects
554
for _ob in Blender.Object.GetSelected():
556
#FIXED TestLIST[j][0].sel=0, but no j. So ob.sel and above variable changed in obj
558
Blender.Run('Edit_Trajectory')
562
Blender.Set('curframe', TestFRAME[k])
565
#retrieve a normal matrix
567
glMatrixMode(GL_PROJECTION)
569
glMatrixMode(GL_MODELVIEW)
573
text_remove('Trajectory')
574
write_script('Trajectory', DrawPart)
576
Blender.UpdateMenus()
578
link_script('Trajectory', 'Redraw')
580
text_remove('Trajectory')
581
print("---End of Trajectory_"+str(__version__)+".py---\n--- Thanks for use ---")
b'\\ No newline at end of file'
155
handle_mode= handle_mode.val
156
selection_mode= selection_mode.val
162
#put names of selected objects in objects_select if option choosen by user
163
if selection_mode==1:
164
objects_select= [ob.name for ob in scene.objects.context]
171
DrawPart="#SPACEHANDLER.VIEW3D.DRAW\n"
174
except:DrawPart="#BadlyMade"
177
#Here is the script to write in Blender and to link, options are also written now
178
DrawPart=DrawPart+"#"+str(__version__)+"""
179
#This script is a part of Trajectory.py and have to be linked to the scene in Redraw if not in HANDLER mode.
180
#Author : 3R - Regis Montoya
181
#It's better to use the Trajectory_"version_number".py
182
#You can modify the two following value to change the path settings
183
future="""+str(future)+"""
184
past="""+str(past)+"""
185
object_init_names="""+str(objects_select)+"""
189
from Blender import BGL, Draw, Ipo
190
from Blender.BGL import *
191
from Blender.Draw import *
194
from Blender.Mathutils import Vector
197
frameC=Blender.Get('curframe')
198
scene = Blender.Scene.GetCurrent()
199
render_context=scene.getRenderingContext()
200
#ajust number of frames with NewMap and OldMapvalue values
201
k=1.00*render_context.oldMapValue()/render_context.newMapValue()
203
tr=-1*int(log(k*0.1, 10))
205
tr=-1*int(log(k, 10))
206
#The real and integer frame to compare to ipos keys frames
207
frameCtr=round(frameC*k, tr)
209
frameC=int(round(frameC*k, 0))
212
#List objects that we have to show trajectory in $objects
213
# In this case, using a dict for unique objects is the fastest way.
214
object_dict= dict([(ob.name, ob) for ob in scene.objects.context])
215
for obname in object_init_names:
216
if not object_dict.has_key(obname):
217
try: # Object may be removed.
218
object_dict[obname]= Blender.Object.Get(obname)
220
pass # object was removed.
222
#This fonction give the resulting matrix of all parents at a given frame
223
#parent_list is the list of all parents [object, matrix, locX_ipo, locY, Z, rotX, Y, Z, sizeX, Y, Z] of current object
224
def matrixForTraj(frame, parent_list):
225
DecMatC=Blender.Mathutils.Matrix([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1])
227
for parent_data in parent_list:
228
parent_ob= parent_data[0]
230
try: X= parent_data[5][frame]*pi/18
231
except: X= parent_ob.RotX
232
try: Y= parent_data[6][frame]*pi/18
233
except: Y= parent_ob.RotY
234
try: Z= parent_data[7][frame]*pi/18
235
except: Z= parent_ob.RotZ
236
try: LX= parent_data[2][frame]
237
except: LX= parent_ob.LocX
238
try: LY= parent_data[3][frame]
239
except: LY= parent_ob.LocY
240
try: LZ= parent_data[4][frame]
241
except: LZ= parent_ob.LocZ
242
try: SX= parent_data[8][frame]
243
except: SX= parent_ob.SizeX
244
try: SY= parent_data[9][frame]
245
except: SY= parent_ob.SizeY
246
try: SZ= parent_data[10][frame]
247
except: SZ= parent_ob.SizeZ
249
NMat=Blender.Mathutils.Matrix([cos(Y)*cos(Z)*SX,SX*cos(Y)*sin(Z),-SX*sin(Y),0],
250
[(-cos(X)*sin(Z)+sin(Y)*sin(X)*cos(Z))*SY,(sin(X)*sin(Y)*sin(Z)+cos(X)*cos(Z))*SY,sin(X)*cos(Y)*SY,0],
251
[(cos(X)*sin(Y)*cos(Z)+sin(X)*sin(Z))*SZ,(cos(X)*sin(Y)*sin(Z)-sin(X)*cos(Z))*SZ,SZ*cos(X)*cos(Y),0],
253
DecMatC=DecMatC*parent_data[1]*NMat
258
matview=Blender.Window.GetPerspMatrix()
260
#Fonction to draw trajectories
264
global TestLIST, matview
265
#we draw trajectories for all objects in list
270
#List with trajectories' vertexs
275
ipoLocX=contextIpo[Ipo.OB_LOCX]
276
ipoLocY=contextIpo[Ipo.OB_LOCY]
277
ipoLocZ=contextIpo[Ipo.OB_LOCZ]
278
ipoTime=contextIpo[Ipo.OB_TIME]
279
else: # only do if there is no IPO (if no ipo curves : return None object and don't go in this except)
280
ipoLocX= ipoLocY= ipoLocZ= ipoTime= None
285
#Get all parents of ob
291
#Get parents's infos :
292
#list of [name, initial matrix at make parent, ipo in X,Y,Z,rotX,rotY,rotZ,sizeX,Y,Z]
294
Init_Mat=Blender.Mathutils.Matrix(child.getMatrix('worldspace')) #must be done like it (it isn't a matrix otherwise)
296
Init_Mat=Init_Mat*child.getMatrix('localspace')
297
Init_Mat=parent.getMatrix()*Init_Mat
300
contextIpo= parent.ipo # None or IPO
302
ipo_Parent_LocX=contextIpo[Ipo.OB_LOCX]
303
ipo_Parent_LocY=contextIpo[Ipo.OB_LOCY]
304
ipo_Parent_LocZ=contextIpo[Ipo.OB_LOCZ]
305
ipo_Parent_RotX=contextIpo[Ipo.OB_ROTX]
306
ipo_Parent_RotY=contextIpo[Ipo.OB_ROTY]
307
ipo_Parent_RotZ=contextIpo[Ipo.OB_ROTZ]
308
ipo_Parent_SizeX=contextIpo[Ipo.OB_SIZEX]
309
ipo_Parent_SizeY=contextIpo[Ipo.OB_SIZEY]
310
ipo_Parent_SizeZ=contextIpo[Ipo.OB_SIZEZ]
312
ipo_Parent_LocX=ipo_Parent_LocY=ipo_Parent_LocZ=\
313
ipo_Parent_RotX=ipo_Parent_RotY=ipo_Parent_RotZ=\
314
ipo_Parent_SizeX=ipo_Parent_SizeY=ipo_Parent_SizeZ= None
316
parent_list.append([parent, Init_Mat, ipo_Parent_LocX, ipo_Parent_LocY, ipo_Parent_LocZ, ipo_Parent_RotX, ipo_Parent_RotY, ipo_Parent_RotZ, ipo_Parent_SizeX, ipo_Parent_SizeY, ipo_Parent_SizeZ])
321
#security : if one of parents object are a path>>follow : trajectory don't work properly so it have to draw nothing
322
for parent in parent_list:
323
if parent[0].type == 'Curve':
324
if parent[0].data.flag & 1<<4: # Follow path, 4th bit
327
#ob >> re-assign obj and not parent
332
if ipoLocX: LXC= ipoLocX[frameC]
334
if ipoLocY: LYC= ipoLocY[frameC]
336
if ipoLocZ: LZC= ipoLocZ[frameC]
339
vect= Vector([ob.LocX, ob.LocY, ob.LocZ, 1])
342
#If trajectory is being modified and we are at a frame where a ipo key already exist
343
if round(ob.LocX, 5)!=round(LXC, 5):
344
for bez in ipoLocX.bezierPoints:
345
if round(bez.pt[0], tr)==frameCtr:
346
bez.pt = [frameCr, vect[0]]
348
if round(ob.LocY, 5)!=round(LYC, 5):
349
for bez in ipoLocY.bezierPoints:
350
if round(bez.pt[0], tr)==frameCtr:
351
bez.pt = [frameCr, vect[1]]
353
if round(ob.LocZ, 5)!=round(LZC, 5):
354
for bez in ipoLocZ.bezierPoints:
355
if round(bez.pt[0], tr)==frameCtr:
356
bez.pt = [frameCr, vect[2]]
359
#change trajectory color if at an ipoKey
362
if ipoLocX: # FIXED like others it was just in case ipoLocX==None
363
for bez in ipoLocX.bezierPoints:
364
bezier_Coord=round(bez.pt[0], tr)
365
if bezier_Coord not in VertexFrame:
366
VertexFrame.append(bezier_Coord)
367
if bezier_Coord==frameCtr:
368
color=[1, color[1]-0.3]
370
for bez in ipoLocY.bezierPoints:
371
bezier_Coord=round(bez.pt[0], tr)
372
if bezier_Coord not in VertexFrame:
373
VertexFrame.append(bezier_Coord)
374
if round(bez.pt[0], tr)==frameCtr:
375
color=[1, color[1]-0.3]
377
for bez in ipoLocZ.bezierPoints:
378
bezier_Coord=round(bez.pt[0], tr)
379
if bezier_Coord not in VertexFrame:
380
VertexFrame.append(bezier_Coord)
381
if round(bez.pt[0], tr)==frameCtr:
382
color=[1, color[1]-0.3]
385
#put in LocX, LocY and LocZ all points of trajectory
386
for frame in xrange(frameC-past, frameC+future):
387
DecMat=matrixForTraj(frame, parent_list)
389
if ipoLocX: LX= ipoLocX[frame]
391
if ipoLocY: LY= ipoLocY[frame]
393
if ipoLocZ: LZ= ipoLocZ[frame]
396
vect=Vector(LX, LY, LZ)*DecMat
402
#draw part : get current view
403
MatPreBuff= [matview[i][j] for i in xrange(4) for j in xrange(4)]
405
MatBuff=BGL.Buffer(GL_FLOAT, 16, MatPreBuff)
408
glMatrixMode(GL_PROJECTION)
410
glLoadMatrixf(MatBuff)
412
#draw trajectory line
413
glLineWidth("""+str(width)+""")
415
glBegin(GL_LINE_STRIP)
416
for i in xrange(len(LocX)):
417
glColor3f((i+1)*1.00/len(LocX)*color[0], 0, (i+1)*1.00/len(LocX)*color[1])
418
glVertex3f(LocX[i], LocY[i], LocZ[i])
422
#draw trajectory's "vertexs"
423
if not Blender.Window.EditMode():
429
for frame in VertexFrame:
430
ix=int(frame)-frameC+past
431
if ix>=0 and ix<len(LocX):
432
glColor3f(1, 0.7, 0.2)
433
glVertex3f(LocX[ix], LocY[ix], LocZ[ix])
434
TestPOINTS.append(Vector([LocX[ix], LocY[ix], LocZ[ix], 1]))
435
TestFRAME.append(int(frame))
438
#this list contains info about where to check if we click over a "vertex" in 3D view
439
TestLIST.append((ob, TestPOINTS, TestFRAME))
445
for ob in object_dict.itervalues():
449
#Fonction to handle trajectories
453
#use TestLIST and matview defined by Trace_Traj
454
global TestLIST, matview
455
for screen in Blender.Window.GetScreenInfo(Blender.Window.Types.VIEW3D):
456
if screen['id']==Blender.Window.GetAreaID():
457
x0, y0, x1, y1= screen['vertices']
460
#Projection of GL matrix in 3D view
462
glMatrixMode(GL_PROJECTION)
465
#Global coordinates' matrix
466
glOrtho(x0, x1, y0, y1, -1, 0)
467
glMatrixMode(GL_MODELVIEW)
469
#Test mouse clics and other events
472
if Blender.Window.QTest():
473
evt, val= Blender.Window.QRead()
474
if (evt==LEFTMOUSE or evt==RIGHTMOUSE) and not Blender.Window.EditMode():
475
mouse_co=Blender.Window.GetMouseCoords()
476
#if click on trajectory "vertexs"...
477
for ob, TestPOINTS, TestFRAME in TestLIST: # ob is now used, line 552 to know what object it had to select
478
for k, Vect in enumerate(TestPOINTS):
481
pt=[(proj[0]/proj[3])*(x1-x0)/2+(x1+x0)/2, (proj[1]/proj[3])*(y1-y0)/2+(y1+y0)/2]
483
if mouse_co[0]<pt[0]+4 and mouse_co[0]>pt[0]-4 and mouse_co[1]>pt[1]-4 and mouse_co[1]<pt[1]+4:
485
#remember current selected object
486
object_names=[obj.name for obj in Blender.Object.GetSelected()]
487
#this script allow to simulate a GKey, but I have to write a script
488
#another way would made a infinit redraw or don't allow to move object
489
#it auto unlink and delete itself
492
from Blender import Draw, Window
493
from Blender.Window import *
494
from Blender.Draw import *
496
from Blender.Mathutils import Vector
498
# The following code is a bit of a hack, it allows clicking on the points and dragging directly
499
#It simulate user press GKey
500
#It also set the cursor position at center (because user have previously clic on area and moved the cursor):
501
#And I can't get previous cursor position : redraw appear after it has been moved
502
#If there is no better way you can remove this comments
505
#SetKeyQualifiers(1) #FIXED : the bug in older versions seems to have been fixed
507
QAdd(f, Blender.Draw.GKEY, 1, 0)
512
while Blender.Window.QTest():
513
ev=Blender.Window.QRead()[0]
514
if ev not in (4, 5, 18, 112, 213): #all event needed to move object
515
#SetKeyQualifiers(1) #FIXED too, same reason that above
517
SetKeyQualifiers(Blender.Window.GetKeyQualifiers())
521
if ev in (RIGHTMOUSE, LEFTMOUSE, ESCKEY):
523
Blender.Set('curframe',\"\"\"+str(Blender.Get('curframe'))+\"\"\")
524
Blender.Object.GetSelected()[0].sel= False
525
for obname in \"\"\"+str(object_names)+\"\"\":
526
ob=Blender.Object.Get(obname)
529
scripting=Blender.Text.Get('Edit_Trajectory')
531
Blender.Text.unlink(scripting)
534
#FIXED Edit_Trajectory was longer : all SetKeyQualifiers removed
535
scene=Blender.Scene.GetCurrent()
537
scripting=Blender.Text.Get('Edit_Trajectory')
540
scripting=Blender.Text.New('Edit_Trajectory')
542
scripting.write(script)
543
#script= scripting #FIXED seems not needed anymore
545
#Go to frame that correspond to selected "vertex"
546
Blender.Set('curframe', TestFRAME[k])
548
scene.objects.selected = [] #un select all objects
550
#FIXED TestLIST[j][0].sel=0, but no j. So ob.sel and above variable changed in obj
552
Blender.Run('Edit_Trajectory')
556
Blender.Set('curframe', TestFRAME[k])
559
#retrieve a normal matrix
561
glMatrixMode(GL_PROJECTION)
563
glMatrixMode(GL_MODELVIEW)
567
text_remove('Trajectory')
568
write_script('Trajectory', DrawPart)
570
Blender.UpdateMenus()
572
link_script('Trajectory', 'Redraw')
574
text_remove('Trajectory')
575
print("---End of Trajectory_"+str(__version__)+".py---\n--- Thanks for use ---")