5
$\begingroup$

Trying to figure out how to read the uv channel used by an Image Texture input from Python. So, in effect, how to access the "Vector", then grab the attribute name:

enter image description here

$\endgroup$
2
$\begingroup$

You need to use a UV Map node:

enter image description here

You use it like this:

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Updated. Thank you. The question remains how to access Texture "Vector" input from Python, please. $\endgroup$ – Steve Williams Nov 11 '15 at 19:10
  • $\begingroup$ So you would like to assign the UV Map from a script? $\endgroup$ – VRM Nov 11 '15 at 19:16
  • 1
    $\begingroup$ I want to read it from a script, for an exporter. Thanks. $\endgroup$ – Steve Williams Nov 11 '15 at 19:26
2
$\begingroup$

Figured it out - if anyone else needs this you can pull it out of the following :

def GetUV(self, element):
    for inp in element.inputs:
        if inp.is_linked and inp.type == 'VECTOR' :
            l = inp.links[0]
            link_src = l.from_node
            if link_src.type == 'UVMAP':
                return link_src.uv_map
    return None

def Input_Image_Write(self, element):

    uv_map = self.GetUV(element)

    fn = bpy.path.abspath(element.image.filepath)
    texture_path = bpy.path.abspath(element.image.filepath)
    texture_filename = bpy.path.basename(texture_path)
    self.File.Write('<input color="{:9f} {:9f} {:9f}" src="{}"'.format(element.color[0], element.color[1], element.color[2], texture_filename))

    if uv_map != None:
        self.File.Write(' uv="{}"'.format(uv_map), Indent=False)

    self.File.Write("/>\n", Indent=False)

    # Copy texture file to output directory
    dest_path = os.path.join( self.texture_path, texture_filename)
    shutil.copyfile(texture_path, dest_path)    

def WriteMaterial(self, mtl):

    self.File.Indent()

    # Write default material if requested to do so
    if mtl == None :
        self.File.Write('<material id="default"><component id="diffuse"><input color="0.5 0.5 0.5"/></component></material>\n')
        self.File.Unindent()
        return

    self.File.Write('<material id="{}'.format(Util.SafeName(mtl.name)))
    self.File.Write('">\n', Indent=False)
    self.File.Indent()

    diffuse_BSDF = mtl.node_tree.nodes.get("Diffuse BSDF")

    if diffuse_BSDF != None:

        self.File.Write('<component id="diffuse">\n')

        self.File.Indent()

        # grab color input
        diffuse_colour = diffuse_BSDF.inputs[0]

        if diffuse_colour.is_linked:
            src = diffuse_colour.links[0].from_node
            if src.type == 'TEX_IMAGE':
                self.Input_Image_Write(src)
            elif src.type == 'MIX_RGB':
                for i in src.inputs:
                    if i.type == 'RGBA':
                        if i.is_linked:
                            src_i = i.links[0].from_node
                            if src_i.type == 'TEX_IMAGE':
                                self.Input_Image_Write(src_i)
            else:
                dump(src)

        self.File.Unindent()
        self.File.Write('</component>\n')
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.