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
    • Register
    • Login

    find material slot for shader

    PYTHON Development
    0
    3
    1.0k
    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.
    • H
      Helper
      last edited by

      On 26/08/2018 at 07:20, xxxxxxxx wrote:

      hi there,

      is it possible to find the shader slot in a material for a given shader?

      i.e. if i have this setup
      Mat[c4d.MATERIAL_USE_COLOR] = some_shader

      can i find c4d.MATERIAL_USE_COLOR if i have only Mat and some_shader?

      --> I'd need something like:
      findSlot(Mat, some_shader)  # 2000 (c4d.MATERIAL_USE_COLOR)

      best, index

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 27/08/2018 at 11:29, xxxxxxxx wrote:

        Hi index,

        welcome to the Plugin Café forums 🙂

        Unfortunately no, there is no such function. If the shader setup is simple enough, you could just have an array with parameter IDs and then compare the linked shaders in your own FindSlot() function. A bit more complicated it gets, if the shader setups can be more complicated, including sub-shaders, like e.g. layer shader. Then you have to additionally iterate the shader tree for each slot.

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 28/08/2018 at 02:22, xxxxxxxx wrote:

          tx andreas, an exciting world 🙂

          i got a bit further with my findSlot(Mat, some_shader) 
          and dont understand why i get Attribute Errors when i iterate over a bc:

          def findSlot_v1(sh) : # sh = some shader already inserted in a material slot
          bc = mat.GetDataInstance()
          for i, v in bc:
          print i, v
          if v == sh: return i

          --> AttributeError: Parameter value not accessible (object unknown in Python)

          def findSlot_v2(sh) : # sh = some shader already inserted in a material slot
          bc = mat.GetDataInstance()
          for i, v in bc:
          try:
          if v == sh: return i
          except AttributeError:
          pass

          --> AttributeError: Parameter value not accessible (object unknown in Python)

          def findSlot_v3(sh) : # sh = some shader already inserted in a material slot
          bc = mat.GetDataInstance()
          for i in range(len(bc) - 1) :
          try:
          index = bc.GetIndexId(i)
          if op[index] == sh: return index
          except AttributeError:
          pass

          --> this finally works

          but could anybody tell me why i get these strange AttributeErrors ?

          the first version is straight from the book:
          https://developers.maxon.net/docs/py/2023_2/modules/c4d/BaseContainer/index.html?highlight=basecontainer#BaseContainer.__iter__

          and... i guess there no nicer/faster solution without iterating over the complete bc?

          best, index

          1 Reply Last reply Reply Quote 0
          • First post
            Last post