Author Topic: Help with Max scripting  (Read 3357 times)

2023-10-12, 15:03:26

aaouviz

  • Active Users
  • **
  • Posts: 1003
    • View Profile
    • Another Angle 3D
Man, I suck at maxscript...

I want to make a simple "Save and then Exit" script (via toolbar button).

This is what I have, but I can't quite get it to work as intended. Any helpers out there? Many thanks!

Code: [Select]
--Original script by: aaouviz
------

macroscript SaveAndExit
category: "AAScripts"
buttonText: "Save and Exit"
tooltip: "Save and Exit"
Icon:#("AAGen_01",1)


(
saveMaxFile
)
quitmax #noprompt
« Last Edit: 2023-10-12, 15:51:29 by maru »
Nicolas Pratt
Another Angle 3D
https://www.instagram.com/anotherangle3d/

2023-10-12, 15:32:07
Reply #1

James Vella

  • Active Users
  • **
  • Posts: 678
    • View Profile
I assume you just want File > Save correct? Overwrite previous version?

Code: [Select]
--Original script by: aaouviz
------

macroscript SaveAndExit
category: "AAScripts"
buttonText: "Save and Exit"
tooltip: "Save and Exit"
Icon:#("AAGen_01",1)
(
        max file save
quitmax #noprompt
)

2023-10-12, 20:39:34
Reply #2

aaouviz

  • Active Users
  • **
  • Posts: 1003
    • View Profile
    • Another Angle 3D
Amazing, and perfect as always James.

Thanks a million!
Nicolas Pratt
Another Angle 3D
https://www.instagram.com/anotherangle3d/

2023-10-12, 21:47:25
Reply #3

James Vella

  • Active Users
  • **
  • Posts: 678
    • View Profile
Glad to hear its working as expected :)

2024-01-11, 09:17:53
Reply #4

Frood

  • Active Users
  • **
  • Posts: 2001
    • View Profile
    • Rakete GmbH
Hi,

picked up the idea but implemented it as file menu item. Additionally, saving only happens if a save is required (no changed made to scene, relying on getSaveRequired()). Script goes to any autostart folder (usually scripts\startup). Sharing it as an alternate option.


Good Luck



Never underestimate the power of a well placed level one spell.

2024-01-11, 10:48:00
Reply #5

aaouviz

  • Active Users
  • **
  • Posts: 1003
    • View Profile
    • Another Angle 3D
Excellent!

The 'saving only happens if a save is required' seemed a bit beyond my capabilities, so I didn't bother. But this is clever of you, and I feel pleased to have I've influenced you, Frood :)
Nicolas Pratt
Another Angle 3D
https://www.instagram.com/anotherangle3d/

2024-01-11, 13:21:42
Reply #6

Frood

  • Active Users
  • **
  • Posts: 2001
    • View Profile
    • Rakete GmbH
Yes, thanks for letting me steal that idea to use them for myself - which is my daily deed ;)


Good Luck



Never underestimate the power of a well placed level one spell.

Yesterday at 17:50:26
Reply #7

aaouviz

  • Active Users
  • **
  • Posts: 1003
    • View Profile
    • Another Angle 3D
Hello all again,

I'm wondering if someone smarter than me can help figure this one out...

I'm trying to automate 'Unwrap UVW' to a default "Flatten: Custom" workflow. (See the picture)

I got a pretty good result with vibe coding (chatGPT) but it can't quite figure out the final, and most important component. It does everything right but doesn't get the right result (or at least not the same result as if I manually press that button).

Please see the code below and offer a suggestion if possible. Many, many thanks!

Code: [Select]
/* ===========================================================
   HELPER – unwrap one object, set channel, run Pack-Custom
   =========================================================== */
fn addUnwrapPackSingle obj mapChannel:2 =
(
    local prevSel = selection as array
    select obj

    -- Add temporary Edit Poly modifier to force full poly selection
    local epMod = Edit_Poly()
    addModifier obj epMod
    epMod.SetSelection #Face #{1..(getNumFaces obj)}
   
    -- Add the unwrap modifier
    local uMod = Unwrap_UVW()
    addModifier obj uMod
    uMod.unwrap.setMapChannel mapChannel
   
    -- Ensure full polygon selection for Unwrap as well
    uMod.unwrap.selectPolygons #{1..(uMod.unwrap.numberPolygons())}

   
    -- Set current modifier to Unwrap
    modPanel.setCurrentObject uMod
   
    -- Run Pack ► Custom
    if isProperty uMod #packNoParams then
    (
        uMod.packNoParams()
    )
    else if isProperty uMod.unwrap #packNoParams then
    (
        uMod.unwrap.packNoParams()
    )
   
    -- Optionally remove the Edit Poly modifier to clean up
    -- deleteModifier obj epMod
   
    -- Restore selection
    select prevSel
)


/* ===========================================================
   BATCH – loop through initial selection one-by-one
   =========================================================== */
fn batchUnwrapPack mapChannel:2 =
(
    local workList = for o in selection where isKindOf o GeometryClass collect o
   
    if workList.count == 0 then
    (
        messageBox "Select one or more geometry objects first!"
        return undefined
    )
   
    with undo off
    (
        for o in workList do
        (
            format ">> Unwrapping & packing : %\n" o.name
            addUnwrapPackSingle o mapChannel:mapChannel
        )
    )
   
    format "=== Finished : processed % objects ===\n" workList.count
)

/* ===========================================================
   RUN   (change the channel if you like)
   =========================================================== */
batchUnwrapPack mapChannel:2
Nicolas Pratt
Another Angle 3D
https://www.instagram.com/anotherangle3d/

Today at 09:35:16
Reply #8

clemens_at

  • Active Users
  • **
  • Posts: 161
    • View Profile
try this:

Code: [Select]
    if isProperty uMod #packNoParams then
    (
        uMod.flattenMapNoParams()
    )
    else if isProperty uMod.unwrap #packNoParams then
    (
        uMod.unwrap.flattenMapNoParams()

also, I don't think you need the edit poly and force selection - so the simplified function:

Code: [Select]
fn addUnwrapPackSingle obj mapChannel:2 =
(
    local prevSel = selection as array
    select obj
   
    -- Add the unwrap modifier
    local uMod = Unwrap_UVW()
    addModifier obj uMod
    uMod.unwrap.setMapChannel mapChannel

    -- Set current modifier to Unwrap
    modPanel.setCurrentObject uMod
   
    -- Run Pack ► Flatten
    if isProperty uMod #packNoParams then
    (
        uMod.flattenMapNoParams()
    )
    else if isProperty uMod.unwrap #packNoParams then
    (
        uMod.unwrap.flattenMapNoParams()
    )
   
    -- Restore selection
   select prevSel
)