def addToRigidSet( *args, **kw ): rbSets = _findRigidSetsFromSelection(excludeTransforms=True) if len(rbSets)>1: OpenMaya.MGlobal.displayError(maya.stringTable['y_RigidBody.kAmbiguousAddToSet' ]) return if len(rbSets)==0: OpenMaya.MGlobal.displayError(maya.stringTable['y_RigidBody.kNoRigidSetToAddSelected' ]) return addObjects = kw['members'] if kw.has_key('members') else maya.cmds.ls(sl=True, type='transform') hideShape = kw['hideShape'] if kw.has_key('hideShape') else True # filter out objects with rigid body shapes filteredAddObjects = [] bulletObjects = [] for object in addObjects: if BulletUtils.getRigidBodyFromTransform(object): OpenMaya.MGlobal.displayInfo(maya.stringTable['y_RigidBody.kAlreadyRigidBody' ].format(object)) bulletObjects.append(object) else: filteredAddObjects.append(object) # ensure exclusive if len(filteredAddObjects): CreateRigidSet.removeFromRigidSet(members=filteredAddObjects, excludeSets=rbSets) # remove bullet rigid shape from objects if len(bulletObjects): BulletUtils.removeBulletObjectsFromList(bulletObjects) filteredAddObjects.extend( bulletObjects ) if len(filteredAddObjects): maya.cmds.sets( filteredAddObjects, e=True, addElement=rbSets[0] ) if hideShape: BulletUtils.setAsIntermediateObjects( filteredAddObjects, 1, deferred=True )
def doCommand(name='bulletRigidBodyShape#', transformName = None, bAttachSelected = True, ignoreShape = False, hideShape = False, # Attrs colliderShapeType = None, axis = None, length = None, radius = None, extents = None, bodyType = None, initiallySleeping = None, neverSleeps = None, mass = None, linearDamping = None, angularDamping = None, friction = None, restitution = None, initialVelocity = None, initialAngularVelocity = None, impulse = None, torqueImpulse = None, centerOfMass = None, autoFit = None, colliderShapeOffset = None, colliderShapeMargin = None, **kwargs ): '''Create a bulletRigidBody ''' # Explicitly list the names of settable attributes iterated over below # Check for selection being a rigid body already if ( bAttachSelected ): selectedObjs = _getSelectedTransforms() selectedObj = selectedObjs[0] if len(selectedObjs) else None # don't attach a rigid body underneath another one during # creation, it's more likely that the user is invoking # this command multiple times, and the selection is # leftover from a previous invocation. if ( BulletUtils.getRigidBodyFromTransform(selectedObj) is not None ): selectedObj = None else: transformName = selectedObj else: selectedObj = None if not transformName: transformName='' # Create from scratch rbShape = maya.cmds.createNode( "bulletRigidBodyShape", name=name, parent=transformName ) rbXform = _firstParent(rbShape) if not rbXform: OpenMaya.MGlobal.displayError(maya.stringTable[ 'y_RigidBody.kErrorRigidBodyNotCreated' ]) return [ None, None ] # Performance: if we're attaching to an object # make rbShape an visibility so it doesn't draw by default. # NOTE: since the rigid body is not a MPxShape it doesn't # support an intermediateObject attribute. if transformName and hideShape: _setAttr(rbShape, 'visibility', 0) # Create Solver (proto) solver = BulletUtils.getSolver() # Set Attrs (optional, set if value != None) # Use the settableAttrs list above to qualify kwargs passed into the function for k,v in locals().iteritems(): if k in CreateRigidBody.settableAttrs and v != None: _setAttr( rbShape, k, v ) # Additional Actions # ******* TODO: Need to enable local transform instead of just worldSpace. Pass in parentMatrix to rigidbody # Store t and r (used below) origTranslate = _getAttr(rbXform, 'translate')[0] origRotate = _getAttr(rbXform,'rotate')[0] # Connect _connectAttr( _attr(rbXform,'worldMatrix'), _attr(rbShape,'inWorldMatrix') ) _connectAttr( _attr(rbXform,'parentInverseMatrix'), _attr(rbShape,'inParentInverseMatrix') ) _connectAttr( _attr(solver,'outSolverInitialized'), _attr(rbShape,'solverInitialized') ) _connectAttr( _attr(solver,'outSolverUpdated'), _attr(rbShape,'solverUpdated') ) _connectAttr( _attr(rbShape,'outRigidBodyData'), _attr(solver,'rigidBodies'), nextAvailable=True ) # REVISIT: Consider alternatives like a single initSystem bool # attr instead of startTime and currentTime. # Might be able to get around needing it at all _connectAttr( _attr(solver,'startTime'), _attr(rbShape,'startTime') ) _connectAttr( _attr(solver,'currentTime'), _attr(rbShape,'currentTime') ) _setAttr(rbShape, 'initialTranslate', origTranslate) deg2Rad = 3.14159 / 180 _setAttr(rbShape, 'initialRotateX', origRotate[0] * deg2Rad) _setAttr(rbShape, 'initialRotateY', origRotate[1] * deg2Rad) _setAttr(rbShape, 'initialRotateZ', origRotate[2] * deg2Rad) pairBlend = maya.cmds.createNode( "pairBlend", name= "translateRotate") _setAttr(pairBlend, 'inTranslate1', origTranslate) _setAttr(pairBlend, 'inRotate1', origRotate) _connectAttr( _attr(rbShape, 'outSolvedTranslate'), _attr(pairBlend, 'inTranslate2') ) _connectAttr( _attr(rbShape, 'outSolvedRotate'), _attr(pairBlend, 'inRotate2') ) _connectAttr(_attr(pairBlend, 'outTranslateX'), _attr(rbXform, 'translateX'), force=True) _connectAttr(_attr(pairBlend, 'outTranslateY'), _attr(rbXform, 'translateY'), force=True) _connectAttr(_attr(pairBlend, 'outTranslateZ'), _attr(rbXform, 'translateZ'), force=True) _connectAttr(_attr(pairBlend, 'outRotateX'), _attr(rbXform, 'rotateX'), force=True) _connectAttr(_attr(pairBlend, 'outRotateY'), _attr(rbXform, 'rotateY'), force=True) _connectAttr(_attr(pairBlend, 'outRotateZ'), _attr(rbXform, 'rotateZ'), force=True) _connectAttr(_attr(rbXform, 'isDrivenBySimulation'), _attr(pairBlend,'weight'), force=True) _connectAttr(_attr(rbXform, 'rotatePivot'), _attr(rbShape,'pivotTranslate') ) # ****** TODO: Remove the unused pairBlend weight attrs # Select the rigidBody transform and return the resulting values maya.cmds.select( rbXform, replace=True ) return [ rbXform, rbShape ]
def command(*args, **kwargs ): ret = [] transformNames = [] if (kwargs.has_key('bAttachSelected') and not kwargs['bAttachSelected']): if kwargs.has_key('transformName'): transformNames = [kwargs['transformName']] else: # Make sure the list doesn't contain any bullet objects. transformNames = _getSelectedTransforms() if (kwargs.has_key('ignoreShape') and kwargs['ignoreShape']): shapes = [] else: shapes = _getSelectedShapes(transformNames) # remove transforms already processed as shapes if shapes and transformNames and len(transformNames): filteredTransformNames = [] for transformName in transformNames: ts = _getSelectedShapes(transformName) ts = ts if ts else [] if len(ts)==0 or len(set(ts).intersection(shapes))==0: filteredTransformNames.append(transformName) transformNames = filteredTransformNames # if no shapes and no transforms create bullet object without node if (not transformNames or len(transformNames)==0) and (not shapes or len(shapes) == 0): transformNames = [None] if shapes and len(shapes): kwargs['shapes'] = shapes rbShapes = CreateRigidBody.command_shape(**kwargs) ret += rbShapes if rbShapes else [] if len(transformNames): # Create rigid bodies without an associated shape # Verify collision shape type is valid. if (kwargs.has_key('colliderShapeType')): shapeType = kwargs['colliderShapeType'] if (shapeType == eShapeType.kColliderHull) or (shapeType == eShapeType.kColliderMesh): kwargs['colliderShapeType'] = eShapeType.kColliderBox for transformName in transformNames: # Make sure the transformName doesn't contain any bullet objects. if transformName and BulletUtils.getRigidBodyFromTransform(transformName): OpenMaya.MGlobal.displayWarning(maya.stringTable['y_RigidBody.kAlreadyBulletObject2' ].format(transformName) ) continue kwargs['transformName'] = transformName rbShapes = CreateRigidBody.doCommand(*args, **kwargs) ret += rbShapes rbShape = _longName(rbShapes[1]) if transformName and kwargs.has_key('autoFit') and kwargs['autoFit']: (radius, length, ex, ey, ez, cx, cy, cz) = refitCollisionShape(rbShape, transform=transformName) kwargs['radius'] = radius kwargs['length'] = length kwargs['extents'] = [ex, ey, ez] kwargs['centerOfMass'] = [cx, cy, cz] if len(ret): maya.cmds.select(ret, r=True) # If command echoing is off, echo this short line. if (not maya.cmds.commandEcho(query=True, state=True)): print("RigidBody.CreateRigidBody().executeCommandCB()") print "// Result: %s //" % string.join(ret, " ") return ret