Start a new topic

New to creator API. Need some guidance.

Original Post by: jschwall Wed Jul 28 15:38:02 2010


Hello, been using creator for many years but now I need to get into scripting for creator. I have been tasked to edit 900+ models and need info on calling certain tools to add to a script. I read through the API forums and did not find what I was looking for. I saw a post from early last year saying tools could not be called up to run in a script. Has this changed in a years time?


What I am needing to do is open a model and grab the faces that are checked render both sides visible, uncheck it and while the faces are selected run the reverse face tool.


Is this possible to do? I saw someone stating to call up a keyboard shortcut in the script but I dont know how to find a way to check for faces with the render both side visible box checked.


any insight would be greatly appreciated.


John


Original Post by: SteveThompson Wed Jul 28 17:18:29 2010


I read through the API forums and did not find what I was looking for. I saw a post from early last year saying tools could not be called up to run in a script. Has this changed in a years time?

This is still the case - As of version 4.1 OpenFlight Script does not yet provide access to Creator tools. But...what you want to do is very simple in OpenFlight Script (even without access to the Creator tool). I have provide a script below that will do this.


But before you jump to it, you ask...

...but I dont know how to find a way to check for faces with the render both side visible box checked.

This is a fundamental part of the OpenFlight API you should learn if you are to have any success using it. You need to know how to query a node for any of its attributes. To learn this, I suggest you review the OpenFlight API User's Guide Volume 1, chapter 4, section Storing and Retrieving Attributes. Then check out functions mgGetAttList (to retrieve) and mgSetAttList (to store). And there are many many samples in the API SDK that show you how to do this.


Ok... now to the script: The following script prompts you to select one or more OpenFlight files to process. Then for each file you select, it opens the database and looks for all polygons with the "Both Sides Visible" attribute set to TRUE. For each polygon it finds, it "reverses" the order of the vertices exactly as the Creator tool would do (including flipping the vertex normal).


I have not tested this exhaustively but it should provide you with a good starting point.


def ReverseFace(poly):

numChildren = mgCountChild (poly)

if (numChildren > 1):

# make a list to store the face verts

vertices = []

# loop thru face vertices

vtx = mgGetChild (poly)

while (vtx):

# transfer the vtx from the face to our list

vertices.insert (0, vtx)

# detach the vertex from the face

mgDetach (vtx)

# go get next (now first) face vertex

vtx = mgGetChild (poly)

# now all the face vertices are in our list

# and detached from the face

# it's time to put them back on the face

# but in reverse order

for vtx in vertices:

mgAppend (poly, vtx)

# also, Creator's Reverse Face tool "flips" the

# vertex normal. do that here.

for vtx in vertices:

hasNormal,i,j,k = mgGetVtxNormal (vtx)

if (hasNormal == MG_TRUE):

mgSetVtxNormal (vtx, -i, -j, -k)


# declare a class to "wrap" a count variable

class counter_wrapper:

count = 0

def ProcessFaceCB (db, parent, rec, counter):

# this "mgWalk" callback processes each face marked as

# render both sides visible

type = mgGetCode (rec)

if (type == fltPolygon):

numAttr,code,drawType = mgGetAttList (rec, fltPolyDrawType)

if (drawType == 1):

# increment counter to keep track of how many polygons

# were actually changed

counter.count = counter.count + 1

ReverseFace(rec)

return MG_TRUE


def ProcessOneFile (db):

# here we create a counter to "count" how many polygons

# we changed. this function returns the number of polygons

# changed so caller can know the changes in the file need

# to be saved

counter = counter_wrapper()

mgWalk (db, ProcessFaceCB, None, counter, MWALK_NOREADONLY)

return counter.count

outs = mgPromptDialogFile (None,

MPFM_OPEN,

MPFA_FLAGS, MPFF_FILEMUSTEXIST|MPFF_MULTISELECT,

MPFA_PATTERN, "OpenFlight Files|*.flt",

MPFA_TITLE, "Select OpenFlight Files to Process" )

status = outs[0]

if (MSTAT_ISOK(status)):

numFiles = outs[1]

fileNames = outs[2:len(outs)]

for fileName in fileNames:

db = mgOpenDb (fileName)

numPolysChanged = ProcessOneFile (db)

print numPolysChanged,"polygon(s) reversed in",fileName

if (numPolysChanged > 0):

mgWriteDb (db)

mgCloseDb (db)

print "Finished"

else:

print "User Canceled"

Original Post by: jschwall Wed Jul 28 17:54:23 2010


Steve,


Thank you for the quick response. I only just cracked the user manual this morning. I will definitly give the manual a good read through before I get into anything more complicated. I will give this a try in a little bit and let you know how it goes. Thank you again for the jumpstart.

Original Post by: SteveThompson Wed Jul 28 18:20:02 2010


Hey John... Great to have you join in on the Forum discussion... And welcome!

Login to post a comment