• Set VERTEXCOLOR in PaintTool

    Cinema 4D SDK python r25 windows
    3
    0 Votes
    3 Posts
    125 Views
    R
    @m_adam Thank you so much for the help, that works perfectly and thank you for showing me both methods, that's very helpful and appreciated!
  • 0 Votes
    4 Posts
    682 Views
    ferdinandF
    Hey @lionlion44, Well, doing what you want to do is only halfway possible. An object being soloed just means setting the flag EHIDE on all othe objects. And while there is OHIDE which can be used to hide scene elements in managers, e.g., an object in the Object Manager, it is not being used by the 'Set as Root/Path Bar'-function of the Object Manager. So, you cannot hook into that. What you can do is just write a simple script which operates both flags for you. But to make this air-tight, you will have to implement a plugin (to avoid accidentally saving a scene with hidden elements). Cheers, Ferdinand Result vid.mp4 Code """Demonstrates how to set the visibility of objects in the viewport and the managers. - When CTRL is not pressed while the script is invoked, the visibility state of the selected objects is set, i.e., everything that is not selected will be hidden. - When CTRL is pressed, the visibility state of the selected objects is cleared. - When HIDE_DESCENDANTS_OF_SELECTED_OBJECTS is set to True, the descendants of selected objects will be considered selected as well. WARNING: The visibility state is written PERMANENTLY into the scene graph, i.e., when one hides objects and saves the scene, the visibility state will be saved as well. One can of course just run the script while pressing CTRL to clear the visibility state of such saved and then loaded back scene, but this could possibly brick scenes for other users. To make this air-tight, one would have to implement a plugin which handles un-hiding objects before a scene is being saved (which is not super trivial in Python atm). Use this script at your own risk. """ __author__ = "Ferdinand Hoppe" __copyright__ = "Copyright 2025, Maxon Computer GmbH" import c4d import mxutils doc: c4d.documents.BaseDocument # The currently active document. op: c4d.BaseObject | None # The primary selected object in `doc`. Can be `None`. # Wether to hide objects that are descendants of selected objects. I.e., when you have A-> B -> C, # and A is selected, B and C will be considered selected as well. HIDE_DESCENDANTS_OF_SELECTED_OBJECTS: bool = True def IsSelected(node: c4d.BaseObject) -> bool: """Returns if #node is selected or if any of its predecessors are selected (when selecting descendants implicitly is enabled). """ while node: if node.GetBit(c4d.BIT_ACTIVE): return True if not HIDE_DESCENDANTS_OF_SELECTED_OBJECTS: break node = node.GetUp() return False def SetObjectVisibility(doc: c4d.documents.BaseDocument, clearSate: bool = False) -> None: """Sets the visibility of the object in the managers and the viewport. """ for node in mxutils.IterateTree(doc.GetFirstObject(), True, True, True): isSelected: bool = clearSate or IsSelected(node) node.ChangeNBit(c4d.NBIT_OHIDE, c4d.NBITCONTROL_CLEAR if isSelected else c4d.NBITCONTROL_SET) node.ChangeNBit(c4d.NBIT_EHIDE, c4d.NBITCONTROL_CLEAR if isSelected else c4d.NBITCONTROL_SET) def main() -> None: """Called by Cinema 4D whhen the """ if not op: c4d.gui.MessageDialog("No object selected.") return state: c4d.BaseContainer = c4d.BaseContainer() if not c4d.gui.GetInputState(c4d.BFM_INPUT_MOUSE, 0, state): raise RuntimeError("Failed to get input state") ctrlIsPressed: bool = state[c4d.BFM_INPUT_QUALIFIER] & c4d.QUALIFIER_CTRL # If Ctrl is pressed, clear the visibility state, otherwise set it. SetObjectVisibility(doc, True if ctrlIsPressed else False) c4d.EventAdd() if __name__ == '__main__': main()
  • 0 Votes
    4 Posts
    904 Views
    ferdinandF
    Hey @Fabio-B, Please note that we cannot provide support for AI generated code. See Support Procedures: Scope of Support for details. When you want the clones to move, or in other words to be animated over time, you will need a noise and cannot use hashes or random values, as they are not interpolated, i.e., continous. """Realizes an effector that attracts MoGraph particles spherically around its origin. Add the example to a Matrix object to understand its effect. In Full Control mode we can realize a true attraction force as we have full control over the particle values. Compare this example to Parameter Control mode to understand the difference. """ import c4d import mxutils op: c4d.BaseObject # The Python Effector object containing this code. gen: c4d.BaseObject # The MoGraph Generator executing `op`. doc: c4d.documents.BaseDocument # The document `op` and `gen`are contained in. def main() -> bool: """Called by Cinema 4D to evaluate the effector. """ # Get the particle data for the effector #op. Get out when either the data cannot be retrieved. data: c4d.modules.mograph.MoData = c4d.modules.mograph.GeGetMoData(op) if data is None: return 1.0 # Get the matrices of the particles. This is the array we will modify. matrices: list[c4d.Matrix] = data.GetArray(c4d.MODATA_MATRIX) # For each particle write a new persistent random height value, hashed over the index of the # particle. This will cause the height each time to be the same, as long as the index of the # particle does not change. One could also hash the position of the original particle to get a # height value that is not dependent on the index of the particle (e.g., when the user changes # the order of the particles in the Matrix object). for i in range(data.GetCount()): pos: c4d.Vector = matrices[i].off y: float = c4d.utils.noise.Noise(matrices[i].off, doc.GetTime().Get()) * 25.0 matrices[i].off = c4d.Vector(pos.x, y, pos.z) # Hash a height value over the position of the particle. # y: float = mxutils.g_random.HashVectorToNumber(matrices[i].off) * 25.0 # Write the new data back. data.SetArray(c4d.MODATA_MATRIX, matrices, op[c4d.FIELDS].HasContent()) return True Recording 2025-05-02 141611.mp4 Cheers, Ferdinand
  • New Condition Naming in Attribute Manager

    Cinema 4D SDK c++ r25
    3
    1
    0 Votes
    3 Posts
    527 Views
    D
    Thank you so much! I couldn't ask for a more thorough response. Dan
  • why my interface is sealed in maxon::Result?

    Cinema 4D SDK r25 c++
    4
    0 Votes
    4 Posts
    740 Views
    Y
    @ferdinand cool. this really helpful!
  • 0 Votes
    2 Posts
    1k Views
    ferdinandF
    Hey @moghurt, Thank you for reaching out to us. I tried using the pyp plugin to execute Python scripts when starting the software to modify the project file, but when I use the '-render' parameter, the Python code in the pyp plugin is executed after the rendering is complete. I can only speculate as to what exactly you have been doing here, as you do not provide any details or code. Please have a look at Support Procedures: Asking Questions for how to ask good technical questions. The general workflow for what you want to do here (given that one really wants to follow the plugin route) would be to implement PluginMessage for your plugin, and then try to run your code from there at a fitting point of time in the startup sequence of Cinema 4D. And while I would not rule out that -render has an impact on the startup sequence of Cinema 4D, the general claim that your Python code only runs after the rendering seems a bit far fetched. Because for events such as C4DPL_INIT_SYS and C4DPL_INIT modules and plugins have not been loaded yet, i.e., rendering is literally impossible at these points. The plugin approach also assumes that you can inject without problems alien command line arguments into a Cinema 4D app call, e.g., \CINEMA 4D.exe"-nogui -render "render.c4d" -script "myscript.py" with -script being the alien argument. I am not quite sure that this would work, I would have to try myself. What also is problematic, is that you are likely not able to get hold of the render document on this route. The active document will be the empty document, not the render document, i.e., you cannot get hold of the BaseDocument for render.c4d. And you cannot just open and modify the document yourself, as Cinema might have already a lock on that file or is even already loading it. All this makes using c4dpy instead much more attractive as you won't have these problems there. It is also the intended solution for what you want to do here: Modify one or many documents without a GUI in a fire and forget manner. A while ago we had the topic Extending the Command Line with Python where I showed a workflow for how to treat c4dpy as a Script Manager environment. The thread is four years old, written in Python 2 syntax (you will have to convert to Python 3 syntax), and I was then not yet a Maxon employee but a student. So, you might run into some issues. But the principle is definitively reproduceable and I also do not see anything in my old code which seems like a really bad idea. Your workflow could then look like this for doing the pre and post rendering events (as a bat, ps1, sh, or command file and assuming that you do not want to overwrite files): c4dpy script_manager_environment.py -script my_script.py -in my_doc.c4d -out my_doc_pre.c4d CommandLine -my_doc_pre.c4d ... c4dpy script_manager_environment.py -script my_script.py -in my_doc_pre.c4d -out my_doc_post.c4d Cheers, Ferdinand
  • 0 Votes
    7 Posts
    1k Views
    S
    Yes, I know about not using multiple events. I was playing with that, I thought that's the reason the button was not being called Thank you @ferdinand
  • Negative Vertex Map values

    Moved General Talk python r25
    3
    0 Votes
    3 Posts
    993 Views
    P
    hey, found the error. works exactly as i had planned. my code was correct, but i forgot to switch off use fields on the new vertex i mapped. thanks for the help anyway! [image: 1717679020556-4761c896-9141-42d3-8872-df429c5441df-grafik.png]
  • Error compiling R25 framework on MacOS

    Cinema 4D SDK r25 macos
    3
    0 Votes
    3 Posts
    625 Views
    B
    Thank you, @ferdinand. I modified sourceprocessor.py for now, and it works.
  • GetCache Does Not Work Inside the Python Generator

    Cinema 4D SDK r21 r25 python
    10
    0 Votes
    10 Posts
    2k Views
    ferdinandF
    Hey @bentraje, I am glad that you found your solution, and yes, you are absolutely right, it should be GeGetCurrentThread . I fixed that in my pseudo-code. Cheers, Ferdiand
  • 0 Votes
    4 Posts
    1k Views
    B
    @ferdinand yea python is much more forgiving than C++. i was just wondering if there was an already existing API for such task.
  • 0 Votes
    4 Posts
    982 Views
    ferdinandF
    Good to hear!
  • 0 Votes
    8 Posts
    2k Views
    C
    @i_mazlov , Do you speak Russian?
  • 0 Votes
    4 Posts
    936 Views
    ferdinandF
    Hey @GordOrb, This is not possible. You are not really meant to distinguish the file names you are writing for. I also do not understand the purpose of doing what you want to do. You can already name the beauty file and the multipass file differently in the UI. And when you write out each pass on its own, Cinema 4D will already name them differently for you. There will be singular token call for all pass layers, and before that a call for the RGB/beauty layer. None of the calls for naming the file contains pass information. The inner logic of it seems to be that you first get the call for evaluating the token for a filename and after that the actual passes. The purpose of some fields in the passed data is probably more of internal than public nature, since you cannot really do much with that info due to how the token calls are coming in. In some contexts these fields might be useful when you use the Token System (C++ Manual) more manually. But at least for the passes case, this is not really true either, because when you start to render manually, you will have to fill in the pass data on your own when constructing a RenderPathData. So, long story short: this is not really possible, at least in the intended way. Upon having a closer look at the time stamps and my file output, I realized that you could try to rely on the rule 'the call after the call for the RGB pass', because that is at least how it works in my example. isMultipass: '1', pname: ' ', ptype: ' ', pid: ' -1', t: 9366346949848 // First call, irrelevant isMultipass: '1', pname: ' ', ptype: ' ', pid: ' -1', t: 9367113676312 // Time stamp of saved RGB/beauty. isMultipass: '1', pname: ' RGB', ptype: ' RGB', pid: ' -1', t: 9367113987757 // RGB/beauty pass isMultipass: '1', pname: ' ', ptype: ' ', pid: ' -1', t: 9367114252401 // Time stamp of all passes. ... If this works, this would be hack, and it would be then up to you establish if that holds true or not. To establish "what is the last call", you could have some global list into which you push things. Make sure not to store any Cinema 4D rendering data in that global list (BaseDocument, BaseContainer, RenderData) or you might end up in world of hurt. Cheers, Ferdinand Written Files: [image: 1707477127170-fc970227-90fb-437e-b5d7-b7a25a9e2da0-image-resized.png] My console dump, here I search for the time stamp 9367114252401 of my files, it is the fourth call, long before we get the calls for the passes. The only thing that comes before is a call for the RGB, i. e., the beauty pass. [image: 1707477097394-b5e92449-2ffa-4d4b-a0b7-de02968635d8-image-resized.png] Code: """ """ import c4d import mxutils import time def get_render_type(data: c4d.BaseContainer) -> str: """Dumps information about #data into the token and the console. """ # Get the render document #rdoc, and build a string for the UUID of rdoc as well as its memory # location and #data. Unsurprisingly, the mem locs are meaningless here in Python as everything # must be wrapped by Python objects in each call. In C++ you might be able to establish "sameness" # between calls by the location of #data (the rendering document which is used is always the # same as shown by its UUID). But I doubt that you will have luck with #data in C++, because it # is likely constructed for each call there too (and not reused). rdoc: c4d.documents.BaseDocument = mxutils.CheckType(data[0], c4d.documents.BaseDocument) uuid: bytes = bytes(rdoc.FindUniqueID(c4d.MAXON_CREATOR_ID)) ident: str = f"doc-uuid: {uuid}, doc-mem: {id(rdoc)}, bc-mem: {id(data)}" # Build the token. token: str = (f"isMultipass: '{rdoc.GetActiveRenderData()[c4d.RDATA_MULTIPASS_ENABLE]}', " f"pname: '{data[5]:>20}', ptype: '{data[6]:>10}', pid: '{data[7]:>3}', t: {time.perf_counter_ns()}") print (ident, token) return token if __name__ == "__main__": if "r_type" not in set(item["_token"] for item in c4d.modules.tokensystem.GetAllTokenEntries()): c4d.plugins.RegisterToken("r_type", "[Render] Type", "reg/multi", get_render_type)
  • Crash from processing too many xrefs.

    Cinema 4D SDK r25 python macos
    6
    0 Votes
    6 Posts
    1k Views
    ferdinandF
    Hey @Visualride-0, 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. 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. 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 fileList. Cinema 4D should have caught your mistake there and prevented you from opening the same file twice, but I have not tested it. 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) 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 fileList are all *.c4d files, right? 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. 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 sdk_support(at)maxon(dot)net. Mail attachments will not work here due to the 50MB limitation. 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. Cheers, Ferdinand
  • 0 Votes
    3 Posts
    792 Views
    D
    Hi Maxime, thank you very much for your answer! I wasn't sure if storing things in the global document BaseContainer was a good approach, but it's working well. Cheers, Daniel
  • how to program plugins with python?

    Cinema 4D SDK r25 python
    20
    0 Votes
    20 Posts
    6k Views
    love_me_renderL
    Hi, thank you for the tips. Here is my C++ -treegenerator-plugin, written for R25: http://www.klausfilm.bplaced.net/TreeGenerator/Website_Treegenerator.html Cheers Klaus
  • Error building R25 macOS - TypeTraitExpr

    Cinema 4D SDK r25 c++ macos
    7
    1
    0 Votes
    7 Posts
    1k Views
    rsodreR
    Thanks for all the tips @ferdinand The solution was Catalina + XCode 12.4
  • Simulating mouse movement in C4D

    Cinema 4D SDK python r25 windows
    5
    0 Votes
    5 Posts
    3k Views
    D
    Hi @ferdinand, Thank you so much for the comprehensive reply! Given all this I don't think it would make sense to keep trying to unit test this particular part of our code with Python. The last approach you highlighted with testing directly on the C++ side seems like the best option, so I'll give that a try. I'll have to see how to integrate this test with the rest of the unit tests (we have a lot and they're all in Python) but hopefully that won't be too much of a problem. Thanks again for all the help, Daniel
  • Can't snap to Generator output spline

    Cinema 4D SDK r25 python windows
    4
    0 Votes
    4 Posts
    762 Views
    ferdinandF
    Hey @Madara, in short, a Python generator object is not meant to generate splines. You can collapse the cache of your constructed spline and return its LineObject cache instead of the spline itself. For an example, see [1]. In your case that would be connect_obj. This will work in the sense that the generator cache will be then like a SplineObject cache and when you 'Current State to Object' your generator, it will behave like a spline and give you the interpolated editable linear spline and not a spline generator. However, spline snapping not only inspects caches, but also checks if OBJECT_ISSPLINE is true. So, you cannot bamboozle it by faking a SplineObject cache. You will have to implement a proper spline plugin for what you want to do, as you can overwrite there GetContour Cheers, Ferdinand PS: Your code contains multiple elements that can crash Cinema 4D and/or corrupt the loaded document. Specifically, these sections: if link_clone: #Parametric object pobj = u.SendModelingCommand( command = c4d.MCOMMAND_CURRENTSTATETOOBJECT, list = [connect_obj], mode = c4d.MODELINGCOMMANDMODE_ALL, doc = op.GetMain()) connect_obj = pobj[0] ... offspline = u.SendModelingCommand( c4d.MCOMMAND_SPLINE_CREATEOUTLINE, [connect_obj], c4d.MODELINGCOMMANDMODE_ALL, bc=settings, doc=doc) A Python generator's main() function is subject to the threading restrictions of Cinema 4D because it is just a wrapper for ObjectData.GetVirtualObjects. Executing SendModelingCommand in a threaded context is fine, but you cannot do it on the active document (both doc and op.GetDocument() are the active document). This is here even worsened by the fact that that the objects in list are not actually part of the document you pass as doc. To circumvent that you must create your own document just as I have in [1] with temp. Find a more detailed example of that technique in smc_extrude_s26.py. [1] import c4d op: c4d.BaseObject # The Python generator object. def main() -> c4d.BaseObject: """ """ # A user data Boolean to toggle collapsing the cache of this generator. collapse: bool = op[c4d.ID_USERDATA, 1] spline: c4d.BaseObject = c4d.BaseObject(c4d.Osplinecircle) if not spline: return c4d.BaseObject(c4d.Onull) # Just return the circle spline itself when #collapse is false. if not collapse: return spline # Otherwise grab the LineObject cache of the spline and return a copy of it. temp: c4d.documents.BaseDocument = c4d.documents.BaseDocument() temp.InsertObject(spline) temp.ExecutePasses(c4d.threading.GeGetCurrentThread(), False, False, True, c4d.BUILDFLAGS_NONE) cache: c4d.LineObject | None = spline.GetCache() if not isinstance(cache, c4d.LineObject): return c4d.BaseObject(c4d.Onull) return cache.GetClone(c4d.COPYFLAGS_NONE)