Jump to content

Creating Local Variables Dynamically


Recommended Posts

Hi,

I'm trying to make a sop where a user can create their own local variables (each variable has a user defined

name and value). I had a look at the SOP_Star example, but it looks like the names of the

local variables are established in the constructor. As far as I can tell this method won't

allow for dynamic local variables names.

Does anyone have some insight as to how I might get around this?

Many thanks

L

Link to comment
Share on other sites

This is actually very easy

//function to read the user supplied name
void  USERATTNAME(UT_String &str)  { evalString(str, 5, 0, 0); }

//get the user named attribute
UT_String username;
USERATTNAME(username);

//make it uppercase
UT_String usernameUpper = username;
usernameUpper.toUpper();

//add a local var map
gdp->addVariableName(username, usernameUpper );

Then of course you have to do the usual to actually create the attributes and use the name defined in the code above

Edited by sibarrick
Link to comment
Share on other sites

Then of course you have to do the usual to actually create the attributes and use the name defined in the code above

Hey Simon,

Many thanks for your help,

but I think I've explained myself poorly, I'm trying create some Local Variables (like $PT, $NPT, $ID) that the user

can define via a string parameter(the value would be established via a seperate float field). And if

I understand your example, it seems to be the setup for adding user defined attributes.

Here's a bit more info about my problem:

In the star sop example the local variable names seem to be established in the constructor:

OP_StarOperator::OP_StarOperator()

: OP_Operator("star",

"Star",

SOP_Star::myConstructor,

SOP_Star::myTemplateList,

0,

0,

SOP_Star::myVariables,

OP_FLAG_GENERATOR)

{

}

CH_LocalVariable

SOP_Star::myVariables[] = {

{ "PT", VAR_PT, 0 }, // The table provides a mapping

{ "NPT", VAR_NPT, 0 }, // from text string to integer token

{ 0, 0, 0 },

};

Which doesn't seem to allow for dynamic local variable names. So I

was just wondering if anybody new how to get around this issue.

Many thanks and much gratitude.

L

Link to comment
Share on other sites

You can define your own local variables to be used inside expressions in the current sop like this. These obviously aren't added as local variable mappings as I was decribing before, but do act in the same way as $PT etc.

CH_LocalVariable
SOP_PointSlide::myVariables[] = {
	  { "PT0", VAR_PTS, 0 },
	  { "PT1", VAR_PTE, 0 },
	  { "EDGE", VAR_E, 0 },
	  { 0, 0, 0 },
};

float
SOP_PointSlide::getVariableValue(int index, int) {

	if (myCurrPoint < 0) // Sorry, we're in an invalid state
	return 0;

	switch (index)
	{
	  case VAR_PTS: return myCurrPoint;
	  case VAR_PTE: return myCurrEndPoint;
	  case VAR_E:   return myCurrEdge;
	}
	return 0; //Whoops, forgot a variable
}

You then set myCurrPoint etc inside the cook method so they are available to be used in expressions in the parameters of the sop

But make sure you also get the values from parameters for each point and not just once.

Inside a loop that parses every point

myCurrPoint = ppt0->getNum();
myCurrEndPoint = ppt1->getNum();
myCurrEdge = edgeCount;

You define myCurrPoint etc in the header file so they are globally available.

Is this any more help?

Edited by sibarrick
Link to comment
Share on other sites

  • 2 weeks later...

I figured this out!

The issue is that I wanted the user to define the names of their temporary variables.

I realized that all I had to do was change the entries in myVariables and

everything would be cool

inside cookMySop

evalString(tempVarNames],temp_var_names.getToken(),0,now);

CH_LocalVariable entry={ tempVarNames], VAR_TMP1, 0 };

myVariables[1] = entry;

L

Link to comment
Share on other sites

  • 3 months later...

sorry to excavate this thread, but i have a basic understanding problem:

are there two ways of creating Local Variables ?

one time using

gdp->addVariableName(attribName,LocalVarName)?

and second time using the my variables array to register them and use

getVariableValue(int index,int thread)

to evaluate them ?

thx for helping

Sebastian

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...