Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Points in local and global coordinates [SOLVED]

  Topic Search Topic Search
Herbie View Drop Down

Joined: 2014 Oct 28
Online Status: Offline
Posts: 28
Direct Link To This Post Topic: Points in local and global coordinates [SOLVED]
    Posted: 2014 Nov 24 at 5:09pm
I am confused as to how some of this works. I've read the documentation, tried to implement it, and I just can't seem to get it to work. I read up on how to do the math on matrices, however, I am a bit lost.
I've looked into numPy but I currently don't have it.. and I would rather not use something that would have to be installed on every machine just to use this particular code.

Here's the rundown.
In the documentation it says:
What if you have a position in global coordinates and want to move an object to that position? Then you need a transformation back from global coordinates to the object’s local position coordinates. Mathematically this is done by multiplication by the global matrix’ inverse. If Mg is a matrix in global space and Mag is object a’s global matrix, then Mag-1*Mg equals the first matrix’ in a’s local coordinates.
It also says that you don't need to do the math because there are the functions:
SetMl (ml) and SetMg (mg)
Those only work for the baseObject.. and not a point.

I've tried to plug in the Mag-1*Mg into some test code, just so I can see if the numbers come out correctly. However, I don't understand how to use that correctly. From my understanding of the documentation and the provided code, Mag is globalPoint and Mg would be worldSpaceCoords.
    doc = c4d.documents.GetActiveDocument ()
    op = doc.GetActiveObject ()
    worldSpaceCoords = op.GetMg()
    allPoints = op.GetAllPoints()
    for i,a in enumerate (allPoints):
        localPoint = op.GetPoint (i)
        print 'local', localPoint

        globalPoint = localPoint * worldSpaceCoords
        print 'global', globalPoint
        backLocalPoint = globalPoint-1*worldSpaceCoords
        print 'back to local', backLocalPoint
        print '--'
Currently I'm running into the following error: 
TypeError: unsupported operand type(s) for -: 'c4d.Vector' and 'c4d.Matrix'
I'm lost in the math as to how to get back to the local coordinates.

Back to Top
S_Bach View Drop Down
Forum Moderator
Forum Moderator

Joined: 2011 Jun 27
Online Status: Offline
Posts: 1379
Direct Link To This Post Posted: 2014 Nov 25 at 1:29am

as the documentation says “Mag” should be a matrix. In your code you try to handle a vector:

backLocalPoint = globalPoint-1*worldSpaceCoords

Also, “Mag-1” does not mean “minus one”. It stands for the inverse matrix. You can create the inverse matrix using Python's “invert” operator:

        inverseMatrix = ~worldSpaceCoords   
        backLocalPoint = globalPoint * inverseMatrix

You find some examples of complex matrix use in the SpherifyModifier example.

best wishes,
SDK Support Engineer
Back to Top
Herbie View Drop Down

Joined: 2014 Oct 28
Online Status: Offline
Posts: 28
Direct Link To This Post Posted: 2014 Nov 25 at 4:23pm
I thought the documentation sounded weird with the -1 in there.
I was looking for the syntax for the invert operator. Everything is working now, which is fantastic thanks to you!
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.109 seconds.