list indices must be integers, not tuple?

Need help, or want to share a macro? Post here!
mrrclb48z
Posts: 27
Joined: Wed Feb 07, 2018 11:20 am

list indices must be integers, not tuple?

Postby mrrclb48z » Tue May 08, 2018 1:36 pm

What am I doing wrong with the macro?
Thanks in advance
full script:

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
def myTetrahedronSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z):
        xDummy=0
        yDummy=0
        zDummy=0
        a =[[xDummy,yDummy,zDummy],      #dummy
            [P1x,P1y,P1z],
            [P2x,P2y,P2z],
            [P3x,P3y,P3z],
            [P4x,P4y,P4z]]
        m1 = Mesh.Mesh([a[1],a[2],a[3]]) #4
        m2 = Mesh.Mesh([a[1],a[2],a[4]]) #3
        m3 = Mesh.Mesh([a[1],a[3],a[4]]) #2
        m4 = Mesh.Mesh([a[2],a[3],a[4]]) #1
        mesh=Mesh.Mesh()
        mesh.addMesh(m1)
        mesh.addMesh(m2)
        mesh.addMesh(m3)
        mesh.addMesh(m4)
        shape = Part.Shape()        
        shape.makeShapeFromMesh(mesh.Topology,0.05)
        solid = Part.makeSolid(shape).removeSplitter()
        myWedgei='x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
        f = App.activeDocument().addObject("Part::Feature",myWedgei)
        f.Shape = solid

h=10
xDum=0
yDum=0
zDum=0
a =[[xDum,yDum,zDum],
    [0,0,h],           
    [h,0,h],           
    [h,h,h],           
    [0,h,h],           
    [0,0,0],           
    [h,0,0],           
    [h,h,0],           
    [0,h,0]]           
myTetrahedronSld(a[1,0],a[1,1],a[1,2],a[2,0],a[2,1],a[2,2],a[3,0],a[3,1],a[3,2],a[5,0],a[5,1],a[5,2])
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
#Traceback (most recent call last): File "C:/xxx/FreeCAD/001Tetrahedron.FCMacro", 
#line 44, in myTetrahedronSld(a[1,0],a[1,1],a[1,2], a[2,0],a[2,1],a[2,2], 
#                             a[3,0],a[3,1],a[3,2], a[5,0],a[5,1],a[5,2]) : 
#list indices must be integers, not tuple
User avatar
Chris_G
Posts: 1034
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: list indices must be integers, not tuple?

Postby Chris_G » Tue May 08, 2018 2:13 pm

mrrclb48z wrote:
Tue May 08, 2018 1:36 pm
What am I doing wrong with the macro?
When accessing a multi-dimension array, each index must be between brackets, like this : a[0][0] ,not this : a[0,0]

Code: Select all

myTetrahedronSld(a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2],a[3][0],a[3][1],a[3][2],a[5][0],a[5][1],a[5][2])
mrrclb48z
Posts: 27
Joined: Wed Feb 07, 2018 11:20 am

Re: list indices must be integers, not tuple?

Postby mrrclb48z » Thu May 10, 2018 2:14 pm

Sorry
(expect a sequence of floats or Vector)

full script :

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
def myTetrahedronSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z):
        xDummy=0
        yDummy=0
        zDummy=0
        a =[[xDummy,yDummy,zDummy],      
            [P1x,P1y,P1z],
            [P2x,P2y,P2z],
            [P3x,P3y,P3z],
            [P4x,P4y,P4z]]
        m1 = Mesh.Mesh([a[1],a[2],a[3]]) 
        m2 = Mesh.Mesh([a[1],a[2],a[4]]) 
        m3 = Mesh.Mesh([a[1],a[3],a[4]]) 
        m4 = Mesh.Mesh([a[2],a[3],a[4]]) 
        mesh=Mesh.Mesh()
        mesh.addMesh(m1)
        mesh.addMesh(m2)
        mesh.addMesh(m3)
        mesh.addMesh(m4)
        shape = Part.Shape()        
        shape.makeShapeFromMesh(mesh.Topology,0.05)
        solid = Part.makeSolid(shape).removeSplitter()
        myWedgei='x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
        f = App.activeDocument().addObject("Part::Feature",myWedgei)
        f.Shape = solid

h=10
xDum=0
yDum=0
zDum=0
a =[[xDum,yDum,zDum],
    [0,0,h],           
    [h,0,h],           
    [h,h,h],           
    [0,h,h],           
    [0,0,0],           
    [h,0,0],           
    [h,h,0],           
    [0,h,0]]           
myTetrahedronSld(a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2],a[3][0],a[3][1],a[3][2],a[5][0],a[5][1],a[5][2])
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
#Traceback (most recent call last): File "C:/xxx/FreeCAD/Sorry.FCMacro", 
#line 44, in myTetrahedronSld(a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2],a[3][0],a[3][1],a[3][2],a[5][0],a[5][1],a[5][2]) 
#File "C:/xxx/FreeCAD/Sorry.FCMacro", 
#line 15, in myTetrahedronSld m1 = Mesh.Mesh([a[1],a[2],a[3]]) : expect a sequence of floats or Vector 
TheMarkster
Posts: 820
Joined: Thu Apr 05, 2018 1:53 am

Re: list indices must be integers, not tuple?

Postby TheMarkster » Thu May 10, 2018 7:57 pm

FreeCAD can convert int types to float types, but you have them buried inside a list of lists and it's failing a parameter type check somewhere along the way. You can either create Vector objects during the call to Mesh.Mesh()...

Code: Select all

        m1 = Mesh.Mesh([a[1],a[2],a[3]]) 
        m2 = Mesh.Mesh([a[1],a[2],a[4]]) 
        m3 = Mesh.Mesh([a[1],a[3],a[4]]) 
        m4 = Mesh.Mesh([a[2],a[3],a[4]])
becomes:

Code: Select all

        m1 = Mesh.Mesh([App.Vector(a[1]),App.Vector(a[2]),App.Vector(a[3])]) 
        m2 = Mesh.Mesh([App.Vector(a[1]),App.Vector(a[2]),App.Vector(a[4])]) 
        m3 = Mesh.Mesh([App.Vector(a[1]),App.Vector(a[3]),App.Vector(a[4])]) 
        m4 = Mesh.Mesh([App.Vector(a[2]),App.Vector(a[3]),App.Vector(a[4])]) 
...or, expressly tell python these are floats, not ints, by adding this to your function definition:

Code: Select all

#instead of this: def myTetrahedronSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z):
def myTetrahedronSld(In_P1x,In_P1y,In_P1z,In_P2x,In_P2y,In_P2z,In_P3x,In_P3y,In_P3z,In_P4x,In_P4y,In_P4z):
        P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z = float(In_P1x),float(In_P1y),float(In_P1z),float(In_P2x),float(In_P2y),float(In_P2z),float(In_P3x),float(In_P3y),float(In_P3z),float(In_P4x),float(In_P4y),float(In_P4z)