Page 1 of 1

Why wont this python script loop?

Posted: 17 Feb 2016, 17:06
by Pooby
You make some shapes.. select them and its meant to make Objects from them that store the Blendshape in point positions..

So it works but only on the first.. Can anyone see why?

Code: Select all

xsi = Application
lm = LogMessage
from win32com.client import constants as c

sel = xsi.Selection

grab = sel(0)

cls = grab.Parent
par = cls.Parent
mesh = par.Parent
obj = mesh.Parent

lm (sel.count)
lm (grab)
lm (grab.Name)
lm (obj)
lm (mesh)
lm (cls)
lm (par)

for i in sel:
	nam = (i.Name)
	full = (i.FullName)
	lm (nam)
	lm (full)
	dup = xsi.Duplicate(obj, "", 2, 1, 1, 0, 0, 1, 0, 1, "", "", "", "", "", "", "", "", "", "", 0)
	Application.DeleteObj(str(dup(0))+".polymsh.cls.Shape")
	Application.SetValue(str(dup(0))+".Name", nam, "")
	tree = xsi.ApplyOp("ICETree", dup(0), "siNode", "", "", 0)
	Application.AddICECompoundNode("MakeShapeMesh", tree)
	Application.ConnectICENodes(str(tree)+".port1", str(tree)+".MakeShapeMesh.Execute")
	Application.SetValue(str(tree)+".MakeShapeMesh.Reference", str(nam), "")
	Application.SetValue(str(tree)+".MakeShapeMesh.Reference1", str(obj.Name), "")
	xsi.FreezeObj((tree), "", "")
	xsi.DeleteObj(tree)


Mod Edit ET: Added code tags which should be used in all posted code

Re: Why wont this python script loop?

Posted: 17 Feb 2016, 17:55
by rray
The only explanation I can think of is one of the commands in the loop deselects everything, which would cause the loop to exit then.

You could try putting the selection into a list, and then looping throught the list because that list wouldn't be changed by the command.

Re: Why wont this python script loop?

Posted: 17 Feb 2016, 22:16
by EricTRocks
The Duplicate() command sets the selection to the newly created object. You'll have to store your initial selection and iterate over that an not the current selection.

Code: Select all

from win32com.client import Dispatch

initSel = Dispatch("XSI.Collection")
initSel.SetAsText(sel.GetAsText())

# do your stuff here


Re: Why wont this python script loop?

Posted: 18 Feb 2016, 09:22
by Pooby
Hi Eric,

Thanks.

It was my understanding that whatever variable triggered the loop was fixed until the loop ended. Obviously that's wrong. So it's good to know.

Re: Why wont this python script loop?

Posted: 18 Feb 2016, 10:18
by myara
That only happens if you are working with strings or values.
If you assing an object directly to a variable, it will update with the object.
xsi.Selection gives you a collection of objects so it will update the variable too.

Personally I use:

Code: Select all

xsi.GetValue("SelectionList")
It returns a collection of objects, but since it is a value that GetValue gave you, it won't change even if you change your selection.

Re: Why wont this python script loop?

Posted: 19 Feb 2016, 13:50
by Pooby
thanks Myara.. that worked a treat.

Re: Why wont this python script loop?

Posted: 19 Feb 2016, 14:56
by EricTRocks
Just for thoroughness, all of the GetValue() calls are commands. They will log in the script editor and when used in scripts that are processing a lot of elements, will slow down the overall process. Better to use the object model.