ChatGPT - Maya city generator v002

PHOTO EMBED

Thu Dec 08 2022 15:07:46 GMT+0000 (Coordinated Universal Time)

Saved by @HiveHD #python #maya2023

import maya.cmds as cmds
import random

# Create a new window
window = cmds.window(title="Generating your city", widthHeight=(300, 200))
 
# Add a layout to the window
layout = cmds.columnLayout()
 
# Add a text field to the layout
text_field = cmds.text(label="..............{--Generating your city--}..............")
 
# Display the window
cmds.showWindow(window)

# Creates a "City_GRP" group
city_grp = cmds.group(name="City_GRP", empty=True)

for i in range(2000):
    # Create a cube with the "Building_####_GEO" name
    building_name = "Building_%04d_GEO" % i
    cube = cmds.polyCube(name=building_name)

    # Generate random X and Z coordinates with a maximum distance of 100 units from the world origin
    x = random.uniform(-100, 100)
    z = random.uniform(-100, 100)

    # Calculate the distance of the cube from the origin using the Pythagorean theorem
    distance = (x**2 + z**2)**0.5

    # Generate random scale values for the X, Z, and Y axes, with a max X and Z scale of 6 and a max Y scale of 20
    # and divide it by the pythagorean
    x_scale = random.uniform(1, 6) / (distance / 9)
    y_scale = random.uniform(1, 20) / (distance / 9)
    z_scale = random.uniform(1, 6) / (distance / 9)

    # Check if any of the scales are larger than 35 and delete the cube if they are
    if x_scale > 15 or y_scale > 35 or z_scale > 15:
        cmds.delete(building_name)
        continue

    # Randomize the cube's scale using the cmds.scale command
    cmds.scale(x_scale, y_scale, z_scale, building_name)

    # Position the cube at the random X and Z coordinates
    cmds.move(x, 0, z, building_name)

    # store the X and Z coordinates
    x_coord = cmds.getAttr(building_name + ".translateX")
    z_coord = cmds.getAttr(building_name + ".translateZ")

    # get the cubes bounding box
    bbox = cmds.xform(building_name, query=True, boundingBox=True)

    # store the minimum y value of the bounding box
    min_y = bbox[1]

    # Subtract the minimum y value from the cube's y coordinate
    y = 0 - min_y

    # set the new transform of the cube using the stored X and Z coordinates
    cmds.move(x_coord, y, z_coord, building_name)

    # Add the cube to the City_GRP group
    cmds.parent(building_name, city_grp)

	# ------------------------- group buildings

# Get all children of City_GRP that start with "Building"
buildings = cmds.listRelatives("City_GRP", children=True, type="transform")
buildings = [b for b in buildings if b.startswith("Building")]

# Create three groups to hold the buildings, using the desired names
group1 = cmds.group(empty=True, name="cityCenter_GRP")
group2 = cmds.group(empty=True, name="cityLimits_GRP")
group3 = cmds.group(empty=True, name="cityOutskirts_GRP")

# Add the groups as children of City_GRP
cmds.parent([group1, group2, group3], "City_GRP")

# Iterate over the buildings and add them to the appropriate group
for building in buildings:
    # Get the distance of the building from the world origin
    position = cmds.xform(building, query=True, worldSpace=True, translation=True)
    distance = (position[0]**2 + position[1]**2 + position[2]**2)**0.5

    if distance < 33.33:
        cmds.parent(building, group1)
    elif distance < 66.66:
        cmds.parent(building, group2)
    else:
        cmds.parent(building, group3)

    # ----------------------- build environment


# Create a plane with a scale of 200 in each direction
cmds.polyPlane(name="groundPlane_GEO", w=200, h=200)

# Create a directional light
cmds.directionalLight(name="sun_LGT")

# Set the position and rotation of the light using the move and rotate commands
cmds.move(0, 60, 0, "sun_LGT", absolute=True)
cmds.rotate(-22, 50, 0, "sun_LGT", absolute=True)

# Set the light shadow color to a grey with a value of 0.35
cmds.setAttr("sun_LGT.shadowColor", 0.35, 0.35, 0.35, type="double3")

# Create a group to hold the plane and light
cmds.group(name="environment_GRP", empty=True)

# Add the plane and light to the group
cmds.parent("groundPlane_GEO", "environment_GRP")
cmds.parent("sun_LGT", "environment_GRP")

# Add the environment group as a child of the City group
cmds.parent("environment_GRP", "City_GRP")

# Update the text field to say "Done"
cmds.text(text_field, edit=True, label=".............._____Done_____..............")
content_copyCOPY

New version that makes sure each cube is dropped to the floor and the closer to the centre the larger the building, deletes any buildings that are too large