<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python: How to inject code into a Python tag?]]></title><description><![CDATA[<p dir="auto">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.<br />
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:</p>
<pre><code>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()
</code></pre>
<p dir="auto">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:</p>
<pre><code>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()
</code></pre>
<p dir="auto">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?</p>
]]></description><link>http://developers.maxon.net/forum/topic/15297/python-how-to-inject-code-into-a-python-tag</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 01:53:54 GMT</lastBuildDate><atom:link href="http://developers.maxon.net/forum/topic/15297.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Dec 2023 17:31:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Python: How to inject code into a Python tag? on Thu, 21 Dec 2023 08:57:02 GMT]]></title><description><![CDATA[<p dir="auto">Thanks to you both for your answers.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/m_adam">@<bdi>m_adam</bdi></a> 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.</p>
]]></description><link>http://developers.maxon.net/forum/post/73392</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73392</guid><dc:creator><![CDATA[gaschka]]></dc:creator><pubDate>Thu, 21 Dec 2023 08:57:02 GMT</pubDate></item><item><title><![CDATA[Reply to Python: How to inject code into a Python tag? on Wed, 20 Dec 2023 09:53:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/gaschka">@<bdi>gaschka</bdi></a> said in <a href="https://developers.maxon.net/forumhttps://developers.maxon.net/forum/post/73372" target="_blank" rel="noopener noreferrer nofollow ugc">Python: How to inject code into a Python tag?</a>:</p>
<blockquote>
<p dir="auto">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</p>
</blockquote>
<p dir="auto">It will be nice if you have clear reproduction step, if I understand correctly you did the following, is that correct?:</p>
<ol>
<li>Start Cinema 4D</li>
<li>Start VSCode</li>
<li>Connect VsCode and Cinema 4D together</li>
<li>Create a new Empty document in VsCode</li>
<li>Write some stuff into it</li>
<li>Execute it from VsCode via the <code>Execute in C4D command</code>.</li>
</ol>
<p dir="auto">What confuse me is when you say "especially after restarting C4D. " So the first time it work, but not the next one? At least here its working as expected. May I ask which version of Cinema 4D and VsCode extension do you use?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/gaschka">@<bdi>gaschka</bdi></a> said in <a href="https://developers.maxon.net/forumhttps://developers.maxon.net/forum/post/73372" target="_blank" rel="noopener noreferrer nofollow ugc">Python: How to inject code into a Python tag?</a>:</p>
<blockquote>
<p dir="auto">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</p>
</blockquote>
<p dir="auto">This is correct, if you want the ability you can save the file somewhere and just read it like so on the fly:</p>
<pre><code>import os

def get_python_code():
    s = ""
    
    # Get the path of the current directory, requiere the current script to be saved
    currentDirectory = os.path.dirname(__file__)
    
    # Retrieve the file path of a file called "other_file.py" located in the same directory as this one
    otherFileFullPath = os.path.join(currentDirectory, "other_file.py")
    
    # Read the file
    with open(otherFileFullPath) as file:
        s = file.read()
        
    return s

def add_python_tag():
    if not isinstance(obj, c4d.BaseObject):
        return None

    python_tag = obj.MakeTag(c4d.Tpython)
    python_tag[c4d.TPYTHON_CODE] = get_python_code()

    return python_tag

if __name__ == '__main__':
    add_python_tag()
</code></pre>
<p dir="auto">Cheers,<br />
Maxime.</p>
]]></description><link>http://developers.maxon.net/forum/post/73385</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73385</guid><dc:creator><![CDATA[m_adam]]></dc:creator><pubDate>Wed, 20 Dec 2023 09:53:37 GMT</pubDate></item><item><title><![CDATA[Reply to Python: How to inject code into a Python tag? on Wed, 20 Dec 2023 07:22:22 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/gaschka">@<bdi>gaschka</bdi></a></p>
<p dir="auto">Nothing really confusing in your code, python is not very strict...</p>
<p dir="auto">I can only suggest to move out constants of the function scope,<br />
and there are plenty space to add verifications...</p>
<p dir="auto">And <code>c4d.EventAdd()</code> might be executed just once, in the end of the parent function where you creating objects and attaching tags<br />
Also don't forget to use document's <code>StartUndo()</code> <code>AddUndo()</code> <code>EndUndo()</code> methods when creating your objects.</p>
<pre><code>SOME_PYTHON_TAG_CODE = """
import c4d

def main():
    # my code for the Python tag

# Execution
if __name__ == '__main__':
    main()
"""

def add_python_tag(obj):
    if not isinstance(obj, c4d.BaseObject):
        return None

    python_tag = obj.MakeTag(c4d.Tpython)
    python_tag[c4d.TPYTHON_CODE] = SOME_PYTHON_TAG_CODE

    return python_tag
</code></pre>
]]></description><link>http://developers.maxon.net/forum/post/73380</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73380</guid><dc:creator><![CDATA[baca]]></dc:creator><pubDate>Wed, 20 Dec 2023 07:22:22 GMT</pubDate></item></channel></rss>