Runs from the script editor - out of range from hotkey/bar

Discussions concerning programming of SOFTIMAGE©
Post Reply
bergj
Posts: 36
Joined: 06 May 2012, 05:58

Runs from the script editor - out of range from hotkey/bar

Post by bergj » 07 Jun 2012, 19:15

I have a small script that will align selected verts to the last selected vert in X/Y/Z. I want to map it to f5/f6/f7.

If I run it from the script editor it works perfect. But, if I add it to the custom toolbar/hotkey I get an error message of

line 38
lastComponent = selectionLAST[0]
IndexError: list index out of range.


Code: Select all

###Match Points X/Y/Z
##### Y
#Only works on points

if Application.Selection(0).type == "edgeSubComponent":
	Application.LogMessage("Align points works with points ONLY")
	
if Application.Selection(0).type == "polySubComponent":
	Application.LogMessage("Align points works with points ONLY")
################

if Application.Selection(0).type == "pntSubComponent":
	
	#	
	selectionALL = []
	selectionFIRST = []
	selectionLAST = []	

	allTemp = Application.Selection(0).SubComponent.ComponentCollection

	for i in allTemp:
		selectionALL.append(i.Index)

	#
	Application.Undo("")	
	#
	firstTemp = Application.Selection(0).SubComponent.ComponentCollection

	for i in firstTemp:
		selectionFIRST.append(i.Index)

	for i in selectionALL:
		if i not in selectionFIRST:
			selectionLAST.append(i)
			
	lastComponent = selectionLAST[0] #######################################

	##go to item mode and get the name
	Application.ActivateObjectSelTool("")
	rootObject = Application.Selection(0)

	#switch back to vert mode
	Application.ActivateVertexSelTool("")	

	positionOfLastSelectedComponentY = Application.ActiveProject.ActiveScene.Root.FindChild(rootObject).ActivePrimitive.Geometry.Points(lastComponent).Position.Y

	####Works to get them to the same position
	moveALL = []

	for i in firstTemp:
		moveALL.append(i.SubComponent)

	for i in range(len(moveALL)):
		number = i

		PTtoMove = moveALL[number]
		Application.Translate(PTtoMove, 0, positionOfLastSelectedComponentY, 0, "siAbsolute", "siPivot", "siObj", "siY", "", "", "", "", "", "", "", "", "", 0, "")	
		
	#reselect starting verts	
	Application.SelectGeometryComponents(firstTemp)	

Thank ye. :ymhug:

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Runs from the script editor - out of range from hotkey/bar

Post by xsisupport » 07 Jun 2012, 19:32

That doesn't work in the script editor either.
Your script has an Undo that undoes the selection of points, so the next line of code causes an error.

Code: Select all

# I do this:
Application.SelectGeometryComponents("sphere.pnt[1,20-22,27-29,33-36,40-43,47-50]")

# Then run the script in the script editor...
Application.Undo("")    # Select Geometry Components
# ERROR : Traceback (most recent call last):
#   File "<Script Block >", line 27, in <module>
#     firstTemp = Application.Selection(0).SubComponent.ComponentCollection
# AttributeError: 'NoneType' object has no attribute 'SubComponent'
#  - [line 27]

// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

bergj
Posts: 36
Joined: 06 May 2012, 05:58

Re: Runs from the script editor - out of range from hotkey/bar

Post by bergj » 07 Jun 2012, 22:13

xsisupport wrote:That doesn't work in the script editor either.
Your script has an Undo that undoes the selection of points, so the next line of code causes an error.

Code: Select all

# I do this:
Application.SelectGeometryComponents("sphere.pnt[1,20-22,27-29,33-36,40-43,47-50]")

# Then run the script in the script editor...
Application.Undo("")    # Select Geometry Components
# ERROR : Traceback (most recent call last):
#   File "<Script Block >", line 27, in <module>
#     firstTemp = Application.Selection(0).SubComponent.ComponentCollection
# AttributeError: 'NoneType' object has no attribute 'SubComponent'
#  - [line 27]


No, while i hate to argue - you are wrong. I spent hours writing this and testing it and it does work as written in the script editor. I undo the selection back 1 point so I can find out what the last selected is by comparing two lists, selectionALL and selectionFIRST to get the single vert in selectionLAST. Thats the only way it can work. And it does work.

Here is a video showing the exact steps to reproduce the problem. Thanks in advance.


User avatar
rray
Moderator
Posts: 1775
Joined: 26 Sep 2009, 15:51
Location: Bonn, Germany
Contact:

Re: Runs from the script editor - out of range from hotkey/bar

Post by rray » 07 Jun 2012, 22:48

You don't need undo to get the last selected point index. The point indices are stored in the CompentCollection in the order of selection, so doing

Code: Select all

allTemp = Application.Selection(0).SubComponent.ComponentCollection
Application.LogMessage(allTemp[allTemp.Count-1].Index)
should give you the index of the last point that has been selected.

Your script can be shortened a lot if you use the scale command
This example is for "y", using 1,0,1 as scale values. For "x" just use 0,1,1 and for "z" 1,1,0

Code: Select all

if Application.Selection(0).type == "pntSubComponent":
	pts = Application.Selection(0).SubComponent.ComponentCollection
	pt_last_pos = pts[pts.Count-1].Position
	Application.Scale("", 1, 0, 1, "siRelative", "siGlobal", "siObj", "siXYZ", "", "", "", True, pt_last_pos.X, pt_last_pos.Y, pt_last_pos.Z, 0, "")
else:
	Application.LogMessage("Align points works with points ONLY")
softimage resources section updated Jan 5th 2024

User avatar
rray
Moderator
Posts: 1775
Joined: 26 Sep 2009, 15:51
Location: Bonn, Germany
Contact:

Re: Runs from the script editor - out of range from hotkey/bar

Post by rray » 07 Jun 2012, 23:10

Btw there seems to be a bug with accessing the component selection:

Typing

Code: Select all

Application.LogMessage(Application.Selection(0).SubComponent.ComponentCollection.Count)
in the script editor and running it logs "2" when 2 points are selected. If you select another point, running the script logs "2" again instead of "3".

The ComponentCollection is being refreshed though when the selection mode is changed. So for the above script to work properly, you should do this:

Code: Select all

if Application.Selection(0).type == "pntSubComponent":
	Application.ActivateObjectSelTool("")
	Application.SetSelFilter("Vertex")
	pts = Application.Selection(0).SubComponent.ComponentCollection
	pt_last_pos = pts[pts.Count-1].Position
	Application.Scale("", 1, 0, 1, "siRelative", "siGlobal", "siObj", "siXYZ", "", "", "", True, pt_last_pos.X, pt_last_pos.Y, pt_last_pos.Z, 0, "")
else:
	Application.LogMessage("Align points works with points ONLY") 
softimage resources section updated Jan 5th 2024

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests