split string with python

Discussions concerning programming of SOFTIMAGE©
caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

split string with python

Post by caledonian_tartan » 13 Jan 2013, 12:09

Hello everyone

how can i split a string into a list?

i have a string list and get this log:

Code: Select all

# Pass;Pass1;Pass2;Pass3;Pass4
What can i do to get this:

Code: Select all

# Pass
# Pass1
# Pass2
# Pass3
# Pass4
preferably in python

thanks in advance
SI 2015 @ WIN7-64

User avatar
rray
Moderator
Posts: 1810
Joined: 26 Sep 2009, 13:51
Location: Bonn, Germany

Re: split string with python

Post by rray » 13 Jan 2013, 12:17

Not sure about python but in JS there's a split command that makes a string array out of a string and another separator string. Isn't there something similar in python?
softimage resources section updated Sep 26th 2026

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

Re: split string with python

Post by xsisupport » 13 Jan 2013, 12:22

Yes, of course there is.
http://docs.python.org/2/library/stdtypes.html#str.split
for p in 'Pass;Pass1;Pass2;Pass3;Pass4'.split(';'): print p
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 13 Jan 2013, 13:44

cool, thanks for your help!

its working great on a simple example like:

Code: Select all

oSelectedPasses = "Pass2;Pass3;Pass4"
for p in oSelectedPasses.split(';'): print p
On the other Hand i use a callback of a selection of a PPG List Box as the string
in this case it was not successfull

Code: Select all

# ERROR : Traceback (most recent call last):
#   File "<Script Block 2>", line 146, in VRay_DR_Render_Render_OnClicked
#     for p in oSelectedPasses.split(';'):
#   File "C:\Autodesk\Softimage 2013\Application\python\Lib\site-packages\win32com\client\dynamic.py", line 500, in __getattr__
#     raise AttributeError, "%s.%s" % (self._username_, attr)
# AttributeError: <unknown>.split
#  - [line 145 in C:\Users\dude\Autodesk\Softimage_2013\Application\Plugins\VRay_DR_Render.py]
# ERROR : Property Page Script Logic Error (Python ActiveX Scripting Engine)
# ERROR :    [143] 		print "Render Selected Passes"
# ERROR :    [144] 		print PPG.PassesListToRender
# ERROR :   >[145] 		oSelectedPasses = PPG.PassesListToRender
# ERROR :    [146] 		for p in oSelectedPasses.split(';'): 
# ERROR :    [147] 			print p
# ERROR :    [148] 		print oSelectedPasses
# ERROR : 	Traceback (most recent call last):
#   File "<Script Block 2>", line 146, in VRay_DR_Render_Render_OnClicked
#     for p in oSelectedPasses.split(';'):
#   File "C:\Autodesk\Softimage 2013\Application\python\Lib\site-packages\win32com\client\dynamic.py", line 500, in __getattr__
#     raise AttributeError, "%s.%s" % (self._username_, attr)
# AttributeError: <unknown>.split
# 
# Render Selected Passes
# Pass2;Pass3;Pass4
SI 2015 @ WIN7-64

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

Re: split string with python

Post by xsisupport » 13 Jan 2013, 14:04

PPG.PassesListToRender is a Parameter object, not a string.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 13 Jan 2013, 14:18

hmm.
can't i split the result of this?


PPG.PassesListToRender prints

# Pass1;Pass2;Pass3

how do i need to treat a Parameter Object?
SI 2015 @ WIN7-64

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

Re: split string with python

Post by xsisupport » 13 Jan 2013, 14:33

You get the parameter value with its Value property.

With print PPG.SomeParam you are taking advantage a shortcut provided by the PPG object: a shortcut to the parameter value eg PPG.SomeParam.Value.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 13 Jan 2013, 14:49

ahyea. it's .value

this works!

thank you steve.

i think i'll need to look at this page again and again.
SI 2015 @ WIN7-64

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 13 Jan 2013, 17:26

i'm sorry, but again, it still confuses me.

how can i pack it back into one oSomething?
the following code does not work in line 4

Code: Select all

oSelectedPasses = PPG.PassesListToRender.Value
for p in oSelectedPasses.split(';'): 
    print p
    oSomething = p

i'd like to

Code: Select all

    print oSomething

# Pass1
# Pass2
# Pass3
so that i can print the values without the for loop...
some sort of a context problem i think
Last edited by caledonian_tartan on 13 Jan 2013, 17:38, edited 1 time in total.
SI 2015 @ WIN7-64

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

Re: split string with python

Post by xsisupport » 13 Jan 2013, 17:37

Why don't you just print PPG.PassesListToRender.Value ?
-or-

Code: Select all

x = "a;b;c;d;e"
print '\n'.join( x.split(';') )
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 13 Jan 2013, 17:52

i finally got it working with your second method. thanks steve!

Code: Select all

oSelectedPasses = PPG.PassesListToRender.Value
oSome = '\n'.join( oSelectedPasses.split(';') )
print oSome
for frame in range( CStartVal , CEndVal + 1):
	Application.RenderPasses( "Passes." + oSome , frame, frame, 1, "" )
SI 2015 @ WIN7-64

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 14 Jan 2013, 09:07

apparently i was distracted and made a mistake.
this is only working if only one pass is selected.
otherwise it logs:

Code: Select all

Application.RenderPasses( "Passes.Pass2/nPass3n/Pass4" , frame, frame, 1, "" )
i eighter need to read the selected items of the box as an array/collection/group

Code: Select all

// VBS
var oListBox = oLayout.AddItem( "MyList", "", siControlListBox );
oListBox.SetAttribute( siUICY, 100 );

// 1D array of key,value pairs (since this is a string, just repeat them)
var aListItems = new Array( 
	"light", "light", 
	"sphere", "sphere", 
	"Camera", "Camera", 
	"Camera_Root", "Camera_Root", 
	"Camera_Interest", "Camera_Interest" 
);
oListBox.UIItems = aListItems;
or convert my splitted string into an an array/collection/group

Code: Select all

no clue...
SI 2015 @ WIN7-64

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

Re: split string with python

Post by xsisupport » 14 Jan 2013, 10:44

You need to pass a comma-separated string of pass names eg

Code: Select all

Application.RenderAllPasses( "Passes.Default_Pass,Passes.Isolate_Matte" )
So you just need to replace the semi-colons with commas.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

caledonian_tartan
Posts: 253
Joined: 17 Feb 2010, 14:13

Re: split string with python

Post by caledonian_tartan » 14 Jan 2013, 12:40

you, Sir, are great! thanks mr. blair.

Code: Select all

oSelectedPasses = "Pass1;Pass2;Pass3"
oSelectedPasses = oSelectedPasses.replace(";",",Passes.")
print oSelectedPasses
for frame in range( 1 , 2 + 1):
   Application.RenderPasses( "Passes." + oSelectedPasses , frame, frame, 1, "" )
works great. tool almost finished.
this tool serves as a workaround to render VRay Animations with Distributed Rendering.
i will release it in the VRay4SI Forum tonight and maybe here if someone is interested.

thanks again for all of your help
Cheers
Jan

VRay_DR_Animation_Render.zip
EDIT.funFact: " i noticed, that if running this sript in an empty scene, it renders much faster on the following Passes than on the first. as it renders frame1, pass1, pass2, pass3, f2, p1, p2, p3... "
i'm not shure why, but rendering like this seems to increase mental ray renderspeed a little.
You do not have the required permissions to view the files attached to this post.
SI 2015 @ WIN7-64