-
Notifications
You must be signed in to change notification settings - Fork 213
Allow create_text to accept multiple texts and materials fixes #1012 #1101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from all commits
07aa68a
e67cd6f
6222c44
c97f3d8
dbd6206
8ed4b87
4b51fa2
2eb8b9b
39d166e
f8f2635
fbc73c8
051b313
7fd2c98
c2bee3f
b77ab86
a1d469d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -262,35 +262,101 @@ def create_point(geometry, material): | |||
|
|
||||
|
|
||||
| def create_text(text, material, **kwargs): | ||||
| """Create a text object. | ||||
| """Create a text object or a group of text objects. | ||||
|
|
||||
| Parameters | ||||
| ---------- | ||||
| text : str | ||||
| The text content. | ||||
| material : TextMaterial | ||||
| The material object. | ||||
| text : str or list of str | ||||
| The text content or list of text contents. | ||||
| material : TextMaterial or list of TextMaterial | ||||
| The material object or list of material objects. | ||||
| **kwargs : dict | ||||
| Additional properties like font_size, anchor, etc. | ||||
| Additional properties like font_size, anchor, position, etc. | ||||
| When ``text`` is a list, each kwarg can be either: | ||||
| - A single value applied to all text objects, or | ||||
| - A list of values (one per text) for per-text customization. | ||||
|
|
||||
| Returns | ||||
| ------- | ||||
| Text | ||||
| The text object. | ||||
| Text or Group | ||||
| A single Text object if text is a string, or a Group of Text | ||||
| objects if text is a list. | ||||
|
|
||||
| Raises | ||||
| ------ | ||||
| TypeError | ||||
| If text is not a string or material is not an instance of TextMaterial. | ||||
| If text is not a string or list of strings, or material is not | ||||
| an instance of TextMaterial or list of TextMaterial. | ||||
| ValueError | ||||
| If the length of material list or any kwarg list does not match | ||||
| the length of the text list. | ||||
|
|
||||
| Examples | ||||
| -------- | ||||
| >>> mat = TextMaterial() | ||||
| >>> # Single text (unchanged behavior) | ||||
| >>> t = create_text("Hello", mat) | ||||
|
|
||||
| >>> # Multiple texts, shared material and kwargs | ||||
| >>> t = create_text(["Hello", "World", "FURY"], mat) | ||||
|
|
||||
| >>> # Multiple texts with individual materials and per-text positions | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comments from examples. |
||||
| >>> t = create_text( | ||||
| ... ["Hello", "World"], | ||||
| ... [TextMaterial(), TextMaterial()], | ||||
| ... position=[(0, 0, 0), (1, 0, 0)], | ||||
| ... ) | ||||
| """ | ||||
| if isinstance(text, list): | ||||
| if not all(isinstance(t, str) for t in text): | ||||
| raise TypeError("All items in text list must be strings.") | ||||
|
|
||||
| n = len(text) | ||||
|
|
||||
| if isinstance(material, list): | ||||
| if len(material) != n: | ||||
| raise ValueError( | ||||
| "Length of material list must match length of text list." | ||||
| ) | ||||
| if not all(isinstance(m, TextMaterial) for m in material): | ||||
| raise TypeError( | ||||
| "All items in material list must be instances of TextMaterial." | ||||
| ) | ||||
| materials = material | ||||
| else: | ||||
| if not isinstance(material, TextMaterial): | ||||
| raise TypeError("material must be an instance of TextMaterial.") | ||||
| materials = [material] * n | ||||
|
|
||||
| # Normalize kwargs: expand scalars to per-text lists | ||||
| norm_kwargs = {} | ||||
| for k, v in kwargs.items(): | ||||
| if isinstance(v, list): | ||||
| if len(v) != n: | ||||
| raise ValueError( | ||||
| f"Length of kwarg '{k}' list must match length of text list." | ||||
| ) | ||||
| norm_kwargs[k] = v | ||||
| else: | ||||
| norm_kwargs[k] = [v] * n | ||||
|
|
||||
| group = Group() | ||||
| for i, (t, m) in enumerate(zip(text, materials, strict=True)): | ||||
| per_text_kwargs = {k: norm_kwargs[k][i] for k in norm_kwargs} | ||||
| position = per_text_kwargs.pop("position", None) | ||||
| actor = Text(text=t, material=m, **per_text_kwargs) | ||||
| if position is not None: | ||||
| actor.local.position = position | ||||
| group.add(actor) | ||||
| return group | ||||
|
|
||||
| if not isinstance(text, str): | ||||
| raise TypeError("text must be a string.") | ||||
|
|
||||
| if not isinstance(material, TextMaterial): | ||||
| raise TypeError("material must be an instance of TextMaterial.") | ||||
|
|
||||
| text = Text(text=text, material=material, **kwargs) | ||||
| return text | ||||
| return Text(text=text, material=material, **kwargs) | ||||
|
Comment on lines
264
to
+359
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this should not be done here. Please check the text method in the planar.py Line 497 in 8bb525c
This method should be updated to accept multiple text not this method. The text method is the one which people should use. |
||||
|
|
||||
|
|
||||
| def create_image(image_input, material, **kwargs): | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also think about how do you provide all the other keyword arguments in the case of multiple text?
Will the position be same for all of them?