Maya Python: Building Generator - Getting Width and Height of Object

Maya Python: Building Generator - Getting Width and Height of Object

Anonymous
Not applicable
1,748 Views
0 Replies
Message 1 of 1

Maya Python: Building Generator - Getting Width and Height of Object

Anonymous
Not applicable

Hello! I'm having some problems with my script and was wondering if anyone wouldn't mind taking a look at it and give me some feedback! I'm building a procedural building generator, and I've got part of it working, however, I'm struggling to combine all of my elements in a cohesive and functional manner. Here's what I'm trying to do:

 

- Attempt to get the xmax, xmin, ymax, and ymin values of my window model so I can get a float of the model's width and height. I'm trying to use this for when I use while loops to duplicate, translate, and rotate my duplicated pieces to make up each section of my building.

 

- Get the attribute of the window section that has the xmax, xmin, ymax, ymin values and tell the script to duplicate x times, translate each window section equal distance based on the width of the section, and duplicate the sections vertically to create the different floors of my building.

 

These two goals haven't been very successful! However, if I were to create a distance tool for both the width and height of the window section, the script seems to work. Any ideas or things I could change to get the results I'm looking for? I may be placing certain information in the wrong spots. My biggest goal however is to get it all working through one button push. Any suggestions would be really helpful!

 

Thanks so much!

 

import maya.cmds as cmds


def buildingGeneratorUI():
    if cmds.window("generator_Win", exists=True):
        cmds.deleteUI("generator_Win", window=True)

    if cmds.windowPref("generator_Win", exists=True):
        cmds.windowPref("generator_Win", remove=True)

    cmds.window("generator_Win", title="BUILDING GENERATOR 2018 v1.0", sizeable=False, tlc=(30, 1500))

    cmds.columnLayout("mainUI_C", parent="generator_Win")

    cmds.separator(h=10, style="none")
    cmds.rowColumnLayout(numberOfColumns=1, columnWidth=[(1, 400)], p="mainUI_C")
    cmds.button(label="Generate Windows", bgc=(.4, .4, .4), c=lambda args: generateWindows())
    cmds.button(label="Generate Building", bgc=(.4, .4, .4), c=lambda args: windowConstruction())
    cmds.separator(h=10, style="none")

    cmds.showWindow("generator_Win")
buildingGeneratorUI()

def windowConstruction():
    # Creating Window Panel#
    w = 5
    h = 8
    sx = 5
    sy = 6

    # Modeling the Windows#
    bWindow = cmds.polyCube(w=w, h=h, sx=sx, sy=sy, n="window")
    cmds.polyExtrudeFacet("window.f[21]", "window.f[22]", "window.f[23]", "window.f[16]", "window.f[17]",
                          "window.f[18]", "window.f[11]", "window.f[12]", "window.f[13]", "window.f[6]", "window.f[7]",
                          "window.f[8]", thickness=-0.2, offset=0.1)
    cmds.polyExtrudeFacet("window.f[75]", "window.f[74]", "window.f[73]", "window.f[72]", "window.f[71]",
                          "window.f[70]", "window.f[76]", "window.f[77]", "window.f[78]", "window.f[79]",
                          "window.f[80]", "window.f[81]", thickness=1.5)
    cmds.polyExtrudeFacet("window.f[111]", "window.f[113]", "window.f[115]", "window.f[117]", "window.f[119]",
                          "window.f[121]", "window.f[109]", "window.f[106]", "window.f[104]", "window.f[102]",
                          "window.f[100]", "window.f[98]", thickness=0.3)
    cmds.polyExtrudeFacet("window.f[0]", "window.f[1]", "window.f[2]", "window.f[3]", "window.f[4]", offset=0.2,
                          thickness=0.4)
    cmds.polyExtrudeFacet("window.f[6]", "window.f[7]", "window.f[8]", "window.f[11]", "window.f[12]", "window.f[13]",
                          "window.f[16]", "window.f[17]", "window.f[18]", "window.f[21]", "window.f[22]",
                          "window.f[23]", offset=0.1)
    cmds.polyExtrudeFacet("window.f[6]", "window.f[7]", "window.f[8]", "window.f[11]", "window.f[12]", "window.f[13]",
                          "window.f[16]", "window.f[17]", "window.f[18]", "window.f[21]", "window.f[22]",
                          "window.f[23]", offset=0.1, thickness=-0.3)
    cmds.select("window.e[279]", "window.e[274]", "window.e[269]", "window.e[264]", "window.e[259]", "window.e[252]",
                "window.e[246]", "window.e[241]", "window.e[236]", "window.e[231]", "window.e[226]", "window.e[219]")
    cmds.polyBevel("window.e[279]", "window.e[274]", "window.e[269]", "window.e[264]", "window.e[259]", "window.e[252]",
                   "window.e[246]", "window.e[241]", "window.e[236]", "window.e[231]", "window.e[226]", "window.e[219]")
    cmds.polyExtrudeFacet("window.f[110]", "window.f[126]", "window.f[30]", "window.f[31]", "window.f[32]",
                          "window.f[33]", "window.f[34]", "window.f[103]", "window.f[119]", "window.f[121]",
                          "window.f[104]", "window.f[65]", "window.f[66]", "window.f[67]", "window.f[68]",
                          "window.f[69]", "window.f[96]", "window.f[112]", thickness=1.5)
    cmds.select("window.f[110]", "window.f[126]", "window.f[30]", "window.f[31]", "window.f[32]", "window.f[33]",
                "window.f[34]", "window.f[103]", "window.f[119]", "window.f[121]", "window.f[104]", "window.f[65]",
                "window.f[66]", "window.f[67]", "window.f[68]", "window.f[69]", "window.f[96]", "window.f[112]", d=True)
    cmds.move(h / 2.0 + 1.49, bWindow, moveY=True)
    cmds.move(w / 2.0 + 1.49, bWindow, moveX=True)
    cmds.move(8, 0, 0, bWindow[0] + ".scalePivot", bWindow[0] + ".rotatePivot", absolute=True)
    cmds.makeIdentity(bWindow, apply=True, translate=True, t=1, r=1, s=1, n=0)


def generateWindows():
    windowItem = 'window'

    xmin, ymin, zmin, xmax, ymax, zmax = cmds.xform(windowItem, query=True, bb=True)
    widthSections = 5
    depthSections = 3
    heightSections = 7
    sectionWidth = xmax - xmin
    sectionHeight = ymax - ymin

    i = 1

    while i <= widthSections:
        cmds.getAttr(sectionWidth)
        cmds.duplicate()
        cmds.move(-sectionWidth, 0, 0, relative=True)
        i += 1
    cmds.rotate(0, -90, 0, relative=True)

    while i <= depthSections:
        cmds.getAttr(sectionWidth)
        cmds.duplicate()
        cmds.move(0, 0, -sectionWidth, relative=True)
        i += 1
    cmds.rotate(0, -90, 0, relative=True)

    while i <= widthSections:
        cmds.getAttr(sectionWidth)
        cmds.duplicate()
        cmds.move(sectionWidth, 0, 0, relative=True)
        i += 1
    cmds.rotate(0, -90, 0, relative=True)

    while i < depthSections:
        cmds.getAttr(sectionWidth)
        cmds.duplicate()
        cmds.move(0, 0, sectionWidth, relative=True)
        i += 1

    cmds.select('window*')
    while i < heightSections:
        cmds.getAttr(sectionHeight)
        cmds.duplicate()
        cmds.move(0, sectionHeight, 0, relative=True)
        i += 1

    cmds.select('window*')
    cmds.group('window*', name="Windows")

 

 

0 Likes
1,749 Views
0 Replies
Replies (0)