Start a new topic

python example for mgNewRecList

Could someone please show an example of using mgNewRecList

I have dug around all or our scripts we have in house and have yet to see it in use.

side would be usefull( to me anyway) to be able to see an example of everything in the api reference documentation. 

I  am a visual learner and seeing the examples have really helped me out so far. 

what I am trying to do here is just find the polygons using the grass01.rgb texture from a tv output. if its in the file, select the polygon and delete it. if not grass01, leave it alone.

what I have so far is

db = mgGetCurrentDb()

def findGrassPolys( db,par,rec,userData):

    type = mgGetCode(rec)

    if (type == fltPolygon):

        outs = mgGetAttList (rec, fltPolyTexture)

        for i in range(1,2):

            idx = i*2

            name = mgGetTextureName (db, outs[idx])

            print "texture name: ", name

#      if name == "grass01.rgb":

this is where I am stuck. I dont know what to do next. I want to select the polygons that use grass01

i dont know if they need to go into a list and then select the list or what.

Hi,  Most of the functions in the reference documentation do have examples in the documentation. Since examples tend to use a large number of functions, they may not be on the specific function you are looking at. They tend to be on the more commonly used but related functions. You can use the related links at the bottom to check and see if a related link has the example that includes the one you are looking for.

Unfortunately not all of them do have examples. mgNewRecList for does not for example. The reason behind this is users very rarely create a rec list to fill out. C++ and Python have very well understood containers that are far more common to use. reclists are mainly used when dealing with OpenFlight's select list.

Here is an example of how to create a reclist, add a node and check to see if it contains a node:

node = mgNewRec(fltPolygon)
ok, m = mgMatrixIdentity()
recList = mgNewRecList()
mgAppendRecList(recList, node, m)

print mgIsRecInList (node)


With that said, your problem is easier to do a different way

I've taken your short script, fixed a few things and added code to show you how to:

1) add nodes to a python list

2) select the nodes directly without a need for a list


db = mgGetCurrentDb()
def findGrassPolys( db,par,rec,polylist):
    type = mgGetCode(rec)
    if (type == fltPolygon):
        ok, code, texIndex = mgGetAttList (rec, fltPolyTexture)
        print texIndex
        name = mgGetTextureName (db, texIndex)
        print "texture name: ", name
        if "grass01.rgb" in name:
    return MG_TRUE
recList = []
mgWalk(db, findGrassPolys, None, recList, MWALK_MASTER)
print recList


Fantastic, this works..I have been trying to get something to work for about 4 days( went as long as I could without asking for any help, was getting nowhere)

While this does work, I do get a typeError in the log

typeError: argument of type 'NoneType' is not iterable

its questioning the line for

if "grass01.rgb" in name:


if "grass01.rgb" in name:

with if name == "grass01.rgb":

seems to run slower but it fixed the typeError

ok, so changing the grass01 line does break the script to where it doesnt select the polygons at all

so with the grass01 line as you had it in your script, all the polygons get selected that use grass01 as a texture. I am trying to delte them with a 

mgDelete (rec) doesnt work

mgDelete (recList) doesnt work

mgDelete (polyList) doesnt work

if you do a direct comparison name == "grass01.rgb" it will fail to detect textures that include the path. Notice when you print out name it will probably include C:\...\grass01.rgb

When you use "grass01.rgb" in name it will check to see if grass01.rgb is found anywhere in the name string. With the none type error it is probably complaining that the name is none. This will happen when you find any polygon without a texture. You can simply add a check to skip polygons without a texture. The texture index will be -1 in these cases.

for deleting, you have to be careful when deleting during a walk. if you mgDelete the current rec while in the pre-walk function it will break things. You can mgDelete the rec during the post walk function.

In order to delete from a list (like polylist) you need to iterate and delete each element. You can use foreach rec in polyList:  to iterate the python list.

ok, let me wrap my head around your last reply.. im not sure I will be adding this to my master script that im trying to create. It takes a really long time to check all the polygons to see if its using a texture. this is being run on one of my tiles that doesnt have alot of polygons in it. There are quite a few tiles this script would take forever to run on.

just to fill you in on what my grand plan is.

I have the script you came up with to move all the tiles to 0,0,0

i then have it run through the files to delete everything but xrefs

then i have it export the files to fbx.

what i was wanting to do was an all in one, but i have to have two separate scripts to run on terrain and model outputs. which isnt a big deal, I was just trying to see if I could have it all in one.

so for the terrain since I want to keep all the polygons with the terrain textures, i run the script I have that excludes removing polys with a texture.

for the models, I have it to where it deletes everything but xrefs.

what I was trying to do with what you were helping with today was to have the one script check for polygons using a specific texture(grass01). if it had it, delete it, if not leave it alone. but having to run through every polygon to check just takes to long.

It could be that the slowdown you are seeing is due to print statements. This is by far the slowest thing you can do. Check the performance after removing all the print statements.

oh, yeah that definitely is faster

i hate to be so needy but i have been looking through our scripts and tried searching around here with not much luck. im looking for examples of what you can do with a list once you have it. 

for example, i was looking at a missing texture thread

i added lines to create a list of the missing textures( i guess it works, dont understand what the printed output means)

but say I wanted to look at the list, and maybe have it search a few directories for the missing textures.

Login to post a comment