Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Plug in to help streamline Cinema 4D to Unity workflow

Discussion in 'External Tools' started by chuckyrobin, Feb 11, 2011.

  1. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    I wrote this plug in purely for myself, to solve some very specific problems that have always bugged the heck out of me, so I’m not entirely sure how much relevance/help it will be for others, but I’m publishing it just in case there are other folks out there who might make use of it.

    I use Cinema 4D for most of my modelling and texturing, and when I first started out I quickly found out that there were some problems with my workflow:

    (1) Not all objects were imported from C4D files. Parametric objects in Cinema had to be converted to polygon meshes.
    (2) The symmetry helper in Cinema would not be imported correctly, so I would only get one half of my mesh
    (3) The HyperNURBS object was not handled, so I would not get the smoothed mesh I wanted but only the low res cage

    These problems meant that my workflow would consist of a cycle of make changes, save, convert all to polygons, resave under a different file, etc etc.

    (I should add here that things have improved with time, and now using when Cinema R12 with Unity 3.1/3.2 points 1 and 2 above no longer appear to be an issue. But as far as I can work out, 3 is still a problem. I realise that these things are not the fault of Unity itself, but limitations in Cinema/FBX export.)

    And last but not least ...

    (4) Whilst it is sometimes convenient to model/texture multiple models in one Cinema 4D file, I often then find myself saving off bits and pieces of models into separate files for import into Unity. This quickly gets tedious. I wanted to automate the process.

    (5) I might have objects in my modelling file that I did not want exported, for instance intermediate steps, guide objects or alternative ideas.

    This very simple plug in helps me with all the above. In summary, it:

    (1) Creates meshes of everything
    (2) Allows saving of separate objects/hierarchies into separate Cinema 4D files
    (3) Allows only specific objects/hierarchies to be exported, allowing me to keep other stuff in the C4D file and not worry about it getting into Unity.
    (4) Leaves the original master file unaltered

    (As regards (4), this is not quite true, as there is a “Keep” option which leaves the mesh versions in the original file. This was always more for debugging purposes than anything else.)

    The version here is a Cinema R12 Python plug in, but I started with this years ago, so there is a version for older versions (R11.5 and earlier). If you need that version please let me know and I’ll clean it up for posting here.

    The ZIP file contains the plug in, and a scene (export_helper_object.c4d) which is empty apart from the ExportHelper object, which is required for the plug in to function.

    There’s also a sample scene (sample_scene.c4d), with some objects already linked to the relevant fields on the ExportHelper object. You still have to provide some file paths, though.

    INSTALL:

    To install the plug in, put the ExportToUnity folder into your Cinema 4D plug ins directory. I work on a Mac, and I put mine in:

    /Applications/MAXON/CINEMA 4D R12/plugins/

    (There is also a “user’s local” plug ins directory. I’ve not tested putting this in there, but there’s no reason why it wouldn’t work the same.)

    On Windows there is a plug in directory at:

    C:\Program Files\MAXON\CINEMA 4D R12\plugins

    You need to quit and relaunch Cinema for the plug in to register. It will then appear under the Python menu -> Plug ins submenu.

    OPTIONAL:

    It helps to create a toolbar button that calls the plug in. If you do this the button should display a Unity logo on it. (Hint: Use the Command Manager, to be found under the Window menu -> Layout submenu.)

    USAGE:

    Copy and paste the ExportHelper object into your scene. It’s simply a Cinema 4D Null object with a load of User Data fields attached to it. (Yes, clunky, I know.)

    There are ten “slots” which you can use to define which objects/object hierarchies get exported. For each you specify a directory to export to. (Either by typing it in or using the select directory button to the right of the path field.) If you just specify a path, the filename of the exported file is derived from the specified object/hierarchy. If you prefer you can specify your own filename by adding it on to the end of the displayed path.

    There is an “Active?” checkbox for each item, so you can turn exporting on/off on an item by item basis.

    There is also an “Active?” checkbox for the whole ExportHelper object, I’m not sure what I was thinking when I did this, so it will probably be deleted eventually.

    There is also “Keep” option. Checking this leaves the polygon meshes that were exported in your scene. This was more for my debugging than for anything else, but it does give you a way to check up on what is being created when you use the plug in.

    With the “Keep” option checked, each exported mesh object appears immediately below the original object in the Object Manager.

    The “Dynamic” option is a throwback to when I was tinkering with making the plug in so that it constantly updated the export files every time changes were made to any of the specified hierarchies. In this version it doesn’t do anything, so, like the “Active?” checkbox for the whole ExportHelper object it will probably end up getting deleted.

    There is a report field at the bottom of the ExportHelper object’s data fields, where the plug in provides some feedback about what it has done. (It frequently does not update unless you collapse it down and then expand it up again. I’m guessing I’m missing a UI refresh call somewhere. It’s on the list of things to look at and fix.)

    KNOWN ISSUES/GOTCHAS:

    There is some basic error checking going on, but not a whole load. For instance, the plug in does check if the directory path exists, but some other situations are not checked for. I hope to improve this over time.

    At the moment if you are editing an object in points, edges or faces mode, the plug in doesn’t clean up after itself very neatly (although you don’t lose any work, you just have to change tools and do some deletion of objects yourself as opposed to have the plug in do it all for you). So it’s best to only call the plug in whilst in the “Use Model Tool” or the “Use Object Tool”.

    Textures: the plug in does not handle texture bitmaps, although any materials that are used are copied across into the export files without a hitch. This means that at the moment you have to move across any linked bitmaps into the appropriate Unity assets directory yourself. (Making this seamless and automatic is my number one priority when I get a moment.)

    Saving: at the moment you run the plug in every time you want to export. Eventually I’d like to hook this in to Cinema 4D so that it automatically executes every time you hit command S or select “Save” from the File menu.

    When I get a moment I intend to revise the old Cinema 4D SDK version so that users of Cinema 4D 11.5 and earlier can benefit too.

    TESTING:

    I’ve tested this with Cinema R12 on my Mac and with a time limited activated version of the R12 demo on Windows 7. I don’t have the resources at the moment to carry out more extensive testing.

    If you find errors, have constructive criticism, suggestions, etc, please feel free to post/contact me.

    HOW I USE IT ...

    I guess if people find this useful they will find their own way with it, but this is how I tend to use it:

    UserHomeDirectory
    -> MyGameProjectDirectory
    -> -> UnityProjectDirectory
    -> -> -> Assets

    I keep all my “master” Cinema 4D files in the “MyGameProjectDirectory” (or in their own folders within that directory), and then use this plug in to export to the Unity project’s “Assets” directory.

    I find that with some judicious organisation of the master models I can avoid fiddling in the “Assets” directory with constant cycles of saving and/or exporting as FBX. I’m not very good at finalising models but like to constantly tinker and improve, and I personally find that using this plug in makes that process far less painful.
     

    Attached Files:

  2. 01iv3r

    01iv3r

    Joined:
    Mar 6, 2011
    Posts:
    17
    Dude, this is awesome. Haven't tested it yet but thank you for posting. Workflow makes perfect sense.
     
  3. vnomad

    vnomad

    Joined:
    Jun 11, 2010
    Posts:
    6
    Yes!
    Grabbed this, and happy to report that it appears to also work well on R12 in WinXP.
    Nice to simply grab hyperNURBS like this!
    I tested with your sample file, and also with a more complex architectural scene.
    The plugin comes in very handy when I have to experiment with aligning surface normals to get past U3Ds backface culling.
    Thanks a lot, a real timesaver!
     
  4. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    Thanks for the thanks, so to speak ... I'm hoping to get a little update out next that uses tags in the standard Cinema 4D way to specify what gets exported and where, which means that there will be no more ExportHelper object bodge. At the moment whatever hierarchy is specified for export gets exported in its entirety. I'm hoping to add a feature which will allow parts of hierarchies to be excluded from export, for that little extra bit of flexibility. I've made some progress in having the plug in support textures (ie: automatically copying them across into the assets directory along with the meshes) but I doubt that will make this next version.

    If there are any feature requests etc, comments that you have after having used this thing, please let me know.
     
  5. vnomad

    vnomad

    Joined:
    Jun 11, 2010
    Posts:
    6
    Getting specific textures automatically copied to the right folder would be another bonus timesaver.
    Thanks again.
     
  6. Qubits_Toy

    Qubits_Toy

    Joined:
    Mar 13, 2011
    Posts:
    2
    I use Cinema4D 11.0 - I can't get the export helper to load - "Unknown Format" - I am new to all this so I might have done something wrong.
     
  7. Adamo

    Adamo

    Joined:
    Dec 17, 2010
    Posts:
    55
    @ chuckyr : Thank you! I tested your plugin on C4D R12 running on 64 bits Windows 7, and it works.. Great...
    However, when I export models from my C4D into the assets directory of my Unity project director, most of the surfaces wrongly appear as transparent. I assume that this is about the surface normals and backface culling problem that @vnomad mentioned in his message.

    @ vnomad : Can you please explain how to "..aligning surface normals to get past U3Ds backface culling." ?

    Thx
     
  8. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    @ Qubits_Toy: The plug in is a Cinema 4D R12 item for now.

    @Adamo: One place to look is under the Functions menu, where you will find the Align Normals and Reverse Normals commands.

    Make sure you are editing a mesh in polygon mode (as opposed to object mode, points mode, lines mode etc - ie: make sure "Polygons" is selected under the Tools menu.). Select the polygons that are causing problems, use the functions to fix.

    It helps to make sure you have some form of surface normals display turned on under view port configuration: under the Edit menu of your viewport (not the main Edit menu) you will find the Configure and Configure All commands.

    You can then turn "Normals" on (if it is not already on).

    You can then also use "Tinted Poly Selection" and "Tinted Poly Normals" to further customise polygon/normal display.

    Tinted Poly Selection is good, as it tints polygons with different colours depending on their orientation. If you want both the tinted polygons and the little normal direction indicator lines, check Tinted Poly Normals.

    Hope this helps. Regards.
     
  9. Adamo

    Adamo

    Joined:
    Dec 17, 2010
    Posts:
    55
    @ chuckyr : Thank you for your excellent explanation. It certainly helped me a lot.
    Is there a way in C4D to make a polygon surface "double sided" i.e it has normals on both side of the polygon?
     
  10. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    @Adamo: No. Polygons always have one direction only.
     
  11. HeliosDoubleSix

    HeliosDoubleSix

    Joined:
    Jun 30, 2013
    Posts:
    148
    I want to kiss you full on the mouth

    FYI given Unity just converts C4D files to FBX, you can change the export format to FBX ( and thus speed things up and no longer have it opening C4D just to reimport a model ):

    1026370 is the code for FBX file format:

    c4d.documents.SaveDocument( theNewDocument, theSaveFilePath, c4d.SAVEDOCUMENTFLAGS_DIALOGSALLOWED, 1026370 )

    Then also change line

    theFileNameForExport = theObjectHierarchyToExport.GetName() + CINEMA_4D_FILE_EXTENSION

    to

    theFileNameForExport = theObjectHierarchyToExport.GetName()
     
  12. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    This is an update to the plugin above.

    It still basically addresses the same core issues:

    When modelling, I create a lot of intermediate/alternative versions of meshes, alternate ideas, and meshes that are there purely for use as “guides”, and so on. The “normal” Cinema 4D to Unity workflow would involve either:

    (1) saving this whole Cinema 4D file into the Unity Assets directory, or

    (2) manually sorting out the required mesh groups from all the other stuff and then saving them into separate files in the Assets directory.

    Neither of which I’m willing to waste my time doing.

    In addition, there recently seems to be an issue with Unity that means native Cinema 4D files are not being imported (ie: not converted by the Unity/Cinema combo into FBX) correctly. (This appears to even include the latest beta version of the Unity editor I am using at the moment, version 2017.1.0b6. I think this all started with the R18 upgrade? But I’m not positive.)

    Therefore I’ve updated the export options to include an FBX option, sidestepping the whole business of problems with C4D files. (Also inspired by HeliosDoubleSix pointing out that the original would be improved by changing the export format from C4D to FBX. Thank you HeliosDoubleSix.)

    Since release 12, when I last mucked around with this code, most of the other things with Cinema 4D’s FBX output seem to have been more or less sorted out: getting meshes from subdivision surfaces, symmetry objects etc — all of that seems to be a lot less hassle than it used to be years ago.

    One final big thing I added was (again) something help me cut down on the button pushing/key bashing in Cinema 4D: the option to do a standard Cinema 4D “Save” command before doing the export to Unity commands.

    This involves some extra setup: you need customise your Cinema 4D commands (Window -> Customisation -> Customise Commands) so that command-S (or control-S or whatever) is no longer the standard “Save” command, but is instead assigned to the plug in’s “Export to Unity” command. Then you enable “Do Cinema 4D Save command”. (Under the Unity menu that is attached to the main Cinema 4D menu bar.)

    So now, every time you hit command-S, your document is saved as normal, and then your exports are carried out, all on one keystroke.

    There is also now a “stop tag”, which, when placed on a sub-hierarchy of objects excludes them from export. This was because I would sometimes find myself rearranging a hierarchy from something that was convenient for me into something that was convenient just because of the plugin. Which is not the way things are supposed to be.

    If you place an export tag at the top of a hierarchy, and then another one further down, the one further down also implicitly stops the upper one: so you get one file with everything up until the second lower tag, then a second file with the lower tag and everything below.

    Other than that, the main change is that it is now written as a proper Python plug in, as opposed to the bodge up it was before. This means there is no longer a helper object with a limited number of available slots for exports: instead, you apply a “Export to Unity” tag, and set up your output settings (path, optional filename, file format, FBX options, mesh creation/flattening options) on that tag. (Different objects/object groups can have different tag settings.) You apply as many tags as you want.

    Then, every time you hit “Do the export to Unity” (or select it from the Unity menu) every one of your export tags is executed.

    As regards the mechanics of the code: the first version did some processing of geometry in the user’s “main” document before then moving it all to a temporary document to finish things off and do the actual export. I think this was less than ideal, as sometimes there would be problems, which would mean that the script might stop in an error state whilst only half-complete, leaving the user’s work in a potentially messed up state. Not good.

    The script also used to mess with the user’s current tool/selection settings, ie: if you were in polygon editing mode, and ran the command, it dropped you back into object editing mode. Very annoying.

    These issues have all been fixed: all work geometry is isolated out of your document into a new one, and only then are things like “Current state to object” and “Connect and delete” executed. Once the export has been done, you should be back in your own document, with the same selection/modelling options you had before.

    There were some options on the original helper object which I never used (dynamic, keep etc), so they are all gone. I really don’t know why I bothered with them in the first place.

    Installation is the usual: unzip, drag the resulting “Export To Unity” directory to your Cinema 4D plugins directory. (The old version was “ExportToUnity”, so there should be no messages about overwriting old versions. There is no way of migrating from the old version to the new one, you have to just manually reconfigure each of your exports on the old helper object to a new tag version.)

    Any warnings/problems that the script encounters are output to the standard Python console.

    I’ve been able to test it on Cinema 4D R18, and on a friend’s old installs of R17, 16 and 15 (all Macintosh OSX) where it runs fine. R15 does not seem to allow mucking around with the user’s FBX export settings, so those options are absent from that version. For R16 and R17, there are a couple of specific FBX options which are not relevant, so they are marked R18 only. (For instance, Allegorithmic Substance support etc.)

    I have also been able to borrow a little time on a local studio’s workstation and to quickly check it out on Windows versions of R16 and R17, where it also seems to run fine.

    R14 and below? I haven’t got a clue what happens ...

    The plugin uses the user’s preferences for FBX export. All the options for overriding these FBX settings are only there because of the (very very rare) times when I found that I needed a different setting for one particular object/file.

    But generally it seems to work great without these FBX option overrides, so most of the time that part of the tag is left disabled.

    I see that the last version was designated version 0.9, so I figured I might as well call this version 1.0 beta, as it’s pretty much all the way I envisaged it being. (Finally.)

    As before, it’s really designed purely to help me with the way that I seem to prefer working, so I’m not sure how useful other folks will find it. But ... here it is if you do fancy trying it out. (Or hacking the source code into something that is more useful to you.)

    KNOWN ISSUES/GOTCHAS:

    (1) The “connect and delete” option only works if the “current state to object” option is selected (for parametric/generated objects like cubes, spheres, tubes etc.) If you have parametric/generator objects and you don’t check “current state to object”, all you’ll get out of “connect and delete” is empty nulls.

    (2) Backslashes, forward slashes and colons are not allowed in file names, they are all replaced with “-”. I always get a little mixed up about which characters are the path delimiters on which platforms, so I get a bit over zealous about just banning them all.

    If you encounter any problems or have any suggestions, please don’t hesitate to contact me or post here.

    Charlie
     

    Attached Files:

  13. chuckyrobin

    chuckyrobin

    Joined:
    Feb 11, 2011
    Posts:
    9
    A postscript: I forgot to include the link in the previous post, the source code for the plug-in can be viewed on GitHub:

    https://github.com/charlierobin/cinema4d-to-unity

    Also, there is a simpler plug-in that slightly simplifies the export of Cinema 4D scenes as FBXs. I just wanted something that would allow me to hit command-S and (1) have the normal “save” command executed and then (2) have the scene exported to my assets/meshes directory.

    https://github.com/charlierobin/automatic-fbx-export

    Very very basic, but sometimes I have found it a useful timesaver.

    Charlie
     

    Attached Files:

    razzraziel likes this.
unityunity