List comprehension FAIL ......anyone tell me why ?

Discussions concerning programming of SOFTIMAGE©
Post Reply
Manticor
Posts: 160
Joined: 09 Jun 2011, 22:41

List comprehension FAIL ......anyone tell me why ?

Post by Manticor » 15 May 2014, 15:49

Hi ,Im starting to use list comprehensions more in Python but for some reason I can't get this to work.

This code works

Code: Select all

xsi= Application
lm = xsi.LogMessage
oSel = xsi.Selection
oRoot = xsi.ActiveSceneRoot
cluster = oSel(0).ActivePrimitive.Geometry.Clusters

for i in cluster:
	if i.type == 'sample':
		lm(i)
but this list comprehension version of the loop doesn't .... (Im assuming Im doing it right )

Code: Select all

i = [ i for i in cluster if i.type == 'sample']
It returns this ------ > [<COMObject <unknown>>]

Can any Python gurus's here tell me why?

Im using XSI version 7.01

Manticor
Posts: 160
Joined: 09 Jun 2011, 22:41

Re: List comprehension FAIL ......anyone tell me why ?

Post by Manticor » 15 May 2014, 15:58

Sorry I figured it out.
its because it was passed into a list and I should have checked the properties of the item in the list not the list itself.

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: List comprehension FAIL ......anyone tell me why ?

Post by csaez » 15 May 2014, 21:32

Hi there,
This is a stupid suggestion but here it goes anyway: i as a variable name is usually associated to an index (1, 2, 3...), so if you're looping through objects (properties in this case) try to use a descriptive name, like p or prop or whatever suit your needs.
List comprehensions are short enough and this should not be a problem at all, but naming things properly will save you many headaches in the future.

Cheers!

Manticor
Posts: 160
Joined: 09 Jun 2011, 22:41

Re: List comprehension FAIL ......anyone tell me why ?

Post by Manticor » 17 May 2014, 12:07

Ill bear that in mind for the future .I do this when Im iterating over a list as the container list describes what Im iterating over .But I do see your point .Thanks for the advice

Btw what software do you use for scripting ? Im using sublime but it doesn't have autocomplete for available methods for softimage keywords.And this is something that would save me quite a bit of time trawling through the sdk help files to see the available methods and properties.
I looked at degugging in pywin the other day and that has the softimage autocomplete but its black text on white which I dont like that for coding.I prefer light grey text on Dark grey.

Do you have any suggestions ?

User avatar
csaez
Posts: 253
Joined: 09 Jul 2012, 15:31
Skype: csaezmargotta
Location: Sydney, Australia
Contact:

Re: List comprehension FAIL ......anyone tell me why ?

Post by csaez » 17 May 2014, 21:38

I'm using sublime too.
Softimage SDK is all about dynamic dispatching and setup an autocomplete is not that straight forward. It's possible to inspect the sdk through win32com API but I've never tried it by myself (there are a couple of threads on the mailing list about this).

I use a couple of extra packages for python development (available through package control).
- SublimeCodeIntel to autocomplete python stuff (including my own code).
- SublimeLinter as a linter.
- Python PEP8 Autoformat to ensure PEP8 formatting on my code.

Cheers!

Manticor
Posts: 160
Joined: 09 Jun 2011, 22:41

Re: List comprehension FAIL ......anyone tell me why ?

Post by Manticor » 19 May 2014, 06:10

Nice one.cheers mate

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests