1
$\begingroup$

I am writing a Python script for Blender 2.8 in which I replace objects in the scene with new objects. I start by importing the new object as a .FBX and linking the original object data to the new object. The new object then needs to be scaled down to fit where the old objects were.

The issue is that when I run the script it appears to have worked until I change the animation frame and the scaling I have applied is erased. Here is a snippet of the code

import bpy

file = '\\Fruit&Veg\\Banana_FBX\\Banana.FBX'
i = file.rfind('\\')
new_asset_name = file[i + 1:-4]
bpy.ops.import_scene.fbx(filepath=file)
bpy.context.view_layer.objects.active = bpy.data.objects[new_asset_name]
bpy.ops.object.scale_clear()

bpy.data.object['Orange'].select_set(True)
bpy.ops.object.make_links_data(type='OBDATA')
bpy.context.view_layer.objects.active = obj

ratio = bpy.data.objects[new_asset_name].dimensions[2] / 0.255 # how much larger new object is

bpy.ops.transform.resize(value=(1/ratio,1/ratio, 1/ratio), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, False, True), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)
$\endgroup$
2
  • $\begingroup$ What is the pivot point of the transform resize operator. Setting scale eg blender.stackexchange.com/a/160978/15543 will scale about the object origin. $\endgroup$
    – batFINGER
    Commented Jun 8, 2020 at 8:57
  • $\begingroup$ Solved! I solved it by setting the object's origin to the center of its geometry. The origin was set to the center of the object before scaling it down which explains why it appeared to jump in space $\endgroup$
    – JakeRSA
    Commented Jun 9, 2020 at 13:47

2 Answers 2

1
$\begingroup$

I'm pretty sure your scale is keyframed. Just like when you scale manually and there are scale keyframes present, it will ignore your changes (unless you use auto-keying) and reset to the scale saved in the keyframe. FBX often have everything keyed by default export options of the program they were created with, even if the property isn't animated.
If that's not the case, please don't downvote, but provide more detail, maybe share the .blend. Thank you.

$\endgroup$
2
  • $\begingroup$ Thanks yes there were keyframes present. I have deleted the scale keyframes and my scaling is now rendering as expected. But now the new object jumps to a new location when I scrub through frames! I can't delete location keyframes because I need to maintain the original animation. My script does not apply any location transforms so I'm not sure what the issue is $\endgroup$
    – JakeRSA
    Commented Jun 8, 2020 at 8:35
  • $\begingroup$ Try not to use bpy.ops, whenever you can. There's a chance that you are scaling relative to the cursor, that would explain the jumping. So instead use: myob.scale = Vector((x,x,x)) $\endgroup$ Commented Jun 8, 2020 at 14:12
0
$\begingroup$

Solved! There were existing keyframes on the object that I imported so I had to delete them before changing the scale. The origin was set to the center of the object before scaling which resulted in the object jumping in space This was fixed by setting the origin to the center of its geometry after the rescaling operation

$\endgroup$

You must log in to answer this question.

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