Embedded Script Working Inside Layer Groups (Question)

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
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Embedded Script Working Inside Layer Groups (Question)

Post by Rai López »

Well, I'm involved in a very first steps of Embedded Scripting, ejem, ejem... you know... I'm very sorry :oops:, but I'm fill of basic and (possibly) stupid questions... Well, seems that the "target_object.lua", embedded script sample made by the GREAT Marble, only take effect about direct layers of Moho, nothing about layers inside a group... because when you make a group and drag all layers of "targeting_sample.moho" inside that group, you receibe the message: "Target layer not found" because Target layers is not detected... I was wondered about the way to avoid this, I've been investigating without results and I'd like to know the code lines (or some clue at least) for it can works with layers inside a certain group... I think that I must change, add or eliminate something at this lines of code...

Code: Select all

	local targetLayer = moho.document:LayerByName("Target")
	if (targetLayer == nil) then
		print("Target layer not found.")
		return
	end
...but I don't know what, and I'm afraid I need some help or clue... If somebody could help me on this search, I'd be very GRATEFULL! *CIAO*
User avatar
Lost Marble
Site Admin
Posts: 2356
Joined: Tue Aug 03, 2004 6:02 pm
Location: Scotts Valley, California, USA
Contact:

Post by Lost Marble »

The document:LayerByName function only looks at top-level layers. There is also a group:LayerByName function that returns a layer in a group.

To dig way down for a layer that is inside multiple groups, you may have to use this function several times:

Code: Select all

local layer= moho.document:LayerByName("Group1")
if (layer == nil) then
    print("Group1 not found.")
    return
end
layer = moho:LayerAsGroup(layer)
layer = layer:LayerByName("Sub-Group2")
if (layer == nil) then
    print("Sub-Group2 not found.")
    return
end
layer = moho:LayerAsGroup(layer)
layer = layer:LayerByName("The Layer I Want")
if (layer == nil) then
    print("The Layer I Want not found.")
    return
end
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

...WOW!!! :D THANK YOU! THANK YOU!! THANK YOU!!! :D :D :D Thank you very much LM for the quick answer! :) I really needed help with this and now I can't wait to try it! You've created an INCREDIBLE feature and I can't be more GRATEFULL :D *CIAO*

PS: EMBEDDED SCRIPTS (can) ROCKS!!! :D
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

...HELLO! The "group:LayerByName function" works :), yes... but I'm wondering if really is the one way to get that one layer locates another layer (...or bone?) inside the same group because when you move all that group inside/outside a new group you obtain an error and you must rewrite (add or remove part of the code) the script for it works again... I mean, if two layers like in the case of Targering example are inside the SAME group, is not possible that the script only search for the other layer INSIDE this group? Independently of the level where the group be situated? Seems logic and I have the curiosity/necessity of be sure to can work in my Bone_Control embedded script (to can control one bone with the movement of other situated in other Bone Layer, but both inside the same group) Uf... I hope someone can HELP me in this impossible task :roll: BYE! (and THANKS)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Maybe something like this:

Code: Select all

local layer = moho.layer  --layer you're on
local parent = LayerAsGroup(layer:Parent()) -- the parent layer if it has one
local target = nil  --where the target goes if you find one

if (parent == nil) then --No parent, so it's a root layer
     target = moho.document:LayerByName("Target")
     if (target == nil) then
          print("Target not found.")
          return
     end 
else
     -- It's inside some "group" type of layer
     target = parent:LayerByName("Target") 
     if (target == nil) then
          print("Target not found.")
          return
     end 
end   
Something like that should do it, I think. I haven't tried it, so I may have done something dumb, but it looks about right. Give it a shot.

Also, LM, I don't know if it was added later, but LayerByName isn't in the Scripting Reference. I think there's a few other bits, has the Ref been updated lately? Thanks
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

THANK YOU 7feet!!! ...I'm now a little desperate :( about all this "Bone_Control" embedded script that I'm treating to do... so I think I'm going to the bed now, forget it for an hours and try it tomorrow, hopping that things going better... (maybe your codes help ;)) so :arrow: THANKS! (and good night 8) ).
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

HELLO 7feet, LM and others... :) I've "insert" the code that you bring us and apply the script to the targeting_sample.moho file but, of course, with the "Target" and "Scripted Group" layers inside a group... well I obtain this error in the Lua Console:

...\Target_ObjectInAnyGroup.lua:11:attempt to call global 'LayerAsGroup" (a nil value)

I dont' know if I'll have done something wrong (as usually), or what the cause of the error is, because I can't understand what does it means (as usually too :roll: ) ...I only can put this here for if this can help someone to solve the problem or know about it... CIAO! :)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Oops. Try this bit (I forgot something - picky damn computers)

local parent = moho:LayerAsGroup(layer:Parent())

alternately, just "local parent = layer:Parent()" might work fine too, you'd have to test it.
User avatar
mr. blaaa
Posts: 622
Joined: Sun Jul 31, 2005 12:41 am
Location: ---
Contact:

Post by mr. blaaa »

yeeehaaw! 8)
Image
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

7feet, YOU ARE HERE! And you come with some homeworks to me :D, VIVA! Jaja... and some of they, like a good pupil, I had made it before :) ...because I think I tryed the local parent = moho:LayerAsGroup(layer:Parent()) sugestion yet and other error appeared, I'll treat to repeat it to be sure and, if necessary put here the Lua Console information. About the other thing... I'm going to try it now! :D And I'll put here the results! THANKS!!! :D

PS: Mmm... Jaja, I don't know how to ask... ammm... can you tell me something about the Bone_Control script issue? Anything? It's only for curiosity... PLEASE, I don't want that you think I want to put pressure on you :wink: but you puted some of HOPE over all this and now I can't think in other thing! :roll: Jaja... SORRY :oops: and THANK YOU for all! CIAO!!! :D
User avatar
Rai López
Posts: 2294
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

Hmmm... seems that don't work to me yet... I have tried all the possibilities and 7feet suggestions but nothing :( ...I suppose that there is something that I'm doing wrong, but the script even crash Moho if I apply directly in a parent (not sub-layer) layer instead display an error in Lua Console... Well, I'll go on investigating... BYE! :)
Post Reply