C4D might have been 'messed up' based on my prior attempts. I've restarted it and may be making progress.
I'm a c4d guy that knows enough about c4d to know that I don't know anything about it. I'm a python hack that tries to make productivity tools for my workflow. Most of my solutions don't have proper error catching and are not fit for public consumption. I write them for me and assume the the input is what the script is expecting.
Most of all I appreciate that Maxon has the python sdk and I get a chance to participate in creating things to automate c4d.
Best posts made by .del
found it!!! I had to float('0.5')
Latest posts made by .del
Thanks Ferdinand. I'll take a stab at it again. I have to start by learning more about your Python syntax.
Did I mention I'm a 3D/mograph guy that pretends to understand python and the c4d sdk? :)
Thanks for taking the time to help people like me out. I try to do what I can with the documentation, forums and search but sometimes it's the syntax in the documentation that I don't quite understand. That's the downside for me. I learn a little more each time though and that helps.
Thanks again for the example. I'll see if I can get GetAllTextures() to work for me.
That makes total sense @ferdinand.
Kind of have to start at the beginning to understand where I'm coming from. I found myself exporting individual .fbx files on a more regular basis but only a couple at a time so it was not a problem to do it in the interface. I added the Export Selected Objects As to my toolbar for ease of use as it fills in the fbx file name with the selected objects name by default. I have my fbx export preferences preset to Selection Only. This work flow has gotten me into a habit of clicking a button to open the export panel and then clicking ok. Easy.
That brings us to this week where I have to export 160 objects. Writing a script to do exactly what I was doing was easy enough. But then I got greedy and wanted to collect the textures as well. We don't use embedded textures in our fbx so collecting them for transfer has been a manual process. That's when I was faced with two options.
- Get a cake :) (I love that analogy by the way) I do this for many of my other exports like .c4d, .obj and .gltf
- Leverage the built in Selection Only aspect of the fbx exporter and then simply use Python to copy some texture files from my tex folder to my delivery folder. It felt like it would be a simpler, lower overhead solution versus creating a 'donor' doc. Or at least it feels like it should be simple. There's even a function called GetAllTextures and an option to have it restricted to a single model rather than from the entire doc.
I'm ok with how I had to make it in the end but now I'm really curious how the GetAllTextures() works in case I can leverage it for this or something else down the road.
The FBX exporter is unique in Cinema since Maxon added the Selection Only option to it. If it weren't for that thing I'd be at the bakery....all.....day.....long :)
import c4d
import os
from c4d import gui
from c4d import documents
from c4d import plugins
def listModels():
selected = []
rawSelection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_NONE)
for item in rawSelection:
if item.CheckType(5100) or item.CheckType(5140): # PolygonalObject or Null
selected.append(item)
rawSelection = []
return selected
models = listModels()
if models == []:
message = "You must select at least one object"
c4d.gui.MessageDialog(message, c4d.GEMB_OK)
else:
gui.SetMousePointer(c4d.MOUSE_BUSY)
c4d.StatusSetSpin()
# Get fbx export plugin,
plug = plugins.FindPlugin(c4d.FORMAT_FBX_EXPORT, c4d.PLUGINTYPE_SCENESAVER)
if plug is None:
print("noPlug")
op = {}
# Send MSG_RETRIEVEPRIVATEDATA to fbx export plugin
if plug.Message(c4d.MSG_RETRIEVEPRIVATEDATA, op):
# BaseList2D object stored in "imexporter" key holds the settings
fbxExport = op["imexporter"]
# Define the settings
fbxExport[c4d.FBXEXPORT_FBX_VERSION] = c4d.FBX_EXPORTVERSION_NATIVE
fbxExport[c4d.FBXEXPORT_SELECTION_ONLY] = True
fbxExport[c4d.FBXEXPORT_TRIANGULATE] = True
fbxExport[c4d.FBXEXPORT_SAVE_NORMALS] = True
fbxExport[c4d.FBXEXPORT_UP_AXIS] = c4d.FBXEXPORT_UP_AXIS_Y
fbxExport[c4d.FBXEXPORT_FLIP_Z_AXIS] = False
fbxExport[c4d.FBXEXPORT_MATERIALS] = True
fbxExport[c4d.FBXEXPORT_EMBED_TEXTURES] = False
fbxExport[c4d.FBXEXPORT_BAKE_MATERIALS] = False
#check for target directory
targetPath = os.path.join(doc.GetDocumentPath(), "FBX_Exports")
if os.path.exists(targetPath) == False: #makes a new directory to store the converted files in
os.mkdir(targetPath)
for obj in (models):
fbxName = c4d.BaseList2D.GetName(obj) + ".fbx" #get the name of the model
fbxPath = os.path.join(targetPath,fbxName)
# Get the texture files and a save a c4d file for rendering
theTextures = doc.GetAllTextures(False, [obj])
print(theTextures)
# if I can get the textures I will write some shutil.copyFile type of stuff here
#export the fbx
if c4d.documents.SaveDocument(newDoc, fbxPath, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, c4d.FORMAT_FBX_EXPORT) == False:
if gui.MessageDialog("Export failed!" + " - " + fbxName, c4d.GEMB_OKCANCEL) == c4d.GEMB_R_CANCEL:
break
c4d.gui.SetMousePointer(c4d.MOUSE_NORMAL)
gui.MessageDialog("Export Complete", c4d.GEMB_OK)
c4d.StatusClear()
Thanks @ferdinand for the reply and the example.
You are correct that my current solution uses IsolateObjects and creates a new doc. Since that doc only has the single mesh and materials in it I can use SaveProject to collect the textures.
In reference to GetAllAssets vs GetAllAssetsNew - I had originally written an exporter in R19 and pulled some of my script forward to begin this new version. I saw the new version for this but I kept getting an error and the old command still worked. I didn't understand what the last parameter was supposed to be. The documentation shows this - assetList (List[Dict]). After awhile I gave up because the old command still worked. I know that isn't the right thing to do but when the clock is ticking I have to go with what I can get working. Your example sheds more light on how I have to format that last parameter. I'm going to give it a try.
Ultimately I was trying to create a different solution that didn't require creating an isolation doc. I feel like I should be able to iterate through a list of selected objects and export them as fbx files without creating temporary documents. I have that working. The next step is to collect the textures for those objects. I can get the material for each object and crawl through the shader tree looking for bitmaps but I was hoping for something that was going to be quicker to set up. GetAllTextures() looked promising but I struggled to get it to work and I haven't been able to find any threads or examples that.
thanks,
.del
I found a way around what I want to do by using this -
c4d.documents.SaveProject(newDoc, c4d.SAVEPROJECT_NONE|c4d.SAVEPROJECT_ASSETS|c4d.SAVEPROJECT_DIALOGSALLOWED, targetPath, theAssets)
I didn't realize that I could skip saving the actual project file but instead use the call just to gather textures. It's probably not very clean creating a new document and isolating my selected models to it just for the purpose of texture collection but it gets me through this jam while I learn more about how to do it the right way.
Hi- I'm a bit stumped and looking for some direction. I have a file with multiple objects in it. I'd like to select 10 of those objects and export each one as an individual fbx file that has only a single object in it. 10 selected objects equates to 10 fbx files. That part I have working.
I'd also like to collect the textures associated with the single material that has been applied to the single object in the fbx.
I tried using doc.GetAllAssets() and set the flag to textures only but I could not find a way to link the single models to the correct texture files in the list. Maybe there is an identifier in that list that I'm not seeing that will allow me to find only the texture files I want versus the list of all textures used?
Since I couldn't figure that out I moved on to doc.GetAllTextures() . I feel like the optional [List[c4d.C4DAtom]] is the thing I need to use in order to get the texture file list for the single, selected model instead of the all of the textures for the entire document.
models = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_NONE)
for obj in (models):
#theAssets = c4d.documents.GetAllAssets(doc, False, lastPath, c4d.ASSETDATA_FLAG_TEXTURESONLY)
theTextures = doc.GetAllTextures(False, [obj])
print(theTextures)
The script returns a c4d.BaseContainer. I'm scratching my head on what to do with that. Am I on the right path or am I missing something?
Any tips on where to look next or reference material on how to get my texture file names out of that BaseContainer will be greatly appreciated.
thanks,
.del
Thanks for the info and adding the tag. I totally forgot that the Cafe' merged. I'm so used to approaching from my view. I actually answered somebodies question yesterday and at the very end saw the C++ tag and hit delete :)
Thanks for tips on the GUI. I was able to implement what I needed.
Hi - I'm currently making an attempt to better understand the gui.GeDialog stuff and ran across this in the sdk.
GeDialog.GroupBorder(self, borderstyle)
Sets the border type of the current group, and displays the title in the border if possible.
GeDialog.GroupBorderNoTitle(self, borderstyle)
Sets the border type of the current group, and displays the title in the border if possible.
Am I reading these wrong and haven't learned anything yet or should GroupBorderNoTitle have a different explanation and not have border title parameters listed below it?
Thanks for looking,
.del
HI -
Thanks for the response!
I'm going to go with the sendModelingCommand method. I never would have thought to put the CallCommand value in there.
This is really going to help me finish this script. I appreciate your help.
thank you,
.del
Hi - I'm wondering if the UV Peeler is accessible via python. I see it listed in the SDK but I'm not familiar with how to use it.
Can it be used with SendModelingCommand or bodypaint.CallUVCommand?
The documentation references C4DAtom.SetParameter() but I've never used this.
Any info that can push me in the right direction is appreciated.
thanks,
.del
@ferdinand Thank you so much for digging into this. I'll look at your example more and try quantitizing the numbers. In the meantime I had written a catch for the -1.1102230246251565e-16 to treat it as zero but it's probably not a very robust solution. I like your idea of rounding them off instead. I didn't fully understand what that number represented. I tried searching it but didn't get any useful results. I tried again today and now I see what it is. I must have had something else in my search field when I originally tried.
We have a script for creating atlases of textures and if the UV is outside of the bounds it gets placed incorrectly on the atlased texture. This new script is intended to catch those so they can be corrected before hand. I think rounding anything within two decimal places of zero will work for our needs.
Is there a way to change the tags for this this thread as it's not a bug report. I thought it was but it turns out it's not.
Thanks you for the help. It's greatly appreciated.