X-Plane / XPlane2Blender

Scenery & Aircraft export addon for Blender and X-Plane
GNU General Public License v3.0
197 stars 67 forks source link

Text substitution in dataref paths #398

Closed dpospisil closed 5 years ago

dpospisil commented 5 years ago

It would be very useful if the exporter could do some sort of text substitution in dataref paths.

The use case is multiple objects in scene with the same animations which are required to animate individually. Currently, one needs to manually override dataref names in every object.

If there is a way to include object name or other blender properties in the dataref path, that would be awesome.

tngreene commented 5 years ago

I think a Python script could solve this easily instead of adding to the XPlane2Blender code base. Make Blender file with 4 cubes in it called "Cube", "WingL", "WingR", and "SomethingElse", a text block and paste this in:

# bpy is the module that contains all code related to the Blender data model
import bpy

# Make a list of objects you want to alter.
# You could also change it to all the objects currently selected,
# or get every object with part of a certain name,
# or every object that has some certain piece of data.
# This is programming, do what you want!
# 
# You'll see that Cube is untouched because it isn't in the list
objs_with_same_dataref = [
    bpy.data.objects["WingL"], 
    bpy.data.objects["WingR"],
    bpy.data.objects["SomethingElse"]
]

# The dataref you want to apply to every one of those objects
# Try uncommenting only one of the following to rewrite the datarefs
#dataref_for_all = "sim/graphics/animation/sin_wave_2"
dataref_for_all = "third/party/ref"
#dataref_for_all = ""

# Iterate through every object and change them
for ob in objs_with_same_dataref:
    # Selecting which dataref in the list of datarefs
    # might be tough, but you can figure it out
    # For now I'll just assume that each of these has one dataref
    #
    # See what happens if you include "Cube" in your list!
    ob.xplane.datarefs[0].path = dataref_for_all

Run with the "Run Script" button or with Alt+P.

If you run this, every object in your list of objects to alter will have the same new dataref path. No special parenting needed. Since the path is just a string, whatever you want or need it to be is easy.

Python programming is very easy and this sort of stuff where you access the Blender data model and change parts of it is also very easy. It is a great productivity booster! Are you very familiar with it?

tngreene commented 5 years ago

I'm closing this issue because it is a feature that won't be added, but please feel free to make more comments or e-mail me about the example I posted.

dpospisil commented 5 years ago

Using blender python scripting is certainly an option. However if you later need to update the model and undo and redo the changes done by the script it gets complicated.

Currently, we are working on a complex aircraft with many many switches, levers, etc. One need to manually enter dataref names for each of the animated component. It would be so cool if the dataref name could by inherited from the scene structure... It would save so much time! I am planning to write some script to post process the obj file generated by the exporter to do just that for the next project. I was just thinking that it would be so cool if it was integrated into exporter... :)