StrataCafe-Forums/その8 増強

Last-modified: 2008-09-15 (月) 17:29:13

This post is to add some enhancements to the Random Replication script, covered in Part I - III of the tutorials on this fourm.
このポストは、部分Iでカバーされて、任意の応答スクリプトにいくつかの増強を加えることです-このfourmの上の教本のIII。

note: rotation is now updated to work with the settings
注:回転はセッティングで働くために今更新されます。

Here's a snapshot of the dialog:
ここに、対話のスナップ写真があります:

What was changed:
変更されたもの:
Lots actually. This module is much more robust than that covered in the tutorials. This includes a full dialog to adjust minimum and maximum values on position, scale, and rotation. It also allows you to save the settings for the numbers you enter for the current application session (until you quit CX). This will allow you to replicate all selected instances in your scene or only the first instance selected. Finally, enhancements were added to perform selection checking to ensure that the script is unavailable until the user actually has something selected.
ロット、実際に。このモジュールは教本でカバーされたそれよりはるかに強健です。これは、位置、規模および回転の最小および極大値を調節するために十分な対話を含んでいます。さらに、それは、現在の適用セッション(CXを中止するまで)に登録する数のためにあなたがセッティングを保存することを可能にします。これは、あなたが場面の選択された実例をすべて模写することを可能にするでしょう。あるいは、最初の実例だけが選択しました。最後に、増強はユーザが実際に何かを選択するまでスクリプトが利用不可能であることを保証するためにチェックする選択を行なうために付け加えられました。

As always, additional enhancements could be added. Support for autofilling in the input fields with numbers from selected objects or for turning on and off features are possibilities that you could explore.
常にとともに、追加の増強を加えることができるかもしれません。選択されたオブジェクトからの数を備えた入力分野でautofillingするための、あるいは断続的に特徴をつけるための支援、探求することができる可能性です。

Copy/Paste the code below into a new text file and save it as "ReplicateModule.lua". Place this file in your Scripts/Autoload directory.
新しいテキストファイルにコードを下にコピーしてくださる/貼って、「ReplicateModule.lua」としてそれを保存してください。このファイルを置く、の中で、あなたの、スクリプト/ディレクトリーに自動的にロードします。

  • =========================================================================
  • Random Replicate Lua Module for Strata CX 5
  • MODULE ReplicateModule
  • VERSION 1.0
  • AUTHOR Jon Bradley
  • DATE 06.18.2006
  • DESCRIPTION
  • This module is to randomly replicate either the first or all in
  • the list of selected instances in your model view of CX 5.
  • LICENSE
  • This work is licensed under the Creative Commons Attribution 2.5 License. http://creativecommons.org/licenses/by/2.5/legalcode
  • =========================================================================

module("ReplicateModule", package.seeall)

  • Internal constants for minimum and maximum
  • translation, scale and rotation
    local pMin = { x=-5, y=-5, z=-5 }
    local pMax = { x=5, y=5, z=5 }
    local sMin = { x=0.1, y=0.1, z=0.1 }
    local sMax = { x=1, y=1, z=1 }
    local rMin ={ x=-360, y=-360, z=-360 }
    local rMax ={ x=360, y=360, z=360 }

function Replicate(numCopies, doAll, values)

   local c = s3d.database.GetActiveContext()
   local m = c and c:GetManipulatorManager()
   local size = m and m:GetSize()
   if size > 0 then
       -- A local function inside our Replicate function call that
       -- sets the properties of the duplicated instance.
       -- Note: We use this function instead of having to write out all
       -- of this code twice - once if we're duplicating the first
       -- selected instance, and second if we're duplicating all
       local function setProperties(v,inst)
           --	Random translation within defined limits
           local tx = v.position.min.x + math.random()*( v.position.max.x - v.position.min.x )
           local ty = v.position.min.y + math.random()*( v.position.max.y - v.position.min.y )
           local tz = v.position.min.z + math.random()*( v.position.max.z - v.position.min.z )
           local position = s3d.vec.Point3d(tx,ty,tz)
           --	Random scale within defined limits
           local sx = v.scale.min.x + math.random()*( v.scale.max.x - v.scale.min.x )
           local sy = v.scale.min.y + math.random()*( v.scale.max.y - v.scale.min.y )
           local sz = v.scale.min.z + math.random()*( v.scale.max.z - v.scale.min.z )
           local scale = s3d.vec.Point3d(sx,sy,sz)
            --  Random rotation within defined limits
           local rx = math.rad(v.rotation.min.x + math.random()*( v.rotation.max.x - v.rotation.min.x ) )
           local ry = math.rad(v.rotation.min.y + math.random()*( v.rotation.max.y - v.rotation.min.y ) )
           local rz = math.rad(v.rotation.min.z + math.random()*( v.rotation.max.z - v.rotation.min.z ) )
           local c1 = math.cos(ry/2)
           local s1 = math.sin(ry/2)
           local c2 = math.cos(rz/2)
           local s2 = math.sin(rz/2)
           local c3 = math.cos(rx/2)
           local s3 = math.sin(rx/2)
           local qx = s1*s2*c3 + c1*c2*s3
           local qy = s1*c2*c3 + c1*s2*s3
           local qz = c1*s2*c3 + s1*c2*s3
           local w = c1*c2*c3 - s1*s2*s3
           local rotation = s3d.vec.Quaternion(qx,qy,qz,w)
           --  Set values on the instance
           inst:SetTranslationAt(0,position)
           inst:SetScaleAt(0,scale)
           inst:SetRotationAt(0, rotation )
        end
       if doAll == 1 then
           --  Create a table to contain all our selected instances
           --  and loop over all selected instances to store them
           local instList = {}
           for s=1, size do
               instList[s] = m:GetIndexedInstance(s)
           end
           --  Finally, for all the instances we've got, duplicate
           --  each one requested number of times
           for s=1, size do
               for t=1, numCopies do
                   local inst = instList[s]:Clone()
                   c:AddInstance(inst)
                   setProperties(values,inst)
               end
           end
       else
           i = m:GetIndexedInstance(size)
           for t=1, numCopies do
               local inst = i:Clone()
               c:AddInstance(inst)
               setProperties(values,inst)
           end
       end
       c:Refresh()
	else
		s3d.ui.Alert ("You must select an object.")
	end

end

function RandomReplicateUI()

	local d = s3d.ui.Dialog("Random Replicate")
   local transCluster = d:BeginCluster("Transformations", false, 225)
       d:AddStaticText("")
       d:AddStaticText("Position Min:")
       d:AddStaticText("Position Max:")
       d:AddStaticText("Scale Min:")
       d:AddStaticText("Scale Max:")
       d:AddStaticText("Rotation Min:")
       d:AddStaticText("Rotation Max:")
       --  Create input fields, populated with local min/max constants
       d:AddColumn()
       d:AddStaticText("X")
       local txMin = d:AddNumberText( pMin.x,2,50 )
       local txMax = d:AddNumberText( pMax.x,2,50 )
       local sxMin = d:AddNumberText( sMin.x,2,50 )
       local sxMax = d:AddNumberText( sMax.x,2,50 )
       local rxMin = d:AddNumberText( rMin.x,2,50 )
       local rxMax = d:AddNumberText( rMax.x,2,50 )
       d:AddColumn()
       d:AddStaticText("Y")
       local tyMin = d:AddNumberText( pMin.y,2,50 )
       local tyMax = d:AddNumberText( pMax.y,2,50 )
       local syMin = d:AddNumberText( sMin.y,2,50 )
       local syMax = d:AddNumberText( sMax.y,2,50 )
       local ryMin = d:AddNumberText( rMin.y,2,50 )
       local ryMax = d:AddNumberText( rMax.y,2,50 )
       d:AddColumn()
       d:AddStaticText("Z")
       local tzMin = d:AddNumberText( pMin.z,2,50 )
       local tzMax = d:AddNumberText( pMax.z,2,50 )
       local szMin = d:AddNumberText( sMin.z,2,50 )
       local szMax = d:AddNumberText( sMax.z,2,50 )
       local rzMin = d:AddNumberText( rMin.z,2,50 )
       local rzMax = d:AddNumberText( rMax.z,2,50 )
       d:AddColumn()
   d:EndCluster()
   d:AddStaticText("")
   d:BeginCluster("Settings", true, 225)
       -- Handle UI input for number of copies of each selected instance
       d:AddStaticText("Number of Copies:")
       local doAll = d:AddCheckbox("Replicate All Selected")
       local saveSettings = d:AddCheckbox("Save settings for this session")
       d:SetValue(saveSettings, 1)
       d:AddColumn()
       local numCopies = d:AddNumberText(0,0,40)
       --  Use all selected instances or only the first
   d:EndCluster()
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
		if id == s3d.ui.Dialog.kOkay then
			local num = d:GetValue(numCopies)
			local dupAll = d:GetValue(doAll)
			if num ~= 0 then
               --  Now, have some fun grabbing all the values and converting them
               --  to internal CX units.
               local c = s3d.database.GetActiveContext()
               local u = c:GetUnits()
               local values = {
                   --  position min/max
                   position = {
                           min = {
                                   x=u:ConvertToStrataUnits( d:GetValue(txMin) ),
                                   y=u:ConvertToStrataUnits( d:GetValue(tyMin) ),
                                   z=u:ConvertToStrataUnits( d:GetValue(tzMin) )
                               },
                           max = {
                                   x=u:ConvertToStrataUnits( d:GetValue(txMax) ),
                                   y=u:ConvertToStrataUnits( d:GetValue(tyMax) ),
                                   z=u:ConvertToStrataUnits( d:GetValue(tzMax) )
                               }
                       },
                   --  scale min/max
                   scale = {
                           min = {
                                   x=u:ConvertToStrataUnits( d:GetValue(sxMin) ),
                                   y=u:ConvertToStrataUnits( d:GetValue(syMin) ),
                                   z=u:ConvertToStrataUnits( d:GetValue(szMin) )
                               },
                           max = {
                                   x=u:ConvertToStrataUnits( d:GetValue(sxMax) ),
                                   y=u:ConvertToStrataUnits( d:GetValue(syMax) ),
                                   z=u:ConvertToStrataUnits( d:GetValue(szMax) )
                               }
                       },
                   --  rotation min/max
                   rotation = {
                           min = {
                                   x=d:GetValue(rxMin),
                                   y=d:GetValue(ryMin),
                                   z=d:GetValue(rzMin)
                               },
                           max = {
                                   x=d:GetValue(rxMax),
                                   y=d:GetValue(ryMax),
                                   z=d:GetValue(rzMax)
                               }
                       }
               }
               --  If user requested to save settings for this application session
               --  save the settings.
               if d:GetValue(saveSettings) == 1 then
                   pMin = { x=d:GetValue(txMin), y=d:GetValue(tyMin), z=d:GetValue(tzMin) }
                   pMax = { x=d:GetValue(txMax), y=d:GetValue(tyMax), z=d:GetValue(tzMax) }
                   sMin = { x=d:GetValue(sxMin), y=d:GetValue(syMin), z=d:GetValue(szMin) }
                   sMax = { x=d:GetValue(sxMax), y=d:GetValue(syMax), z=d:GetValue(szMax) }
                   rMin ={ x=d:GetValue(rxMin), y=d:GetValue(ryMin), z=d:GetValue(rzMin) }
                   rMax ={ x=d:GetValue(rxMax), y=d:GetValue(ryMax), z=d:GetValue(rzMax) }
               end
               --  Finally, perform the replication
				Replicate(num, dupAll, values)
			end
		end
	end
	d:Present( checkUI )

end

local function GetActiveSelectionSize ()

	local c = s3d.database.GetActiveContext()
	local m = c and c:GetManipulatorManager()
	return m and m:GetSize()

end

local function EnableWithSelection (item)

	local size = GetActiveSelectionSize()
	return size and size > 0

end

local separator = { "-" }
local randReplicateMenuItem = { "Random Replicate",

       function (menuItem)
           RandomReplicateUI()
       end,
       EnableWithSelection
   }
  • Insert this menu item (and a separator) into the Scripting Menu
    table.insert(s3d.ui.scripting_menu, separator)
    table.insert(s3d.ui.scripting_menu, randReplicateMenuItem)
  • =========================================================================-- 層CX 5のランダム折り返したLuaモジュール---- モジュールReplicateModule-- バージョン1.0-- 著者ジョン・ブラッドリー-- 日付06.18.2006---- 記述---- このモジュールは任意に中へ1番目かすべてのいずれかを模写することです -- CX 5のあなたのモデル見解中の選択された実例のリスト。---- ライセンス---- この仕事は創造的な下院議員帰着2.5ライセンスの下で許可されます。-- http://creativecommons.org/licenses/by/2.5/legalcode-- =========================================================================モジュール(「ReplicateModule」(package.seeall))-- 最小と最大用の内部定数-- 翻訳、規模および回転ローカルのpMin={x=-5、y=-5、z=-5}のローカルのpMax={x=5、y=5、z=5}、ローカルのsMin={x=0.1、y=0.1、z=0.1}、ローカルのsMax={x=1、y=1、z=1}、ローカルのrMin={x=-360、y=-360、z=-360}のローカルのrMax={x=360、y=360、z=360}折り返した(numCopies、doAll、値)機能ローカルのc=s3d.database。
    GetActiveContext ()ローカルのm=cおよびc:GetManipulatorManagerの()のローカルのサイズ=mおよびm:GetSize()場合、サイズ>その後0-- 私たちの折り返した関数呼び出しの内部のローカルの関数、複写された実例の特性をセットします。-- 注:私たちは、すべて(このコードに2度-私たちが1番目を複写していれば一度)を書き上げなければならない代わりにこの関数を使用します、私たちがローカルの関数setProperties(v、inst)をすべて複写していれば、実例と第2を選択した。-- 定義された範囲内の任意の翻訳ローカル?フtx=v.position.min.x+math.random()*(v.position.max.x -v.position.min.x)ローカルのty=v.position.min.y+math.random()*(v.position.max.y -v.position.min.y)ローカルのtz=v.position.min.z+math.random()*(v.position.max.z(v.position.min.z)のローカルの位置=s3d.vec.Point3d(tx、ty、tz)) -- 定義された範囲内の任意の規模ローカルのsx=v.scale.min.x+math.random()*(v.scale.max.x -v.scale.min。)
    xのローカルのsy=v.scale.min.y+math.random()*(v.scale.max.y(v.scale.min.y)のローカルのsz=v.scale.min.z+math.random()*(v.scale.max.z(v.scale.min.z)のローカルの規模=s3d.vec.Point3d(sx、sy、sz)))-- 定義された範囲内の任意の回転ローカルのrx=math.rad(v.rotation.min.x+math.random()*(v.rotation.max.x -v.rotation.min.x))ローカルのry=math.rad(v.rotation.min.y+math.random()*(v.rotation.max.y -v.rotation.min.y))ローカルのrz=math.rad(v.rotation.min.z+math.random()*、ローカルのc1=math.cos(ry/2)ローカルのs1=math.sin(ry/2)ローカルのc2=math.cos(rz/2)ローカルのs2=math.sin(rz/2)ローカルのローカルのc3=math.cos(rx/2)(v.rotation.max.z(v.rotation.min.z))) s3=math.sin(rx/2)ローカルのqx=s1*s2*c3+c1*c2*s3のローカルのqy=s1*c2*c3+c1*s2*s3のローカルのqz=c1*s2*c3+s1*c2*s3のローカルのw=c1*c2*c3 - s1*s2*s3のローカルの回転=s3d.vec。
    四元数(qx、qy、qz、w)-- 実例上の設定値inst:SetTranslationAt(0と位置)inst:SetScaleAt(0と規模)inst:SetRotationAt(0と回転)終了場合、doAll==1、その後-- 私たちの選択された実例をすべて含むためにテーブルを作成します-- そして、それらを格納するために選択された実例をの上にすべてループにしますs=1のローカルのinstList={}およびサイズは行います[s]=mをinstListする:GetIndexedInstance(s)終了 -- 最後に、すべての実例については、私たちは得ており複写します-- 各々は、回の数を要求しましたs=1およびサイズのために、t=1およびnumCopiesのために行う、ローカルのinst=instListを行う[s]:Clone()c:AddInstance(inst)setProperties(値、inst)終了終了 ほかにi=m:GetIndexedInstance(サイズ)t=1については、numCopiesはローカルのinst=iを行います:クローン()c:AddInstance(inst)
    setProperties(値、inst)終了終了c:()をリフレッシュしますほかにs3d.ui.Alert(「オブジェクトを選択しなければなりません。。」)終了終了関数RandomReplicateUI()ローカルのd=s3d.ui.Dialog(「折り返したランダム」)ローカルのtransCluster=d:BeginCluster(偽って「変形」、225)d:AddStaticText("")d:AddStaticText(「位置min」:)d:AddStaticText(「位置マックス:」:)d:AddStaticText(「規模min」:)d:AddStaticText(「規模マックス:」:)d:AddStaticText(「回転min」:)d:AddStaticText(「回転マックス:」:)-- ローカルのmin/max定数で占められて、入力フィールドを作ります。d:AddColumn()d:AddStaticText(「X」)ローカルのtxMinの=のd:AddNumberText(pMin.x、2と50)のローカルのtxMax=d:AddNumberText(pMax.x、2と50)の?香[カルのsxMin=d:AddNumberText(sMin.x、2と50)のローカルのsxMax=d:AddNumberText(sMax.x、2と50)のローカルのrxMin=d:AddNumberText(rMin.x、2と50)のローカル?フrxMax=d:AddNumberText(rMax。)
    x、2、50のd:AddColumn()d:AddStaticText(「Y」)ローカルのtyMinの=のd:AddNumberText(pMin.y、2、50)のローカルのtyMax=d:AddNumberText(pMax.y、2、50)のローカルのsyMin=d:AddNumberText(sMin.y、2、50)のローカルのsyMax=d:AddNumberText(sMax.y、2、50)のローカルのryMin=d:AddNumberText(rMin.y、2、50)のローカルのryMax=d:AddNumberText(rMax.y、2、50)d:AddColumn()d:AddStaticText(「Z」)ローカルのtzMinの=のd:AddNumberText(pMin.z、2、50)のローカルのtzMax=d:AddNumberText(pMax.z、2、50)のローカルのszMin=d:AddNumberText(sMin.z、2、50)のローカルのszMax=d:AddNumberText(sMax.z、2、50)のローカルのrzMin=d:AddNumberText(rMin.z、2、50)のローカルのrzMax=d:AddNumberText(rMax。)
    z、2と50d:AddColumn()d:EndCluster()d:AddStaticText("")d:BeginCluster(「セッティング」、真実、225) -- 個々の選択された実例のd:AddStaticText(「コピーの数」:)のローカルのdoAll=dのコピーの数のためのUI入力を扱います: AddCheckbox(「すべてを模写する、選択された」)のローカルのsaveSettings=d:AddCheckbox(「このセッションの間セッティングを取っておく」)d:SetValue(saveSettings、1)d:AddColumn()ローカルのnumCopies=d:AddNumberText(0、0、40)-- すべての選択された実例あるいは1番目だけを使用します。d:EndCluster()d:SetLabel(s3d.ui.Dialog.kOkay、「折れ重なってください」)d:SetLabel(s3d.ui.Dialog.kCancel、「取り消してください」)ローカルの関数checkUI(id)場合、id==s3d.ui.Dialog.kOkay、その後、ローカルのnumの=のd:GetValue(numCopies)のローカルのdupAll=d:GetValue(doAll)場合、num、その後0-- さて、値をすべてつかみ、それらを変換するある楽しみを持ってください -- 内部CXユニットに。ローカルのc=s3d.database。
    GetActiveContext ()ローカルのu=c:GetUnits()ローカルの値={--位置min/max位置={min={、x=u:ConvertToStrataUnits(d:)GetValue(txMin)(y=u):ConvertToStrataUnits(d:)GetValue(tyMin)(z=u):ConvertToStrataUnits(d:)GetValue(tzMin)}(max={x=u):ConvertToStrataUnits(d:)GetValue(txMax)(y=u):ConvertToStrataUnits(d:)GetValue(tyMax)、z=u:ConvertToStrataUnits(d:GetValue(tzMax))}}、-- 規模min/max規模={min={x=u:ConvertToStrataUnits(d:GetValue(sxMin))(y=u:Conver)
    tToStrataUnits(d:GetValue(syMin))、z=u:ConvertToStrataUnits(d:GetValue(szMin))}、max={x=u:ConvertToStrataUnits(d:GetValue(sxMax))、y=u:ConvertToStrataUnits(d:GetValue(syMax))(z=u:ConvertToStrataUnits(d:GetValue(szMax)))}}、-- 回転min/max回転={min={x=d:GetValue(rxMin)、y=d:GetValue(ryMin)、z=d:GetValue(rzMin)}、max={x=d:GetValue(rxMax)、y=d:GetValue(ryMax)(z=d:GetValue(rzMax)})}
    }-- ユーザがこの適用セッションの間セッティングを取っておくことを要求した場合--セッティングを保存します。場合、d:GetValue(saveSettings)==1、その後、pMin={x=d:GetValue(txMin)(y=d):GetValue(tyMin)(z=d):GetValue(tzMin)}pMax={x=d:GetValue(txMax)(y=d):GetValue(tyMax)(z=d):GetValue(tzMax)}sMin={x=d:y=d:GetValue(syMin)(z=d):GetValue(szMin)}sMax={x=d:GetValue(sxMax)(y=d):GetValue(syMax)(z=d):GetValue(szMax)}rMin={x=d:GetValue(rxMin)(y=d):GetValue(ryMin)(z=d):GetValue(rzMin)}rMax={x=d:GetValue(rxMax)、y=d:GetValue(ryMax)(z=d:GetValue(rzMax)}終了)-- 最後に、応答を行なってください終了を模写します(num、dupAll、値)。終了終了d:現在(checkUI)終了ローカルの関数GetActiveSelectionSize()のローカルのc=s3d.database。
    GetActiveContext ()ローカルのm=cおよびc:GetManipulatorManager()リターンmおよびm:GetSize()終了ローカルの関数のEnableWithSelection(アイテム)のローカルのサイズ=GetActiveSelectionSize()リターン、サイズおよびサイズ>0の終了 ローカルのセパレーター={「-」}のローカルのrandReplicateMenuItem={「ランダム、折れ重なる。」関数(menuItem)RandomReplicateUI()終了、EnableWithSelection }-- スクリプトを書くメニューtable.insert(s3d.ui.scripting_menu、セパレーター)table.insert(s3d.ui.scripting_menu、randReplicateMenuItem)にこのメニュー項目(またセパレーター)を挿入します。
    Good luck!
    成功を祈ります!
    Jon Bradley

This just in:
次のものにおいて正当なこれ:
Unfortunately, the autoloading mechanism in CX 5 does not support Mac line endings in the text file. To alleviate this, ensure that the line endings are either Unix or DOS (Windows) line endings.
不運にも、CX 5の中のオートローディング・メカニズムはテキストファイルの中でマック・ラインendingsを支援しません。これを緩和するためには、ラインendingsがUnixあるいはDOS(ウインドウズ)ラインendingsのいずれかであることを確認してください。

You will need to check your text or code editor to make sure that it has the capability to modify the line endings in the file.
それがファイル中のラインendingsを修正する能力を持っていることを確かめるためにテキストかコード・エディターを?`ェックする必要があるでしょう。

We found this after the last 3d Power bulletin which included this script. If you were one of the users that downloaded the script and it wouldn't work for you, just adjust the line endings in the file and it should then autoload properly.
私たちは、このスクリプトを含んでいた最後の3dパワー公報の後にこれを見つけました。あなたがスクリプトをダウンロードしたユーザのうちの1人で、それがあなたのために作動しな?ゥった場合、ファイル中のラインendingsおよびそれを単に調節する、その後、適切に自動的にロードするに違いありません。

As always, feel free to post any questions.
常にように、自由にどんな質問も記入してください。

Jon Bradley