def _makeMolecule(self): #H2O O = cmds.polySphere(r=1, n='O', ax = [0,0,0]); H1 = cmds.polySphere(r=0.8, n='H1', ax=[0,0,0]); H2 = cmds.polySphere(r=0.8, n='H2', ax=[0,0,0]); cmds.move(0.0,0.0,1,H1, r=True) cmds.move(0.0,0.0,-1,H2, r=True) cmds.xform(H1, piv=[0,0,0], ws=True) cmds.xform(H2, piv=[0,0,0], ws=True) cmds.rotate(0,'60',0, H1); #group O, H1, H2 as a water molecule H2O = cmds.group( empty=True, name='H2O' ) cmds.parent(H1,H2,O,H2O) #paint on colors for the water molecule #create red lambert cmds.sets( renderable=True, noSurfaceShader=True, empty=True, name='O_WhiteSG' ) cmds.shadingNode( 'lambert', asShader=True, name='O_White' ) cmds.setAttr( 'O_White.color', 1, 1, 1, type='double3') cmds.connectAttr('O_White.outColor', 'O_WhiteSG.surfaceShader') #create red lambert cmds.sets( renderable=True, noSurfaceShader=True, empty=True, name='H_RedSG' ) cmds.shadingNode( 'lambert', asShader=True, name='H_Red' ) cmds.setAttr( 'H_Red.color', 1, 0, 0, type='double3') cmds.connectAttr('H_Red.outColor', 'H_RedSG.surfaceShader') #assign the material cmds.sets('H1', edit=True, forceElement='H_RedSG') cmds.sets('H2', edit=True, forceElement='H_RedSG') cmds.sets('O', edit=True, forceElement='O_WhiteSG') return H2O
def render(self): if (self.name == "unnamed"): self.name = cmds.polyCube(n='visBox')[0] cmds.xform(sp=[self.posX, -.5, self.posZ], ro=[0, 0, self.theta]) for i in range(len(self.heights)): cmds.setKeyframe(self.name, v=self.heights[i], at='scaleY', t=i * BoxOnSphere.SPHERE_STEP) cmds.setKeyframe(self.name, v=self.positions[i][0], at='translateX', t=i * BoxOnSphere.SPHERE_STEP) cmds.setKeyframe(self.name, v=self.positions[i][1], at='translateY', t=i * BoxOnSphere.SPHERE_STEP) cmds.setKeyframe(self.name, v=self.widths[i], at='scaleX', t=i * BoxOnSphere.SPHERE_STEP) cmds.setKeyframe(self.name, v=self.widths[i], at='scaleZ', t=i * BoxOnSphere.SPHERE_STEP)
def setVertexHeight(self, vertex, val): flat_index = self.getFlatIndex(vertex) index_string = str(self.terrain) index_string += ".pnts[" index_string += str(flat_index) index_string += "]" vertex_position = cmds.xform(index_string, query=True, translation=True, worldSpace=True) cmds.xform(index_string, t=(vertex_position[0], val, vertex_position[2]), worldSpace=True)
def render(self): if (self.name == "unnamed"): self.name = cmds.polyCube(n='VisBox')[0] cmds.xform(t=(self.posX, self.posY, self.posZ)) for i in range(len(self.heights)): cmds.setKeyframe(self.name, v=self.heights[i], at='scaleY', t=i * Box.FRAME_STEP)
def render(self): if (self.name == "unnamed"): self.name = cmds.polyCube(n='VisBox')[0] cmds.xform(sp=[self.posX, -.5, self.posZ]) cmds.xform(t=[self.posX, self.posY, self.posZ]) for i in range(0, len(self.heights), Box.AVG_WIN): cmds.setKeyframe(self.name, v=self.avg( [x for x in self.heights[i:i + Box.AVG_WIN]]), at='scaleY', t=i)
def render(self): if (self.name == "unnamed"): self.name = cmds.polyCube(n='VisDrop')[0] frameStart = max(0, self.frameHit - 3) cmds.xform(t=(self.offset, self.posY + 10, self.posZ)) cmds.setKeyframe(self.name, v=self.posY + 10, at='translateY', t=frameStart * Drop.FRAME_STEP) cmds.setKeyframe(self.name, v=self.posY, at='translateY', t=self.frameHit * Drop.FRAME_STEP)
def getVertex(self, vertex): x, y = vertex if x * y < (self.size * self.size) - 1: flat_index = y * self.size + x index_string = str(self.terrain) index_string += ".pnts[" index_string += str(flat_index) index_string += "]" vertex_position = cmds.xform(index_string, query=True, translation=True, worldSpace=True) return vertex_position
def calculateRadii(self): print('Calculating radii...') for frame in range(self.boxes[0].getNumberOfFrames()): actualFrame = frame * BoxOnSphere.SPHERE_STEP cmds.currentTime( actualFrame, update=True) # try to replace with query at a particular frame self.radii[frame] = cmds.xform( self.sphere, q=True, s=True, relative=True)[ 0] # get radius of sphere from x scale attribute
def _handleExampleButton(self): """ This callback creates a polygonal cylinder in the Maya scene. """ random.seed(1234) #check sphereList = cmds.ls('hydrogen1', 'hydrogen2', 'oxygen', 'H2O') if len(sphereList) > 0: cmds.delete(sphereList) #create 2 hydrogen and oxygen h1 = cmds.polySphere(r=12.0, name='hydrogen1') h2 = cmds.polySphere(r=12.0, name='hydrogen2') oxygen = cmds.polySphere(r=15.0, name='oxygen') #move cmds.move(-15, 0, 0, h1) cmds.move(15, 0, 0, h2) cmds.xform(h1, piv=[0, 0, 0], ws=True) cmds.xform(h2, piv=[0, 0, 0], ws=True) cmds.rotate(0, '75', 0, h1) #group hydrogen and oxygen together H2O = cmds.group(empty=True, name='H2O#') cmds.parent('hydrogen1', 'hydrogen2', 'oxygen', 'H2O1') #add color def createMaterial(name, color, type): cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=name + 'SG') cmds.shadingNode(type, asShader=True, name=name) cmds.setAttr(name + '.color', color[0], color[1], color[2], type='double3') cmds.connectAttr(name + '.outColor', name + 'SG.surfaceShader') def assignMaterial(name, object): cmds.sets(object, edit=True, forceElement=name + 'SG') def assignNewMaterial(name, color, type, object): createMaterial(name, color, type) assignMaterial(name, object) #H is white and O is red assignNewMaterial('Red', (1, 0, 0), 'lambert', 'oxygen') assignNewMaterial('White', (1, 1, 1), 'lambert', 'hydrogen1') assignMaterial('White', 'hydrogen2') #key frame def keyFullRotation(pObjectName, pStartTime, pEndTime, pTargetAttribute, pValueStart, pvalueEnd): keyt = (pStartTime[0], pStartTime[0]) cmds.cutKey(pObjectName, time=(keyt, keyt), attribute=pTargetAttribute) cmds.setKeyframe(pObjectName, time=pStartTime, attribute=pTargetAttribute, value=pValueStart) cmds.setKeyframe(pObjectName, time=pEndTime, attribute=pTargetAttribute, value=pvalueEnd) #cmds.selectKey( pObjectName, time=(pStartTime, [pEndTime]), attribute=pTargetAttribute, keyframe=True ) #duplicate H2O for i in range(1, 52): cmds.duplicate(H2O) #get random coord x = random.uniform(-200, 200) y = random.uniform(0, 300) z = random.uniform(-200, 200) cmds.move(x, y, z, H2O) xRot = random.uniform(0, 360) yRot = random.uniform(0, 360) zRot = random.uniform(0, 360) cmds.rotate(xRot, yRot, zRot, H2O) startTime = cmds.playbackOptions(minTime=1) endTime = cmds.playbackOptions(maxTime=30) h2o = "H2O" + str(i) for y in range(3): coordsX = cmds.getAttr(h2o + '.translateX') coordsY = cmds.getAttr(h2o + '.translateY') coordsZ = cmds.getAttr(h2o + '.translateZ') ranStartX = int(random.uniform(0, 15)) ranStartY = int(random.uniform(0, 15)) ranStartZ = int(random.uniform(0, 15)) ranEndX = int(random.uniform(15, 30)) ranEndY = int(random.uniform(15, 30)) ranEndZ = int(random.uniform(15, 30)) x = random.uniform(coordsX - 50, coordsX + 50) y = random.uniform(coordsY, coordsY + 50) z = random.uniform(coordsZ - 50, coordsZ + 50) #print x,y,z keyFullRotation(h2o, ranStartZ, 15, 'translateZ', coordsZ, z) keyFullRotation(h2o, ranStartX, 15, 'translateX', coordsX, x) keyFullRotation(h2o, ranStartY, 15, 'translateY', coordsY, y) keyFullRotation(h2o, 15, ranEndZ, 'translateZ', z, coordsZ) keyFullRotation(h2o, 15, ranEndX, 'translateX', x, coordsX) keyFullRotation(h2o, 15, ranEndY, 'translateY', y, coordsY) RcoordsX = cmds.getAttr(h2o + '.rotateX') RcoordsY = cmds.getAttr(h2o + '.rotateY') RcoordsZ = cmds.getAttr(h2o + '.rotateZ') xRot = random.uniform(0, 360) yRot = random.uniform(0, 360) zRot = random.uniform(0, 360) keyFullRotation(h2o, ranStartZ, 15, 'rotateZ', RcoordsZ, zRot) keyFullRotation(h2o, ranStartX, 15, 'rotateX', RcoordsX, xRot) keyFullRotation(h2o, ranStartY, 15, 'rotateY', RcoordsY, zRot) keyFullRotation(h2o, 15, ranEndZ, 'rotateZ', zRot, RcoordsZ) keyFullRotation(h2o, 15, ranEndX, 'rotateX', xRot, RcoordsX) keyFullRotation(h2o, 15, ranEndY, 'rotateY', zRot, RcoordsY) print 'done' cmds.delete('H2O52')
def _handleExampleButton(self): """ This callback creates a polygonal cylinder in the Maya scene. """ random.seed(1234) #check sphereList = cmds.ls('hydrogen1','hydrogen2', 'oxygen','H2O') if len(sphereList)>0: cmds.delete(sphereList) #create 2 hydrogen and oxygen h1 = cmds.polySphere(r=12.0, name='hydrogen1') h2 = cmds.polySphere(r=12.0, name='hydrogen2') oxygen = cmds.polySphere(r=15.0, name='oxygen') #move cmds.move(-15,0,0,h1) cmds.move(15,0,0,h2) cmds.xform(h1, piv=[0,0,0],ws=True) cmds.xform(h2, piv=[0,0,0],ws=True) cmds.rotate(0,'75',0,h1) #group hydrogen and oxygen together H2O = cmds.group(empty=True, name='H2O#') cmds.parent('hydrogen1','hydrogen2','oxygen','H2O1') #add color def createMaterial( name, color, type ): cmds.sets( renderable=True, noSurfaceShader=True, empty=True, name=name + 'SG' ) cmds.shadingNode( type, asShader=True, name=name ) cmds.setAttr( name+'.color', color[0], color[1], color[2], type='double3') cmds.connectAttr(name+'.outColor', name+'SG.surfaceShader') def assignMaterial (name, object): cmds.sets(object, edit=True, forceElement=name+'SG') def assignNewMaterial( name, color, type, object): createMaterial (name, color, type) assignMaterial (name, object) #H is white and O is red assignNewMaterial('Red', (1,0,0), 'lambert', 'oxygen'); assignNewMaterial('White',(1,1,1),'lambert', 'hydrogen1'); assignMaterial('White', 'hydrogen2'); #key frame def keyFullRotation( pObjectName, pStartTime, pEndTime, pTargetAttribute,pValueStart, pvalueEnd ): keyt = (pStartTime[0], pStartTime[0]) cmds.cutKey( pObjectName, time=(keyt, keyt), attribute=pTargetAttribute ) cmds.setKeyframe( pObjectName, time=pStartTime, attribute=pTargetAttribute, value=pValueStart ) cmds.setKeyframe( pObjectName, time=pEndTime, attribute=pTargetAttribute, value=pvalueEnd ) #cmds.selectKey( pObjectName, time=(pStartTime, [pEndTime]), attribute=pTargetAttribute, keyframe=True ) #duplicate H2O for i in range(1,52): cmds.duplicate(H2O) #get random coord x = random.uniform(-200,200) y = random.uniform(0,300) z = random.uniform(-200,200) cmds.move(x,y,z, H2O) xRot = random.uniform(0,360) yRot = random.uniform(0,360) zRot = random.uniform(0,360) cmds.rotate(xRot,yRot,zRot,H2O) startTime = cmds.playbackOptions(minTime=1 ) endTime = cmds.playbackOptions( maxTime=30 ) h2o = "H2O"+str(i) for y in range(3): coordsX = cmds.getAttr( h2o+'.translateX' ) coordsY = cmds.getAttr( h2o+'.translateY' ) coordsZ = cmds.getAttr( h2o+'.translateZ' ) ranStartX = int(random.uniform(0,15)) ranStartY = int(random.uniform(0,15)) ranStartZ = int(random.uniform(0,15)) ranEndX = int(random.uniform(15,30)) ranEndY = int(random.uniform(15,30)) ranEndZ = int(random.uniform(15,30)) x = random.uniform(coordsX-50,coordsX+50) y = random.uniform(coordsY,coordsY+50) z = random.uniform(coordsZ-50,coordsZ+50) #print x,y,z keyFullRotation( h2o, ranStartZ, 15, 'translateZ',coordsZ,z) keyFullRotation( h2o, ranStartX, 15, 'translateX', coordsX,x) keyFullRotation( h2o, ranStartY, 15, 'translateY', coordsY,y) keyFullRotation( h2o, 15, ranEndZ, 'translateZ',z,coordsZ) keyFullRotation( h2o, 15, ranEndX, 'translateX', x,coordsX) keyFullRotation( h2o, 15, ranEndY, 'translateY', y,coordsY) RcoordsX = cmds.getAttr( h2o+'.rotateX' ) RcoordsY = cmds.getAttr( h2o+'.rotateY' ) RcoordsZ = cmds.getAttr( h2o+'.rotateZ' ) xRot = random.uniform(0,360) yRot = random.uniform(0,360) zRot = random.uniform(0,360) keyFullRotation( h2o, ranStartZ, 15, 'rotateZ',RcoordsZ,zRot) keyFullRotation( h2o, ranStartX, 15, 'rotateX', RcoordsX,xRot) keyFullRotation( h2o, ranStartY, 15, 'rotateY', RcoordsY,zRot) keyFullRotation( h2o, 15, ranEndZ, 'rotateZ',zRot,RcoordsZ) keyFullRotation( h2o, 15, ranEndX, 'rotateX', xRot,RcoordsX) keyFullRotation( h2o, 15, ranEndY, 'rotateY', zRot,RcoordsY) print 'done' cmds.delete('H2O52')
def getPositionOfVertex(cls, transform, vertexIndex): return cmds.xform('%s.pnts[%s]' % (transform, vertexIndex), query=True, worldSpace=True, translation=True)
def createLight(self, name, translate, rotate): cmds.spotLight(n=name) cmds.xform(name, t=[translate[0], translate[1], translate[2]]) cmds.rotate(rotate[0], rotate[1], rotate[2], name) cmds.setAttr(self.getShapeString(name) + '.intensity', 1.5) cmds.setAttr(self.getShapeString(name) + '.penumbraAngle', 5.)