1
$\begingroup$

In the example below, I'm using an empty object that I've named "Empty XYZ" as the Coordinate Object, right in the Coordinate node into the Sphere Material (Named: "Strip Material")

Now I would like to delete my Empty XYZ object and insert the values into the mapping node, converting the position of the object (Loc, Rot, Scale) and inserting them into the Mapping node, in order to keep the strip exactly where it was placed thanks to the Empty Object

enter image description here

I would like to use Python for this operation, but I'm not sure about this, I'm doing some tests that don't produce satisfactory results, in some cases the Empty XYZ object might be a child of the object on which the material is applied, and in others not. So I'm looking for a decent solution that works (As long as it can be done)

import bpy

mat = bpy.data.materials['Strip Material']
nodes = mat.node_tree.nodes

coordinate = nodes['Texture Coordinate']
mapping = nodes['Mapping']

sphere = bpy.data.objects['Sphere']  # The sphere object
empty_xyz = bpy.data.objects['Empty XYZ'] # The empty object

loc = empty_xyz.location[:]
rot = empty_xyz.rotation_euler[:]
scale = empty_xyz.scale[:]

mapping.inputs[1].default_value = loc
mapping.inputs[2].default_value = rot
mapping.inputs[3].default_value = scale

coordinate.object = sphere

This example is a basic example in order to explain better what i want to do, I don't know if I have to apply matrix inverse, or other mathematical formulas to convert.

I can't find the right approach to this solution, and I'm not even sure if I'm doing anything that would work.

Note: I would like to avoid Bake at all costs

Any help will be appreciated

$\endgroup$
1
  • 1
    $\begingroup$ I don't quite have the time/energy to do this right now, but I would start by taking the matrix_world of both objects to get rid of the parenting issues, then take the location difference, that's simple. I'm sure there's some rotational difference calculations here on BSE to find, and once you have that, you have to figure out the order (use a separate mapping for each). Once you get that working, do also a scale difference, see if you need to put that mapping first or last. Then see if it can all be expressed as one mapping (depends on order) $\endgroup$ Commented Jan 31 at 18:27

1 Answer 1

2
$\begingroup$

Here is the second part of your script:

# We need to get relative transform of two transform matrixes
# to do this multiply inverted matrix of empty over sphere matrix:
matrix = empty_xyz.matrix_world.inverted() @ sphere.matrix_world

# Decompose matrix into location, rotation and scale:
(loc, rot, scale) = matrix.decompose()

mapping.inputs[1].default_value = loc
# convert quaternion to Euler:
mapping.inputs[2].default_value = rot.to_euler()
mapping.inputs[3].default_value = scale

# Remove empty object from node:
coordinate.object = None
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .