2

I want to manipulate options like Disk Cache with python:

enter image description here

Any Ideas?

p2or
  • 15,860
  • 10
  • 83
  • 143

1 Answers1

6

Direct Access

You can access all 'cloth properties' of an object by referencing the cloth modifier:

>>> bpy.data.objects['Cube'].modifiers['Cloth'].settings.mass
>>> 0.30000001192092896
>>> bpy.data.objects['Cube'].modifiers['Cloth'].point_cache.frame_start
>>> 0
>>> bpy.data.objects['Cube'].modifiers['Cloth'].point_cache.use_disk_cache
>>> False

To find the desired property you can use python's dir() method, which returns a list of valid attributes for e.g.:

  • modifiers['ClothModifierName'].collision_settings
  • modifiers['ClothModifierName'].settings
  • modifiers['ClothModifierName'].point_cache

which also contain all the relevant attributes.

enter image description here Click to enlarge


Setting Cache attributes like use_disk_cache and frame_start of the active object:

>>> obj = bpy.context.active_object
>>> obj.modifiers['Cloth'].point_cache.use_disk_cache = True
>>> obj.modifiers['Cloth'].point_cache.use_disk_cache
>>> True
>>> obj.modifiers['Cloth'].point_cache.frame_start = 10
>>> obj.modifiers['Cloth'].point_cache.frame_start
>>> 10

To avoid assignment errors you can check the respective 'property type' by using python's built-in type() method:

>>> type(bpy.data.objects['Cube'].modifiers['Cloth'].point_cache.use_disk_cache)
>>> <class 'bool'>
>>> type(bpy.data.objects['Cube'].modifiers['Cloth'].settings.mass)
>>> <class 'float'>

Multiple Objects

In order to set the attributes for multiple 'cloth objects' I'd suggest to check if the respective object has modifiers (also see this answer: Check if active object has a modifier) then iterate through the modifier list and check whether the type is correct:

import bpy

get objects in selection

for obj in bpy.context.selected_objects: # check if object has modifiers if obj.modifiers: print ("has modifiers") for modifier in obj.modifiers: # if object has cloth modifier if modifier.type == "CLOTH": # use disk cache attribute print (modifier.point_cache.use_disk_cache) # set the attribute modifier.point_cache.use_disk_cache = True # mass attribute print (modifier.settings.mass) # set the attribute modifier.settings.mass = 1.0

If you need to bake the simulation via python, see this answer: Setting the context for cloth bake

p2or
  • 15,860
  • 10
  • 83
  • 143