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

    how to set the path of a Textre node manually?

    Cinema 4D SDK
    windows macos python 2024
    2
    5
    574
    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.
    • M
      momoko
      last edited by

      Lovely people,

      Is this a wrong attempt to set the Path of a Texture node manually?

      def create_material(self):
              doc = c4d.documents.GetActiveDocument()
              material = c4d.BaseMaterial(c4d.Mmaterial)
              if not material:
                  raise MemoryError(f"{material = }")
              doc.InsertMaterial(material)
          
              # Get the node material for it and add a graph for the currently active material space to it.
              node_material = material.GetNodeMaterialReference()
              graph = node_material.CreateDefaultGraph(c4d.GetActiveNodeSpaceId())
              if graph.IsNullValue():
                  raise RuntimeError("Could not add graph to material.")
              
              # Start a transaction to modify the graph
              with graph.BeginTransaction() as transaction:
                  # Get texture files from the selected folder
                  texture_files = self.print_allowed_files_in_folder(self.FOLDER_PATH)
          
                  # Add texture nodes to the material graph for each texture file
                  for texture_file in texture_files:
                      texture_node = graph.AddChild(maxon.Id(), maxon.Id("com.redshift3d.redshift4c4d.nodes.core.texturesampler"))
                      if texture_node is None:
                          continue
                      
                      # Set the texture file path
                      path_port = texture_node.GetInputs().FindChild("com.redshift3d.redshift4c4d.nodes.core.texturesampler.tex0").FindChild("path")
                      path_port.SetDefaultValue(maxon.Url("file:///" + texture_file))  # Assuming local file path
          
                      # Connect the texture node to the output node of the material
                      output_port = graph.GetOutputPort(0)  # Get output port of the material
                      texture_color_port = texture_node.GetOutputs().FindChild("com.redshift3d.redshift4c4d.nodes.core.texturesampler.outcolor")
                      texture_color_port.Connect(output_port, modes=maxon.WIRE_MODE.NORMAL, reverse=False)
          
              # Commit the transaction to apply the changes to the graph
              transaction.Commit()
      
      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @momoko
        last edited by ferdinand

        Hey @momoko,

        Thank you for reaching out to us. That depends what you mean by "is this a wrong attempt to set the Path of a Texture node manually?.

        The lines

        path_port = texture_node.GetInputs().FindChild(
            "com.redshift3d.redshift4c4d.nodes.core.texturesampler.tex0").FindChild("path")
        path_port.SetDefaultValue(maxon.Url("file:///" + texture_file))  # Assuming local file path
        

        are the correct statements to address the path component of a Filename port bundle in a Texture node and then set its value. It depends here a bit what the author meant with "local" file, if he/she meant an URL in the file scheme/protocol, than this is the correct syntax. When texture_file is meant to be a relative path, you would have to use the relative:/// URL scheme.

        The code before these two lines is also okay, but the code after these two lines is pure gibberish.

        1. No offense, but your code somewhat strikes me as chat-bot (ChatGPT, CoPilot, etc.) generated. We do not support chat-bot output. They are helpful tools, but we cannot fix the gibberish they will come up with from time to time.
        2. We have multiple Nodes API Python examples and create_redshift_nodematerial_2024.py is very close to what you want to do here.
        3. When the Nodes API is too much for you, you could have a look at graph descriptions. They are more artist friendly, and loading a bunch of textures there should be pretty straight forward.

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • M momoko deleted this topic on
        • ferdinandF
          ferdinand
          last edited by ferdinand

          Hey @momoko,

          please do not delete topics to which we have already answered. I am not sure if this is tied to you either having found a solution yourself, or that I pointed out that your code looks chat-bot-ish because it contains made-up function calls.

          I have restored the topic, so that future readers can see our answer here.

          Cheers,
          Ferdinand

          MAXON SDK Specialist
          developers.maxon.net

          M 1 Reply Last reply Reply Quote 0
          • ferdinandF ferdinand restored this topic on
          • M
            momoko @ferdinand
            last edited by

            @ferdinand I found a solution. Sorry

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

              No need to be sorry 🙂

              MAXON SDK Specialist
              developers.maxon.net

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