Why wont this python script loop?

Discussions concerning programming of SOFTIMAGE©
Pooby
Posts: 493
Joined: 27 Aug 2010, 22:25

Why wont this python script loop?

Post by Pooby » 17 Feb 2016, 17:06

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

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

Re: Why wont this python script loop?

Post by rray » 17 Feb 2016, 17:55

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.
"as a maya discussion grows longer, the probability of a suggestion involving deleting the preferences approaches one"

softimage resources section (formerly known as rray.de/xsi) updated May 30 2018

EricTRocks
Moderator
Posts: 754
Joined: 25 Nov 2009, 01:41

Re: Why wont this python script loop?

Post by EricTRocks » 17 Feb 2016, 22:16

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

Eric Thivierge
Lead Kraken Developer, Fabric Engine
http://fabric-engine.github.io/Kraken

Pooby
Posts: 493
Joined: 27 Aug 2010, 22:25

Re: Why wont this python script loop?

Post by Pooby » 18 Feb 2016, 09:22

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.

User avatar
myara
Posts: 357
Joined: 28 Sep 2011, 10:33

Re: Why wont this python script loop?

Post by myara » 18 Feb 2016, 10:18

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.
M.Yara
Character Modeler | Softimage Generalist (sort of)

Pooby
Posts: 493
Joined: 27 Aug 2010, 22:25

Re: Why wont this python script loop?

Post by Pooby » 19 Feb 2016, 13:50

thanks Myara.. that worked a treat.

EricTRocks
Moderator
Posts: 754
Joined: 25 Nov 2009, 01:41

Re: Why wont this python script loop?

Post by EricTRocks » 19 Feb 2016, 14:56

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.
Eric Thivierge
Lead Kraken Developer, Fabric Engine
http://fabric-engine.github.io/Kraken