Hi!
For a python plugin I need to use some GLTF files as "asset library" files. For example, I have a gltf with hundreds of meshes in it. Ideally I would be able to scan the gltf for specific meshes by name and attach these to the scene without having to load the complete gltf in memory.
Coming mostly from Blender plugin development, this is my first C4D plugin, so please forgive me that I am not quite fluent in C4D python yet and may have some stupid assumptions.
As far as I can see I need to use c4d.documents.LoadDocument and made some quick tests. I didn't run into anything major as far as I can tell, but I am not sure if I will run into a dead end with this approach.
Here, the first quick tryout:
import c4d
def findChildByName(mom, name, recursive = True):
"""Naive function to find a child object by name. Can produce only one child and is therefore not very sexy."""
if hasattr(mom, 'GetObjects'):
for child in mom.GetObjects():
if child.GetName() == name:
return child
elif recursive:
c = findChildByName(child, name)
if c:
return c
if hasattr(mom, 'GetChildren'):
for child in mom.GetChildren():
if child.GetName() == name:
return child
elif recursive:
c = findChildByName(child, name)
if c:
return c
return None
filepath = "C:/path/to/file.glb"
f = c4d.documents.LoadDocument(filepath, c4d.SCENEFILTER_OBJECTS)
doc = c4d.documents.GetActiveDocument()
eberhardt = findChildByName(f, "Eberhardt")
if eberhardt != None:
doc.InsertObject(eberhardt.GetClone(c4d.COPYFLAGS_0))
Of course the whole glb file is loaded into memory, but otherwise it works. How would I go about without loading the whole file into memory? Do I need to adjust the gltf importer settings similar to the obj importer example somehow? I saw the c4d.SCENEFILTER_IDENTIFY_ONLY
flag... is this one leading to the road of glory?
Is it even possible to load files only partially, or do I need to write my own gltf importer?
I'd be super happy if someone could point me in the right direction!