Home Technical Talk

First Macro, please help!

polycounter lvl 11
Offline / Send Message
butt_sahib polycounter lvl 11
Hello everyone!

So ive been really interested in scripting and the sorts and i thought i should give it a try.

Basically what i am trying to do:-
Im making a Blueprint manager. What it would do is let you select images (Front,Top,Left,RIght,Back) and create planes with the same measurements as the image and place it in the viewports. So basically,you could easily just plug in your images and hit create to get them.
I also added in a Freeze Images and Darken images options. The former is pretty easy to do i guess.For the darkening, ive decided to have a function to reduce the gamma of the images.People who usually use blueprints will understand why i have the darken option there.
macroscript BlueprintManager 
Category: "Butt tools" buttontext: "Butt tools"
----------------------------------------------
--Errors and mistakes expected
--Thanks to Polycount and CGEmpire
----------------------------------------------
(
    local ROL_Bmanager
    local PIC_Front
    local TEX_Front



--ROLL-OUT    
----------------------------------------------    
    rollout ROL_Bmanager "Blueprint Manager" width:193 height:250
    (
     -- Set up the controls on our interface

        button BTN_Create "Create!" pos:[110,211] width:70 height:24
        button BTN_Front "Front" pos:[13,25] width:70 height:26 enabled:true
        button BTN_Top "Top" pos:[110,25] width:70 height:24
        button BTN_Left "Left" pos:[13,55] width:70 height:24
        button BTN_Right "Right" pos:[110,55] width:70 height:24
        button BTN_Back "Back" pos:[13,85] width:70 height:24
        GroupBox LBL_select "Select Images" pos:[6,3] width:180 height:120
        GroupBox grp3 "Options" pos:[7,127] width:180 height:73
        checkbox CHK_Darken "Darken Image(s)" pos:[20,146] width:133 height:26
        checkbox CHK_Freeze "Freeze Image(s)" pos:[20,172] width:133 height:26
        label LBL_author "Author: Saad Butt " pos:[3,215] width:97 height:15
        label lbl3 "buttsahib.wordpress" pos:[3,230] width:97 height:15
    
        -- Set what each control does when it's used
        --For Front
        ---------------------------------------------
        
        on BTN_Front pressed  do
        (
        PIC_Front = selectBitmap caption: "Select Front Image" 
        
        If PIC_Front != undefined then
        PIC_Front = TEX_Front
        TEX_Front.bitmap=bitmaptexture
        X = TEX_Front.length
        Y = TEX_Front.Height
        Front= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
        Filepath=TEX_Front.filename
        Front.material = standard diffuseMap: (Bitmaptexture fileName:Filepath) showInViewport:true
        showTextureMap P.material on
        Filename=TEX_Front Filepath
        Front.name=Front
        deselect Front
        )
        
        
    )    createdialog ROL_Bmanager
    
)
The code is just for the front as you can see,after i get it working ill add all the sides in.
I have really no idea whats wrong. But after i select the image for the front, i get a -- error which says that the "bitmap" is undefined. All the knowledge i have is from that cgempire tutorial psyko posted and cgtalk and the book i have 3dsmax bible.

Heres what the interface looks like btw:-
3246,blah8EMAD.jpg

Any and ALL help is appreciated!

Replies

  • butt_sahib
    Offline / Send Message
    butt_sahib polycounter lvl 11
    Ok so im getting a hang of it :D shouldnt be such a wuss i thought.
    Will update soon. Have got some things working :D
  • butt_sahib
    Offline / Send Message
    butt_sahib polycounter lvl 11
    ok so i Fking hate this. Max crashed for some goddamn reason and when i loaded the script it was working?! WTF :(
    So i had to write it again. It was working perfectly before (after my first post) but now im getting this error "cannot covert bitmaptexture to type:TextureMap"
    I have no idea why im getting this as it was working before.
    But here is the related code:
    --For Front
            ---------------------------------------------
            
            on BTN_Front pressed  do
            (
            
            PIC_Front = selectBitmap caption: "Select Front Image" 
            TEX_Front = bitmaptexture ()
            TEX_Front.bitmap = PIC_Front
            TEX_Front = PIC_Front
            X = TEX_Front.height
            Y = TEX_Front.width
            F= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
            .name = "Front"
            .wirecolor = color 0 0 0
            frontRotationObject = eulerangles 90 0 0
            rotate  frontRotationObject
            .material = meditMaterials[1] 
            meditMaterials[1].diffuseMap = bitmaptexture
            bitmaptexture = TEX_Front  
            showTextureMap .material on   showInViewport:true
            deselect F
            )        
            
        )    
        createdialog ROL_Bmanager
        clearSelection()
    


    man i was so happy :/ please help :(
  • Mark Dygert
    1st thing that jumps out at me, is that you're not defining anything before ".name" ".wirecolor" or ".material".
    So it should bomb on ".name" or ".n"

    For example if you where trying to change the wire color of the selected object you would use "$.wirecolor 0 0 0" $ is used for selection.

    There are other problems after that, but that will solve your immediate problem and since you're learning I won't hold your hand and write it for ya. You're getting it, just keep plugging away. I suggest getting the Autodesk approved, MaxScript Essentials book.
  • butt_sahib
    Offline / Send Message
    butt_sahib polycounter lvl 11
    thanks for checking in vig :)
    I have no idea why,but this is teh actual code. It somehow changes when i put code tags :/

    PIC_Front = selectBitmap caption: "Select Front Image"
    TEX_Front = PIC_Front
    X = TEX_Front.height
    Y = TEX_Front.width
    F= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
    Filepath = TEX_Front.filename
    Filename = getFilenameFile Filepath
    $Plane01.name = "Front"
    $Front.wirecolor = color 0 0 0
    frontRotationObject = eulerangles 90 0 0
    rotate $front frontRotationObject
    $Front.material = meditMaterials[1]
    meditMaterials[1].diffuseMap = PIC_Front showInViewport:true
    deselect F
    )

    Everything works right till the second last line of code. When i run it, i get a plane,renamed,rotated,black wired,etc
    hehe thanks for making me try my hand at this but PLEASE,for godssake....WTF should i do about the "unabel to convert" problem.just a teenchy weency push would be appreciated! :P looking at all this code is makes me want to jump out my window :)
  • Mark Dygert
    Wow that's weird for some reason the code tags don't like $, lame.

    1) It might be a bad idea to name things the same as a viewport like "front" so change that to something like FrontPlane.

    2) You'll need to define the texture type in material[1] then you can pass it the name/path of your bitmap.

    So instead of using:
    meditMaterials[1].diffuseMap = PIC_Front showInViewport:true

    Use:
    meditMaterials[1].diffusemap = bitmapTexture() -- Creates the proper texture type. Just like clicking the diffuse slot, picking bitmap and then canceling it without selecting a file (the map slot will be blank, but the material type will be set to bitmap which is what the next line needs)
    meditMaterials[1].diffusemap.bitmap = PIC_Front -- Applies the bitmap to the now correctly defined mat diffuse slot.
    showTextureMap $FrontPlane.material $FrontPlane.material.diffuseMap on -- Turns on the diffuse map in the viewport

    What you had was assuming there was already a bitmap "texture type" inside the diffuse slot. It found "none" instead of "bitmap" and then would error out, because "none" does not have a map slot to define.

    You also might want to to make sure that material1 is available, or make sure its set to standard before you start to monkey with it.
    meditMaterials[1] = Standardmaterial () --Will reset the material to standard

    So your button should look like this:
    on BTN_Front pressed do
    (
    PIC_Front = selectBitmap caption: "Select Front Image"
    TEX_Front = PIC_Front
    X = TEX_Front.height
    Y = TEX_Front.width
    F= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
    Filepath = TEX_Front.filename
    Filename = getFilenameFile Filepath
    $Plane01.name = "FrontPlane"
    $FrontPlane.wirecolor = color 0 0 0
    frontRotationObject = eulerangles 90 0 0
    rotate $FrontPlane frontRotationObject

    meditMaterials[1] = Standardmaterial ()
    $FrontPlane.material = meditMaterials[1]
    meditMaterials[1].diffusemap = bitmapTexture()
    meditMaterials[1].diffusemap.bitmap = PIC_Front
    showTextureMap $FrontPlane.material $FrontPlane.material.diffuseMap on
    clearSelection()
    max zoomext sel all

    )

    UI Suggestions:
    - Instead of regular buttons use MapButton, MaterialButton or maybe just PickButton?
    - Don't actually create the planes until you click create, heh.
    - Might be easier to use check buttons instead of boxes for the options, might save space also since your title function take up the same visual space.
    - Simplify how many planes you need. I bet you could get by with, Front, Side, Top.
  • butt_sahib
    Offline / Send Message
    butt_sahib polycounter lvl 11
    @ vig:
    You have no idea how much help you have been.seriously. thank you very much :)
    Oh and you edited the post later? :D
    Sweeet. Ill be sure to read it once i up this post and my throbbing err.headache subsides:)

    UPDATES THANKS TO VIG
    Btw,some of the things addressed would/should definitely be similar to what you posted. I will make more changes after going through your extremely generous help.

    New look:-
    scriptwip1.jpg
    I have it so that when you load an image from any of the "browse" buttons, the respective checkbutton is ticked marked.
    Also,everything is created once you hit create and that also destroys the dialog aswell :)

    And here is the code for those who are interested:
    macroScript BlueprintManager
    Category: "Butt tools" buttontext: "Butt tools" 
    (
        local ROL_Bmanager   
        local PIC_Front    
        local PIC_Left  
        local PIC_Back  
        local PIC_Right  
        local PIC_Top  
        local TEX_Front  
        local TEX_Left  
        local TEX_Back  
        local TEX_Right  
        local TEX_Top  
        local CHK_Front 
        local CHK_Left 
        local CHK_Back 
        local CHK_Right 
        local CHK_Top 
        local BTN_Front
        local BTN_Left
        local BTN_Back
        local BTN_Right
        local BTN_Top
        local BTN_Create
        local BTN_Cancel
    
    --ROLL-OUT      
    ----------------------------------------------     
        rollout ROL_Bmanager "Blueprint Manager" width:210 height:271 
        ( 
              button BTN_Create "Create!" pos:[134,169] width:70 height:24  
            button BTN_Cancel "Exit" pos:[133,200] width:70 height:24 
            GroupBox LBL_select "Select Images" pos:[6,3] width:198 height:145  
            GroupBox LBL_Options "Options" pos:[6,155] width:122 height:73 
            checkbox CHK_Darken "Darken Image(s)" pos:[11,171] width:107 height:26  
            checkbox CHK_Freeze "Freeze Image(s)" pos:[10,196] width:107 height:26  
            button BTN_Front "Browse" pos:[134,22] width:61 height:16 enabled:true  
            button BTN_Back "Browse" pos:[134,47] width:61 height:16 enabled:true   
            button BTN_Left "Browse" pos:[134,70] width:61 height:16 enabled:true   
            button BTN_Right "Browse" pos:[134,97] width:61 height:16 enabled:true   
            button BTN_Top "Browse" pos:[134,122] width:61 height:16 enabled:true   
            checkbox CHK_Front "Front" pos:[14,22] width:61 height:20   
            checkbox CHK_Back "Back" pos:[14,47] width:61 height:20  
            checkbox CHK_Left "Left" pos:[14,70] width:61 height:20  
            checkbox CHK_Right "Right" pos:[14,97] width:61 height:20  
            checkbox CHK_Top "Top" pos:[14,122] width:61 height:20  
            label LBL "......................" pos:[75,95] width:54 height:13  
            label lbl8 "......................" pos:[75,45] width:54 height:13 
            label lbl9 "......................" pos:[75,70] width:54 height:13  
            label lbl10 "......................" pos:[75,120] width:54 height:13  
            label lbl11 "......................" pos:[75,20] width:54 height:13  
            label LBL_author "Author: Saad Butt " pos:[5,231] width:97 height:15  
            label lbl3 "buttsahib.wordpress" pos:[5,249] width:97 height:15 
            label lbl33 "-Thanks to Vig" pos:[130,231] width:86 height:17 
            label lbl34 "vigville.com" pos:[144,249] width:61 height:17
            
            --Commands for buttons:
            on BTN_Front pressed do     --Loads Front image
            (
                PIC_Front = selectBitmap caption: "Select Front Image" 
                if BTN_Front != undefined do
                    (
                    CHK_Front.state =  true
                    )  
            )
            
            on BTN_Back pressed do         --Loads Back image
            (
                PIC_Back = selectBitmap caption: "Select Back Image"
                if BTN_Back != undefined do
                    (
                    CHK_Back.state =  true
                    )     
            )
            
            on BTN_Left pressed do         --Loads Left image
            ( 
                PIC_Left = selectBitmap caption: "Select Left Image" 
                if BTN_Left != undefined do
                    (
                    CHK_Left.state =  true
                    )    
            )
            
            on BTN_Right pressed do     --Loads Right image
            (
                PIC_Right = selectBitmap caption: "Select Right Image" 
                if BTN_Right != undefined do
                    (
                    CHK_Right.state =  true
                    )    
            )
            
            on BTN_Top pressed do       --Loads Top image
            (
                PIC_Top = selectBitmap caption: "Select Top Image"   
                if BTN_Top != undefined do
                    (
                    CHK_Top.state =  true
                    )  
            )
            
                
            --Create Button 
                on BTN_Create pressed do
                (
                 --This is for the FRONT image 
                    if BTN_Front != undefined do         
                    (
                    TEX_Front =    PIC_Front 
                    X = TEX_Front.height
                    Y = TEX_Front.width
                    F= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
                    .name = "Front_View" 
                    .wirecolor = color 0 0 0
                    frontRotationObject = eulerangles 90 0 0
                    rotate  frontRotationObject
                    move  [0,-300,0]
                     .material = meditMaterials[1]
                    meditMaterials[1].diffusemap = bitmapTexture()
                    meditMaterials[1].diffusemap.bitmap = PIC_Front
                    showTextureMap .material .material.diffuseMap on   
                    deselect F
                    )
                    
                
                --This is for the LEFT image  
                    if BTN_Left != undefined then         
                    (
                    TEX_Left = PIC_Left 
                    X = TEX_Left.height
                    Y = TEX_Left.width
                    L= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
                    .name = "Left_View"  
                    .wirecolor = color 0 0 0 
                    LeftRotationObject = eulerangles 0 -90 0 
                    rotate  LeftRotationObject 
                    rotate  (angleaxis 90 [1,0,0])
                    move  [-200,0,0]
                    .material = meditMaterials[2] 
                    meditMaterials[2].diffusemap = bitmapTexture() 
                    meditMaterials[2].diffusemap.bitmap = PIC_Left 
                    showTextureMap .material .material.diffuseMap on   
                    deselect L
                    )
                    else 
                    (
                    destroydialog ROL_Bmanager  --Destroy Dialog After creation
                    )
            
                --This is for the BACK image  
                    if BTN_Back != undefined then 
                    (
                    TEX_Back = PIC_Back 
                    X = TEX_Back.height
                    Y = TEX_Back.width
                    B= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
                    .name = "Back_View"  
                    .wirecolor = color 0 0 0
                    BackRotationObject = eulerangles -90 0 0
                    rotate  BackRotationObject
                    .pos.y=300
                    rotate  (angleaxis -180 [0,1,0])
                    .material = meditMaterials[3]
                    meditMaterials[3].diffusemap = bitmapTexture()
                    meditMaterials[3].diffusemap.bitmap = PIC_Back
                    showTextureMap .material .material.diffuseMap on   
                    deselect B
                    )
                    else 
                    (
                    destroydialog ROL_Bmanager  --Destroy Dialog After creation
                    )
            
                --This is for the TOP image  
                    if BTN_Top != undefined then          
                    (
                    TEX_Top = PIC_Top
                    X = TEX_Top.height
                    Y = TEX_Top.width
                    T= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
                    .name = "Top_View"   
                    .wirecolor = color 0 0 0  
                    .material = meditMaterials[5] 
                    .pos.z=-150  
                    rotate  (angleaxis -90 [0,0,1])
                    meditMaterials[5].diffusemap = bitmapTexture()  
                    meditMaterials[5].diffusemap.bitmap = PIC_Top 
                    showTextureMap .material .material.diffuseMap on   
                    deselect T 
                    )
                    else 
                    (
                    destroydialog ROL_Bmanager  --Destroy Dialog After creation
                    )
                    
                --This is for the RIGHT image 
                    if BTN_Right != undefined then         
                    (
                    TEX_Right = PIC_Right
                    X = TEX_Right.height 
                    Y = TEX_Right.width
                    R= plane length:X width:Y isSelected:false lengthsegs:1 widthsegs:1 showFrozenInGray:false max backface
                    .name = "Right_View"  
                    .wirecolor = color 0 0 0   
                    RightRotationObject = eulerangles 0 90 0  
                    rotate  RightRotationObject 
                    move  [200,0,0]
                    rotate  (angleaxis 90 [1,0,0])
                    .material = meditMaterials[4] 
                    meditMaterials[4].diffusemap = bitmapTexture()  
                    meditMaterials[4].diffusemap.bitmap = PIC_Right 
                    showTextureMap .material .material.diffuseMap on   
                    deselect R 
                    ) 
                    else
                    (
                    destroydialog ROL_Bmanager  --Destroy Dialog After creation
                    )
                )
        )    
        
        createdialog ROL_Bmanager 
        actionMan.executeAction 0 "311"
        clearSelection () 
    
    )
    

    Issue for me right now:- (strict game artists-ala me, a day ago- look away! :P)
    ok so you can see the image right? you can see that the ends of the planes arent meeting.What i want to do is something like this (in my logically ill-logical terms):
    We are going to take the front for this issue. What i want to do is:
    if BTN_Left or BTN_Right != undefined then
    (
    $Front_View.pos.y = (TEX_Left.Length/2) or (TEX_Right.length/2)
    )
    else
    (
    move $Front_View [0,-300-0]
    )

    BTN being the browse button for the left and right respectiveley where the images are stored, Frontview being the plane, TEX_ being their textures respectively. Now the problem is im taking in two variables (BTN_LEft and BTN_Right) and maybe two operations aswell (If>then>else and or).

    I used TEX_Left.length/2 because as the left and right imges are created at the origin,i would want to move the front_view plane just half their lengths in the y-axis so their ends would meet.

    I will up it at scriptspot after i work in the above mentioned issue and the other two options aswell :D
    Man scripting is weird.Its rewarding.Techincal. Often becomes hair-pullingly crazy...and often....arouses.....:poly141:

    Again,Vig,Cant thank you enough :)
  • Japhir
    Offline / Send Message
    Japhir polycounter lvl 17
    cool stuff mate :D. you sure learned a whole lot in a couple of days! (or did you cheat by looking in to it before starting on this? :))
    looks like a useful script as well! maybe you could include something like this:
    -move all the planes so that x position of the left view is on front_width/2 and so on. (don't know if that was what you where talking about earlier).
    -when all the planes are created, convert them to editable poly
    -attach everything together
    -weld corner verts
    -turn on backface culling

    i have NO idea if that is all possible, but it'd be nice :D.
  • Mark Dygert
    You're totally welcome I wouldn't be scripting as much as I do, if a few people here hadn't smiled on me during my headaches =)

    I don't have time to explain right now, there is a way to move the planes to where you need them to be. Basically you find an extreme point and tell it to move the plane there.

    There is also room for a lot of optimization in your code, there's quite a bit of redundant stuff that could be handled with simple functions. But dude you got it working and that's awesome! Nice work! Way to stick it out!

    Edit: Actually I used a tiny bit of it in this script, it might be of some use. The script takes the pivot point moves it to the lowest local Z axis point, then moves the whole model to the world 0 0 0. If I have time later I'll expand on it, as well as some ways to put in some functions.

    Now I really gotta get home its snowing like crazy here...
Sign In or Register to comment.