Reputation: 3
I have this code
cmds.select('cat', 'dog', 'cow', 'mouse', hierarchy=True, r=True)
min_time = cmds.playbackOptions(q=True, min=True)
max_time = cmds.playbackOptions(q=True, max=True)
cmds.bakeResults(simulation=True, time=(min_time, max_time))
If, for example, the "dog" object doesn't exist, the command won't run because it is looking for that object. But I do want the command to select all the rest of the objects even if one of them doesn't exist, so that it continues with the baking with the others. To sum up, how can I do to run it even if on of the objects listed don't exist in the scene? I need this script to run in different scenes where sometimes not all of these objects will exist. Thanks.
PS. these objects are only an example, I have like 20 specific objects I must always include in this script.
Upvotes: 0
Views: 794
Reputation: 4777
You can use cmds.ls
to pass a series of object names to it. It will return a list of objects it finds in the scene, so that it will filter out the objects that don't exist. In general, most commands don't require you to pass a selection so you don't have to select anything at all. It's a bad habit to get into since it forces the scene to redraw and it's extra overhead that can easily be avoid. Instead, we can pass the results directly to cmds.bakeResults
:
import maya.cmds as cmds
min_time = cmds.playbackOptions(q=True, min=True)
max_time = cmds.playbackOptions(q=True, max=True)
objs = cmds.ls('cat', 'dog', 'cow', 'mouse') # Returns a new list of objects it finds.
if objs: # Only bake if there is at least one object.
cmds.bakeResults(objs, simulation=True, time=(min_time, max_time)) # Pass new list as first argument.
Upvotes: 1