Python string wrong characters

1.2k views Asked by At

let me start by saying that I attempted to create a minimal example but was unable to replicate the results. I try to not make it a habbit to dump all my code on here but I am not sure of another way to illustrate the issue. I am writing a class to output data from my application to a specific format and I am getting the wrong results in the code.

The format I am attempting to output should look like this:

Nodestyle "nodeType" "name"

"attribute-type attribute-name" [value]

If the attribute is linked to another node

"refrence attribute-type attribute-name" ["nodename:output-attribute-name"]

Example:

 Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32" 
 "color baseColor" [0.20.50.8] 
 "color emitColor" [0.00.00.0] 
 "float subsurface" [0] 
 "reference color subsurfaceColor" ["a3bb37f2-dbb8-407b8a1ac93822ebef7c:resultRGB"] 
 "float metallic" [0] 
 "float specular" [.5] 
  "float specularTint" [0] 
  "float roughness" [.25] 
  "float anisotropic" [0] 
  "float sheen" [0] 
  "float sheenTint" [.5] 
  "float clearcoat" [0] 
  "float clearcoatGloss" [1] 
  "float presence" [1] 
  "int inputAOV" [0] 
  Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c" 
  "color inputRGB" [0.00.00.0] 
  "float hue" [0.0] 
  "float saturation" [1.0] 
  "float luminance" [1.0] 

The results I am getting look like this:

('Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32" \n "color baseColor" [0.20.50.8] \n "color emitColor" [0.00.00.0] \n "float subsurface" [0] \n "reference color subsurfaceColor"', '["a3bb37f2-dbb8-407b-8a1a-c93822ebef7c:resultRGB"] \n ')"float metallic" [0] 
 "float specular" [.5] 
 "float specularTint" [0] 
 "float roughness" [.25] 
 "float anisotropic" [0] 
 "float sheen" [0] 
 "float sheenTint" [.5] 
 "float clearcoat" [0] 
 "float clearcoatGloss" [1] 
 "float presence" [1] 
 "int inputAOV" [0] 
Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c" 
 "color inputRGB" [0.00.00.0] 
 "float hue" [0.0] 
 "float saturation" [1.0] 
 "float luminance" [1.0] 

It looks like it if outputting special characters and extra items in the string up to where there is a reference to another node. if I link to another attribute lets say basecolor it will only give the wrong results up to basecolor. If there is no connections at all there is no issue. Why would a string give this behaviour? Here is the class in question. The class take an array of node objects I step through.

#This will handel the export of the RIB contaning all the shader calls
class exporter ():

    nodes = None
    dicNodes = {}
    outStream = []

    #Method Two: Use shader as a starting point
    #Find the shader
    #go through each attribute on the shader

    #if connected check attrs on connected node

    def __init__(self, incomenodes):
        self.nodes = incomenodes
        dicNodes = {}
        outStream = []

        #create a dic using nids as keys
        for node in self.nodes:
            self.dicNodes[str(node.nid)] = node

        #Run Export Command for each shader
        x = 0
        for node in self.nodes:
            print str(node.nodeType.nType)
            if str(node.nodeType.nType) == 'bxdf':
                self.streamMe(node)

        target = open('/home/jspada20/outStream.RIB', 'w')

        print 'JAMES! There are ', len(self.outStream), 'items in the out stream'
        for line in self.outStream:
            target.write(line)
            print line
        target.close()

    def streamMe(self, node):

        #Layout for the header of a node call is....

        #Pattern "PxrThinFilm" "PxrThinFilm1"
        #Bxdf "PxrDisney" "PxrDisney1"
        #{Type} "{Identifyer}" "{Name/UID}"

        moreNodes = []

        #need to fix lower case letters comming in from XML source
        nodefunction = None
        if str(node.nodeType.nType[0]) == 'p':
            nodefunction = 'Pattern'
        elif str(node.nodeType.nType[0]) == 'b':
            nodefunction = "Bxdf"
        else:
            nodefunction = str(node.nodeType.nType)


        self.outStream.append(nodefunction + ' "' + str(node.nodeType.nName) + '" ' + '"' + str(node.nid) + '" \n ')



        for attr in node.inputs:

            #Check to see if it is connected

            #if not connected:
            if attr.currentConnectedNode is None:

                #Check to see what type the value is. This will govern the export scheme
                if str(attr.argType) == "int":
                    #print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value) + ']'
                    self.outStream[len(self.outStream) - 1] = self.outStream[len(self.outStream) - 1] + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '
                elif str(attr.argType) == "float":
                    if str(attr.value[len(str(attr.value))-1]) == '.':
                        outVal = str(attr.value) + '0'
                    else:
                        outVal = str(attr.value)
                    #print '"'+ str(attr.argType), attr.name + '" [' + outVal + ']'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '

                elif str(attr.argType) == "color":
                    #print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value.r), str(attr.value.g), str(attr.value.b) + ']'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value.r) + str(attr.value.g) + str(attr.value.b) + '] \n '

                elif str(attr.argType) == "string":
                    #print '"'+ str(attr.argType), attr.name + '" ["' + attr.value + '"]'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '

            else:
                self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"', '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '
                moreNodes.append(str(attr.currentConnectedNode.nid))

        for ids in moreNodes:
            self.streamMe(self.dicNodes[ids])
1

There are 1 answers

3
jspada On BEST ANSWER

When I was converting this code from print statements to storing the information in a list, I missed a comma. Tuples can be declared like this:

myTuple = "1", "2", "3"

By having a comma in the statement I was converting the string.

Changing the comma to a + in this statement resolved the issue. Coming from c++ I was not used to mutable data types!

            self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"' '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '