0

I've been running through the forums and documentation, and would appreciate any help! I want to offset the rotation of an object by [Vector3(X, Y, Z)]

I need to add or subtract the "radians" consistently across multiple scenes

Offset Rotation Process

The juice of the code I'm struggling with is here:

_

 #move the new obj to the original
 ob.location = (ob2.location + mathutils.Vector(list_PosOffset[int_ListLength-1]) )

#assign rotations to be Euler XYZ ob.rotation_mode = 'XYZ' ob2.rotation_mode = 'XYZ'

#match the original object's rotation ob.rotation_euler = ob2.rotation_euler

#Ammend the rotation (+180, +0, -90) #???? I've tried with Euler, Quaternion, and AxisAngle... I'm not sure I understand matrices enough

_

Everything else is working, but after I make 2 objects' pivots line up, I'm having trouble;

  1. adding 180 degrees to whatever the X rotation is
  2. subtracting 90 degrees from whatever the Y rotation is.

If it helps, here's the goal of my script .

INTRO Compare objects replace script is intended to two dictionaries/libraries of objects and arrange them to match up and offset the pivots. Unfortunately many of the pivot offsets are unique, and with 105 instances of this issue, it is ideal to solve this problem with a script.

WHAT SHOULD THIS DO

  1. This script will take 2 lists of all the object names available (manual lits atm)
  2. Then it will loop through each object from the main list

a) it will look for it's matched partner in the 2nd list

b) it will move to that object's pivot

c) it will make (manual) adjustments based on the offset

d) it will delete the 2nd object

  1. After all objects are correctly placed, it will select all objects and export them NOTE: The script may need a step to import scenes (as I don't want to import 105 meshes)

. .

WillSmithsRobot
  • 195
  • 2
  • 12
  • 1
    Related https://blender.stackexchange.com/a/102356/15543 (pre 2.8 answer & will require edit to use @ for matrix multiplication) Please always post text as text , not as an image. https://blender.meta.stackexchange.com/questions/2788/policy-on-posting-code-error-message-etc-as-images – batFINGER Jun 11 '21 at 06:51
  • 1
    @batFINGER , thank you for your reply. I'm following along, but this doesn't really help me as I understand it. I will spend time today and learn about Matrices in blender. I hope I will understand better after – WillSmithsRobot Jun 11 '21 at 15:58
  • In Unity3D & C# this would be the equivalent of

    GameObject.Rotate(X float, Y Float, Z Float, Space.Self);

    – WillSmithsRobot Jun 11 '21 at 16:26
  • I'm following the Matrix documentation, (using a few sites) but I cannot find anything yet that talks about getting and setting these values. I have found people doing it, but I am having trouble finding any way to understand how I can rotate by X and Z locally and with intention.

    Here's the documentation I'm following https://docs.blender.org/api/current/mathutils.html#mathutils.Matrix

    – WillSmithsRobot Jun 11 '21 at 16:57

1 Answers1

0

Well, I got a friend to look at it and this solved my issue.

I think a more practical version requires matrices, but I was able to solve it with the following

#assign rotations to be Euler XYZ
            ob.rotation_mode = 'XYZ'
            ob2.rotation_mode = 'XYZ'
        ob.rotation_euler = ob2.rotation_euler


        ob.rotation_euler[0] += radians(90)
        ob.rotation_euler[1] += radians(90)
        ob.rotation_euler[2] += radians(-90)

FULL SCRIPT HERE (it's not perfect yet, but here's where I got it working)

#INTRO
#Compare objects replace script is intended to two dictionaries/libraries of objects and arrange them to match
# up and offset the pivots. Unfortunately many of the pivot offsets are unique, and with 105 instances of this
# issue, it is ideal to solve this problem with a script.

#WHAT SHOULD THIS DO

1) This script will take 2 lists of all the object names available (manual lits atm)

2) Then it will loop through each object from the main list

a) it will look for it's matched partner in the 2nd list

b) it will move to that object's pivot

c) it will make (manual) adjustments based on the offset

d) it will delete the 2nd object

3) After all objects are correctly placed, it will select all objects and export them

#NOTE: The script may need a step to import scenes (as I don't want to import 105 meshes)

#log print("Starting 'CompareObjs_Replace' script")

#Import references import bpy import math import mathutils from mathutils import Matrix, Euler, Quaternion, Vector from math import radians from bpy_extras.io_utils import axis_conversion

#to access current scene OBJ context = bpy.context scene = context.scene

#Lists of name references list_Updated = ["Couch", ] list_Original = ["sofa", ] #the id of object we're on int_ListLength = len(list_Updated) #the list of position data offsets (XYZ) list_PosOffset = [(0,0,-0.525) , ] #the list of quaternion rotation changes (WXYZ) list_RotOffset = [(180,0,-90) , ]

#log print("Counted: " + str(int_ListLength) + " names in the new list")

iterate thru all obs in scene

for ob in scene.objects[:]: #if we found the new model with our list string reference if list_Updated[int_ListLength-1] in str(ob): #log print("Found New List Ref: " + str(ob.name)) # iterate thru all obs in scene again (to find the match for ob2 in scene.objects[:]: #if we found the original model with our list string reference if list_Original[int_ListLength-1] in str(ob2): #log print("Found Match: " + str(ob2.name))

            #move the new obj to the original
            ob.location = ob2.location + mathutils.Vector(list_PosOffset[int_ListLength-1])




            #rotate the new obj to the originals


            print(" || Rotating || ")
            #___________________________________________

            #assign rotations to be Euler XYZ
            ob.rotation_mode = 'XYZ'
            ob2.rotation_mode = 'XYZ'


            ob.rotation_euler = ob2.rotation_euler


            ob.rotation_euler[0] += radians(90)
            ob.rotation_euler[1] += radians(90)
            ob.rotation_euler[2] += radians(-90)





WillSmithsRobot
  • 195
  • 2
  • 12