Python Scripting in Softimage XSI: Selection

I was originally planning to post purely about the incredibly useful simple task of making a loop to do repetitive tasks for you. You don’t need to be a TD for this to make things easier day to day, modelling, texturing, lighting, scene organising etc. You could use this in any situation.
ANYWAY as I wrote it I realised I was getting deep into some fundamentals whilst I was explaining how to access the object selection. So For this post I’ll just stick to selection and then advance that in the loop later.

    • Using this command, you can access the objects that are currently selected:

      This is a collection, so if you were to ‘print’ that command, you wouldn’t get great feedback about what it contains.
      In the following code examples, the lines that begin with # hashes show the results of the previous line of code when they are run.

      Note: if you ‘comment’ out a line in a script with the # it will be ignored when run – thus you can use it for commenting your code)

      See! Not so helpful. But as a collection, you can iterate through it using a ‘for loop’. Good old loop.

      Here there are a couple of important things to note in the loop.
      The indent is used by python instead of {} or things like that in other code. So after the loop begins, the next line that is INSIDE that loop, is in the indent, when lines stop being indented, the loop is over – it should be clear to read.
      “oObject” is a variable being declared at this point to be used in the loop. So you can name this whatever you like it is not in relation to anything previously (this is something that confused me to begin with so I’ll try and make that clear)
      I prefix the variables with ‘o’ to make it clear what they are, I picked this up from somewhere..

      So that’s the way to access each object in a loop. I’ll write up why that’s useful in the next post about loops specifically.

    • If you want to specifically pick the first, last, or any other object by index you can do that too.
      Note here I will create variables and then print whatever they contain, instead of printing the full command, this is a main principle while scripting, for tidiness, clarity of reading, organising, swapping data in and out.. And other great mysterious reasons I don’t understand.

      So that’s getting the first object (or ONLY object if there’s only one selected)
      Arrays/indices always start from 0 in scripting, so the FIRST index is always 0. Keeping that in mind, how to get the last object…

      To do that you need to get the length of the selection – the number of objects selected.
      For instance if you have 4 selected, the indices would be 0,1,2,3. So after getting the length (4) you need to subtract 1 to get the last index in the list. To get the length, use this command:

      Then you can use it like so, subtracting 1:

I guess that is about that for accessing the selection! I’ll write about what to do with the selection with loop later.