[python] SetFont not working



  • Hello
    C4D version: 20.059
    OS: Windows 10

    I select motext and try to change font(fontdata) for text by script

    import c4d
    from c4d import plugins
    def main() :
      bc = c4d.BaseContainer()
      bc.SetString(500, 'Arial')
      bc.SetString(501, '11')
      bc.SetInt32(502, 400)
      bc.SetInt32(503, 0)
      bc.SetString(509, 'Arial')
      bc.SetString(508, 'ArialMT')
      # select mograph text
      op[c4d.PRIM_TEXT_FONT].SetFont(bc)
      op.Message(c4d.MSG_CHANGE)
      c4d.EventAdd()
    if __name__=='__main__':
      main()
    

    can not get it work ☹



  • Found such solution:

    import c4d
    
    def main():
    
        bc = c4d.BaseContainer()
        ids = c4d.PRIM_TEXT_FONT
        #ids = c4d.DescID(c4d.DescLevel(2117,1009372,5178))
        op.SetParameter(ids, bc, c4d.DESCFLAGS_SET_0)
        bc.SetString(500, 'Arial')
        bc.SetString(501, '11')
        bc.SetInt32(502, 400)
        bc.SetInt32(503, 0)
        bc.SetString(509, 'Arial')
        bc.SetString(508, 'ArialMT')
        new_fd = c4d.FontData()
        new_fd.SetFont(bc)
        op.SetParameter(ids, new_fd, c4d.DESCFLAGS_SET_0)    
        c4d.SendCoreMessage(c4d.COREMSG_CINEMA, c4d.BaseContainer(c4d.COREMSG_CINEMA_FORCE_AM_UPDATE))
        c4d.gui.GeUpdateUI()
        c4d.EventAdd()
    
    # Execute main()
    if __name__=='__main__':
        main()
    


  • The issue you figured out is pretty common in python. So actually when you do op[c4d.PRIM_TEXT_FONT] (aka GetParameter) you retrieve a copy of this CustomDataType. That means if you want to modify something it will not be reflected in the UI.

    Finally, you don't need all the update code only a c4d.EventAdd is enough.

    import c4d
    
    def main():
        # Retrieves a copy of the actual FontData
        fontData = op[c4d.PRIM_TEXT_FONT]
        
        # Defines parameters for this FontData
        bc = c4d.BaseContainer()
        bc.SetString(500, 'Arial')
        bc.SetString(501, '11')
        bc.SetInt32(502, 400)
        bc.SetInt32(503, 0)
        bc.SetString(509, 'Arial')
        bc.SetString(508, 'ArialMT')
        fontData.SetFont(bc)
        
        # Push back this FontData into the object
        op[c4d.PRIM_TEXT_FONT] = fontData
        
        # Pushes an update event to Cinema 4D
        c4d.EventAdd()
    
    # Execute main()
    if __name__=='__main__':
        main()
    

    Cheers,
    Maxime.



  • Hello Maxime
    Thank you.
    I tried such form and can not get it work. I think i'm "messing"(replace FontData to BaseContainer) data for GUI, so it does not work for me.



  • In which version are you? The code I posted is working nicely in R20.059.



  • @m_adam said in [python] SetFont not working:

    In which version are you? The code I posted is working nicely in R20.059.

    20.059. Before i tried any methods, that's my fail. Not work for me. From new start of c4d. - it works