New to python. Trying to put an image on a cube.

37 views
Skip to first unread message

jul...@valkov.com

unread,
Aug 24, 2016, 2:50:57 PM8/24/16
to Python Programming for Autodesk Maya
I am rather new to both python and maya. I'm trying to build a tool to create a skybox, so I have already created the cube and inverted its normals, but i can't seem to find how to apply a texture to it. All help is appreciated. Thank you.

Here is my code:
import maya.cmds as cmds
from os import listdir


def makeSphere(*args):
cmds.polySphere( n='mySphere', sx=100, sy=100)
return

def makeCube(*args):
cmds.polyCube( sx=5, sy=5, sz=5 )
return

def makeCylinder(*args):
cmds.polyCylinder(n='myCylinder', sx=5, sy=5, sz=5)
return

def makeCone(*args):
cmds.polyCone( sx=10, sy=15, sz=5, r=20, h=10)
return

def makePlane(*args):
cmds.polyPlane( sx=10, sy=15, w=15, h=20)
return

def makeTorus(*args):
cmds.polyTorus( sx=8, sy=16, r=10, sr=1 )
return

def makePrism(*args):
cmds.polyPrism ( sc=10, sh=15, w=20)
return

def makePyramid(*args):
cmds.polyPyramid( sc=10, sh=15, ns=5, w=20)
return

def makePipe(*args):
cmds.polyPipe( sh=10, h=20 )
return

def makeHelix(*args):
cmds.polyHelix(r=1, d=1, cuv=0)
return

def makeSoccerball(*args):
cmds.polyPrimitive( r=1, l=0.4036, pt=0)
return

def makePlatonicSolids(*args):
cmds.polyPlatonicSolid( r=2, l=2, st=1)
return


def importModel(*args):
filename = cmds.fileDialog2(fileMode=1, caption="Import model")
cmds.file( filename[0], i=True );
return

def makeSkybox(*args):

cmds.polyCube( sx=100, sy=100, sz=100, name = "skybox" )
# cmds.polyCut(deleteFaces = True, cuttingDirection = "y", cutPlaneCenter = (0, 0, 0) )
cmds.polyNormal('skybox', nm = 0)
filename = cmds.fileDialog2(fileMode=1, caption="Import texture")
#cmds.polyProjection('skybox', type = 'Spherical' ,uvSetName= filename[0])

cmds.file( filename[0], i=True, applyTo = "skybox")

#image_skybox = input("Please input the full file location of the desired image to be applied to your skybox: ")
#cmds.image(image = image_skybox)
return



winID = "kevsFrameWin"
if cmds.window(winID, exists=True):
cmds.deleteUI(winID)

# Create the window
cmds.window(winID,t="frames")

# Create a 'container' Layout to keep our frames in
rootLayout = cmds.columnLayout()

# Create a frame layout. Note that I am not declaring a
# variable here as frameLayout are only supposed to
# support a single child, and our child will be another
# layout... So we really want to reference the child
# rather then the frameLayout parent anyway.
cmds.frameLayout( label="Top frame" )

# Generate a child Layout inside the frame.
# declare a variable here as it will allow us to reference
# contents inside our frame (within this layout).
frameOne = cmds.columnLayout()
cmds.button( label='Sphere', command=makeSphere)
cmds.button( label='Cube', command=makeCube)
cmds.button( label='Cylinder', command=makeCylinder)
cmds.button( label='Cone', command=makeCone)
cmds.button( label='Plane', command=makePlane)
cmds.button( label='Torus', command=makeTorus)
cmds.button( label='Prism', command=makePrism)
cmds.button( label='Pyramid', command=makePyramid)
cmds.button( label='Pipe', command=makePipe)
cmds.button( label='Helix', command=makeHelix)
cmds.button( label='Soccerball', command=makeSoccerball)
cmds.button( label='Platonic Solid', command=makePlatonicSolids)
cmds.button( label='Skybox', command=makeSkybox)
cmds.button( label='Browse to import models', command=importModel)



# Jump back to our root container Layout to add a new Frame
# If we were to use the '..' approach, we would need *2* of
# them to get back to the root.
#
# '..' back to the frame parent, then '..' to the root parent
cmds.setParent(rootLayout)

# Display the UI
cmds.showWindow(winID)

Justin Israel

unread,
Aug 24, 2016, 8:26:05 PM8/24/16
to python_in...@googlegroups.com
Hi and welcome!

First off let me just throw out a comment about code sharing. For large snippets like this, can you make sure to use something like pastebin or gist or your favourite code sharing solution? It helps make the code formatting more readable. 

As for your question, you would need to create a shader of some type, and then connect a File node to the desired parameter, which is commonly the "color" param for what you are asking. You can see a similar example in this StackOverflow question:

One thing that helps when learning the commands API, is if you turn on "Echo Commands" in your Script Editor. Then perform the desired operation through the UI and note the commands that get generated. If you were to apply the texture by assigning a shader and setting the texture on the color param, you would see this similar code being emitted.

Hope that helps
Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/7a3a7c79-f988-4787-930c-43df5b1320a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages