<?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[Crash from processing too many xrefs.]]></title><description><![CDATA[<p dir="auto">My plugin searches for and replaces strings of text in xrefs. It first gathers into a list all xref c4d files from a designated directory tree. If the directory contains more than a hundred or so legacy xrefs, which have several embedded xrefs, the plugin crashes.<br />
Two questions:</p>
<ol>
<li>Based on my supplied code, is there a way to optimize what I have to avoid crashes?</li>
<li>Is there a way to flush the cache after every directory of files processed? I've tried cache_clear() with no luck.<br />
Here are, hopefully, the relevant parts of my code:</li>
</ol>
<pre><code>def GetVirtualObjects(self, op, hierarchyhelp):
        dirty = op.CheckCache(hierarchyhelp) or op.IsDirty(c4d.DIRTY_DATA)
        if dirty is False:
            return op.GetCache(hierarchyhelp)

        baseNull = c4d.BaseObject(c4d.Onull)
        pluginNode = c4d.BaseObject(c4d.Onull)
        pluginNode.InsertUnder(baseNull)

        c4d.EventAdd()
        return baseNull

def Process_Process(self, op, rootDir, findTextList, replaceTextList, simulateFolderOnOff, simulateFolder, logFile_OnOff, logFileFolder):
        filesList = []
        now = datetime.now()
        logReport = ("Date: " + now.strftime("%m-%d-%Y") + '\n' + "Time: " + now.strftime("%H:%M:%S") + '\n' + '\n')

        for root, subFolders, files in os.walk(rootDir):
            for file in files:
                if file[-4:] == ".c4d":
                    pathWithFile = root + "/" + file
                    filesList.append(pathWithFile)

        changed = False
        for fixFile in filesList:
            c4d.documents.LoadFile(fixFile)
            doc = c4d.documents.GetActiveDocument()
            c4d.documents.InsertBaseDocument(doc)
            c4d.documents.SetActiveDocument(doc)
            logFile = True

            xList = []
            Selected = self.get_all_objects(doc.GetFirstObject(), [])
            for ListOb in Selected:
                theType = ListOb.GetTypeName()
                if theType == "Legacy XRef" or theType == "Simple XRef":
                    if ListOb[c4d.SCENEINSTANCE_FILENAME] != None:
                        xList.append(ListOb) #Build xref list
            for y in xList:
                xrefNameAndPath = y[c4d.SCENEINSTANCE_FILENAME]
                originalXrefNameAndPath = xrefNameAndPath
                for x in range(10):
                    if findTextList[x] != "":
                        finalXrefNameAndPath = self.ireplace(findTextList[x], replaceTextList[x], xrefNameAndPath)
                        if xrefNameAndPath != finalXrefNameAndPath:
                            y[c4d.SCENEINSTANCE_FILENAME] = finalXrefNameAndPath
                            if logFile == True:
                                logReport = (logReport + "File: " + fixFile + '\n')
                            logFile = False
                            xrefNameAndPath = finalXrefNameAndPath
                            changed = True
                if changed == True:
                    logReport = (logReport + "Original Xref Name: " + originalXrefNameAndPath + '\n' + "New Xref Name: " + finalXrefNameAndPath + '\n')

            if changed == True:
                logReport = (logReport + '\n')
                if simulateFolderOnOff == False:
                    c4d.documents.SaveDocument(doc, fixFile, c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)
                if simulateFolderOnOff == True:
                    theFileName = self.get_name(fixFile)
                    simFolder_name = (simulateFolder + '/' + theFileName)
                    c4d.documents.SaveDocument(doc, simFolder_name, c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)

            changed = False
            c4d.documents.KillDocument(doc)

        return logReport
</code></pre>
]]></description><link>http://developers.maxon.net/forum/topic/15351/crash-from-processing-too-many-xrefs</link><generator>RSS for Node</generator><lastBuildDate>Thu, 12 Nov 2026 07:30:53 GMT</lastBuildDate><atom:link href="http://developers.maxon.net/forum/topic/15351.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 Jan 2024 20:11:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Crash from processing too many xrefs. on Fri, 09 Feb 2024 09:44:50 GMT]]></title><description><![CDATA[<p dir="auto">Hey <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/visualride-0">@<bdi>Visualride-0</bdi></a>,</p>
<p dir="auto">you seem to run into some form of memory leak. That you open that many files in a row on one Cinema 4D instance is of course not really intended. This could either be a l smaller leak in our API that simply manifests more or only at all when you load that many files.</p>
<p dir="auto">Generally, you could separate out the file iteration into an external script, a shell script or a vanilla Python script if you want, and then simply run c4dpy with your script per file or for batches of 10 or 100 files. Starting and shutting down c4dpy 8000 times in a row will of course take its toll in time, so one would be incentivized to bundle up more files to bring down that number of starts and shutdowns.</p>
<p dir="auto">I had a look at your code, and apart from your redundant document insertion, I do not see much wrong with it, as it is basically just the loop on <code>fileList</code>. Cinema 4D should have caught your mistake there and prevented you from opening the same file twice, but I have not tested it.</p>
<pre><code class="language-py">for f in filesList:
    # This line will load #f into a BaseDocument, insert it into the opened files, and make it the
    # active document. Unlike for BaseDocument.LoadDocument, the following lines are therefore 
    # redundant.
    c4d.documents.LoadFile(f)
    # doc = c4d.documents.GetActiveDocument()
    # c4d.documents.InsertBaseDocument(doc)
    # c4d.documents.SetActiveDocument(doc)
    c4d.documents.SaveDocument(c4d.documents.GetActiveDocument(), f, c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)
    # This does what it should do, close the active document, and create a new one. But to be
    # ultra safe, you could also call c4d.documents.CloseAllDocuments().
    c4d.documents.KillDocument(doc)
</code></pre>
<p dir="auto">What escapes mea bit too, is the purpose of what we are doing here, since we just overwrite files with itself. But I guess this is minimized code, so it is only meant to demonstrate the problem. For my own understanding, the files in <code>fileList</code> are all <code>*.c4d</code> files, right?</p>
<p dir="auto">Fixing your code as shown above will likely not fix the problem, it seems more likely to me that there is a memory leak somewhere. For an intermediate solution, you will probably have to do what I proposed above, split this into batches of Cinema 4D or c4dpy instances to flush your memory by shutting down Cinema 4D.</p>
<p dir="auto">But we would like to have a look at the problem. Could you provide a sample of the documents, something between 10 or 100 files, so that we can test it with your data? You can send me a cloud storage link via a PM on the forum or via a mail to <code>sdk_support(at)maxon(dot)net</code>. Mail attachments will not work here due to the 50MB limitation.</p>
<p dir="auto">We strive for Cinema 4D being memory leak free, and if there is a severe leak we are not aware of, we should fix it. But this might take time.</p>
<p dir="auto">Cheers,<br />
Ferdinand</p>
]]></description><link>http://developers.maxon.net/forum/post/73728</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73728</guid><dc:creator><![CDATA[ferdinand]]></dc:creator><pubDate>Fri, 09 Feb 2024 09:44:50 GMT</pubDate></item><item><title><![CDATA[Reply to Crash from processing too many xrefs. on Thu, 08 Feb 2024 18:22:58 GMT]]></title><description><![CDATA[<p dir="auto">8000 files.  Average about 4mb.  The total is about 29gb.</p>
]]></description><link>http://developers.maxon.net/forum/post/73726</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73726</guid><dc:creator><![CDATA[Visualride 0]]></dc:creator><pubDate>Thu, 08 Feb 2024 18:22:58 GMT</pubDate></item><item><title><![CDATA[Reply to Crash from processing too many xrefs. on Thu, 08 Feb 2024 17:49:35 GMT]]></title><description><![CDATA[<p dir="auto">Hey <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/visualride-0">@<bdi>Visualride-0</bdi></a>,</p>
<p dir="auto">how many files are in your file list and how heavy is the average file in that list? Very rough ballpark numbers are enough.</p>
<p dir="auto">Cheers,<br />
Ferdinand</p>
]]></description><link>http://developers.maxon.net/forum/post/73724</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73724</guid><dc:creator><![CDATA[ferdinand]]></dc:creator><pubDate>Thu, 08 Feb 2024 17:49:35 GMT</pubDate></item><item><title><![CDATA[Reply to Crash from processing too many xrefs. on Thu, 08 Feb 2024 16:49:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/ferdinand">@<bdi>ferdinand</bdi></a><br />
Thank you for your response and suggestions.</p>
<p dir="auto">I created a script version of my plugin, but it still crashed after running out of memory.  Here it is in the bare bones version paired down to simply loading all the files into a list, opening them and closing the file.  My included screenshot shows the memory load increasing till I run out of memory.  How do I avoid this?</p>
<pre><code>import c4d, math, string
from c4d import gui, plugins, utils, bitmaps
import os


# Main function
def main():
    
    filesList = []
    initialPath = "/Volumes/Argo3D/"
    rootDir = c4d.storage.LoadDialog(c4d.FILESELECTTYPE_ANYTHING, "***** -PLEASE CHOOSE THE DIRECTORY ROOT- *****", c4d.FILESELECT_DIRECTORY, "", initialPath) + "/"

    findText = c4d.gui.InputDialog("Enter the text to find (To replace)", "")
    if findText == "":
        c4d.gui.MessageDialog("No FIND text entered")
        return
    replaceText = c4d.gui.InputDialog("Enter the text to replace the found text", "")
    if replaceText == "":
        c4d.gui.MessageDialog("No REPLACE text entered")
        return

    for root, subFolders, files in os.walk(rootDir):
        for file in files:
            if file[-4:] == ".c4d":
                pathWithFile = root + "/" + file
                filesList.append(pathWithFile)

    for fixFile in filesList:
        changed = False
        c4d.documents.LoadFile(fixFile)
        doc = c4d.documents.GetActiveDocument()
        c4d.documents.InsertBaseDocument(doc)
        c4d.documents.SetActiveDocument(doc)
        c4d.documents.SaveDocument(doc, fixFile, c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)
        c4d.documents.KillDocument(doc)
    c4d.gui.MessageDialog("Done!")


    c4d.EventAdd()

# Execute main()
if __name__=='__main__':
    main()
</code></pre>
<p dir="auto"><img src="/forum/assets/uploads/files/1707410873282-screenshot-2024-02-08-at-8.12.35-am.png" alt="Screenshot 2024-02-08 at 8.12.35 AM.png" class=" img-fluid img-markdown" /></p>
]]></description><link>http://developers.maxon.net/forum/post/73723</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73723</guid><dc:creator><![CDATA[Visualride 0]]></dc:creator><pubDate>Thu, 08 Feb 2024 16:49:04 GMT</pubDate></item><item><title><![CDATA[Reply to Crash from processing too many xrefs. on Mon, 29 Jan 2024 09:58:10 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/visualride-0">@<bdi>Visualride-0</bdi></a>,</p>
<p dir="auto">thank you for reaching out to us. I doubt that a high number of Xrefs are the cause of your problem,it is more likely just a symptom of your problems that they manifest strongly with many objects present.</p>
<h4>Situation</h4>
<p dir="auto">The code you show is here unfortunately does not provide much insight in what you are doing and you forgot to mention quite a bit of key information. Please have a look at  <a href="https://developers.maxon.net/forum/topic/15244/support-procedures" target="_blank" rel="noopener noreferrer nofollow ugc">Support Procedures: Reporting Bugs and Crashes</a> for how to do this better in the future.</p>
<ol>
<li>I am assuming you are writing some kind of <code>NodeData</code> plugin, likely an <code>ObjectData</code> plugin, because you show us here <code>GetVirtualObjects</code> (GVO).</li>
<li>You do not tell or show us in which context your method <code>Process_Process</code> is called, I am going to assume that you are doing this off-main-thread, e.g., in GVO.</li>
</ol>
<h4>Problems</h4>
<ol>
<li>The main problem seems to be that you are violating <a href="https://developers.maxon.net/docs/py/2024_2_0/manuals/manual_threading.html#threading-information" target="_blank" rel="noopener noreferrer nofollow ugc">threading restrictions</a>, as you seem to call your <code>Process_Process</code> off-main-thread (OMT). And while the docs say that changing parameters is allowed but not recommended OMT, I would heavily advise against changing <code>SCENEINSTANCE_FILENAME</code> in Xrefs OMT, as this implies loading a new file which entails a lot of not thread safe things. Inserting documents, setting the active document, and saving a scene are also extreme violations of thread safety.</li>
<li>If you want a node (an object, a tag, a shader, a scene hook, etc.) which modifies the active scene, you will need a button (click) or a message in whose context you will then run your non-thread-safe code (both will result in a message being sent to your node). And while <code>NodeMessage.Message</code> usually does run on the main thread, you will still have to check with <a href="https://developers.maxon.net/docs/py/2024_2_0/modules/c4d.threading/index.html#c4d.threading.GeIsMainThread" target="_blank" rel="noopener noreferrer nofollow ugc">GeIsMainThread</a> before you are doing anything non-threadsafe. Methods like <code>ObjectData.GetVirtualObjects</code> or <code>TagData.Execute</code> will never run on the main thread.</li>
<li>You also seem to reinvent a bit the wheel with finding your Xrefs, the natural way to find scene assets would be <a href="https://developers.maxon.net/docs/py/2024_2_0/modules/c4d.documents/index.html?highlight=allassets#c4d.documents.GetAllAssetsNew" target="_blank" rel="noopener noreferrer nofollow ugc">c4d.documents.GetAllAssetsNew</a>. In Python we do not have the bits to replace assets, as we do not have handling for the actual message <code>MSG_GETALLASSETS</code> as we do in C++. But you could at least streamline the getting Xrefs part.</li>
<li>Please excuse me being blunt here, but your code in <code>Process_Process</code> is quite a bit convoluted which makes it hard to read and debug.
<ul>
<li>Remove clutter like logging until you have working code. Python's file IO is also not thread safe.</li>
<li>Try to split your code into logical blocks , there is at least the scene traversal/Xrefs gathering part, there is the parameter writing part, and there is the scene saving part. It will help you to identify problems with your code.</li>
<li>Give variables, attributes, and functions a meaningful name, the list <code>xList</code>, the loop variables <code>x</code> and <code>y</code>, and many more are violations of that.</li>
<li><code>pathWithFile = root + "/" + file</code> is not a valid way to join paths. Use <code>os.path.join(a, b, c, d, ...)</code> to join two or many path elements in a safe manner for all OS.</li>
</ul>
</li>
</ol>
<blockquote>
<p dir="auto">Is there a way to flush the cache after every directory of files processed? I've tried cache_clear() with no luck.</p>
</blockquote>
<p dir="auto">I do not understand what you are talking about. Are you talking about the cache of an geometry object in a scene? We also do not have any method called <code>cache_clear</code>, as we do not use underscores in function names because we use C++ naming conventions in Python.</p>
<p dir="auto">You cannot flush object geometry caches in Cinema 4D (you can but you really should not unless you really know what you are doing). What you can do is mark an object as dirty with <a href="https://developers.maxon.net/docs/py/2024_2_0/modules/c4d/C4DAtom/index.html#C4DAtom.SetDirty" target="_blank" rel="noopener noreferrer nofollow ugc">C4DAtom.SetDirty</a> and manually invoke the caches for all dirty elements in a scene being rebuilt with <a href="https://developers.maxon.net/docs/py/2024_2_0/modules/c4d.documents/BaseDocument/index.html#BaseDocument.ExecutePasses" target="_blank" rel="noopener noreferrer nofollow ugc">BaseDocument.ExecutePasses</a>. <strong>You should be VERY careful with nodes flagging themselves as dirty</strong>, as this can easily lead to crashes. Manually having to rebuild caches in the context of normal execution of a loaded scene is usually not necessary and a sign of an incorrectly designed plugin.</p>
<p dir="auto">Cheers,<br />
Ferdinand</p>
]]></description><link>http://developers.maxon.net/forum/post/73629</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/73629</guid><dc:creator><![CDATA[ferdinand]]></dc:creator><pubDate>Mon, 29 Jan 2024 09:58:10 GMT</pubDate></item></channel></rss>