MagWeb's mmApi scripts

MagWeb's mmApi scripts

Anonymous
Not applicable
14,073 Views
71 Replies
Message 1 of 72

MagWeb's mmApi scripts

Anonymous
Not applicable

Instead of spreading scripts all over the forum I'll post them in this thread from now on.

 

1 Script:

ToolManager 

 

On a user's request I repost a message I did at MM's old forum on March 23, 2018, 01:30:51 PM:

Download below

"

Here’s a long raw script meant to run api commands without coding a .py file. Well, you’ll need the api to run (important instructions at post’s bottom) or compile the script to a standalone application (recommended it’s easy and seems to be faster) but after that it’s something I find very useful. I post it as it is without any warranty at your own risk in the hope you find it useful and maybe some better coder polishes this…

Why to use it:
For some tools MM remembers the last used parameter setting. For some tools or special parameters it always loads hard coded default values entering a tool. Now what to do if you want the parameters at a step before „last“ or different to defaults? In MM you always have to set this manually - hopefully your own brain remembers all parameters…..

What this script does:
With this ToolManager you can save current tool parameters and reload them. Means you can save several settings for the same tool and others as well.
As there is no direct function to get the current tool’s name in mmApi and it needs to be known to call this very tool and its parameters again, I use a pretty ugly workaround:
There’s a long list of tools and their parameters (BTW: There are more parameters than listed in the documentation or StoredCommands.h, so see this list as a reference too). As the tools have unique (one exception only: Bridge and Handle use the same parameters!) combinations of parameters one can find the tool itself. So the code iterates through the list of params trying to get that kind of value from MM. If MM responds with an empty list the very parameter isn’t currently available so skip to the next…. This ends in a list of available values and a toollname fitting to this combination which is saved in ToolManager.

Usage:
+Standard Tool features:
- Hitting „Save Tool“ button will save current tool settings to the list at top if MM is in some tool. So do your settings in MM and hit „SaveTool“ before leaving the tool. You may do several saves while being in the same tool too.
- The new entry in the list will be called „setting“ + some number by default. RMB-double click (on MAC),  MMB-double click (on WIN) an item in the list opens a dialog where you can set a more meaningful description.
- Hitting „Delete“ will remove an selected item from the list.
- To restore a saved tool setting LMB-double click the item in the list. Note: It will cancel a tool currently open in MM. So if you want to keep the current result hit Accept in MM first.
- To change saved parameters in a saved setting, double click the item to run the tool with saved settings. Now change your settings  in MM and hit UpdateTool in ToolManager. New settings are saved to the item now.
+ AutoSet features:
- Clicking AutoSet will perform listed tool actions from top to bottom and automatically accept them. This way you can perform some kind of simple „scripting“ .
- There are tools in MM not owning some parameters (e.g. Duplicate). Therefor they can’t be detected by my approach as described above. You can add such tools at the list’s bottom by hitting AddCom. A dialog will pop up where you can choose such a command to add.
- You can copy a selected item hitting the Copy button it will be copied to the bottom of the list.
- You can move an item up/down LMB-dragging it in the list. This is needed to get the right order of commands running AutoSet
- By default all added items in the list are active while running AutoSet. If you want to exclude an item without deleting it use the +/-Auto button. This toggles between active and inactive which greys-out  selected item in the list.
I/O:
- You can save the current set of saved tools to a .csv file hitting SaveSet
- You can load such a set via LoadSet. This will replace the current list

Limitations:
- Using mmApi sends each parameter one by one. So expect some „motion“ in the scene while restoring tool settings of tools using a transformation widget.
- There’s no way I found to get/save the brush-type,-falloff,-color,-stencil in SCULPT so it will still use the last used one. Against that all other brush parameters can be saved (even symmetry plane settings)
- In case of SELECT/Edit/Handle and Bridge his approach can’t decide which was used. It will always think it’s Handle (maybe fixed by a decision dialog in the future)
- Don’t expect advanced mmApi stuff on this level of simply calling tools.
- Maybe bad, inefficient coding for now but working….

################
How to run this script:
- Download and setup mmApi using instructions at: https://github.com/meshmixer/mm-api
- Download, unzip attached file „ToolManager.py“ to mm-api-master/distrib/python (or python_osx on MAC)
- Edit def get_toolparam_mat3f(remote, param_name): in tool.py (mm directory) that way:

def get_toolparam_mat3f(remote, param_name):
    """Returns the current value of the given Tool parameter, or empty list if the parameter is not found."""
    cmd = mmapi.StoredCommands()
    key = cmd.AppendGetToolParameterCommand(param_name)
    remote.runCommand(cmd)
    m = mmapi.mat3f()
    bFound = cmd.GetToolParameterCommandResult(key, m)
    if bFound:
        return tuple(m.m) #MagWeb: added output as tuple
    else:
        return ()

tuple(m.m) is needed to return 9 element tuples instead of SWIG objects in case of rotation matrices
##################

enjoy

"

 

You can do stuff like this:

 

 

 

Screencast will be displayed here after you click Post.

bf8ff1a3-6198-4d7d-8e47-90d793fed1fb

 

14,074 Views
71 Replies
Replies (71)
Message 41 of 72

Anonymous
Not applicable

That very PlayDown issue happens on tools requiring an active selection.

I force a cue to pause if there's a tool requiring a selection and switch to SELECT automatically.

Now if you do a PlayDown it accepts the current tool which is SELECT now.

In case of a pause due to a missing selection (if required) the code needs to store the pause causing tool (and its settings), fall to SELECT to allow a selection and restore the pause-causing tool at PlayDown. Think that's doable but not implemented yet.

For now you found the proper solution in adding a SELECT step before a break.

 

0 Likes
Message 42 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 43 of 72

Anonymous
Not applicable

The hotkey thing without disturbing the MM-to-API dialog isn't that easy and possible solutions I found aren't cross platform...Still tying to find some workaround solution.

I thought of using "1" which calls a forgotten old smooth brush (without dynamic tessellation) tool in MM - but unfortunately that tool isn't sent (or I can't find the right string to detect it)... 

0 Likes
Message 44 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 45 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 46 of 72

Anonymous
Not applicable

Gonna look into it - seems there's something broken in V3 😞

0 Likes
Message 47 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 48 of 72

Anonymous
Not applicable

@hfcandrew :

Just found: Restoring a saved cam in MM properly works only in perspective view.

Could you try your issue in perspective instead of orthographic view?

Know, orthographic is needed for stuff like SelectVisible. But if the view mode is the reason for this issue, I could force to switch perspective view to set the cam and back to orthographic view after that.

0 Likes
Message 49 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 50 of 72

hfcandrew
Advisor
Advisor

How goes it my friend, any developments?

0 Likes
Message 51 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 52 of 72

Anonymous
Not applicable

This seems not to be related to the Toolmanager script.

The error occurs running mmapi.py on "import mmapi"...

Can you run test.py (import a bunny to MM and run the code) or another example python code?

Not sure about default Python content on WIN: Maybe you need to install SWIG???

0 Likes
Message 53 of 72

Anonymous
Not applicable

If you've a working mmApi installation on one machine you can compile the code to a standalone application on that machine. The exe should work on different machines without need to install python or mmApi.

Never did this for WIN but py2exe should do it...

0 Likes
Message 54 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 55 of 72

Anonymous
Not applicable

If your labelmaker script works fine on your old machine:

Which SWIG version did you install on your new machine?

By default mmApi is built against swig-3.0.12.

Porting mmApi to Python3 I used swig-4.0.0. Here I found that it might be necessary to edit mm module functions to understand swig-4.0.0 objects properly.

Can you send me your labelmaker code (the one you posted here doesn't use a "zoom_factor")?

0 Likes
Message 56 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 57 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 58 of 72

Anonymous
Not applicable


Found some time to look into the issues you posted at 11-12-2019


@hfcandrew wrote:

1) Update tool isn't working, I get an error message too. See image attached.

2) Transformations of exactly 180 degree rotations do something really strange.

3) The the data stored for xyz translations only works for that specific scene in which they were created (KOOL.mix in my case), if I were to run them in a different scene they are completely off. (Rotations are fine though)


ad1) There's a little mistake in your modified version of the ToolManager (>V5b: line 390):

if param_list[i][ii] == "origin":

should be:

if param_list[i] == "origin":

without [ii] indexing.

 

ad2) 180° issue  (happens on 90° and -90° too) is caused by a wrong regex formula I did. At those angles either sin or cos are equal zero. Instead of a 0.0 value MM sends a "near to zero" value in exponential form (e.g. 8.742277657347586e-08). The old formula cropped off the exponent (e-08) and returned only the basis (8.742277657347586). That caused these strange distortions. The "bracket" query in def ToolSet(src):

values = re.findall(r"([-+]?\d*\.?\d+[e]?[+-]?\d+?|\d*\.\d+|d+)", bracket)

needs to be:

values = re.findall(r"[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?", bracket)

Note: In MM rotations can't be set absolute to some world axis. Rotations  are relative to the current directions.

 

ad3): I think I've modified the code to allow to use a stored .csv on another scene with a different scene/world ratio now. I made several changes for the Transform tool.... Try attachment. Hopefully I didn't break other stuff. Let me know when you find some issues.

----------

Your screenshot issue might be some issue related to the graphic card too. Can you get a screenshot via that code ?

You should find the screenshot in the directory you saved the .py code to.

import os
import mmapi
from mmRemote import *

remote = mmRemote()
remote.connect()

cmd2 = mmapi.StoredCommands()
cmd2.AppendSceneCommand_SaveScreenShot(os.path.join( os.getcwd(), "screenshot.png" ))
remote.runCommand(cmd2)

remote.shutdown()

 

 

 

0 Likes
Message 59 of 72

hfcandrew
Advisor
Advisor

.

0 Likes
Message 60 of 72

Anonymous
Not applicable

Just a short re...

The modifications I did to fix absolute coordinates issue on different scenes were concentrated on EDIT/Transform not on other tools (as selection based face- or smoothTransform). Did you try this too? This works fine here even on completely different scenes.

 

0 Likes