Help needed... "dynamic global variable" from laye

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Help needed... "dynamic global variable" from laye

Post by heyvern »

I am trying to make my bone control scripts simpler and more universal. The current version requires a unique modified script for each controlled bones layer.

The reason for this is that a global variable is created in that script that contains a list of only the specifically named controlled bones that match the master bone layer.

I am "hard coding" this variable... so... if you use the same script for any other bone layers it overwrites the variable and doesn't work.

I figured out how to create an "array variable" name from the layer name it is in that is unique for each layer that uses the same script... I just can't figure out how to access this global variable in the script once I create it (I am using "loadstring()". It's there dagnabit. But I can only access it using the exact name of the variable.

I need to be able to write to the array... with this "unknown" name for each layer the script is used.

If anyone has any tips or links to references that might help I would appreciate.

I will keep plugging away at it... usually figure it out on my own eventually... but I was getting frustrated so I thought I would ask.

-Vern
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

I tried the layerscript basics with the following two layerscripts. As you see, I don't declare the global SampleVariable at all, the first "if (SampleVariable)" returns false if it is the first access to SampleVariable, or if it is containing nil (or false, if it's bool). That's because at the first access lua gives nil value to the variables. But if you explicitly declare SampleVariable=... first, you don't have a chance to recognise the first access (where you can declare the variable, only once).

The only problem with this, is moho doesn't resets the lua system if you load or create a new scene. So a global variable will exists until you exit moho. It could be a problem if you want to use the same layerscript in different scenes, so you have to recognise somehow that the variable is invalid (for example, don't use the same layer name in different scenes)

I don't understand the loadstring part. Are you using lua to create a lua script and then load it back, to ensure the variable names are unique in the layer?

Code: Select all

function LayerScript(moho)
	print("Sample1:")

	if (SampleVariable) then
		print("SampleVariable is already exists:",SampleVariable)
	end
	SampleVariable= "1-" .. moho.layer:Name()
	print("Set SampleVariable to:",SampleVariable)
end

Code: Select all

function LayerScript(moho)
	print("Sample2:")

	if (SampleVariable) then
		print("SampleVariable is already exists:",SampleVariable)
	end
	SampleVariable= "2-" .. moho.layer:Name()
	print("Set SampleVariable to:",SampleVariable)
end
- - - Fazek
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

I just found something interesting: in a layer script, you can assign ANY variable to the actual layer (at least under Linux), for example:

moho.layer.MyOwnVariable= xxxx

and afterwards, it will remember this variable separately for each layer even if you are using the same layerscript for many layers! (of course it lives only until you exit the scene, but this is a good chance to use layer-local variables)

Maybe I am not familiar enough with lua, but this is new for me...
- - - Fazek
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

You are THE MAN!

WoooHoooooo!

That is what I was looking for!

Thank you! I never would have found that... well... maybe not never but it would have taken ages.

------
By the way... FYI...

Code: Select all

loadstring()
This allows you to process ordinary "strings" as LUA code in a script but you can insert variables into the code "string". (not dofile()... or whatever that one is called... not doing that one).

For instance:

Code: Select all

f = loadstring(myvar .. "=" .. anothervar .. "; return " .. myvar)
So you call f which executes loadstring which is a funtion that declares the variable... in the house that jack built... ;)

The "myvar" is a variable whose name is the CONTENT of the var. So it doesn't create a var called myvar... it creates a variable that is whatever is in myvar...

so... if I put the name of the layer in myvar the loadstring creates that variable... but I couldn't figure out how to get to it.

Oh well... don't need to now.

Thanks again!

-Vern
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

IT WORKS!!!


Oh my GOD! IT works it works! Woohooo!

Lucky I have no hair or I would have pulled it out by now.

Thanks Fazek! This is a HUGE help. I can use this in a lot of places.

I am on a Mac so it works there as well as linux.

-Vern
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Works great...

If you load a project with this script, and imediately go to a frame other than "0" all of the variable creation on frame 0 never occurs so you get errors.

However... if you just click in frame 0 once... or click on a layer while in frame 0... everything works fine.

Apparently when a project is first loaded "frame 0 only" scripts don't do their thing.

I had to add a little error checking that tells people to click on frame 0 first when this happens.

I am thrilled! This is so cool.

-Vern
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

sorry I was gone for a bit, but how does it work. C'mon, spill the beans, heyvern, I need the inspiration and youi've been doing some cool stuff, I see.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Welcome back 7feet (I don't feel I know you well enough to call you Brian just yet) ;)

Well...

I have been busy lately. I should just zip up what I got and post it. I have been using it constantly now. It seems to work fine.

In your original bone control script you created a global variable for the master bone.

In my version I expanded the concept so I could match the master bone name to the control bones so you could do more than one. I ran into a glitch because the list of controlled bones was also a global variable and each layer with that script had a different set of bones in it and created a total mess.

With this cool new trick... the same script (and variable) can be used... but it becomes unique for each layer.

-vern
Post Reply