Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_timeofs_follow_act.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 83863da7d8ffdec410b639cb2bb0ea6485b277fc (plain)
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
#!BPY
"""
Name: 'TimeOffset follow Active'
Blender: 245
Group: 'Object'
Tooltip: 'ActObs animated loc sets TimeOffset on other objects at closest frame'
"""
__author__= "Campbell Barton"
__url__= ["blender.org", "blenderartists.org"]
__version__= "1.0"

__bpydoc__= """
"""

# --------------------------------------------------------------------------
# TimeOffset follow Active v1.0 by Campbell Barton (AKA Ideasman42)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

import Blender
from Blender import Image, sys, Draw, Window, Scene, Group
import bpy
import BPyMessages


def main():

	sce = Scene.GetCurrent()

	ob_act = sce.objects.active
	 
	if not ob_act:
		Draw.PupMenu("Error%t|no active object")
		return
	
	objects = list(sce.objects.context)
	
	try:	objects.remove(ob_act)
	except: pass
	
	if not objects:
		Draw.PupMenu("Error%t|no objects selected")
		return
	
	curframe = Blender.Get('curframe')
	
	FRAME_START= Draw.Create( Blender.Get('staframe') )
	FRAME_END= Draw.Create( Blender.Get('endframe') )
		
	# Get USER Options
	pup_block= [\
	('Start:', FRAME_START, 1, 300000, 'Use the active objects position starting at this frame'),\
	('End:', FRAME_END, 1, 300000, 'Use the active objects position starting at this frame'),\
	]
	
	if not Draw.PupBlock('Set timeoffset...', pup_block):
		return
	
	FRAME_START = FRAME_START.val
	FRAME_END = FRAME_END.val
	
	if FRAME_START >= FRAME_END:
		Draw.PupMenu("Error%t|frames are not valid")
	
	
	# Ok - all error checking 
	locls_act = []
	for f in xrange((FRAME_END-FRAME_START)):
		i = FRAME_START+f
		Blender.Set('curframe', i)
		locls_act.append(ob_act.matrixWorld.translationPart())
	
	for ob in objects:
		loc = ob.matrixWorld.translationPart()
		best_frame = -1
		best_dist = 100000000
		for i, loc_act in enumerate(locls_act):
			dist = (loc_act-loc).length
			if dist < best_dist:
				best_dist = dist
				best_frame = i + FRAME_START
		
		ob.timeOffset = float(best_frame)
	
	# Set the original frame
	Blender.Set('curframe', curframe)

if __name__ == '__main__':
	main()