Today we’ll have a look at Cinema 4D Motext & Python interaction…
Changing Text in Motext
(Motext object should be selected.)
This script also renames object’s name as text you defined.
1 2 3 4 5 6 7 8 9 10 |
import c4d def main(): name = "CHANGE TEXT" op[c4d.ID_BASELIST_NAME] = op[c4d.PRIM_TEXT_TEXT] op[c4d.PRIM_TEXT_TEXT] = name c4d.EventAdd() if __name__=='__main__': main() |
Split Motext Letters Into a Group as Separate Motext Objects
This tiny script I’ve written simply splits letters in a word then put them as individual objects under a null group. Objects stay still as a motext object. Their object name changes according to letter as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import c4d def main(): if op==None: return name = "CHANGE TEXT" #letters = str.split(name) letters = list(name) lettercount = len(letters) print lettercount print letters group = c4d.BaseObject(c4d.Onull) doc.InsertObject(group) group.SetName("Motext Group") for i in xrange(lettercount): motext = c4d.BaseObject(1019268) # Motext ID motext.InsertUnder(group) # Insert Under Group motext[c4d.PRIM_TEXT_TEXT] = (letters[i]) motext.SetName (letters[i]) op[c4d.ID_BASELIST_NAME] = op[c4d.PRIM_TEXT_TEXT] op[c4d.PRIM_TEXT_TEXT] = name c4d.EventAdd() if __name__=='__main__': main() |
Split Sentences as Words in a Motext
With a little modification to script above we can easily split motext into words as separate objects. This one also hides render and editor view status of selected motext object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import c4d def main(): if op==None: return op[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = True op[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = True name = "Creativetuts Cinema 4D Python" words = str.split(name) wordcount = len(words) print wordcount print words group = c4d.BaseObject(c4d.Onull) doc.InsertObject(group) group.SetName("Motext Words") for i in xrange(wordcount): motext = c4d.BaseObject(1019268) # Motext ID motext.InsertUnder(group) # Insert Under Group motext[c4d.PRIM_TEXT_TEXT] = (words[i]) motext.SetName (words[i]) op[c4d.ID_BASELIST_NAME] = op[c4d.PRIM_TEXT_TEXT] op[c4d.PRIM_TEXT_TEXT] = name c4d.EventAdd() if __name__=='__main__': main() |
Split Motext into Words with a Special Character Separator
Say we have a sentence : “Creativetuts Cinema 4D Python”, we would like to split this sentences into 3 words like “creativetuts”, “cinema 4d”, “python”. By default if we simply split our words like in previous example it would be 4 words including “4d” .In this case we should define a special character to filter words which we want to hold still. We have used “~” character to define separator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import c4d def main(): if op==None: return op[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = True op[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = True name = "Creativetuts~Cinema 4D~Python" # "~" is our special character to split text words = str.split (name) words = name.split('~') wordcount = len(words) print wordcount print words group = c4d.BaseObject(c4d.Onull) doc.InsertObject(group) group.SetName("Motext Words") for i in xrange(wordcount): motext = c4d.BaseObject(1019268) # Motext ID motext.InsertUnder(group) # Insert Under Group motext[c4d.PRIM_TEXT_TEXT] = (words[i]) motext.SetName (words[i]) op[c4d.ID_BASELIST_NAME] = op[c4d.PRIM_TEXT_TEXT] op[c4d.PRIM_TEXT_TEXT] = name c4d.EventAdd() if __name__=='__main__': main() |
Make Motext String Uppercase, Lowercase and Swapcase
Example script showing how to convert your text to uppercase, lowercase or swap between those.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import c4d def main(): if op==None: return name = op[c4d.PRIM_TEXT_TEXT] print name newname = name.upper() # Make Uppercase #newname = name.lower() # Make Lowercase #newname = name.swapcase() # Swapcase op[c4d.PRIM_TEXT_TEXT] = newname c4d.EventAdd() if __name__=='__main__': main() |
Remove Spaces and Specific Characters from String
In this example script we’ll try to remove specific characters and empty spaces from our string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import c4d def main(): if op==None: return name = op[c4d.PRIM_TEXT_TEXT] #letters = str.split(name) letters = list(name) lettercount = len(letters) print lettercount print letters group = c4d.BaseObject(c4d.Onull) doc.InsertObject(group) group.SetName('Motext Group') for i in xrange(lettercount): motext = c4d.BaseObject(1019268) # Motext ID motext.InsertUnder(group) # Insert Under Group if letters[i] == ' ': motext.Remove() if letters[i] == 'A': letters[i] = letters[i].replace('A', '') motext[c4d.PRIM_TEXT_TEXT] = (letters[i]) motext.SetName (letters[i]) if motext[c4d.ID_BASELIST_NAME] == 'MoText': motext.SetName (' ') motext.Remove() op[c4d.ID_BASELIST_NAME] = op[c4d.PRIM_TEXT_TEXT] op[c4d.PRIM_TEXT_TEXT] = name c4d.EventAdd() if __name__=='__main__': main() |
Split motext letters as separate objects & convert into editable polygons
This scripts basically duplicates selected motext, add connect object into the scene and put motext copy under connect object. Then convert this connect object into an editable polygon and call “Polygon Groups to Objects” command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import c4d def main(): doc = c4d.documents.GetActiveDocument() op = doc.GetActiveObject() if op==None: return op[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = True op[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = True name = op.GetName() newname = name + " GROUP" c4d.CallCommand(100004820) # Copy c4d.CallCommand(100004821) # Paste newop = doc.GetActiveObject() newop[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = False newop[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = False newop.SetName(newname) connectobj = c4d.BaseObject(1011010) # Inserts Connect Object doc.InsertObject(connectobj) newop.InsertUnder(connectobj) connectobj[c4d.CONNECTOBJECT_PHONG_MODE] = 1 obj = newop.GetUp() obj.SetBit(c4d.BIT_ACTIVE) obj.SetName (newname) c4d.CallCommand(12236) # Make Editable c4d.CallCommand(100004768) # Select Children c4d.CallCommand(12236) # Make Editable c4d.CallCommand(100004768) # Select Children c4d.CallCommand(16768) # Connect Objects + Delete c4d.CallCommand(17891, 17891) # Polygon Groups to Objects newop[c4d.ID_BASEOBJECT_GENERATOR_FLAG]=False c4d.EventAdd() if __name__=='__main__': main() |
Leave a Reply
Want to join the discussion?Feel free to contribute!