Jul-21-2020, 09:18 AM
Hello, I am new in coding with python I created this two codes with some help form my teacher and I am trying to connect this two python codes in one with a functional UI , but I have some difficulty when I am trying to combine them , I have a very little knowledge when it comes to python , any help will be appreciated.
Link to the code:
https://drive.google.com/file/d/1ge96yLw...sp=sharing
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
Part 1 of 2: python with classes
------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
import maya.cmds as cmds
class MR_Window(object):
#constructor
def __init__(self):
self.window = "MR_Window"
self.title = "Resize Tool"
# close old window is open
if cmds.window(self.window, exists = True):
cmds.deleteUI(self.window, window=True)
# create new window
self.window = cmds.window(self.window, title=self.title, widthHeight=[10000,200] )
# add a form layout to window
self.mainForm = cmds.formLayout(numberOfDivisions=500)
# add title to window
self.titleDisplay = cmds.text(label = self.title, align="center",font="boldLabelFont")
# position title on the rop left of the screen
cmds.formLayout(self.mainForm, edit=True,attachForm=([self.titleDisplay, 'top', 5],[self.titleDisplay, 'left', 5],[self.titleDisplay, 'right', 5]))
#seperator
self.titleSeperator = cmds.separator();
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.titleSeperator, 'top', 10, self.titleDisplay], attachForm=([self.titleSeperator, 'left', 5], [self.titleSeperator, 'right', 5]) )
#textfield for cube name
self.cubeName = cmds.textFieldGrp(label="Object Name:")
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.cubeName, 'top', 10, self.titleSeperator], attachForm=[self.cubeName, 'left', 5])
#group of three floats for specifying cube size
self.cubeSize = cmds.floatFieldGrp( numberOfFields=3, label='Size:',value1=1, value2=1, value3=1)
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.cubeSize, 'top', 10, self.cubeName], attachForm=[self.cubeSize, 'left', 5] )
#button for creating the cube
self.cubeCreateBtn = cmds.button( label='Apply', width=50,command=self.createCube )
cmds.formLayout(self.mainForm, edit=True, attachForm=[self.cubeCreateBtn, 'left', 50])
#display new window
cmds.showWindow()
#function called by create button
def createCube(self, *args):
#If the same cube existe delete and receate new cube
if cmds.polyCube(self.cubeName, exists = True):
cmds.delete(self.cubeName, window=True)
# get the value set by the user
name = cmds.textFieldGrp(self.cubeName, query=True, text=True)
width = cmds.floatFieldGrp(self.cubeSize, query=True, value1=True)
height = cmds.floatFieldGrp(self.cubeSize, query=True, value2=True)
depth = cmds.floatFieldGrp(self.cubeSize, query=True, value1=True)
# create the cube using values set by user
cmds.polyCube(name=name, width=width, height=height, depth=depth)
myWindow = MR_Window()
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
Part 2 of 2: python with scale tool
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
import maya.cmds as cmds
#select the first object
selection = cmds.ls(selection = True)[0]
# returns xmin ymin zmin xmax ymax zmax in that order.
boundingBox = cmds.xform(selection, query=True, boundingBox=True)
# get the width, height and depth of object form the bounding box
width = boundingBox[3] - boundingBox[0]
height = boundingBox[4] - boundingBox[1]
depth = boundingBox[5] - boundingBox[2]
# set a target size width, height and depth
targetWidth = 10
targetHeight = 6
targetDepth = 1
# determine the amount each dimensions needs to be scaled
scaleWidth = targetWidth / width
scaleHeight = targetHeight / height
scaleDepth = targetDepth / depth
# get existing scale values
objScale = cmds.xform(selection, query=True, scale=True)
# apply scale
cmds.xform(selection, scale=[scaleWidth * objScale[0], scaleHeight * objScale[1], scaleDepth * objScale[2]])
Link to the code:
https://drive.google.com/file/d/1ge96yLw...sp=sharing
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
Part 1 of 2: python with classes
------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
import maya.cmds as cmds
class MR_Window(object):
#constructor
def __init__(self):
self.window = "MR_Window"
self.title = "Resize Tool"
# close old window is open
if cmds.window(self.window, exists = True):
cmds.deleteUI(self.window, window=True)
# create new window
self.window = cmds.window(self.window, title=self.title, widthHeight=[10000,200] )
# add a form layout to window
self.mainForm = cmds.formLayout(numberOfDivisions=500)
# add title to window
self.titleDisplay = cmds.text(label = self.title, align="center",font="boldLabelFont")
# position title on the rop left of the screen
cmds.formLayout(self.mainForm, edit=True,attachForm=([self.titleDisplay, 'top', 5],[self.titleDisplay, 'left', 5],[self.titleDisplay, 'right', 5]))
#seperator
self.titleSeperator = cmds.separator();
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.titleSeperator, 'top', 10, self.titleDisplay], attachForm=([self.titleSeperator, 'left', 5], [self.titleSeperator, 'right', 5]) )
#textfield for cube name
self.cubeName = cmds.textFieldGrp(label="Object Name:")
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.cubeName, 'top', 10, self.titleSeperator], attachForm=[self.cubeName, 'left', 5])
#group of three floats for specifying cube size
self.cubeSize = cmds.floatFieldGrp( numberOfFields=3, label='Size:',value1=1, value2=1, value3=1)
cmds.formLayout(self.mainForm, edit=True, attachControl=[self.cubeSize, 'top', 10, self.cubeName], attachForm=[self.cubeSize, 'left', 5] )
#button for creating the cube
self.cubeCreateBtn = cmds.button( label='Apply', width=50,command=self.createCube )
cmds.formLayout(self.mainForm, edit=True, attachForm=[self.cubeCreateBtn, 'left', 50])
#display new window
cmds.showWindow()
#function called by create button
def createCube(self, *args):
#If the same cube existe delete and receate new cube
if cmds.polyCube(self.cubeName, exists = True):
cmds.delete(self.cubeName, window=True)
# get the value set by the user
name = cmds.textFieldGrp(self.cubeName, query=True, text=True)
width = cmds.floatFieldGrp(self.cubeSize, query=True, value1=True)
height = cmds.floatFieldGrp(self.cubeSize, query=True, value2=True)
depth = cmds.floatFieldGrp(self.cubeSize, query=True, value1=True)
# create the cube using values set by user
cmds.polyCube(name=name, width=width, height=height, depth=depth)
myWindow = MR_Window()
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
Part 2 of 2: python with scale tool
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
import maya.cmds as cmds
#select the first object
selection = cmds.ls(selection = True)[0]
# returns xmin ymin zmin xmax ymax zmax in that order.
boundingBox = cmds.xform(selection, query=True, boundingBox=True)
# get the width, height and depth of object form the bounding box
width = boundingBox[3] - boundingBox[0]
height = boundingBox[4] - boundingBox[1]
depth = boundingBox[5] - boundingBox[2]
# set a target size width, height and depth
targetWidth = 10
targetHeight = 6
targetDepth = 1
# determine the amount each dimensions needs to be scaled
scaleWidth = targetWidth / width
scaleHeight = targetHeight / height
scaleDepth = targetDepth / depth
# get existing scale values
objScale = cmds.xform(selection, query=True, scale=True)
# apply scale
cmds.xform(selection, scale=[scaleWidth * objScale[0], scaleHeight * objScale[1], scaleDepth * objScale[2]])