Page 1 of 1

getting group name

Posted: Tue Aug 31, 2021 4:54 pm
by davoodice2
hi
is there function to get parent of layer that is not selected?
for example

moho.layer:Parent("layerName")

Re: getting group name

Posted: Tue Aug 31, 2021 4:57 pm
by synthsin75
As long as you're storing the unselected layer object (moho.layer) to a variable, even with it unselected, you can do:

var:Parent()
and
var:Parent():Name()

Re: getting group name

Posted: Tue Aug 31, 2021 6:04 pm
by davoodice2
synthsin75 wrote: Tue Aug 31, 2021 4:57 pm As long as you're storing the unselected layer object (moho.layer) to a variable, even with it unselected, you can do:

var:Parent()
and
var:Parent():Name()
thanks. and how can I select a layer ? SetSelLayer()? what is it's argument? is there any better way?

I want store current selected layer to select it later.
how to get current selected layer ID?

Re: getting group name

Posted: Tue Aug 31, 2021 6:30 pm
by hayasidist
if you've got the current active layer and decided that the right thing is to select a different one, make sure that you have a variable that addresses a MohoLayer object (e.g. var = moho.layer:Parent() to get the active layer's parent) then

moho:SetSelLayer(var) will selected the layer identified by "var" in the layers window

ScriptInterface:SetSelLayer(layer, multiSelect, allowDeselect) : see https://mohoscripting.com/methods/60

As Wes has said, you don't need the layer to be selected to work on it ... so (e.g.) you can find the parent, then do something with some / all the child layers but still leave the one original child layer selected

Re: getting group name

Posted: Tue Aug 31, 2021 7:10 pm
by davoodice2
hayasidist wrote: Tue Aug 31, 2021 6:30 pm if you've got the current active layer and decided that the right thing is to select a different one, make sure that you have a variable that addresses a MohoLayer object (e.g. var = moho.layer:Parent() to get the active layer's parent) then

moho:SetSelLayer(var) will selected the layer identified by "var" in the layers window

ScriptInterface:SetSelLayer(layer, multiSelect, allowDeselect) : see https://mohoscripting.com/methods/60

As Wes has said, you don't need the layer to be selected to work on it ... so (e.g.) you can find the parent, then do something with some / all the child layers but still leave the one original child layer selected
thanks. but can you give me an example of 2 line code for store current layer and select that later? I dont understand var in lua. Take it easy on me

Code: Select all

function DV_select_Bone_Layer:Run(moho,group)

	local isFound = false
	local prev_name = moho.layer:Name()

	local parent = moho.layer:Parent()
	if (parent == nil) then
		LM.Beep()
		moho:SetSelLayer(prev_name)
		return -1
	end
	moho:SetSelLayer(parent)
	local layet_type = moho.layer:LayerType(parent)
	if(layet_type==MOHO.LT_BONE) then
		isFound=true
		return -1
	end
    while not isFound do	
		moho:SetSelLayer(parent)
		parent = moho.layer:Parent()
		if parent == nil then
			LM.Beep()
			print(prev_name)
			moho:SetSelLayer(prev_name)
			break	
		end	
		layet_type = moho.layer:LayerType(parent)
		if(layet_type==MOHO.LT_BONE) then
			isFound=true
		end
	end


end
moho:SetSelLayer(prev_name) doesn't work

Re: getting group name

Posted: Tue Aug 31, 2021 7:20 pm
by synthsin75
SetSelLayer doesn't accept names, as more than one layer could have the same name. It needs the layer object.

local layer_object = moho.layer
moho:SetSelLayer(layer_object)

Re: getting group name

Posted: Tue Aug 31, 2021 7:37 pm
by hayasidist
davoodice2 wrote: Tue Aug 31, 2021 7:10 pm thanks. but can you give me an example of 2 line code for store current layer and select that later? I dont understand var in lua. Take it easy on me
var is just an arbitrary variable name

replace the name "layer_object" that Wes used with "var"...
local var = moho.layer
moho:SetSelLayer(var)

If you're struggling with Lua: Moho currently uses 5.2: http://www.lua.org/manual/5.2/

Re: getting group name

Posted: Tue Aug 31, 2021 7:49 pm
by davoodice2
Perfect!
thank you two friends.

Re: getting group name

Posted: Thu Sep 02, 2021 12:34 pm
by davoodice2
davoodice2 wrote: Tue Aug 31, 2021 6:04 pm
synthsin75 wrote: Tue Aug 31, 2021 4:57 pm As long as you're storing the unselected layer object (moho.layer) to a variable, even with it unselected, you can do:

var:Parent()
and
var:Parent():Name()
thanks. and how can I select a layer ? SetSelLayer()? what is it's argument? is there any better way?

I want store current selected layer to select it later.
how to get current selected layer ID?
Can i select parent layer of a layer that isn't selected ?

Re: getting group name

Posted: Thu Sep 02, 2021 1:18 pm
by hayasidist
yes - as long as you have (or can get) the MohoLayer object for the layer whose parent you want -- it doesn't have to be the selected layer -- e.g.

you have a selected layer: (moho.layer)
you find its parent: parent = moho.layer:Parent() (which is not the selected layer!)
and now you want the "grandparent": gp = parent:Parent()

if you then want to make that the selected layer: moho:SetSelLayer(gp)

or if you just want to see all the gp's children..
gp = moho:LayerAsGroup(gp)
for i = 0, gp:CountLayers()-1 do
.. child = gp:Layer(i)

Re: getting group name

Posted: Thu Sep 02, 2021 2:22 pm
by davoodice2
hayasidist wrote: Thu Sep 02, 2021 1:18 pm yes - as long as you have (or can get) the MohoLayer object for the layer whose parent you want -- it doesn't have to be the selected layer -- e.g.

you have a selected layer: (moho.layer)
you find its parent: parent = moho.layer:Parent() (which is not the selected layer!)
and now you want the "grandparent": gp = parent:Parent()

if you then want to make that the selected layer: moho:SetSelLayer(gp)

or if you just want to see all the gp's children..
gp = moho:LayerAsGroup(gp)
for i = 0, gp:CountLayers()-1 do
.. child = gp:Layer(i)
its related to current selected object. I want to select parent of a layer that isn't select now. for eg:
i selected foot layer. but I want to select parent of head layer.

Re: getting group name

Posted: Thu Sep 02, 2021 5:20 pm
by synthsin75
davoodice2 wrote: Thu Sep 02, 2021 2:22 pm its related to current selected object. I want to select parent of a layer that isn't select now. for eg:
i selected foot layer. but I want to select parent of head layer.
Store the head layer object to a variable and you can select its parent layer any time you like.

And if you haven't stored the layer object, as long as you have some way to identify the layer, like unique name, tag, UUID, etc., you can always iterate through all the layers in the document until you find it: https://mohoscripting.com/snippets/1

Re: getting group name

Posted: Fri Sep 03, 2021 5:51 am
by davoodice2
synthsin75 wrote: Thu Sep 02, 2021 5:20 pm
davoodice2 wrote: Thu Sep 02, 2021 2:22 pm its related to current selected object. I want to select parent of a layer that isn't select now. for eg:
i selected foot layer. but I want to select parent of head layer.
Store the head layer object to a variable and you can select its parent layer any time you like.

And if you haven't stored the layer object, as long as you have some way to identify the layer, like unique name, tag, UUID, etc., you can always iterate through all the layers in the document until you find it: https://mohoscripting.com/snippets/1
if want to tell more clearly. I select a layer and i want to find all parents of this layer and do some thing on them. use for loop or some thing like that.
I think it isn't good if i write:
moho.layer:Parent():Parent():Parent():.....

Re: getting group name

Posted: Fri Sep 03, 2021 9:11 am
by hayasidist
davoodice2 wrote: Fri Sep 03, 2021 5:51 am I think it isn't good if i write:
moho.layer:Parent():Parent():Parent():.....
that will work unless you hit the end of the "family tree" -- see my previous post about getting a grandparent -- add to that a test to see if there's a parent.. e.g if not gp then ...

Code: Select all

parent = moho.layer:Parent()
if parent then
	gp = parent:Parent()
	if gp then ...
or you can get cute and use recursion to seek out the root:

Code: Select all

function getParent (layer)
	local parent = layer:Parent()
	if parent then
		getParent (parent)
		...
	...
end

Re: getting group name

Posted: Fri Sep 03, 2021 2:07 pm
by davoodice2
hayasidist wrote: Fri Sep 03, 2021 9:11 am
davoodice2 wrote: Fri Sep 03, 2021 5:51 am I think it isn't good if i write:
moho.layer:Parent():Parent():Parent():.....
that will work unless you hit the end of the "family tree" -- see my previous post about getting a grandparent -- add to that a test to see if there's a parent.. e.g if not gp then ...

Code: Select all

parent = moho.layer:Parent()
if parent then
	gp = parent:Parent()
	if gp then ...
or you can get cute and use recursion to seek out the root:

Code: Select all

function getParent (layer)
	local parent = layer:Parent()
	if parent then
		getParent (parent)
		...
	...
end
Oh God how dumb I am.
I had think I can get parent just with moho.layer:Parent()
now i find out layer:Parent() because of inheritance