In what case, the persistent id of material will change?

I’m developing a plug-in that uses the persistent id of materials to distinguish materials. I found that the persistent_id of the same material in different files is different. This situation is reported by the user and I don’t know how these files have been created. I tried copy file and ‘Save as’,failed to reproduce this problem.

A persistent ID is unique within a single model so that it will be the same if you reopen that model. But it is not unique or matched between different models. For that you need a Globally Unique ID (GUID). Saved components have a GUID. I don’t think materials get a GUID.

So, what you will need to do is generate custom UUIDs and attach them to your materials in attribute dictionaries.

Something like:

# Within your extension submodule ...

MATL_DICT ||= Module.nesting[0].name.gsub('::','_') << '_material'

def assign_material_uuid(matl)
  require 'securerandom' unless defined?(SecureRandom)
  uuid = SecureRandom.uuid
  matl.set_attribute(MATL_DICT, 'uuid', uuid)
  return uuid
end

def get_material_uuid(matl)
  require 'securerandom' unless defined?(SecureRandom)
  uuid = matl.get_attribute(MATL_DICT, 'uuid', nil)
  uuid.nil? ? assign_material_uuid(matl) : uuid
end

def has_material_uuid?(matl)
  require 'securerandom' unless defined?(SecureRandom)
  matl.get_attribute(MATL_DICT, 'uuid', nil) ? true : false
end
2 Likes