The same is true, trying to rename objects in the OM: It works manually, but not via script. Is it an encoding issue?
![](/forum/assets/uploads/profile/12063-profileavatar-1703020362084.png)
Posts made by gaschka
-
RE: Rename Layers with Python containing Emoji?
-
Rename Layers with Python containing Emoji?
import c4d doc: c4d.documents.BaseDocument # The currently active document. op: c4d.BaseObject | None # The primary selected object in `doc`. Can be `None`. def create_and_rename_layer(doc, layer_name): new_layer = c4d.documents.LayerObject() new_layer.SetName(layer_name) root_layer = doc.GetLayerObjectRoot() new_layer.InsertUnderLast(root_layer) c4d.EventAdd() return new_layer def main() -> None: layer_1_string = "Layer 1: \N{grinning face with smiling eyes}" print(layer_1_string) layer_1 = create_and_rename_layer(doc, layer_1_string) layer_2_string = "Layer 2: \U0001F600" print(layer_2_string) layer_2 = create_and_rename_layer(doc, layer_2_string) layer_3_string = "Layer 3: 🤣" print(layer_3_string) layer_3 = create_and_rename_layer(doc, layer_3_string) if __name__ == '__main__': main()
I've the code above, with which I try to create and rename layers, so they contain lovely Emoji. Though when I execute the code, I see the Emoji in the Console, but the layers refuse to rename accordingly.
When I edit a layer by hand, I can input Emoji with no issues:
-
RE: Python tag initalization?
@m_adam said in Python tag initalization?:
OM is not single threaded,
Oh, that's an interesting insight for me. Perhaps I was misinformed, as Character Rigs had the tendency to get slow quite easily in C4D, and Animators look envy over to Maya, as they do have a parallel evaluation (and caching). Though I'm aware that there are changes/improvements to C4D performance lately. Thanks for the link. A lot of new information to learn an absorb
-
RE: Python tag initalization?
@baca thanks for the insights! I guess I sit too long next to game devs who have a tendency for premature optimizations and got infected
But I can second that: I know so many C4D artists who don't have a clue, neither they care, about how to keep theirs scene performant. -
RE: Python script to Python tag hand over?
Thanks for your help and nudging me into the right direction, I think I can work with that
-
RE: Python: How to inject code into a Python tag?
Thanks to you both for your answers.
@m_adam Regarning my issue with the file linking, I'll try to come up with a procedure for reproduction of my issue soon: It was quite random, but I was in the flow of writing my script, so I circumvented with the string solution, but I would prefer the file solution for sure.
-
RE: Python tag initalization?
@m_adam thanks a lot for the explaination and the code. Messages is a new concept for me and something I'll definitely explore.
Referring to point 1: So checking if the object is changed, even with the OM traversal, is something I can ignore performance wise? I'm asking, because I want the tag to control a custom piece of a character rig, perhaps with multiple copies of it. Rigs tend to be quite performance critical and running single threaded in C4D due to the OM so I wonder if this kind of search in the OM can have a significant impact?
-
RE: Python: User Data Float Slider?
@i_mazlov Thank you for the friendly introduction and the hint to CUSTOMGUI and REALSLIDER, exactly what I was looking for.
@baca Thank you for the additional tip. -
Python: How to import files as modules?
Hello everybody,
I'm new to this forum, so please bear with me in case my questions are hardly noob ish. I tried my best to consult the docs and the search first, but still I do have this question:
Right now, I'm working on a script that hit the 600 lines mark, so I thought it's a good idea to create different files and use import to use these files as modules. But somehow C4D python refuses to find the file and use these files as modules. As soon as I deactivate the VSC C4D bridge, the import works (at least this is what the VSC console is telling me).
I've set up an Environment Variable to my script repo with the hope this would fix it, but without any luck.Is this bad practice for a script? Shall I turn it into a plugin to have this as an option?
-
Python: How to inject code into a Python tag?
I'm working on a script, which creates bunch of objects in the OM and then adds a Python Tag to one of them, to control my new objects with a script.
First I tried the following approach, which did not always work reliable from VSC, especially after restarting C4D. My impression was, that I had to open the linked file in the Script Manager once to be able to use the Execute in C4D command in VSC with that file linked successfully:def add_python_tag(obj, config): tag_file = os.path.join(os.path.dirname(__file__), "file_with_code_for_the_python_tag.py") if not obj or not os.path.exists(tag_file): return python_tag = c4d.BaseTag(c4d.Tpython) with open(tag_file, 'r') as f: python_code = f.read() python_tag[c4d.TPYTHON_CODE] = python_code obj.InsertTag(python_tag) c4d.EventAdd()
So I'm using this approach now, but this feels very hacky, and I loose the syntax highlighting for all the injected code, which makes me still maintain the file above and then copy and paste into the string:
def add_python_tag(obj): if not obj: return python_tag = c4d.BaseTag(c4d.Tpython) python_code = """ import c4d def main(): # my code for the Python tag # Execution if __name__ == '__main__': main() """ python_tag[c4d.TPYTHON_CODE] = python_code obj.InsertTag(python_tag) c4d.EventAdd()
I'm new to all of this, so I want to ask, if this a good practice, or are there other more slick ways to approach this?
-
Python: User Data Float Slider?
I create user data with a python script, which works fine.
Now I want to change the appearance of the user data to a Float Slider, at least thats the name of it when I right click it in C4D and choose it underneath User Interface. Can I set it with Python? I'm not able to find the corresponsing call in the Docs, but perhaps I just don't know the correct string to search for.
Please help
# .... define create_null() up here squash_and_stretch_name = "Squash and Stretch" squash_and_stretch_type = c4d.DTYPE_REAL initial_value = 0 min_value = 0.0 max_value = 1.0 step_value = 0.01 user_data_null = create_null("User Data") squash_and_stretch_container = c4d.GetCustomDataTypeDefault(squash_and_stretch_type) squash_and_stretch_container[c4d.DESC_NAME] = squash_and_stretch_name squash_and_stretch_container[c4d.DESC_MIN] = min_value squash_and_stretch_container[c4d.DESC_MAX] = max_value squash_and_stretch_container[c4d.DESC_STEP] = step_value squash_and_stretch_container[c4d.DESC_DEFAULT] = initial_value user_data_null.AddUserData(squash_and_stretch_container)
-
Python script to Python tag hand over?
I'm working on a script that creates a python tag with some code.
Now I want the main script to talk to the python tag script and hand over some information.
I do it in an incredibly hacky way, and like to know if this is appropriate, or if there is a more elegant/reliable way to do it?
def add_python_tag(obj, crtl1, crtl2): if not obj: return pos_1 = get_global_position(crtl1) pos_2 = get_global_position(crtl2) distance_init = (pos_1 - pos_2).GetLength() python_tag = c4d.BaseTag(c4d.Tpython) python_code = """ import c4d def main(): #some more code ... pos_1 = get_global_position(ctrl_01) pos_2 = get_global_position(ctrl_02) distance_const = distance_init distance = (pos_1 - pos_2).GetLength() stretch_coefficient = distance_const / distance # and even more code ... # Execution if __name__ == '__main__': main() """ python_code = python_code.replace("distance_init", str(distance_init)) python_tag[c4d.TPYTHON_CODE] = python_code obj.InsertTag(python_tag) c4d.EventAdd()
-
Python tag initalization?
Hello everyone,
I'm new here
I've a python script that creates a Python tag and injects some code into it. This Python tag is then searching in the hierarchy for objects to link to. This search is in the main() so I guess it executing every frame/interaction, which doesn't sound very performant, especially as it will relink the same object over and over again.
Is there a way to initialize the python tag somehow, so in the example below, crtl_01 gets assigned just a single time, and then keeps its value without updating it?
Is initialization the correct term for this?
The code looks similar to this:
import c4d def get_object_from_python_tag(tag): if tag and tag.GetType() == c4d.Tpython: linked_object = tag.GetMain() if linked_object: return linked_object return None def get_parent_object(obj): return obj.GetUp() def find_object_in_hierarchy_by_name(current_obj, target_name): if current_obj.GetName() == target_name: return current_obj child = current_obj.GetDown() while child: result = find_object_in_hierarchy_by_name(child, target_name) if result: return result child = child.GetNext() return None def main(): python_null = get_object_from_python_tag(op) ctrl_01 = find_object_in_hierarchy_by_name(get_parent_object(python_null), "Helper_01") # Execution if __name__ == '__main__': main()