Working out script improvements

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
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Working out script improvements

Post by synthsin75 »

I figured I'd continue this conversation (with Vern mostly) in a new thread.

I still need to write up the code and debug it, but I'm positive that I have figured out a way to find out each layer's visible (eyes checkbox) setting. I was working out a way to get all of the layer objects (MohoLayer) for improvements to my select layer script, and it turns out that the method I'm using to get these through the script interface is exactly the same order that the layers appear in the file format.

So I can just parse the file format for 'visible' and add these to a table in the order they're found. Matching the indices of the two tables will allow me to exclude non-visible layers from being selected.

-------------------------------------------------------------

This got me thinking. My select layer tool behaves oddly once the layer order has been animated. It searches through layers based on the frame zero order (which appears to be the base value for layer sorting). But I can now get the sorting values and fix this (though I haven't yet figured out how to implement that).


-----------------------------------------------------------

Using this layer object table, I think I can even manage to (eventually) get a mesh and bone instance to work without the original layers being embedded with the layer script.

Do layer scripts treat the layer they are embedded in as the 'current' layer (i.e. moho.layer)? If so, how would I get the currently selected layer in a layer script?

--------------------------------------------------------

Just sort of thinking outloud for now. As soon as I have something useful, I'll post it. If anyone wants any details on any of this, just ask. :wink:
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

This is fantastic news Synth.

As for the script working on other layers... hmm... it might work.

I have not tested this but here goes my "theory":

A script on layer A. A variable is declared that represents that layer object. Now find a new layer object through whatever means you want. Assign THAT layer object to the variable THEN do what needs to be done.

Or, add several layer objects to a table and process a bunch of them. The only way to know if this will work is to do a simple test like printing the names of shapes or whatever of the found layers.

This sounds... too simple. Like so simple why didn't I think of it before? The news about the layer finding in the file format is great though. I may have gotten confused about it before. It could be that I was sorting a table which is always in a funky order. Tables are never ordered unless they are indexed in someway. That script and the layer problems I had was a loooong time ago. I may not have had the "mad" skills I do now. ;)

I would LOVE to see the code you are working on for finding the visible property if I can.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

A script on layer A. A variable is declared that represents that layer object. Now find a new layer object through whatever means you want. Assign THAT layer object to the variable THEN do what needs to be done.
I'm not sure I follow that. To make those layer scripts do what I want, I'll need the currently selected layer stored to a variable. That way I can select that layer again after selecting through other layers to look for, say, a specific layer name.

All that may be a bit to much to do on each frame though. We'll see.
Tables are never ordered unless they are indexed in someway.
Like I said, I don't have too much practice with tables yet, but I think something like this will work for indexing:

Code: Select all

table= {}
n=0
table= {n, value}
n=n+1
I would LOVE to see the code you are working on for finding the visible property if I can.
Don't have it typed up yet. Sadly I do most of my scripting at work where I only have pen and paper (and my memory).

Basically this is the rough sketch. Open the current document in read mode and loop through the lines searching for 'visible'. Capture the following boolean and add these to a table that is incrementally indexed. Pretty straight forward.

The real trick is getting all of the layer objects.

Code: Select all

document = moho.document
  topid = document:CountLayers() -1
  layer = moho.layer
  
  for i = topid, 0, -1 do
    local topobj = document:Layer(i)
    print(i..tostring(topobj))
    if (topobj:IsGroupType()) then
      subid = moho:LayerAsGroup(topobj):CountLayers() -1
      for i = subid, 0, -1 do
        local subobj = moho:LayerAsGroup(topobj):Layer(i)
        print('>'..i..tostring(subobj))
        if (subobj:IsGroupType()) then
          level = '-'
          subsubid = moho:LayerAsGroup(subobj):CountLayers() -1
          for i = subsubid, 0, -1 do
            local subsubobj = moho:LayerAsGroup(subobj):Layer(i)
            print(level..'>'..i..tostring(subsubobj))
            if (subsubobj:IsGroupType()) then
              sssid = moho:LayerAsGroup(subsubobj):CountLayers() -1
              for i = sssid, 0, -1 do
                local sssobj = moho:LayerAsGroup(subsubobj):Layer(i)
                level = level..level
                print(level..'>'..i..tostring(sssobj))
              end
            end
          end
        end
      end 
    end
  end
Obviously I can't keep this up for an almost infinite amount of layer nesting. So I'm thinking I need to make some of this a function that can be repeated as much as needed. I think I know where I need to 'split' the code, but if you have any better ideas on repeating the code, let me know.

:roll: :wink:
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

I'm making good, if aggravating, progress, but I discovered a simple trick I thought I'd share.

I think it was brought up in the discussion about pose array that you couldn't get out of editing an action by script. I don't remember the details of needing this though.

Code: Select all

layer = moho.layer
  layer:ActivateAction('walk')
  moho:UpdateUI()
  moho:SetSelLayer(layer)
Just select the same layer and it will revert to the main timeline. The UI update is just so I could see it happen. I originally thought I'd need to select another layer in the group without selecting the group first, but I started out testing this with one layer in one group.

So I just tried it anyway, and it worked.

Anyone remember why this may be useful?
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

This is HUGE for me!

My "load animation" script. I have it so you can load to an action but there is a problem that I couldn't "load" the main time line after creating and activating an action. This will solve that problem. It was a big sticking point for me.

THANKS DUDE! Cool beans!

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

I knew someone was needing that, just couldn't remember.

Whenever I get kind of bogged down working on one script I'll brainstorm about some of my other script ideas. Sometimes it's useful, but more often than not, it just ends up adding to my scripting 'to do' list.

-----------------------------------------------------------------------

I've got my layer object table built, but I've realized a couple of problems. I could really use some feedback on these.

1) Should I only make 'select layers' work on frame zero?

On frame zero, I can guarantee that it avoids layers that are non-visible (by the eyes checkbox). But above frame zero, if there is any layer sorting, it will throw off my tables.

If layer sorting wasn't such a great feature, I'd be tempted to complain about how hard it is to script around.

2) Right now any tool using 'select layers' will be unusable for actions. Should I disable it in actions?

I plan on making it so that it always selects the layers (to search for shapes/curves) through the group layers, but once again any layer sorting will foul me up. I have a couple of ideas on getting and using the sorting data, but they may all flop.

:? :wink:
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

My opinons are based on how I work. I use bones mostly. I don't do a lot of point animation and wouldn't need the layer selection tool beyond frame 0. don't fall into the trap I always do... getting stuck because it won't work for EVERY situation. If it works only on frame zero and not with actions... even some LM tools have that problem.

My advice would be to have it work on frame zero only and not in actions. That is where a majority of the work is done and also when you would need it the most in my opinion.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Thanks for the feedback, Vern. I'll post it as soon as I think it's ready for use again. :wink:
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Okay, I'm making really good progress. It shouldn't be too much longer now.

I solved the issue of not being able to check a layers 'visible' setting on frames>0. I've gained a TON of practice with tables. Had to, as this tool needs at least four or five tables that I can compare to each other. I love tables now.

It still won't work for actions, though I may get to that later. I also have to avoid searching any sub-layers of a switch, as the inactive layers would be constantly selected.

One thing I need to do is be able to store a table. Is the only was to do that to write it to an external text file? It doesn't seem like the prefs could store a table, off-hand.

I need to store the last 'visible' settings table so that it can be compared to the current 'visible' table. If they are different, then I'll use a warning to tell the user they need to save the file in order to update that info.

If I'm not mistaken, the only other way to handle that would be to save the AS file to a temporary location to read these, but that probably would require like AutoIt and run into that open instance problem you were having.


Let me know if you have any better ideas for storing a table. Right now, I'm thinking I'll just strip the root drive out of the AS file's path. That way there's only one of these 'table' files that can be overwritten instead of a bastard file for every single AS file.


----------------------------------------------------------
I'm planning on having two versions of this tool. One will exist only in a utility to be called by other tools, and it will be written to keep that vector tool active. (The current one disables the tool if you arrow to a non-vector layer) The other will be the standalone tool, and will include layer selection by bones as well. I'm thinking this one will reference a tool for each layer type.

I'll post the utility script for you to test out here when I get it done. :wink:
Post Reply