Loading and positioning an object

Loading and positioning an object

Anonymous
Not applicable
760 Views
11 Replies
Message 1 of 12

Loading and positioning an object

Anonymous
Not applicable
Hello!

With mel scripting, I can import an object into Maya and then move and rotate the object into the position and orientation that I want. A sample mel script is as follows:-

file -import -gr -gl -loadReferenceDepth "all" -shd "renderLayersByName" -gn "my_object" "C:/my_object.3ds";
currentTime 0;
select -r "my_object";
setAttr ".ro" 2;
setAttr ".rp" -type "double3" 0 0 0;
setAttr ".t" -type "double3" -44.059 0.0487435 4.16622 ;
setAttr ".r" -type "double3" 0.0 99.8438 0.0 ;
setKeyframe;


Is it possible to do something similar with a maxscript in 3DMax? Thanks in advance for any help.
0 Likes
761 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Hi,

Do you want a full transcription of the whole script or only rotate/move/scale commands ?

To select an object and move/rotate/scale it you can use either

select $'my_object'
$.pos =
$.rotate = (eulerAngles 0.0 99.8438 0.0)
$.scale =

or

select $'my_object'
move $
rotate $ (eulerAngles 0.0 99.8438 0.0)
scale $


These are commands for basic position, rotation and scale controller types. If any other controller is assigned to position or rotation or scale then the value to be applied might be different but the way it works is similar.

For rotation and scale you have to take care also of transformation center and system coordinates.

To rotate about object's center and object's system coordinates :

in coordsys local (rotate $ (eulerAngles 0.0 99.8438 0.0))


To rotate the object about world center / system coordinates :

in coordsys world (rotate $ (eulerAngles 0.0 99.8438 0.0))


To rotate the object about another object center / system coordinates :

in coordsys $'ref_object' about $'ref_object' (rotate $ (eulerAngles 0.0 99.8438 0.0))


Hope it will help

amarhys
0 Likes
Message 3 of 12

Anonymous
Not applicable
Thank you for your help... it's most helpful. Just to let you know I am completely new to maxscript!

It's the loading of the object that I am having difficulty in figuring out. In the mel script example of my first post, the very first command loads the object and have it assigned to a locator, which is like an empty group, but crucially one that I can give a name to during the running of the script. I then know the name of the object in order to select it when I want move or rotate the object in my mel script.

I would like to do something similar with maxscript, import a 3ds file and and use maxscript to give the rootnode a name of my choosing in order for me to select it. I don't see an option like this in importFile() command. Is this possible or am I missing something?

The reason I need to give the rootnode a name is that I will be importing several objects with my maxscript.
0 Likes
Message 4 of 12

Steve_Curley
Mentor
Mentor
When you ImportFile the object imported is automatically selected, therefore you only need to assign selection to a variable in order to reference it later. You don't actually need its name, though that is easily accessible if you want it. If there is more than 1 object imported from a single file, loop through the selection collection dealing with each one in turn. Note that they retain the names given to them when they were exported (from Max anyway), unless a scene object of the same name already exists in which case you get a pop-up dialog.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 12

Anonymous
Not applicable
That sounds good... it seems like it's possible to do what I need to do in maxscript. I'm going to see how far I can take this on my own... hopefully the answers I've gotten from here should be all that I need.

Thanks for all the help.
0 Likes
Message 6 of 12

Anonymous
Not applicable
I think I am getting there... well I thought I was. It seems I am struggling with setting keyframes for rotation. This is a sample of my maxscript:-


with animate on
(
at time 0 $.pos =
at time 0 $.rotation.x_rotation = 0.0
at time 0 $.rotation.y_rotation = 99.8438
at time 0 $.rotation.z_rotation = 0.0

at time 25 $.pos =
at time 25 $.rotation.x_rotation = 0.0
at time 25 $.rotation.y_rotation = 99.8438
at time 25 $.rotation.z_rotation = 0.0
)


To my mind, the above should make the object be still from frames 0 to 25. However, what I am seeing in Max is the object rotating around y. From looking at the curves, it seems my rotations around y seems to be accumulating. I think I am missing something basic and I am hoping someone can tell me what it is? Thanks.

The odd thing is that the position is behaving as I would expect and not translating.
0 Likes
Message 7 of 12

Steve_Curley
Mentor
Mentor
Rotation is fun - if I could wrap my head around it I'd be a genius...

Go to the Advanced Search and look for "rotation" (without the quotes) and select just this Maxscript forum from the list. There's 5 pages of results - quite a few will relate to reading data and positioning/rotating objects based on that data.

You will almost certainly have to employ eulerAngles as in the 1st reply to this thread.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 8 of 12

Anonymous
Not applicable
Thanks for your reply, but I had a look already and wasn't able to find anything relevant. Hence the question.

I believe my code that I published to be correct though and so I don't understand why I am seeing my object changing orientation between the keys.
0 Likes
Message 9 of 12

Steve_Curley
Mentor
Mentor
Try

at time 0 $.rotation.x_rotation.controller.value = 0.0

and likewise for the other lines.

You can also speed thing up a bit, and do a lot less typing, by assigning the controllers to variables.

--set these up first
xRotC = $.rotation.x_rotation.controller
yRotC = $.rotation.y_rotation.controller
zRotC = $.rotation.z_rotation.controller

with animate on
(
at time 0 $.pos =
at time 0 xRotC.value = 0.0
at time 0 yRotC.value = 99.8438
at time 0 zRotC.value = 0.0

at time 25 $.pos =
at time 25 xRotC.value = 0.0
at time 25 yRotC.value = 99.8438
at time 25 zRotC.value = 0.0
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 10 of 12

Anonymous
Not applicable
Thanks Steve... that works!

The thing I don't get is that the code I published does too, but only sometimes it seems! I guess you couldn't explain to me why my code has problems on occasion?

It's a good tip to assign the controllers to variables but I am hoping to automate this process with a perl script so typing won't be an issue once I've worked out how maxscripting works. I have tons of mel scripts which require translating to their maxscripts equivalent.

Thanks once again.
0 Likes
Message 11 of 12

Steve_Curley
Mentor
Mentor
If I could, I would - hence my comment earlier about (not) being a genius 😉

It's all to do with transform matrices (whatever they are). There's a bunch of stuff in the help about it/them and I'm certain they've been discuss in here as well.

The assigning to a variable isn't just about speed of creating the script it's about the speed of execution. Max has to traverse the whole hierarchy from "$" down to get to the controller before it can assign the value. By doing that once and storing it the access is much faster.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 12 of 12

Anonymous
Not applicable
After lots of fiddling around with the rotation, it seems I've managed to get maxscript to replicate in 3DS Max what my mel script does in Maya. It seems quite simple now but there was a bit of head-scratching to get the rotations to match.

Anyway for posterity and for other maxscript newbies, I'm going to post the maxscript equivalent to the my mel script example in my initial post:-


myobjects = #()

importFile "C:\my_object.3ds" #noprompt
append objects $selection
alignPivot myobjects

with animate on (

select myobjects
at time 0 $.position=
at time 0 $.rotation.controller.x_rotation = 0.0
at time 0 $.rotation.controller.y_rotation = 99.8438
at time 0 $.rotation.controller.z_rotation = 0.0

)


Going from Yup to Zup requires the object be rotated -90 around the x-axis in Max.

Thanks to everybody for their help.
0 Likes