#!BPY

"""
Name: 'Hooks to All Points'
Blender: 245
Group: 'Object'
Tooltip: 'Add a hook to each point of geometry'
"""

__author__ = "Joshua Leung (aligorith)"
__url__ = ("blender", "blenderartist")
__version__ = "1.0 2008-Mar-06"

__doc__ = """\
This script demonstrates a quick hook-modifier assigning hack
It assigns a hook to every control point of a curve.

Only for use in SVN-Trunk versions (i.e.  2.45.15 and above)
"""

# ***** BEGIN GPL LICENSE BLOCK *****
#
# Script copyright (C) Joshua Leung 2008
#
# 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 *****
# --------------------------------------------------------------------------

from Blender import *

# checks if object is suitable for action
def main_func (ob):
	# error checking
	if (ob == None):	
		return;
	
	# get modifiers stack of object
	mods = ob.modifiers;
	
	# add hook for every point (only for curves atm)
	if (ob.type == 'Curve'):
		curvedata = ob.data;
		
		# exit editmode before altering anything
		if (Window.EditMode(-1)):
			Window.EditMode(0, '', 0);
			openEditMode= True;
		else:
			openEditMode= False;
		
		# loop over the separate curves which belong to the curve object 
		for cu in curvedata:
			# loop over all points, deselecting all (to rule out errors, but is slower)
			for point in cu:
				point.selects = [0, 0, 0];
			
			# loop over all points, adding hooks for them
			for point in cu:
				# select this point only
				point.selects= [1, 1, 1];
				
				# now enter editmode to add hook
				Window.EditMode(1, '', 0);
				
				# add a new hook modifier assigned to the active object
				mods.ZanQdoHack();
				
				# (re)select the object, as the previous operation deselects objects
				ob.select(True);
				
				# exit editmode before doing anything to points again
				Window.EditMode(0, '', 0);
				
				# deselect this point now
				point.selects = [0, 0, 0];
		
		# add undo for this operation
		SaveUndoState("Batch Curve Add Hooks");
		
		# be nice to user, and go back to editmode if we started there
		if (openEditMode):
			Window.EditMode(1, '', 0);
	else:
		Draw.PupMenu("Error%t|This script only works for Curves");
	
# main startup stuff for script
if __name__ == '__main__':
	# get active object and active scene too
	scn= Scene.GetCurrent();
	ob= (scn.objects.active if scn else None);
	
	if (ob):
		main_func(ob);
	else:
		Draw.PupMenu("Error%t|Select an object first");
	