Dynamically applying scripts to new selections of obj/clus?

Discussions concerning programming of SOFTIMAGE©
User avatar
Draise
Posts: 813
Joined: 09 Oct 2012, 20:48
Skype: ondraise
Location: Colombia

Dynamically applying scripts to new selections of obj/clus?

Post by Draise » 06 Jan 2016, 00:44

I have recently started to script in SI with Jscript (it's what I know more or less) - to automate some manual work.

It has saved me a lot of time thus far.

Now.. I'd like to save it even more, and learn how to apply a string of commands to dynamic selections.

Say, I have a number of objects with said names on clusters, and I want to apply unique corresponding shaders.

I import a new object with a similar set of cluster naming conventions, but with a different cluster set, and then apply said unique corresponding shaders.

I would want to run the script to be able to recursively apply the materials to different clusters.

Here is what I have used, where the "M05_sideback-C4" would be the "tag" in the cluster selections, and then said tag would assign the corresponding material "tag".

Code: Select all

SelectObj("CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_001_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_002_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_003_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_004_M05_sideback-C4_0000_M05_sideback-C4_0000", null, null);

AssignMaterial("Sources.Materials.DefaultLib.M05_sideback-C4_0000_png,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_001_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_002_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_003_M05_sideback-C4_0000_M05_sideback-C4_0000,CROWD_platform_01.polymsh.cls.M05_sideback-C4_0000_004_M05_sideback-C4_0000_M05_sideback-C4_0000", siLetLocalMaterialsOverlap);
How would I programm this to search for the tags in a new selection/object/set of clusters and apply each material according to their "tag"?

Or can this be done in ICE? I am still new to programming, and have no idea how arrays might work - some pointers would be very much appreciated. ;;)

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

Re: Dynamically applying scripts to new selections of obj/clus?

Post by rray » 06 Jan 2016, 12:30

You'll have use the object model if you want to avoid doing string manipulation to access data
Here's something on clusters: http://docs.autodesk.com/SI/2015/ENU/So ... usters.htm

for example this lists all clusters on a selected object:

Code: Select all

for(i=0; i<Selection(0).ActivePrimitive.Geometry.Clusters.Count;i++)
{
	clustername = Selection(0).ActivePrimitive.Geometry.Clusters(i).Name;
	logmessage (clustername);
}
if you want to do some mapping from clustername to material, you could use a jscript object for that (and if it's just a substring of the cluster name, extract that first):

Code: Select all

materials_to_map = {
	"wood": "Sources.Material.DefaultLib.MaterialMetal",
	"metal": "Sources.Material.DefaultLib.MaterialWood"
};

for(i=0; i<Selection(0).ActivePrimitive.Geometry.Clusters.Count;i++)
{
	clustername = Selection(0).ActivePrimitive.Geometry.Clusters(i).Name;
	logmessage (materials_to_map[clustername]);
        //assign code goes here
}
although I'd probably prefer ID channels instead of using clusters
"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

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

Re: Dynamically applying scripts to new selections of obj/clus?

Post by myara » 06 Jan 2016, 13:02

Getting into Object Model can be a little challenging at first but you'll need it if you want to script in Softimage. I don't know how useful can it be to learn how to write in Softimage (and in JScript) nowadays though.

If you must learn Softimage SDK now, I'd recommend you to learn it with Python. JScript was my main language in Softimage and I loved it, but since it is a dead software and almost all the other packages use Python, it would be better to learn something you'll be able to use in other applications in the future. I write Maya scripts in Python now and I wish I were more fluent and knew more Python tricks, I'm still writing in JScript in Softimage though.

Back to topic, only Poly Clusters have materials so you may want to filter poly clusters with "Filter".

Object Model:
http://docs.autodesk.com/SI/2015/ENU/So ... omref.html

Filter:
http://docs.autodesk.com/SI/2015/ENU/So ... ilter.html

I'd also recommend to use variables:

Code: Select all

oObj = Selection(0)
oPolyCls = oObj.ActivePrimitive.Geometry.Clusters.Filter("poly")

for(i=0; i<oPolyCls.Count;i++){
	LogMessage("Cluster Name = " + oPolyCls(i).Name )
	LogMessage("Cluster Material = " + oPolyCls(i).Material )
}
M.Yara
Character Modeler | Softimage Generalist (sort of)

User avatar
Draise
Posts: 813
Joined: 09 Oct 2012, 20:48
Skype: ondraise
Location: Colombia

Re: Dynamically applying scripts to new selections of obj/clus?

Post by Draise » 06 Jan 2016, 17:32

Actually.. Good idea on going Python. It opens doors with Fabric Engine, Blender, Maya, everything out there... ok.. I'll go with that.

I started in Jscript because I more or less understand it...

In that case, what would the syntax be for the same things above in Python, perchance?

Thanks heaps guys, I'll try apply it. It makes my day. :ymhug:

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

Re: Dynamically applying scripts to new selections of obj/clus?

Post by myara » 08 Jan 2016, 14:41

Code: Select all

xsi = Application
oObj = xsi.Selection(0)
oPolyCls = oObj.ActivePrimitive.Geometry.Clusters.Filter("poly")

# For Loop
for oPolyCl in oPolyCls:
   LogMessage("Cluster Name = " + str(oPolyCl.Name) )
   LogMessage("Cluster Material = " + str(oPolyCl.Material) )

# For Loop (range)
for i in range(len(oPolyCls)):
   LogMessage("Cluster Name = " + str(oPolyCls(i).Name) )
   LogMessage("Cluster Material = " + str(oPolyCls(i).Material) )
When you deal with objects (object model) instead of strings (commands) , you can access its properties and parameters, that's why you can get the material a cluster is using with adding ".Material". Besides reading the SDK Manual, you can use the "SDK Explorer" to learn more about what properties each object have.

In Python you have more loop variations, I wrote a couple of them.
Another difference is that you can't use most Softimage commands without writing "Application" before it. Most people change Application to a variable like "xsi" to make it shorter.

LogMessage is one of those commands that can be used without "Application", but you can also change it to a variable like this:

Code: Select all

l = Application.LogMessage
l ("Test")
Or maybe use Python "print".

Code: Select all

print "Test"
And you can't concatenate strings with numbers or objects, so you'll need to use str(), and remember that Python is case sensitive.

One more thing, it is recommended to use spaces instead of Tabs so set your tab to insert spaces in your preferences.

Good luck
M.Yara
Character Modeler | Softimage Generalist (sort of)

User avatar
Draise
Posts: 813
Joined: 09 Oct 2012, 20:48
Skype: ondraise
Location: Colombia

Re: Dynamically applying scripts to new selections of obj/clus?

Post by Draise » 19 Jan 2017, 15:32

Thanks for these. Finally got the hang of it.

For my own documentation sake, to run a "setvalue" it would look like this:

Code: Select all

for obj in Application.Selection :
	Application.setvalue(".kine.local.posx", -0.1, "")