Python. Cinema4d. All iterations in the Text value
-
Hi all. Tell me, what I need to do with the code so that each iteration is displayed in a separate line in the 3D text? Only the last one is shown. Second screenshot is a result i want!
-
That's because you only have one text object.
You need to also have multiple text object corresponding to the number of items you loop. -
@bentraje thx for answer!
Is it physically impossible to enter random numbers within one text? I, without a script, can drive several lines of text or numbers into one text. Is it possible to do the same with Python? maybe other mechanics, not through iterations? -
I mean you can input all those numbers in one text if you don't mind foregoing individual controls.
You just need to output multiple line of text/str. -
@smetk
You need line break character to join the items.from random import random def main() -> None: global Output1 Output1 = "\n".join(f"{random()*10:.6f}" for i in range(6))
Cheers.
-
something like this
-
-
Hello @smetk,
thank you for reaching out to us. A big thank you also for the community answers given by @bentraje and @iplai. There is not much to add for us here, the solutions provided here are the correct solution.
It is a bit of a question of taste, but you could also write what you want to do in a more compressed and modern Python style. There is also the small problem that when you always append the line break character, this will also happen for the last line where this might not be intended. Find an example below which avoids this.
Cheers,
FerdinandThe file: string_stuff.c4d
The result (I also added justification/null-padding for the decimal places):
The code:
import c4d import random def main() -> None: global text # Compute n random numbers between minValue and maxValue, and round them each to three places. numbers: list[float] = [round(random.uniform(n, n + 1), 3) for n in range(minValue, maxValue)] # Convert them to strings while padding zeros to numbers which do not have three decimal places, # e.g., 3.1 -> "3.100" strings: list[str] = [f"{i}.{d.ljust(3, '0')}" for i, d in [str(n).split(".") for n in numbers]] # Join them into a singular string using the line break character as the concatenation character. text = "\n".join(strings)
-
@ferdinand thx very much! ill save this