Hi,
yes, while I still have no c4d here (so I could not use your zip), this does make things a lot more understandable for me.
Here is a quick sketch on how I would go about implementing, what you want to do.
class LightRig(c4d.plugins.ObjectData):
def __init__(self):
'''
'''
self._light_rig_cache = {}
def Init(self, op):
'''
'''
self.SetOptimizeCache(True)
return True
def _load_first_object_from_file(self, path):
''' Returns the first object from a file path interpreted as a c4d
document.
Args:
path (str): The file path.
Raises:
IOError: When the file path does not exists or could not be red.
IOError: When the document does not contain any objects.
Returns:
c4d.BaseObject: The first object of the document.
'''
doc = c4d.documents.LoadDocument(path, c4d.SCENEFILTER_NONE, None)
if doc is None:
msg = 'Could not load file.'
raise IOError(msg)
op = doc.GetFirstObject()
if op is None:
msg = 'Target object not found.'
raise IOError(msg)
return op.GetClone()
def _get_light_rig(self, lid):
''' Either loads the light rig with the given id from a file or
returns a copy of the local cache.
Args:
lid (int): The integer identifier for the requested light rig.
Raises:
IOError: When the hardcoded file path does not exists or could
not be red.
ValueError: If lid is not a legal light rig id.
Returns:
c4d.BaseObject: The requested light rig.
'''
# Return an cached object from our ligth rig hash table
if lid in self._light_rig_cache:
return self._light_rig_cache[lid].GetClone()
# Or load it from a file and add it to the cache
if lid == c4d.ID_MY_FANCY_LIGHT_RIG_1:
rig = self._load_first_object_from_file(SOME_FRIGGING_FILE_PATH_1)
self._light_rig_cache[lid] = rig
elif lid == c4d.ID_MY_FANCY_LIGHT_RIG_2:
rig = self._load_first_object_from_file(SOME_FRIGGING_FILE_PATH_2)
self._light_rig_cache[lid] = rig
# ...
else:
msg = 'Illegal light rig id: {}.'
raise ValueError(msg.format(lid))
return rig.GetClone()
def GetVirtualObjects(self, op, hierarchyhelp):
''' Returns the preloaded light rig cache.
'''
# The more verbose form would be somehing like:
# if op[c4d.LIGTH_RIG_TYPE] == c4d.ID_MY_FANCY_LIGHT_RIG_1:
# return self.get_light_rig(c4d.ID_MY_FANCY_LIGHT_RIG_1)
# elif ...
return self._get_light_rig(op[c4d.LIGHT_RIG_TYPE])
A few notes:
-
I wrote this on an iPad from my head, so, jeah, view it as a 'draft'.
-
Using predefined data from a document is still a rather exotic concept. You might have to preload all data, when you run into threading issues.
-
The preloading / caching of your light rigs is in general a problem, as it will increase the memory footprint of your plugin.
-
Building your rigs procedurally might be a better option performancewise.
Cheers
zipit