Page number by rs.TextObjectText

I am trying to use rs.TextObjectTextfunction and it works well, but when i am putting %% into text to achieve page number it is not changed into proper number, but displayed(and printed) as “%%” until I double-click on it and deactivate it. is it common issue? Is any smart way of dealing with it?

Does this work?

rs.TextObjectText(txt_obj,"Page {}".format(n))

name ‘n’ is not defined. Maybe i’ll add that i am using python.

The “n” represents your page number…

Yes, but I have template with the same names of text object on a lot of pages, and with this method i will have the same number on every page. I noticed that when I put %pagenumber% in triangle brackets in text it changes into proper number on each page, but i have to refresh it somehow.

@bojdol, maybe you’re missing just a CommitChanges() ? Below will append the %pagenumber% to existing text object using TextFormula:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    id = rs.GetObject("Select text object", 512, True, False)
    if not id: return
    
    currentText = rs.TextObjectText(id)
    te = rs.coercerhinoobject(id, True, True)
    te.Geometry.TextFormula = currentText + " %<pagenumber>%" 
    te.CommitChanges()
    
    scriptcontext.doc.Views.Redraw()
    
DoSomething()

_
c.

1 Like

This works. Thank you a lot.