Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Script that converts Redshift materials back to C4D standard ones

    General Talk
    3
    21
    23.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ferdinandF
      ferdinand @Fremox
      last edited by ferdinand

      Hey @Fremox,

      AttributeError: module 'c4d' has no attribute 'Mrsgraph'

      That is because I have written this code for Cinema 4D 2023.2+. We introduced quite a batch of symbols with 2023.0. I assume you are trying to run the script with an older version?

      You can quite simply rectify this by defining the symbol yourself or by replacing all occurrences with the numeric value. E.g., insert this:

      # [...]
      import c4d
      import typing
      
      from c4d.modules.graphview import *
      
      #The line to add ...
      c4d.Mrsgraph: int = 1036224
      # [...]
      

      But I also used something else in this script which won't run in older versions of Cinema 4D, Python 3.10 style union type hints:

      layer: c4d.ReflectionLayer | None = material.GetReflectionLayerIndex(i)
      

      This means layer is of type c4d.ReflectionLayer or None. Older Python versions will have no clue what to do with that, simply remove the optional types then:

      layer: c4d.ReflectionLayer = material.GetReflectionLayerIndex(i)
      
      # Or this ...
      foo: a | b | c | d = bar()
      
      # will become that ...
      foo: a = bar()
      

      You could of course also use typing.Union if you want to retain that additional information. But type hinting is purely cosmetic and for the benefit of the reader, more fringe applications such as runtime assertions and linting aside.

      Apart from that, I think I did not use anything particularly "modern" in this code.

      PS: We do not track change notes on all documentation units at the moment, the only way to see that information for symbols is to look into the 2023.0 change notes. For stuff like functions and classes, there is usually a note in the documentation unit itself, e.g., "since 2023.0". You can also just put the docs to the version you are using and then search for the symbol. When it is not there, it is not contained in your version 😉

      Cheers,
      Ferdinand

      MAXON SDK Specialist
      developers.maxon.net

      1 Reply Last reply Reply Quote 1
      • FremoxF
        Fremox
        last edited by

        you are right, I tested it in R25 in the first place and that's why it didn't work!

        Now that I've tested it in 2023.2, everything works fine, thanks a lot!!

        Now I will test different things in order to easily replace RS mats by the STD ones in my scenes.
        I've changed the line 198 to

        material.SetName(f"{name}")
        

        instead of

        material.SetName(f"{name} [STD]")
        

        in order for my new STD materials names to be written just like the RS ones, in order to be able to use the Material Exchanger (which needs 2 scenes with exact same materials names to work properly), but I end up with names with ".1" suffixe at the end of the newly created materials.
        Since it seems you can't use the Naming tool on materials (I've tested it and it didn't work)
        is there a way in our srcipt to kind of "force" the newly STD materials to have the exact same name without this .1 suffixe ?

        Sorry to bother you again
        i try to wrap my head to find the best solution in the least amount of time (and your script aleardy does 99% of what I wanted so 1000 thanks)

        What a time to be alive!

        ferdinandF 1 Reply Last reply Reply Quote 0
        • ferdinandF
          ferdinand @Fremox
          last edited by ferdinand

          Hey @Fremox,

          is there a way in our srcipt to kind of "force" the newly STD materials to have the exact same name without this .1 suffixe ?

          Yes, there is. The third argument of BaseDocument.InsertMaterial allows you to turn that smarty-pants behavior of Cinema 4D off.

          Alternatively, you can just reorder the instructions, because when you set the name after the material has been inserted, you will overwrite whatever name Cinema 4D came up with.

          for name, graph in IterRsGraphGraphs(doc):
                  # Create a converted material, rename it, and insert it into the document.
                  material: c4d.BaseMaterial = ConvertMaterial(graph)
                  doc.InsertMaterial(material)
                  material.SetName(f"{name}")
          

          Cheers,
          Ferdinand

          MAXON SDK Specialist
          developers.maxon.net

          FremoxF 1 Reply Last reply Reply Quote 1
          • FremoxF
            Fremox @ferdinand
            last edited by

            @ferdinand

            Perfect, works like a charm!
            I will definitely digg the whole code in order to fully understand each instruction, so that I will be able to adapt it for my needs (for example, finding the textures for the Emission channel in RS Shaders and map it correctly to the luminance channel of the STD ones ; Shouln't be a problem since you have neatly documented all your code 🙂

            What a time to be alive!

            1 Reply Last reply Reply Quote 0
            • FremoxF
              Fremox
              last edited by

              Hey @ferdinand
              Hope you are doing well

              Continuing my script testing I realized that the code above didn't work for a colleague who works on a Mac, but I don't really understand why a Python script wouldn't work on both Windows and Mac systems, or am I missing something?
              Isn't Python working the same way in both cases?

              What a time to be alive!

              ferdinandF 1 Reply Last reply Reply Quote 0
              • ferdinandF
                ferdinand @Fremox
                last edited by ferdinand

                Hey @Fremox,

                Python script wouldn't work on both Windows and Mac systems, or am I missing something? Isn't Python working the same way in both cases?

                Well, just as for JavaScript, you can certainly write Python code that only works on one OS. But Cinema 4D Python code in general and my code from above in particular is usually OS agnostic.

                The big question would be what is not working for your colleague? I just threw the script on a simple material in macOS 2023.2, and everything worked fine:

                Screenshot 2023-06-19 at 10.04.15.png

                Please also remember our forum guidelines, we cannot design or debug applications for you. So, in short, you cannot ask us "what is going wrong?", but instead should ask "this error is raised, this Cinema 4D function/class does not do what I would expect it do: why?" To a certain extent, we will also help you with your own code, but we cannot debug applications for users ("my plugin is crashing, what is going wrong?").

                Finally, if you intend to update/modify the script, we would ask you to create new topics for technical questions as also lined out in our forum guidelines.

                Cheers,
                Ferdinand

                MAXON SDK Specialist
                developers.maxon.net

                FremoxF 1 Reply Last reply Reply Quote 0
                • FremoxF
                  Fremox @ferdinand
                  last edited by

                  @ferdinand

                  Thanks for your reply.
                  I was asking by curiosity rather than asking for a debug on your side.
                  But I get why you understood it that way, so, sorry I'll try to write my posts with that in mind in the future, sorry for the misunderstanding.

                  As Javascript developper, I never had any issue in After Effects with some code only working on one OS and not the other, this is why I got curious and I was thinking that, maybe, Python wasn't OS agnostic after all. But you answered my question so, as far as I understand there isn't any reason why it shouldn't work on my colleague workstation.

                  When he runs the same script on his computer, nothing happens at all, while, with the exact same scene, it does work properly on my PC machine.
                  So I will investigate on my side in order to see what could be wrong with his setup (he uses 2023.2 version too).

                  Anyway, thanks again for the precision

                  What a time to be alive!

                  ferdinandF 1 Reply Last reply Reply Quote 0
                  • ferdinandF
                    ferdinand @Fremox
                    last edited by

                    Hey @Fremox,

                    Well, you should look at the console output, because when something goes wrong, it will tell you that.

                    And OS-agnostic is a big term that never really applies. While Python and JS are certainly not as bad as "write-once-debug-everywhere"-Java, you can certainly produce code in both languages which does not run on all OS, e.g., use Python's os.pipe2() (does not run on Windows) or similar thing for Node.js's os module. JS is in a technical sense more OS agnostic than Python by simply not having a std lib where such problems then occur.

                    But yes, Python just as JS is more or less OS-agnostic.

                    Cheers,
                    Ferdinand

                    MAXON SDK Specialist
                    developers.maxon.net

                    FremoxF 1 Reply Last reply Reply Quote 0
                    • FremoxF
                      Fremox @ferdinand
                      last edited by

                      @ferdinand

                      I get it now, thanks for your detailed explanation!

                      What a time to be alive!

                      1 Reply Last reply Reply Quote 0
                      • ferdinandF ferdinand referenced this topic on
                      • i_mazlovI i_mazlov referenced this topic on
                      • K
                        KolinJ
                        last edited by

                        I have been reading this thread with great interest as this is a quandary I have been struggling with for some time. I have a bunch of models I created and textured with redshift but I want to export them for usage in other applications. I was hoping this script might help me with this issue but I am afraid I am fairly ignorant in the ways and usage of scripts in C4d. I tried to paste the text of the script into the C4d script manager and hit execute while a redshift textured object was open, but nothing happens. Apologies for my lack of technical skills. I'm hoping I'm missing something simple. Any input much appreciated!

                        1 Reply Last reply Reply Quote 0
                        • K
                          KolinJ
                          last edited by

                          And yes, I am running this in version 2023

                          ferdinandF 1 Reply Last reply Reply Quote 0
                          • ferdinandF
                            ferdinand @KolinJ
                            last edited by

                            Hello @KolinJ,

                            Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!

                            Getting Started

                            Before creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:

                            • Support Procedures: Scope of Support: Lines out the things we will do and what we will not do.
                            • Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon.
                            • Forum Structure and Features: Lines out how the forum works.
                            • Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you should follow the idea of keeping things short and mentioning your primary question in a clear manner.

                            About your First Question

                            Thank you for reaching out to us. We cannot write scripts for you and the script above was a technical demo, not a product, so it is delivered as is.

                            You do not have to select anything in the material manager, the script finds on its own all relevant materials. The script then converts all Redshift "C4D Shader" materials into standard renderer materials (Fig. I).

                            80bdc117-909c-4896-9a6c-c90ee1b79388-image.png
                            Fig. I: The script converts RS C4D Shader materials into Standard Renderer materials (within its capabilities)

                            The script has at the top its mappings, I provided only very rough mappings for the RS Material and RS Standard Material, supporting only a subset of their attributes. Everything else, the inverse route, c4d to RS, other material models, the new node system, or other versions would have to be adopted by yourself. This is a programming forum and this was programming advice, not a finished and supported product.

                            Note: In 2024 Redshift renamed all attributes in its legacy material type RS C4D Shader, with the result that the MAPPINGS data defined in the script is incorrect now. While one can easily update the mappings, many ports are just named Color now. You either have to use IDs in the meantime or wait for the update which fixes this port naming issue.

                            Cheers,
                            Ferdinand

                            MAXON SDK Specialist
                            developers.maxon.net

                            1 Reply Last reply Reply Quote 0
                            • K
                              KolinJ
                              last edited by

                              Ah, ok. Thanks for the response. I get what you are saying and will try to further my own understanding of how to implement this script and scripting in general, and I appreciate you taking the time to clarify things. I am surprised that there isn't more of a demand for a more refined tool to revert RS materials. Do you know of any other approaches to achieving this? I realize this outside the realm of this forum an appreciate any input you might be able to provide.

                              1 Reply Last reply Reply Quote 0
                              • J jana referenced this topic on
                              • First post
                                Last post