Community
Meshmixer
Welcome to Autodesk’s Meshmixer Forums. Share your knowledge, ask questions, and explore popular Meshmixer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MagWeb's mmApi scripts

71 REPLIES 71
Reply
Message 1 of 72
MagWeb
7689 Views, 71 Replies

MagWeb's mmApi scripts

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

 



Gunter Weber
Triangle Artisan

71 REPLIES 71
Message 41 of 72
MagWeb
in reply to: hfcandrew

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.

 



Gunter Weber
Triangle Artisan

Message 42 of 72
hfcandrew
in reply to: MagWeb

Ya makes sense. Yep and this method works find, no need to change. Thanks!

 

Made any progress on the hotkey?

Message 43 of 72
MagWeb
in reply to: hfcandrew

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)... 



Gunter Weber
Triangle Artisan

Message 44 of 72
hfcandrew
in reply to: MagWeb

I see, I see.

 

Well I got my fingers crossed and hopes high for your MM wizardry skills!

Message 45 of 72
hfcandrew
in reply to: hfcandrew

Hi @MagWeb 

 

I wanted to get your help again with a few things with tool manager.  See screencast: https://autode.sk/2XdPngf and see the .csv created

 

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)

Message 46 of 72
MagWeb
in reply to: hfcandrew

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



Gunter Weber
Triangle Artisan

Message 47 of 72
hfcandrew
in reply to: MagWeb

Hey it has been a godsend so far for me, but there is always room for improvement!

Message 48 of 72
MagWeb
in reply to: hfcandrew

@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.



Gunter Weber
Triangle Artisan

Message 49 of 72
hfcandrew
in reply to: MagWeb

Sure... but my issue wasn't with the 'StoreCam' function at all, that works perfectly fine for me in either 'Perspective' or 'Orthographic'. I just tested out what you asked and I could produce no errors. I did have a problem with this in v1 a few months ago but you already fixed that.

Message 50 of 72
hfcandrew
in reply to: MagWeb

How goes it my friend, any developments?

Message 51 of 72
hfcandrew
in reply to: hfcandrew

Sorry to request your assistance again @MagWeb , but I was just put the mmapi/toolmanager on another computer and I can't seem to get it to run, I am getting this error message:

 

Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:30:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
====== RESTART: C:\Users\PC\Documents\meshmixerpython\ToolManager_V3.py ======
<open file 'C:\\Users\\PC\\Documents\\meshmixerpython\\_mmapi.pyd', mode 'rb' at 0x03FD6390>
C:\Users\PC\Documents\meshmixerpython\_mmapi.pyd
('.pyd', 'rb', 3)

Traceback (most recent call last):
File "C:\Users\PC\Documents\meshmixerpython\ToolManager_V3.py", line 13, in <module>
import mmapi
File "C:\Users\PC\Documents\meshmixerpython\mmapi.py", line 29, in <module>
_mmapi = swig_import_helper()
File "C:\Users\PC\Documents\meshmixerpython\mmapi.py", line 25, in swig_import_helper
_mod = imp.load_module('_mmapi', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.
>>>

 

Which is strange because mmapi.py is 100% within my 'meshmixerpython' directory.

 

Any idea?

Message 52 of 72
MagWeb
in reply to: hfcandrew

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???



Gunter Weber
Triangle Artisan

Message 53 of 72
MagWeb
in reply to: MagWeb

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...



Gunter Weber
Triangle Artisan

Message 54 of 72
hfcandrew
in reply to: MagWeb

Same error message:

 

=========== RESTART: C:\Users\PC\Documents\meshmixerpython\test.py ===========
<open file 'C:\\Users\\PC\\Documents\\meshmixerpython\\_mmapi.pyd', mode 'rb' at 0x03A93C28>
C:\Users\PC\Documents\meshmixerpython\_mmapi.pyd
('.pyd', 'rb', 3)

Traceback (most recent call last):
File "C:\Users\PC\Documents\meshmixerpython\test.py", line 1, in <module>
import mmapi
File "C:\Users\PC\Documents\meshmixerpython\mmapi.py", line 29, in <module>
_mmapi = swig_import_helper()
File "C:\Users\PC\Documents\meshmixerpython\mmapi.py", line 25, in swig_import_helper
_mod = imp.load_module('_mmapi', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.
>>>

 

So I installed SWIG and its mostly running now! Thanks.

 

So ToolManager is working but my embedded version of the labelmaker is not. I'm getting this error message:

 

 

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1547, in __call__
return self.func(*args)
File "C:\Users\PC\Documents\meshmixerpython\labelmaker.py", line 171, in save_data_and_label_objects
self.zoom_to_target_object()
File "C:\Users\PC\Documents\meshmixerpython\labelmaker.py", line 186, in zoom_to_target_object
z = int(self.zoom_factor.value)
AttributeError: LabelMaker instance has no attribute 'zoom_factor'

 

Again any thoughts?

 

Message 55 of 72
MagWeb
in reply to: hfcandrew

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")?



Gunter Weber
Triangle Artisan

Message 56 of 72
hfcandrew
in reply to: MagWeb

Ahh yes, sorry I forgot I have made several changes to the labelmaker to make it easy to configure the screenshot settings for different versions of windows, monitors, screen resolutions and different way my staff position the MM window (full screen vs partial etc.)

 

Furthermore just in time for your response, I realized somehow my defaultparamaters.csv I use to store the various parameters got its content deleted out, was just a blank file, so I have restored it. So that error message is no longer occurring. But now there is still another new issue. The screen shot is not being taken, just a black background is being created with the labels overlaid.

 

See attached for a .zip of my toolmanagerv5b (which has F2+F3 for hotkeys enabled for playdown+playall)  fyi done via:

 

import keyboard

keyboard.add_hotkey('F2', lambda: Continue_Cue())
keyboard.add_hotkey('F3', lambda: Auto_Set())

 

and it has the labelmaker button embedded on it too.

 

and the .csv for the parameters, raw screenshot (just a big black screencap, and the cropped & labelled map image created, and the .mix used to create it.

 

Message 57 of 72
hfcandrew
in reply to: hfcandrew

zip attachment didn't go. Use this google drive link: https://drive.google.com/open?id=1JTvm9U6zdZvCJheIe1mj0cQ-CUxSC-oS

Message 58 of 72
MagWeb
in reply to: hfcandrew



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()

 

 

 



Gunter Weber
Triangle Artisan

Message 59 of 72
hfcandrew
in reply to: MagWeb

1) Yep that fixed it.

2) Yep that is also fixed

3) Nope still seems to be the same issue, only works accurately within the same scene it was created, same for the stored camera views. Also in v3 I had the ability in one of my work flows to go from a 'soft transform' with falloff, straight to a 'discard' command of the original selection. But I cannot seem to do that in v3AA, as it gets unselected once the API accepts the transform. This is how it typically works when doing it manually in MM but the API seemed to be able to bypass this before which was a nice surprise. I'm not sure how it was possible in v3 but it is not anymore in v3AA.

 

4?) For the screen shot issue, your code still produced the same result of just a black screenshot. But your clue regarding the video card prompted me to play around with some MM settings. If I turn on 'basic rendering' and turn off 'Ant Aliasing' it not produces the proper screen shot. Works 100%! I'm just curious as to why that would interfere with a very basic screen shot function. Ideally I'd like like this API to function with anti-aliasing enable, MM just feels to sharp and choppy without that smoothing effect.

 

Thanks again!

Message 60 of 72
MagWeb
in reply to: hfcandrew

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.

 



Gunter Weber
Triangle Artisan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report