Why does my script fail when multiple objects are selected?

hello
due to the text flips when converting to dwg,
I wrote a script to fix text plane,it works when single textobj selected,
but if i select multiple text objects ,it failed.

I don’t know why, please help me, thanks.


import Rhino as rh
import scriptcontext
import rhinoscriptsyntax as rs

def FixTextPlane():

msg = "Select texts to justify text plane"
text_id = rs.GetObjects(msg,rs.filter.annotation)
if not text_id: return

if rs.IsText(text_id):
        textpoint = rs.TextObjectPoint(text_id)
        rs.TextObjectPlane(text_id,rs.WorldXYPlane())
        rs.TextObjectPoint(text_id,textpoint)
else:
    return

if name==“main”:
FixTextPlane()

The IsText() method is failing because it is expecting a single object, and you are giving it a list (from GetObjects(). If you have methods that accept only single objects and you want to apply them to multiple objects in a list, you need to use a loop.

import rhinoscriptsyntax as rs

def FixTextPlane():
    msg = "Select texts to justify text plane"
    text_ids = rs.GetObjects(msg,rs.filter.annotation)
    if not text_ids: return
    
    #this is your loop
    for text_id in text_ids:
        if rs.IsText(text_id):
                textpoint = rs.TextObjectPoint(text_id)
                rs.TextObjectPlane(text_id,rs.WorldXYPlane())
                rs.TextObjectPoint(text_id,textpoint)

FixTextPlane()

BTW, if you want to post code in here, you can format it so that it looks correctly by enclosing it in 3 “backticks” like this:

image

thank you for solving!

Now I knew what the problem is and learned how to make it works, really thanks you!