Solved Python script for texture baking

I'm working on a python tool for running the bake texture tag on launch of a project. The problem we have here is we do a lot of large scale animated spherical renders that need to be baked down to flat UVs. Running the bake texture tag on a single workstation can take hours and we often have a lot of them to do plus iterating the process every time there are revisions. We have a render farm but currently no way to distribute the baking process to the farm.

What I'm trying to achieve is a script that will execute the bake texture tag when it's opened on a render node. For example, I'll set up a render to submit to our render farm through Deadline, but when the render node launches it will run the python script and bake the texture before it starts rendering.

I wrote the following script that basically does what I want locally:

obj = doc.GetFirstObject()
tag = obj.GetFirstTag()
c4d.CallButton(tag(), c4d.BAKETEXTURE_BAKE)

It works as long as my object and bake texture tag are at the top of the hierarchy.

I tried to put this into a python tag but I got an error:

"RuntimeError: must be called from the main thread"

So I guess I can't use the CallButton command from within the python tag.

Any ideas how can get this to work?

Why not just rendering with a spherical cam or use a camrig for it?

Otherwise: Maybe creating a simple command plugin and run it via callCommand could work.

The renders are spherical but the output needs to be baked out into UV space.

These are for live event projections and the media server has a UV map of the space. So we create scenes in 3D and often render them spherically to preserve the 3D orientation of the scene, but the media server doesn't understand spherical projection so we bake them down.

I'll have to look into callCommand.

Would the command plug-in have to be installed on each render node? Could the callCommand be run from a python tag?

To be honest. Just render the LatLong and then render the texture on a plane with the correct uv layout in the picture viewer or use Nuke or AE for it.

Double the render double the fun.

The Plugin has to be installed on all maschines - but your mentioned deadline so this should be no problem, just create a maintainance job for all clients and copy it via cmd or batch file.

Also it seems that call command isn't an option here at all:

Warning Must be called from the main thread only. Forbidden to call in expressions (Python generator, tag, XPresso node, etc.). See Threading Information.


Sorry for the missleading.

If you want to continue trying to kill the problem with python: you can run a python script as a monitor , waits until the scene loaded and than trigger some sort of buttonpressing in the mainthread.

But after all - rendering should be way faster than developing this.

Hi @GHebert welcome in the plugincafe forum.

Few rules (don't worry I've set up your first post correctly, and moved it to the correct category but for the next one try to follow these rules).

since you are in a threaded environment instead of using CallButton which should occur in the main thread (since it's a GUI interaction) you should instead use the c4d.utils.BakeTexture.

You can find an example in the github repository py-texture_baker_r18.pyp.


Thanks for the response @pyr. We use Nuke to render out the UVs when a perspective camera can be used, but for some reason the spherical camera breaks the process.

The UVs are tied to the geometry of the space, for example: a theater including walls and ceiling. We'll get a 3d model of the theater when the Lidar mapping is done and then we UV that. I'm not sure I understand your suggestion to render the texture on a plane with the correct uv layout. How would the theater UVs then be attached to a plane? I would love it if there was a clever way to do this that I hadn't thought of.

Thanks, @m_adam!

That should put me a step closer.

@GHebert Did you get this figured out? Do you know if you can do it for camera mapped textures?

Hi @emlcpfx please open a new topic with your exact question and with an explanation about what you call "camera mapped textures".