Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

change variable name related to loop iteration number

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Andy_rapid-ood
168 Views, 3 Replies

change variable name related to loop iteration number

Hello,

I am just creating a script that should implement a certain number of text strings (t_1, t_2...) into the same number of sketches. This sketches are always named "Text1_sketch, Text2_sketch..." the text strings ("ZZZ", "UUU"...) are entered manually in the script.

In general I am just starting a loop 1-5 and iterate "i" to use it as indicator for the related sketch AND the related variable. But the variable is not correctly assigned or initialized. I guess it is just my lag in python programming. Has anyone a hint how to "easily" achieve my goal

 

        # change text in sketch "i" to variable "t_i"
        sketches = app.activeProduct.rootComponent.sketches  
        for i in range(1,5😞
            t_1 = "ZZZ"
            t_2 = "UUU"
            t_3 = "PPP"
            t_4 = "EEE"
            sketchname = "Text" + str(i) + "_sketch"
            textstring = "t_"+(i)
            textsketch = sketches.itemByName(sketchname)
            textsketchtexts = textsketch.sketchTexts
            #.item(no.): no. indicates number of text in dedicated sketch
            textinputitem = textsketchtexts.item(0)
            textinputitem.text = textstring
3 REPLIES 3
Message 2 of 4

Hi,

 

You can do it this way:

 

    # change text in sketch "i" to variable "t_i"
    texts = {
        1 : "ZZZ",
        2 : "UUU",
        3 : "PPP",
        4 : "EEE",
    }
    sketches = app.activeProduct.rootComponent.sketches  
    for i in range(1,5):
        sketchname = f"Text{i}_sketch"
        textstring = texts[i]
        textsketch = sketches.itemByName(sketchname)
        textsketchtexts = textsketch.sketchTexts
        #.item(no.): no. indicates number of text in dedicated sketch
        textinputitem = textsketchtexts.item(0)
        textinputitem.text = textstring

 

 

This is the result in my testing:

wtallerdemadera_0-1687265222462.png

I hope this can help.

 

Regards,

Jorge Jaramillo

Message 3 of 4

Hello Jorge,

 

great, yes, that works. Bad python knowledge from my side I guess.

Am I right that {1, 2, 3, 4} is creating a "dictionary" and texts[i] is accessing the dictionary?

I don't understand the "f" and the curly braces here. Can you maybe drop two sentences for explanation?

sketchname = f"Text{i}_sketch"

 

Message 4 of 4

Hi @Andy_rapid-ood ,

 

The "texts" variable is a dictionary.  It is first defined and then with texts[i] you get an item from it.  In this case, an integer value as the key; it also support many other variable types as key values.

 

Regard the f-string, you can read about it here on section 7.1.1.

It is straight way to format strings.  What is included inside curly brackets will be replace by the interpret at run time, and its value is append in the position it was placed.  I used regularly, mainly for debugging purposes.

 

Best regards,

Jorge Jaramillo

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report