Start a new topic

Adding Extended Material ?

How can I write a script to add a extended material to my model with all the texture that was already applied to the model texture palette.


I cant find any "extended material" function in the material functions.


I also new to openflight script. What does the mgbool and mgrec* mean? Do I need to add it infront of the function?



Here is a sample to get you started. This script adds a new extended material to the current database and sets up the ambient and diffuse layers. You an set up other layers / attributes of the extended material as necessary. The functions used here are "Attribute Functions" not "Material Functions" because they are used to set attributes.

db = mgGetCurrentDb()

# get current texture index
b, textureIndex = mgGetCurrentTexture(db)

# create a new material for db
mat, matIndex = mgNewMaterial(db, "new material")
print mat

# make it an extended material
mgSetAttList(mat, fltMatType, 1, fltMatShadeModel, 1)

# now set up the attributes

# assign ambient and diffuse colors
mgSetNormColor (mat, fltAmbientExColor, 1.0, 1.0, 1.0)
mgSetNormColor (mat, fltDiffuseExColor, 1.0, 1.0, 1.0)

# assign current texture to base ambient and diffuse
mgSetMatTextureLayer (mat, fltAmbientEx, textureIndex, 0)
mgSetMatTextureLayer (mat, fltDiffuseEx, textureIndex, 0)

As for mgbool and mgrec*... mgbool is the OpenFlight API type for "boolean" and mgrec* is the OpenFlight API type for any node in the scene graph. When you see (in the OpenFlight API Reference) a function signature like this:


mgrec* mgGetCurrentDb ( void )

 

This means the function mgGetCurrentDb "returns" an object of type mgrec*. So when you call this function you would write something like this:


db = mgGetCurrentDb() 


which means an object (variable) called "db" will be loaded with the result of mgGetCurrentDb. "db" is just the name of the variable you want to set. You can name that variable anything you want.


All of this (and more) is explained in the OpenFlight API Developer Guides included in the OpenFlight API installer. Let us know if you have specific questions.

Hi Steve, sorry for the late reply. Thank again for the example. Am I right to say mgrec* is just an indication on where I need to define a  variable in? same of rec ?


Is there any way to apply the script to a selection? To add comments into a group of selected nodes with the mgSetComment?

In line 4, why did you have a 'b' at the front? do I need to set a variable name in the mgbool if it just defining that the function returns a True or False. 

b, textureIndex = mgGetCurrentTexture(db)

Hi Eric, 

Take another look at my previous response. When you see mgrec* or mgbool, they have different meanings depending on their context. The most important part of the OpenFlight API Reference to understand is the function "specification" (or calling syntax) for any function. When you look at a function in the OpenFlight API Reference, say mgGetCurrentTexture, you will see the following:

image


Since the OpenFlight API has both a C/C++ interface as well as a Python interface, you may see two different specifications. That is because C/C++ and Python handle output parameters differently. Let me try to explain by example. 


For the C Specification of mgGetCurrentTexture you will see the following:


mgbool mgGetCurrentTexture (mgrec*db, int*index );


This means (in C/C++) that mgGetCurrentTexture is a function that returns a mgbool value and has two parameters. The first parameter is an mgrec*(database fltHeader node in the OpenFlight scene graph), the second is an address of an int. This function actually passes two values back to the caller. The first value it passes back is the actual function return value. In C/C++ a function can have at most one function return value. However since this function needs to pass back an additional value, it does so in an "output" parameter that you pass by reference. This output parameter is the address of an int you pass as the second parameter. The function "writes" to that address to pass you back the second value. In this way, the function "returns" two values.


Now look at the Python specification:


mgbool, index mgGetCurrentTexture (mgrec*db )


This means (in Python) that mgGetCurrentTexture is a function that returns two values and has one parameter. Python does not support parameters passed by reference. In other words you cannot pass the address of an int to receive the value. But, unlike C/C++, Python does support multiple return values for functions. So instead of passing an address of an int to receive the texture index like you do in C/C++, you have to account for an additional return value when you call mgGetCurrentTexture in Python. That's what "mgbool, index" means when it appears before mgGetCuttentTexture in the Python Specification. It means this function returns two values. It returns an mgbool value and in int value (index). That's why you call mgGetCurrentTexture in Python by doing this:


b, textureIndex = mgGetCurrentTexture(db)


If you are still having trouble with Python syntax, there are lots of good references online to help you. To use the OpenFlight API, you will need a good understanding of the C/C++ language (if you use the C/C++ version) or Python (if you use the OpenFlight Script Python version).


To answer your other questions:


Is there any way to apply the script to a selection?


Yes, there are several functions you can use to examine the contents of the "select list". Note that you can only access the select list when you run your script inside of Creator. Selection means nothing in a stand-alone program. In the OpenFlight API Reference, see the Function Category "Select List Functions" for more information. Also in the OpenFlight API Developer Guide Volume 2, see the chapter called "The Modeling Context". It contains a section called "The Select List". This section should get you started. 


To add comments into a group of selected nodes with the mgSetComment?


Yes adding a comment to a node is very simple using mgGetComment.


Here is a sample script showing you how to loop through the selection. This script sets the comment on all group nodes selected:

 

db = mgGetCurrentDb ()
selectList = mgGetSelectList (db)
num = mgGetRecListCount (selectList)

if (num == 0):
	mgSendMessage (MMSG_ERROR, "Nothing selected")
else:
	# loop through each selected node
	for i in range (0, num):
		rec,m = mgGetNextRecInList (selectList)
		# if the node is a group
		if (mgGetCode(rec) == fltGroup):
			# set the group comment to
			# "Group name is <name>"
			# where <name> is the name of the group node
			name = mgGetName(rec)
			comment = "Group name is " + name
			mgSetComment(rec, comment)

 

Login to post a comment