Python: Loop write to Variants Script

Python: Loop write to Variants Script

Anonymous
Not applicable
1,025 Views
2 Replies
Message 1 of 3

Python: Loop write to Variants Script

Anonymous
Not applicable

Hi,

 

Simplified example of my script:

varSet = createVariantSet(variantName)
mats = getAllMaterials()
for mat in mats:
    varSet.addScript("my script here" + mat.getName() + "the rest of my script here")

When I execute this script, the variant "variantName" is created, but only with the last material in the scene, showing that it's looped but over-written the script for all the other materials.

I can't seem to figure out a way to loop without overwriting - or loop outside of varSet.addScript() and calling back to it.

 

 

Many thanks!

0 Likes
Accepted solutions (1)
1,026 Views
2 Replies
Replies (2)
Message 2 of 3

marcus_fritzen
Autodesk
Autodesk
Accepted solution

Hi,

 

the addScript function is NOT an appendScript function. So, you will need to create the script string with your loop and afterwards use the addScript function to set the final script string.

 

Cheers,

Marcus



Marcus Fritzen
Principal Engineer
Message 3 of 3

Anonymous
Not applicable

That's brilliant, thanks!

I just needed pointing in the right direction Smiley Very Happy

 

For anyone reading this having a similar problem, this is what I came up with:

varSet = createVariantSet(variantName)
mats = getAllMaterials()
finalScript = ""
for node in nodes: #nodes is defined elsewhere in the script finalScript += ("my script here" + mat.getName() + "the rest of my script here")

varSet.addScript(str(finalScript))

It's worth noting I'm new to coding, so it might not be the best solution...but it's one that works!

0 Likes